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