src/org/sonews/daemon/command/PostCommand.java
author cli
Sun, 29 Aug 2010 18:17:37 +0200
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 42 7f84f4de2893
permissions -rw-r--r--
Switch intent style to Original K&R / Linux / Kernel.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon.command;
    20 
    21 import java.io.IOException;
    22 import java.io.ByteArrayInputStream;
    23 import java.io.ByteArrayOutputStream;
    24 import java.sql.SQLException;
    25 import java.util.Arrays;
    26 import javax.mail.MessagingException;
    27 import javax.mail.internet.AddressException;
    28 import javax.mail.internet.InternetHeaders;
    29 import org.sonews.config.Config;
    30 import org.sonews.util.Log;
    31 import org.sonews.mlgw.Dispatcher;
    32 import org.sonews.storage.Article;
    33 import org.sonews.storage.Group;
    34 import org.sonews.daemon.NNTPConnection;
    35 import org.sonews.storage.Headers;
    36 import org.sonews.storage.StorageBackendException;
    37 import org.sonews.storage.StorageManager;
    38 import org.sonews.feed.FeedManager;
    39 import org.sonews.util.Stats;
    40 
    41 /**
    42  * Implementation of the POST command. This command requires multiple lines
    43  * from the client, so the handling of asynchronous reading is a little tricky
    44  * to handle.
    45  * @author Christian Lins
    46  * @since sonews/0.5.0
    47  */
    48 public class PostCommand implements Command
    49 {
    50 
    51 	private final Article article = new Article();
    52 	private int lineCount = 0;
    53 	private long bodySize = 0;
    54 	private InternetHeaders headers = null;
    55 	private long maxBodySize =
    56 		Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
    57 	private PostState state = PostState.WaitForLineOne;
    58 	private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream();
    59 	private final StringBuilder strHead = new StringBuilder();
    60 
    61 	@Override
    62 	public String[] getSupportedCommandStrings()
    63 	{
    64 		return new String[] {"POST"};
    65 	}
    66 
    67 	@Override
    68 	public boolean hasFinished()
    69 	{
    70 		return this.state == PostState.Finished;
    71 	}
    72 
    73 	@Override
    74 	public String impliedCapability()
    75 	{
    76 		return null;
    77 	}
    78 
    79 	@Override
    80 	public boolean isStateful()
    81 	{
    82 		return true;
    83 	}
    84 
    85 	/**
    86 	 * Process the given line String. line.trim() was called by NNTPConnection.
    87 	 * @param line
    88 	 * @throws java.io.IOException
    89 	 * @throws java.sql.SQLException
    90 	 */
    91 	@Override // TODO: Refactor this method to reduce complexity!
    92 	public void processLine(NNTPConnection conn, String line, byte[] raw)
    93 		throws IOException, StorageBackendException
    94 	{
    95 		switch (state) {
    96 			case WaitForLineOne: {
    97 				if (line.equalsIgnoreCase("POST")) {
    98 					conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
    99 					state = PostState.ReadingHeaders;
   100 				} else {
   101 					conn.println("500 invalid command usage");
   102 				}
   103 				break;
   104 			}
   105 			case ReadingHeaders: {
   106 				strHead.append(line);
   107 				strHead.append(NNTPConnection.NEWLINE);
   108 
   109 				if ("".equals(line) || ".".equals(line)) {
   110 					// we finally met the blank line
   111 					// separating headers from body
   112 
   113 					try {
   114 						// Parse the header using the InternetHeader class from JavaMail API
   115 						headers = new InternetHeaders(
   116 							new ByteArrayInputStream(strHead.toString().trim().getBytes(conn.getCurrentCharset())));
   117 
   118 						// add the header entries for the article
   119 						article.setHeaders(headers);
   120 					} catch (MessagingException e) {
   121 						e.printStackTrace();
   122 						conn.println("500 posting failed - invalid header");
   123 						state = PostState.Finished;
   124 						break;
   125 					}
   126 
   127 					// Change charset for reading body;
   128 					// for multipart messages UTF-8 is returned
   129 					//conn.setCurrentCharset(article.getBodyCharset());
   130 
   131 					state = PostState.ReadingBody;
   132 
   133 					if (".".equals(line)) {
   134 						// Post an article without body
   135 						postArticle(conn, article);
   136 						state = PostState.Finished;
   137 					}
   138 				}
   139 				break;
   140 			}
   141 			case ReadingBody: {
   142 				if (".".equals(line)) {
   143 					// Set some headers needed for Over command
   144 					headers.setHeader(Headers.LINES, Integer.toString(lineCount));
   145 					headers.setHeader(Headers.BYTES, Long.toString(bodySize));
   146 
   147 					byte[] body = bufBody.toByteArray();
   148 					if (body.length >= 2) {
   149 						// Remove trailing CRLF
   150 						body = Arrays.copyOf(body, body.length - 2);
   151 					}
   152 					article.setBody(body); // set the article body
   153 
   154 					postArticle(conn, article);
   155 					state = PostState.Finished;
   156 				} else {
   157 					bodySize += line.length() + 1;
   158 					lineCount++;
   159 
   160 					// Add line to body buffer
   161 					bufBody.write(raw, 0, raw.length);
   162 					bufBody.write(NNTPConnection.NEWLINE.getBytes());
   163 
   164 					if (bodySize > maxBodySize) {
   165 						conn.println("500 article is too long");
   166 						state = PostState.Finished;
   167 						break;
   168 					}
   169 				}
   170 				break;
   171 			}
   172 			default: {
   173 				// Should never happen
   174 				Log.get().severe("PostCommand::processLine(): already finished...");
   175 			}
   176 		}
   177 	}
   178 
   179 	/**
   180 	 * Article is a control message and needs special handling.
   181 	 * @param article
   182 	 */
   183 	private void controlMessage(NNTPConnection conn, Article article)
   184 		throws IOException
   185 	{
   186 		String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
   187 		if (ctrl.length == 2) // "cancel <mid>"
   188 		{
   189 			try {
   190 				StorageManager.current().delete(ctrl[1]);
   191 
   192 				// Move cancel message to "control" group
   193 				article.setHeader(Headers.NEWSGROUPS, "control");
   194 				StorageManager.current().addArticle(article);
   195 				conn.println("240 article cancelled");
   196 			} catch (StorageBackendException ex) {
   197 				Log.get().severe(ex.toString());
   198 				conn.println("500 internal server error");
   199 			}
   200 		} else {
   201 			conn.println("441 unknown control header");
   202 		}
   203 	}
   204 
   205 	private void supersedeMessage(NNTPConnection conn, Article article)
   206 		throws IOException
   207 	{
   208 		try {
   209 			String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
   210 			StorageManager.current().delete(oldMsg);
   211 			StorageManager.current().addArticle(article);
   212 			conn.println("240 article replaced");
   213 		} catch (StorageBackendException ex) {
   214 			Log.get().severe(ex.toString());
   215 			conn.println("500 internal server error");
   216 		}
   217 	}
   218 
   219 	private void postArticle(NNTPConnection conn, Article article)
   220 		throws IOException
   221 	{
   222 		if (article.getHeader(Headers.CONTROL)[0].length() > 0) {
   223 			controlMessage(conn, article);
   224 		} else if (article.getHeader(Headers.SUPERSEDES)[0].length() > 0) {
   225 			supersedeMessage(conn, article);
   226 		} else // Post the article regularily
   227 		{
   228 			// Circle check; note that Path can already contain the hostname here
   229 			String host = Config.inst().get(Config.HOSTNAME, "localhost");
   230 			if (article.getHeader(Headers.PATH)[0].indexOf(host + "!", 1) > 0) {
   231 				Log.get().info(article.getMessageID() + " skipped for host " + host);
   232 				conn.println("441 I know this article already");
   233 				return;
   234 			}
   235 
   236 			// Try to create the article in the database or post it to
   237 			// appropriate mailing list
   238 			try {
   239 				boolean success = false;
   240 				String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
   241 				for (String groupname : groupnames) {
   242 					Group group = StorageManager.current().getGroup(groupname);
   243 					if (group != null && !group.isDeleted()) {
   244 						if (group.isMailingList() && !conn.isLocalConnection()) {
   245 							// Send to mailing list; the Dispatcher writes
   246 							// statistics to database
   247 							Dispatcher.toList(article, group.getName());
   248 							success = true;
   249 						} else {
   250 							// Store in database
   251 							if (!StorageManager.current().isArticleExisting(article.getMessageID())) {
   252 								StorageManager.current().addArticle(article);
   253 
   254 								// Log this posting to statistics
   255 								Stats.getInstance().mailPosted(
   256 									article.getHeader(Headers.NEWSGROUPS)[0]);
   257 							}
   258 							success = true;
   259 						}
   260 					}
   261 				} // end for
   262 
   263 				if (success) {
   264 					conn.println("240 article posted ok");
   265 					FeedManager.queueForPush(article);
   266 				} else {
   267 					conn.println("441 newsgroup not found");
   268 				}
   269 			} catch (AddressException ex) {
   270 				Log.get().warning(ex.getMessage());
   271 				conn.println("441 invalid sender address");
   272 			} catch (MessagingException ex) {
   273 				// A MessageException is thrown when the sender email address is
   274 				// invalid or something is wrong with the SMTP server.
   275 				System.err.println(ex.getLocalizedMessage());
   276 				conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
   277 			} catch (StorageBackendException ex) {
   278 				ex.printStackTrace();
   279 				conn.println("500 internal server error");
   280 			}
   281 		}
   282 	}
   283 }