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