1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/daemon/command/PostCommand.java Wed Jul 01 10:48:22 2009 +0200
1.3 @@ -0,0 +1,350 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.daemon.command;
1.23 +
1.24 +import java.io.IOException;
1.25 +
1.26 +import java.io.ByteArrayInputStream;
1.27 +import java.nio.charset.Charset;
1.28 +import java.nio.charset.IllegalCharsetNameException;
1.29 +import java.nio.charset.UnsupportedCharsetException;
1.30 +import java.sql.SQLException;
1.31 +import java.util.Locale;
1.32 +import javax.mail.MessagingException;
1.33 +import javax.mail.internet.AddressException;
1.34 +import javax.mail.internet.InternetHeaders;
1.35 +import org.sonews.daemon.Config;
1.36 +import org.sonews.util.Log;
1.37 +import org.sonews.mlgw.Dispatcher;
1.38 +import org.sonews.daemon.storage.Article;
1.39 +import org.sonews.daemon.storage.Database;
1.40 +import org.sonews.daemon.storage.Group;
1.41 +import org.sonews.daemon.NNTPConnection;
1.42 +import org.sonews.daemon.storage.Headers;
1.43 +import org.sonews.feed.FeedManager;
1.44 +import org.sonews.util.Stats;
1.45 +
1.46 +/**
1.47 + * Implementation of the POST command. This command requires multiple lines
1.48 + * from the client, so the handling of asynchronous reading is a little tricky
1.49 + * to handle.
1.50 + * @author Christian Lins
1.51 + * @since sonews/0.5.0
1.52 + */
1.53 +public class PostCommand extends AbstractCommand
1.54 +{
1.55 +
1.56 + private final Article article = new Article();
1.57 + private int lineCount = 0;
1.58 + private long bodySize = 0;
1.59 + private InternetHeaders headers = null;
1.60 + private long maxBodySize =
1.61 + Config.getInstance().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
1.62 + private PostState state = PostState.WaitForLineOne;
1.63 + private final StringBuilder strBody = new StringBuilder();
1.64 + private final StringBuilder strHead = new StringBuilder();
1.65 +
1.66 + public PostCommand(final NNTPConnection conn)
1.67 + {
1.68 + super(conn);
1.69 + }
1.70 +
1.71 + @Override
1.72 + public boolean hasFinished()
1.73 + {
1.74 + return this.state == PostState.Finished;
1.75 + }
1.76 +
1.77 + /**
1.78 + * Process the given line String. line.trim() was called by NNTPConnection.
1.79 + * @param line
1.80 + * @throws java.io.IOException
1.81 + * @throws java.sql.SQLException
1.82 + */
1.83 + @Override // TODO: Refactor this method to reduce complexity!
1.84 + public void processLine(String line)
1.85 + throws IOException, SQLException
1.86 + {
1.87 + switch(state)
1.88 + {
1.89 + case WaitForLineOne:
1.90 + {
1.91 + if(line.equalsIgnoreCase("POST"))
1.92 + {
1.93 + printStatus(340, "send article to be posted. End with <CR-LF>.<CR-LF>");
1.94 + state = PostState.ReadingHeaders;
1.95 + }
1.96 + else
1.97 + {
1.98 + printStatus(500, "invalid command usage");
1.99 + }
1.100 + break;
1.101 + }
1.102 + case ReadingHeaders:
1.103 + {
1.104 + strHead.append(line);
1.105 + strHead.append(NNTPConnection.NEWLINE);
1.106 +
1.107 + if("".equals(line) || ".".equals(line))
1.108 + {
1.109 + // we finally met the blank line
1.110 + // separating headers from body
1.111 +
1.112 + try
1.113 + {
1.114 + // Parse the header using the InternetHeader class from JavaMail API
1.115 + headers = new InternetHeaders(
1.116 + new ByteArrayInputStream(strHead.toString().trim()
1.117 + .getBytes(connection.getCurrentCharset())));
1.118 +
1.119 + // add the header entries for the article
1.120 + article.setHeaders(headers);
1.121 + }
1.122 + catch (MessagingException e)
1.123 + {
1.124 + e.printStackTrace();
1.125 + printStatus(500, "posting failed - invalid header");
1.126 + state = PostState.Finished;
1.127 + break;
1.128 + }
1.129 +
1.130 + // Change charset for reading body;
1.131 + // for multipart messages UTF-8 is returned
1.132 + connection.setCurrentCharset(article.getBodyCharset());
1.133 +
1.134 + state = PostState.ReadingBody;
1.135 +
1.136 + if(".".equals(line))
1.137 + {
1.138 + // Post an article without body
1.139 + postArticle(article);
1.140 + state = PostState.Finished;
1.141 + }
1.142 + }
1.143 + break;
1.144 + }
1.145 + case ReadingBody:
1.146 + {
1.147 + if(".".equals(line))
1.148 + {
1.149 + // Set some headers needed for Over command
1.150 + headers.setHeader(Headers.LINES, Integer.toString(lineCount));
1.151 + headers.setHeader(Headers.BYTES, Long.toString(bodySize));
1.152 +
1.153 + if(strBody.length() >= 2)
1.154 + {
1.155 + strBody.deleteCharAt(strBody.length() - 1); // Remove last newline
1.156 + strBody.deleteCharAt(strBody.length() - 1); // Remove last CR
1.157 + }
1.158 + article.setBody(strBody.toString()); // set the article body
1.159 +
1.160 + postArticle(article);
1.161 + state = PostState.Finished;
1.162 + }
1.163 + else
1.164 + {
1.165 + bodySize += line.length() + 1;
1.166 + lineCount++;
1.167 +
1.168 + // Add line to body buffer
1.169 + strBody.append(line);
1.170 + strBody.append(NNTPConnection.NEWLINE);
1.171 +
1.172 + if(bodySize > maxBodySize)
1.173 + {
1.174 + printStatus(500, "article is too long");
1.175 + state = PostState.Finished;
1.176 + break;
1.177 + }
1.178 +
1.179 + // Check if this message is a MIME-multipart message and needs a
1.180 + // charset change
1.181 + try
1.182 + {
1.183 + line = line.toLowerCase(Locale.ENGLISH);
1.184 + if(line.startsWith(Headers.CONTENT_TYPE))
1.185 + {
1.186 + int idxStart = line.indexOf("charset=") + "charset=".length();
1.187 + int idxEnd = line.indexOf(";", idxStart);
1.188 + if(idxEnd < 0)
1.189 + {
1.190 + idxEnd = line.length();
1.191 + }
1.192 +
1.193 + if(idxStart > 0)
1.194 + {
1.195 + String charsetName = line.substring(idxStart, idxEnd);
1.196 + if(charsetName.length() > 0 && charsetName.charAt(0) == '"')
1.197 + {
1.198 + charsetName = charsetName.substring(1, charsetName.length() - 1);
1.199 + }
1.200 +
1.201 + try
1.202 + {
1.203 + connection.setCurrentCharset(Charset.forName(charsetName));
1.204 + }
1.205 + catch(IllegalCharsetNameException ex)
1.206 + {
1.207 + Log.msg("PostCommand: " + ex, false);
1.208 + }
1.209 + catch(UnsupportedCharsetException ex)
1.210 + {
1.211 + Log.msg("PostCommand: " + ex, false);
1.212 + }
1.213 + } // if(idxStart > 0)
1.214 + }
1.215 + }
1.216 + catch(Exception ex)
1.217 + {
1.218 + ex.printStackTrace();
1.219 + }
1.220 + }
1.221 + break;
1.222 + }
1.223 + default:
1.224 + Log.msg("PostCommand::processLine(): already finished...", false);
1.225 + }
1.226 + }
1.227 +
1.228 + /**
1.229 + * Article is a control message and needs special handling.
1.230 + * @param article
1.231 + */
1.232 + private void controlMessage(Article article)
1.233 + throws IOException
1.234 + {
1.235 + String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
1.236 + if(ctrl.length == 2) // "cancel <mid>"
1.237 + {
1.238 + try
1.239 + {
1.240 + Database.getInstance().delete(ctrl[1]);
1.241 +
1.242 + // Move cancel message to "control" group
1.243 + article.setHeader(Headers.NEWSGROUPS, "control");
1.244 + Database.getInstance().addArticle(article);
1.245 + printStatus(240, "article cancelled");
1.246 + }
1.247 + catch(SQLException ex)
1.248 + {
1.249 + Log.msg(ex, false);
1.250 + printStatus(500, "internal server error");
1.251 + }
1.252 + }
1.253 + else
1.254 + {
1.255 + printStatus(441, "unknown Control header");
1.256 + }
1.257 + }
1.258 +
1.259 + private void supersedeMessage(Article article)
1.260 + throws IOException
1.261 + {
1.262 + try
1.263 + {
1.264 + String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
1.265 + Database.getInstance().delete(oldMsg);
1.266 + Database.getInstance().addArticle(article);
1.267 + printStatus(240, "article replaced");
1.268 + }
1.269 + catch(SQLException ex)
1.270 + {
1.271 + Log.msg(ex, false);
1.272 + printStatus(500, "internal server error");
1.273 + }
1.274 + }
1.275 +
1.276 + private void postArticle(Article article)
1.277 + throws IOException
1.278 + {
1.279 + if(article.getHeader(Headers.CONTROL)[0].length() > 0)
1.280 + {
1.281 + controlMessage(article);
1.282 + }
1.283 + else if(article.getHeader(Headers.SUPERSEDES)[0].length() > 0)
1.284 + {
1.285 + supersedeMessage(article);
1.286 + }
1.287 + else // Post the article regularily
1.288 + {
1.289 + // Try to create the article in the database or post it to
1.290 + // appropriate mailing list
1.291 + try
1.292 + {
1.293 + boolean success = false;
1.294 + String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
1.295 + for(String groupname : groupnames)
1.296 + {
1.297 + Group group = Database.getInstance().getGroup(groupname);
1.298 + if(group != null)
1.299 + {
1.300 + if(group.isMailingList() && !connection.isLocalConnection())
1.301 + {
1.302 + // Send to mailing list; the Dispatcher writes
1.303 + // statistics to database
1.304 + Dispatcher.toList(article);
1.305 + success = true;
1.306 + }
1.307 + else
1.308 + {
1.309 + // Store in database
1.310 + if(!Database.getInstance().isArticleExisting(article.getMessageID()))
1.311 + {
1.312 + Database.getInstance().addArticle(article);
1.313 +
1.314 + // Log this posting to statistics
1.315 + Stats.getInstance().mailPosted(
1.316 + article.getHeader(Headers.NEWSGROUPS)[0]);
1.317 + }
1.318 + success = true;
1.319 + }
1.320 + }
1.321 + } // end for
1.322 +
1.323 + if(success)
1.324 + {
1.325 + printStatus(240, "article posted ok");
1.326 + FeedManager.queueForPush(article);
1.327 + }
1.328 + else
1.329 + {
1.330 + printStatus(441, "newsgroup not found");
1.331 + }
1.332 + }
1.333 + catch(AddressException ex)
1.334 + {
1.335 + Log.msg(ex.getMessage(), true);
1.336 + printStatus(441, "invalid sender address");
1.337 + }
1.338 + catch(MessagingException ex)
1.339 + {
1.340 + // A MessageException is thrown when the sender email address is
1.341 + // invalid or something is wrong with the SMTP server.
1.342 + System.err.println(ex.getLocalizedMessage());
1.343 + printStatus(441, ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
1.344 + }
1.345 + catch(SQLException ex)
1.346 + {
1.347 + ex.printStackTrace();
1.348 + printStatus(500, "internal server error");
1.349 + }
1.350 + }
1.351 + }
1.352 +
1.353 +}