chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: package org.sonews.daemon.command; chris@1: chris@1: import java.io.IOException; chris@1: import java.io.ByteArrayInputStream; chris@3: import java.io.ByteArrayOutputStream; chris@1: import java.sql.SQLException; chris@3: import java.util.Arrays; cli@50: import java.util.logging.Level; chris@1: import javax.mail.MessagingException; chris@1: import javax.mail.internet.AddressException; chris@1: import javax.mail.internet.InternetHeaders; chris@3: import org.sonews.config.Config; chris@1: import org.sonews.util.Log; chris@1: import org.sonews.mlgw.Dispatcher; chris@3: import org.sonews.storage.Article; chris@3: import org.sonews.storage.Group; chris@1: import org.sonews.daemon.NNTPConnection; chris@3: import org.sonews.storage.Headers; chris@3: import org.sonews.storage.StorageBackendException; chris@3: import org.sonews.storage.StorageManager; chris@1: import org.sonews.feed.FeedManager; chris@1: import org.sonews.util.Stats; franta-hg@115: import org.sonews.util.io.SMTPInputStream; chris@1: chris@1: /** chris@1: * Implementation of the POST command. This command requires multiple lines chris@1: * from the client, so the handling of asynchronous reading is a little tricky chris@1: * to handle. chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ cli@42: public class PostCommand implements Command { chris@3: cli@37: private final Article article = new Article(); cli@37: private int lineCount = 0; cli@37: private long bodySize = 0; cli@37: private InternetHeaders headers = null; cli@37: private long maxBodySize = cli@42: Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes cli@37: private PostState state = PostState.WaitForLineOne; cli@37: private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream(); cli@37: private final StringBuilder strHead = new StringBuilder(); chris@1: cli@37: @Override cli@42: public String[] getSupportedCommandStrings() { cli@42: return new String[]{"POST"}; cli@37: } chris@1: cli@37: @Override cli@42: public boolean hasFinished() { cli@37: return this.state == PostState.Finished; cli@37: } cli@20: cli@37: @Override cli@42: public String impliedCapability() { cli@37: return null; cli@37: } chris@3: cli@37: @Override cli@42: public boolean isStateful() { cli@37: return true; cli@37: } chris@1: cli@37: /** cli@37: * Process the given line String. line.trim() was called by NNTPConnection. cli@37: * @param line cli@37: * @throws java.io.IOException cli@37: * @throws java.sql.SQLException cli@37: */ cli@37: @Override // TODO: Refactor this method to reduce complexity! cli@37: public void processLine(NNTPConnection conn, String line, byte[] raw) cli@42: throws IOException, StorageBackendException { cli@37: switch (state) { cli@37: case WaitForLineOne: { cli@37: if (line.equalsIgnoreCase("POST")) { cli@37: conn.println("340 send article to be posted. End with ."); cli@37: state = PostState.ReadingHeaders; cli@37: } else { cli@37: conn.println("500 invalid command usage"); cli@37: } cli@37: break; cli@37: } cli@37: case ReadingHeaders: { cli@37: strHead.append(line); cli@37: strHead.append(NNTPConnection.NEWLINE); chris@1: cli@37: if ("".equals(line) || ".".equals(line)) { cli@37: // we finally met the blank line cli@37: // separating headers from body franta-hg@115: // WTF: "." chris@3: cli@37: try { cli@37: // Parse the header using the InternetHeader class from JavaMail API cli@37: headers = new InternetHeaders( cli@42: new ByteArrayInputStream(strHead.toString().trim().getBytes(conn.getCurrentCharset()))); cli@18: cli@37: // add the header entries for the article cli@37: article.setHeaders(headers); cli@50: } catch (MessagingException ex) { cli@50: Log.get().log(Level.INFO, ex.getLocalizedMessage(), ex); cli@37: conn.println("500 posting failed - invalid header"); cli@37: state = PostState.Finished; cli@37: break; cli@37: } chris@1: cli@37: // Change charset for reading body; cli@37: // for multipart messages UTF-8 is returned cli@37: //conn.setCurrentCharset(article.getBodyCharset()); chris@1: cli@37: state = PostState.ReadingBody; chris@1: franta-hg@115: // WTF: do we need articles without bodies? cli@37: if (".".equals(line)) { cli@37: // Post an article without body cli@37: postArticle(conn, article); cli@37: state = PostState.Finished; cli@37: } cli@37: } cli@37: break; cli@37: } cli@37: case ReadingBody: { cli@37: if (".".equals(line)) { cli@37: // Set some headers needed for Over command cli@37: headers.setHeader(Headers.LINES, Integer.toString(lineCount)); cli@37: headers.setHeader(Headers.BYTES, Long.toString(bodySize)); cli@37: franta-hg@115: byte[] body = unescapeDots(bufBody.toByteArray()); cli@37: if (body.length >= 2) { cli@37: // Remove trailing CRLF cli@37: body = Arrays.copyOf(body, body.length - 2); cli@37: } cli@37: article.setBody(body); // set the article body cli@37: cli@37: postArticle(conn, article); cli@37: state = PostState.Finished; cli@37: } else { cli@37: bodySize += line.length() + 1; cli@37: lineCount++; cli@37: cli@37: // Add line to body buffer cli@37: bufBody.write(raw, 0, raw.length); cli@37: bufBody.write(NNTPConnection.NEWLINE.getBytes()); cli@37: cli@37: if (bodySize > maxBodySize) { cli@37: conn.println("500 article is too long"); cli@37: state = PostState.Finished; cli@37: break; cli@37: } cli@37: } cli@37: break; cli@37: } cli@37: default: { cli@37: // Should never happen cli@37: Log.get().severe("PostCommand::processLine(): already finished..."); cli@37: } cli@37: } cli@37: } cli@37: cli@37: /** cli@37: * Article is a control message and needs special handling. cli@37: * @param article cli@37: */ cli@37: private void controlMessage(NNTPConnection conn, Article article) cli@42: throws IOException { cli@37: String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" "); cli@37: if (ctrl.length == 2) // "cancel " cli@37: { cli@37: try { cli@37: StorageManager.current().delete(ctrl[1]); cli@37: cli@37: // Move cancel message to "control" group cli@37: article.setHeader(Headers.NEWSGROUPS, "control"); cli@37: StorageManager.current().addArticle(article); cli@37: conn.println("240 article cancelled"); cli@37: } catch (StorageBackendException ex) { cli@37: Log.get().severe(ex.toString()); cli@37: conn.println("500 internal server error"); cli@37: } cli@37: } else { cli@37: conn.println("441 unknown control header"); cli@37: } cli@37: } cli@37: cli@37: private void supersedeMessage(NNTPConnection conn, Article article) cli@42: throws IOException { cli@37: try { cli@37: String oldMsg = article.getHeader(Headers.SUPERSEDES)[0]; cli@37: StorageManager.current().delete(oldMsg); cli@37: StorageManager.current().addArticle(article); cli@37: conn.println("240 article replaced"); cli@37: } catch (StorageBackendException ex) { cli@37: Log.get().severe(ex.toString()); cli@37: conn.println("500 internal server error"); cli@37: } cli@37: } cli@37: cli@37: private void postArticle(NNTPConnection conn, Article article) cli@42: throws IOException { franta-hg@117: article.setUser(conn.getUser()); franta-hg@115: cli@37: if (article.getHeader(Headers.CONTROL)[0].length() > 0) { cli@37: controlMessage(conn, article); cli@37: } else if (article.getHeader(Headers.SUPERSEDES)[0].length() > 0) { cli@37: supersedeMessage(conn, article); cli@50: } else { // Post the article regularily cli@37: // Circle check; note that Path can already contain the hostname here cli@37: String host = Config.inst().get(Config.HOSTNAME, "localhost"); cli@37: if (article.getHeader(Headers.PATH)[0].indexOf(host + "!", 1) > 0) { franta-hg@101: Log.get().log(Level.INFO, "{0} skipped for host {1}", new Object[]{article.getMessageID(), host}); cli@37: conn.println("441 I know this article already"); cli@37: return; cli@37: } cli@37: cli@37: // Try to create the article in the database or post it to cli@37: // appropriate mailing list cli@37: try { cli@37: boolean success = false; cli@37: String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(","); cli@37: for (String groupname : groupnames) { cli@37: Group group = StorageManager.current().getGroup(groupname); cli@37: if (group != null && !group.isDeleted()) { cli@37: if (group.isMailingList() && !conn.isLocalConnection()) { cli@37: // Send to mailing list; the Dispatcher writes cli@37: // statistics to database cli@50: success = Dispatcher.toList(article, group.getName()); cli@37: } else { cli@37: // Store in database cli@37: if (!StorageManager.current().isArticleExisting(article.getMessageID())) { cli@37: StorageManager.current().addArticle(article); cli@37: cli@37: // Log this posting to statistics cli@37: Stats.getInstance().mailPosted( cli@42: article.getHeader(Headers.NEWSGROUPS)[0]); cli@37: } cli@37: success = true; cli@37: } cli@37: } cli@37: } // end for cli@37: cli@37: if (success) { cli@37: conn.println("240 article posted ok"); cli@37: FeedManager.queueForPush(article); cli@37: } else { cli@50: conn.println("441 newsgroup not found or configuration error"); cli@37: } cli@37: } catch (AddressException ex) { cli@37: Log.get().warning(ex.getMessage()); cli@37: conn.println("441 invalid sender address"); cli@37: } catch (MessagingException ex) { cli@37: // A MessageException is thrown when the sender email address is cli@37: // invalid or something is wrong with the SMTP server. cli@37: System.err.println(ex.getLocalizedMessage()); cli@37: conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage()); cli@37: } catch (StorageBackendException ex) { cli@37: ex.printStackTrace(); cli@37: conn.println("500 internal server error"); cli@37: } cli@37: } cli@37: } franta-hg@115: franta-hg@115: /** franta-hg@115: * TODO: rework, integrate into NNTPConnection franta-hg@115: * franta-hg@115: * @param body message body with doubled dots franta-hg@115: * @return message body with unescaped dots (.. → .) franta-hg@115: */ franta-hg@115: private static byte[] unescapeDots(byte[] body) throws IOException { franta-hg@115: byte[] result = new byte[body.length]; franta-hg@115: int resultLength = 0; franta-hg@115: franta-hg@115: ByteArrayInputStream escapedInput = new ByteArrayInputStream(body); franta-hg@115: SMTPInputStream unescapedInput = new SMTPInputStream(escapedInput); franta-hg@115: franta-hg@115: int ch = unescapedInput.read(); franta-hg@115: while (ch >= 0) { franta-hg@115: result[resultLength] = (byte) ch; franta-hg@115: resultLength++; franta-hg@115: ch = unescapedInput.read(); franta-hg@115: } franta-hg@115: franta-hg@115: return Arrays.copyOfRange(result, 0, resultLength); franta-hg@115: } chris@1: }