1.1 --- a/org/sonews/daemon/command/PostCommand.java Sun Aug 29 17:04:25 2010 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,332 +0,0 @@
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 -import java.io.ByteArrayInputStream;
1.26 -import java.io.ByteArrayOutputStream;
1.27 -import java.sql.SQLException;
1.28 -import java.util.Arrays;
1.29 -import javax.mail.MessagingException;
1.30 -import javax.mail.internet.AddressException;
1.31 -import javax.mail.internet.InternetHeaders;
1.32 -import org.sonews.config.Config;
1.33 -import org.sonews.util.Log;
1.34 -import org.sonews.mlgw.Dispatcher;
1.35 -import org.sonews.storage.Article;
1.36 -import org.sonews.storage.Group;
1.37 -import org.sonews.daemon.NNTPConnection;
1.38 -import org.sonews.storage.Headers;
1.39 -import org.sonews.storage.StorageBackendException;
1.40 -import org.sonews.storage.StorageManager;
1.41 -import org.sonews.feed.FeedManager;
1.42 -import org.sonews.util.Stats;
1.43 -
1.44 -/**
1.45 - * Implementation of the POST command. This command requires multiple lines
1.46 - * from the client, so the handling of asynchronous reading is a little tricky
1.47 - * to handle.
1.48 - * @author Christian Lins
1.49 - * @since sonews/0.5.0
1.50 - */
1.51 -public class PostCommand implements Command
1.52 -{
1.53 -
1.54 - private final Article article = new Article();
1.55 - private int lineCount = 0;
1.56 - private long bodySize = 0;
1.57 - private InternetHeaders headers = null;
1.58 - private long maxBodySize =
1.59 - Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
1.60 - private PostState state = PostState.WaitForLineOne;
1.61 - private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream();
1.62 - private final StringBuilder strHead = new StringBuilder();
1.63 -
1.64 - @Override
1.65 - public String[] getSupportedCommandStrings()
1.66 - {
1.67 - return new String[]{"POST"};
1.68 - }
1.69 -
1.70 - @Override
1.71 - public boolean hasFinished()
1.72 - {
1.73 - return this.state == PostState.Finished;
1.74 - }
1.75 -
1.76 - @Override
1.77 - public String impliedCapability()
1.78 - {
1.79 - return null;
1.80 - }
1.81 -
1.82 - @Override
1.83 - public boolean isStateful()
1.84 - {
1.85 - return true;
1.86 - }
1.87 -
1.88 - /**
1.89 - * Process the given line String. line.trim() was called by NNTPConnection.
1.90 - * @param line
1.91 - * @throws java.io.IOException
1.92 - * @throws java.sql.SQLException
1.93 - */
1.94 - @Override // TODO: Refactor this method to reduce complexity!
1.95 - public void processLine(NNTPConnection conn, String line, byte[] raw)
1.96 - throws IOException, StorageBackendException
1.97 - {
1.98 - switch(state)
1.99 - {
1.100 - case WaitForLineOne:
1.101 - {
1.102 - if(line.equalsIgnoreCase("POST"))
1.103 - {
1.104 - conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
1.105 - state = PostState.ReadingHeaders;
1.106 - }
1.107 - else
1.108 - {
1.109 - conn.println("500 invalid command usage");
1.110 - }
1.111 - break;
1.112 - }
1.113 - case ReadingHeaders:
1.114 - {
1.115 - strHead.append(line);
1.116 - strHead.append(NNTPConnection.NEWLINE);
1.117 -
1.118 - if("".equals(line) || ".".equals(line))
1.119 - {
1.120 - // we finally met the blank line
1.121 - // separating headers from body
1.122 -
1.123 - try
1.124 - {
1.125 - // Parse the header using the InternetHeader class from JavaMail API
1.126 - headers = new InternetHeaders(
1.127 - new ByteArrayInputStream(strHead.toString().trim()
1.128 - .getBytes(conn.getCurrentCharset())));
1.129 -
1.130 - // add the header entries for the article
1.131 - article.setHeaders(headers);
1.132 - }
1.133 - catch (MessagingException e)
1.134 - {
1.135 - e.printStackTrace();
1.136 - conn.println("500 posting failed - invalid header");
1.137 - state = PostState.Finished;
1.138 - break;
1.139 - }
1.140 -
1.141 - // Change charset for reading body;
1.142 - // for multipart messages UTF-8 is returned
1.143 - //conn.setCurrentCharset(article.getBodyCharset());
1.144 -
1.145 - state = PostState.ReadingBody;
1.146 -
1.147 - if(".".equals(line))
1.148 - {
1.149 - // Post an article without body
1.150 - postArticle(conn, article);
1.151 - state = PostState.Finished;
1.152 - }
1.153 - }
1.154 - break;
1.155 - }
1.156 - case ReadingBody:
1.157 - {
1.158 - if(".".equals(line))
1.159 - {
1.160 - // Set some headers needed for Over command
1.161 - headers.setHeader(Headers.LINES, Integer.toString(lineCount));
1.162 - headers.setHeader(Headers.BYTES, Long.toString(bodySize));
1.163 -
1.164 - byte[] body = bufBody.toByteArray();
1.165 - if(body.length >= 2)
1.166 - {
1.167 - // Remove trailing CRLF
1.168 - body = Arrays.copyOf(body, body.length - 2);
1.169 - }
1.170 - article.setBody(body); // set the article body
1.171 -
1.172 - postArticle(conn, article);
1.173 - state = PostState.Finished;
1.174 - }
1.175 - else
1.176 - {
1.177 - bodySize += line.length() + 1;
1.178 - lineCount++;
1.179 -
1.180 - // Add line to body buffer
1.181 - bufBody.write(raw, 0, raw.length);
1.182 - bufBody.write(NNTPConnection.NEWLINE.getBytes());
1.183 -
1.184 - if(bodySize > maxBodySize)
1.185 - {
1.186 - conn.println("500 article is too long");
1.187 - state = PostState.Finished;
1.188 - break;
1.189 - }
1.190 - }
1.191 - break;
1.192 - }
1.193 - default:
1.194 - {
1.195 - // Should never happen
1.196 - Log.get().severe("PostCommand::processLine(): already finished...");
1.197 - }
1.198 - }
1.199 - }
1.200 -
1.201 - /**
1.202 - * Article is a control message and needs special handling.
1.203 - * @param article
1.204 - */
1.205 - private void controlMessage(NNTPConnection conn, Article article)
1.206 - throws IOException
1.207 - {
1.208 - String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
1.209 - if(ctrl.length == 2) // "cancel <mid>"
1.210 - {
1.211 - try
1.212 - {
1.213 - StorageManager.current().delete(ctrl[1]);
1.214 -
1.215 - // Move cancel message to "control" group
1.216 - article.setHeader(Headers.NEWSGROUPS, "control");
1.217 - StorageManager.current().addArticle(article);
1.218 - conn.println("240 article cancelled");
1.219 - }
1.220 - catch(StorageBackendException ex)
1.221 - {
1.222 - Log.get().severe(ex.toString());
1.223 - conn.println("500 internal server error");
1.224 - }
1.225 - }
1.226 - else
1.227 - {
1.228 - conn.println("441 unknown control header");
1.229 - }
1.230 - }
1.231 -
1.232 - private void supersedeMessage(NNTPConnection conn, Article article)
1.233 - throws IOException
1.234 - {
1.235 - try
1.236 - {
1.237 - String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
1.238 - StorageManager.current().delete(oldMsg);
1.239 - StorageManager.current().addArticle(article);
1.240 - conn.println("240 article replaced");
1.241 - }
1.242 - catch(StorageBackendException ex)
1.243 - {
1.244 - Log.get().severe(ex.toString());
1.245 - conn.println("500 internal server error");
1.246 - }
1.247 - }
1.248 -
1.249 - private void postArticle(NNTPConnection conn, Article article)
1.250 - throws IOException
1.251 - {
1.252 - if(article.getHeader(Headers.CONTROL)[0].length() > 0)
1.253 - {
1.254 - controlMessage(conn, article);
1.255 - }
1.256 - else if(article.getHeader(Headers.SUPERSEDES)[0].length() > 0)
1.257 - {
1.258 - supersedeMessage(conn, article);
1.259 - }
1.260 - else // Post the article regularily
1.261 - {
1.262 - // Circle check; note that Path can already contain the hostname here
1.263 - String host = Config.inst().get(Config.HOSTNAME, "localhost");
1.264 - if(article.getHeader(Headers.PATH)[0].indexOf(host + "!", 1) > 0)
1.265 - {
1.266 - Log.get().info(article.getMessageID() + " skipped for host " + host);
1.267 - conn.println("441 I know this article already");
1.268 - return;
1.269 - }
1.270 -
1.271 - // Try to create the article in the database or post it to
1.272 - // appropriate mailing list
1.273 - try
1.274 - {
1.275 - boolean success = false;
1.276 - String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
1.277 - for(String groupname : groupnames)
1.278 - {
1.279 - Group group = StorageManager.current().getGroup(groupname);
1.280 - if(group != null && !group.isDeleted())
1.281 - {
1.282 - if(group.isMailingList() && !conn.isLocalConnection())
1.283 - {
1.284 - // Send to mailing list; the Dispatcher writes
1.285 - // statistics to database
1.286 - Dispatcher.toList(article, group.getName());
1.287 - success = true;
1.288 - }
1.289 - else
1.290 - {
1.291 - // Store in database
1.292 - if(!StorageManager.current().isArticleExisting(article.getMessageID()))
1.293 - {
1.294 - StorageManager.current().addArticle(article);
1.295 -
1.296 - // Log this posting to statistics
1.297 - Stats.getInstance().mailPosted(
1.298 - article.getHeader(Headers.NEWSGROUPS)[0]);
1.299 - }
1.300 - success = true;
1.301 - }
1.302 - }
1.303 - } // end for
1.304 -
1.305 - if(success)
1.306 - {
1.307 - conn.println("240 article posted ok");
1.308 - FeedManager.queueForPush(article);
1.309 - }
1.310 - else
1.311 - {
1.312 - conn.println("441 newsgroup not found");
1.313 - }
1.314 - }
1.315 - catch(AddressException ex)
1.316 - {
1.317 - Log.get().warning(ex.getMessage());
1.318 - conn.println("441 invalid sender address");
1.319 - }
1.320 - catch(MessagingException ex)
1.321 - {
1.322 - // A MessageException is thrown when the sender email address is
1.323 - // invalid or something is wrong with the SMTP server.
1.324 - System.err.println(ex.getLocalizedMessage());
1.325 - conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
1.326 - }
1.327 - catch(StorageBackendException ex)
1.328 - {
1.329 - ex.printStackTrace();
1.330 - conn.println("500 internal server error");
1.331 - }
1.332 - }
1.333 - }
1.334 -
1.335 -}