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:
chris@1: package org.sonews.daemon.command;
chris@1:
chris@1: import java.io.IOException;
chris@1:
chris@1: import java.io.ByteArrayInputStream;
chris@1: import java.nio.charset.Charset;
chris@1: import java.nio.charset.IllegalCharsetNameException;
chris@1: import java.nio.charset.UnsupportedCharsetException;
chris@1: import java.sql.SQLException;
chris@1: import java.util.Locale;
chris@1: import javax.mail.MessagingException;
chris@1: import javax.mail.internet.AddressException;
chris@1: import javax.mail.internet.InternetHeaders;
chris@1: import org.sonews.daemon.Config;
chris@1: import org.sonews.util.Log;
chris@1: import org.sonews.mlgw.Dispatcher;
chris@1: import org.sonews.daemon.storage.Article;
chris@1: import org.sonews.daemon.storage.Database;
chris@1: import org.sonews.daemon.storage.Group;
chris@1: import org.sonews.daemon.NNTPConnection;
chris@1: import org.sonews.daemon.storage.Headers;
chris@1: import org.sonews.feed.FeedManager;
chris@1: import org.sonews.util.Stats;
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: */
chris@1: public class PostCommand extends AbstractCommand
chris@1: {
chris@1:
chris@1: private final Article article = new Article();
chris@1: private int lineCount = 0;
chris@1: private long bodySize = 0;
chris@1: private InternetHeaders headers = null;
chris@1: private long maxBodySize =
chris@1: Config.getInstance().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
chris@1: private PostState state = PostState.WaitForLineOne;
chris@1: private final StringBuilder strBody = new StringBuilder();
chris@1: private final StringBuilder strHead = new StringBuilder();
chris@1:
chris@1: public PostCommand(final NNTPConnection conn)
chris@1: {
chris@1: super(conn);
chris@1: }
chris@1:
chris@1: @Override
chris@1: public boolean hasFinished()
chris@1: {
chris@1: return this.state == PostState.Finished;
chris@1: }
chris@1:
chris@1: /**
chris@1: * Process the given line String. line.trim() was called by NNTPConnection.
chris@1: * @param line
chris@1: * @throws java.io.IOException
chris@1: * @throws java.sql.SQLException
chris@1: */
chris@1: @Override // TODO: Refactor this method to reduce complexity!
chris@1: public void processLine(String line)
chris@1: throws IOException, SQLException
chris@1: {
chris@1: switch(state)
chris@1: {
chris@1: case WaitForLineOne:
chris@1: {
chris@1: if(line.equalsIgnoreCase("POST"))
chris@1: {
chris@1: printStatus(340, "send article to be posted. End with .");
chris@1: state = PostState.ReadingHeaders;
chris@1: }
chris@1: else
chris@1: {
chris@1: printStatus(500, "invalid command usage");
chris@1: }
chris@1: break;
chris@1: }
chris@1: case ReadingHeaders:
chris@1: {
chris@1: strHead.append(line);
chris@1: strHead.append(NNTPConnection.NEWLINE);
chris@1:
chris@1: if("".equals(line) || ".".equals(line))
chris@1: {
chris@1: // we finally met the blank line
chris@1: // separating headers from body
chris@1:
chris@1: try
chris@1: {
chris@1: // Parse the header using the InternetHeader class from JavaMail API
chris@1: headers = new InternetHeaders(
chris@1: new ByteArrayInputStream(strHead.toString().trim()
chris@1: .getBytes(connection.getCurrentCharset())));
chris@1:
chris@1: // add the header entries for the article
chris@1: article.setHeaders(headers);
chris@1: }
chris@1: catch (MessagingException e)
chris@1: {
chris@1: e.printStackTrace();
chris@1: printStatus(500, "posting failed - invalid header");
chris@1: state = PostState.Finished;
chris@1: break;
chris@1: }
chris@1:
chris@1: // Change charset for reading body;
chris@1: // for multipart messages UTF-8 is returned
chris@1: connection.setCurrentCharset(article.getBodyCharset());
chris@1:
chris@1: state = PostState.ReadingBody;
chris@1:
chris@1: if(".".equals(line))
chris@1: {
chris@1: // Post an article without body
chris@1: postArticle(article);
chris@1: state = PostState.Finished;
chris@1: }
chris@1: }
chris@1: break;
chris@1: }
chris@1: case ReadingBody:
chris@1: {
chris@1: if(".".equals(line))
chris@1: {
chris@1: // Set some headers needed for Over command
chris@1: headers.setHeader(Headers.LINES, Integer.toString(lineCount));
chris@1: headers.setHeader(Headers.BYTES, Long.toString(bodySize));
chris@1:
chris@1: if(strBody.length() >= 2)
chris@1: {
chris@1: strBody.deleteCharAt(strBody.length() - 1); // Remove last newline
chris@1: strBody.deleteCharAt(strBody.length() - 1); // Remove last CR
chris@1: }
chris@1: article.setBody(strBody.toString()); // set the article body
chris@1:
chris@1: postArticle(article);
chris@1: state = PostState.Finished;
chris@1: }
chris@1: else
chris@1: {
chris@1: bodySize += line.length() + 1;
chris@1: lineCount++;
chris@1:
chris@1: // Add line to body buffer
chris@1: strBody.append(line);
chris@1: strBody.append(NNTPConnection.NEWLINE);
chris@1:
chris@1: if(bodySize > maxBodySize)
chris@1: {
chris@1: printStatus(500, "article is too long");
chris@1: state = PostState.Finished;
chris@1: break;
chris@1: }
chris@1:
chris@1: // Check if this message is a MIME-multipart message and needs a
chris@1: // charset change
chris@1: try
chris@1: {
chris@1: line = line.toLowerCase(Locale.ENGLISH);
chris@1: if(line.startsWith(Headers.CONTENT_TYPE))
chris@1: {
chris@1: int idxStart = line.indexOf("charset=") + "charset=".length();
chris@1: int idxEnd = line.indexOf(";", idxStart);
chris@1: if(idxEnd < 0)
chris@1: {
chris@1: idxEnd = line.length();
chris@1: }
chris@1:
chris@1: if(idxStart > 0)
chris@1: {
chris@1: String charsetName = line.substring(idxStart, idxEnd);
chris@1: if(charsetName.length() > 0 && charsetName.charAt(0) == '"')
chris@1: {
chris@1: charsetName = charsetName.substring(1, charsetName.length() - 1);
chris@1: }
chris@1:
chris@1: try
chris@1: {
chris@1: connection.setCurrentCharset(Charset.forName(charsetName));
chris@1: }
chris@1: catch(IllegalCharsetNameException ex)
chris@1: {
chris@1: Log.msg("PostCommand: " + ex, false);
chris@1: }
chris@1: catch(UnsupportedCharsetException ex)
chris@1: {
chris@1: Log.msg("PostCommand: " + ex, false);
chris@1: }
chris@1: } // if(idxStart > 0)
chris@1: }
chris@1: }
chris@1: catch(Exception ex)
chris@1: {
chris@1: ex.printStackTrace();
chris@1: }
chris@1: }
chris@1: break;
chris@1: }
chris@1: default:
chris@1: Log.msg("PostCommand::processLine(): already finished...", false);
chris@1: }
chris@1: }
chris@1:
chris@1: /**
chris@1: * Article is a control message and needs special handling.
chris@1: * @param article
chris@1: */
chris@1: private void controlMessage(Article article)
chris@1: throws IOException
chris@1: {
chris@1: String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
chris@1: if(ctrl.length == 2) // "cancel "
chris@1: {
chris@1: try
chris@1: {
chris@1: Database.getInstance().delete(ctrl[1]);
chris@1:
chris@1: // Move cancel message to "control" group
chris@1: article.setHeader(Headers.NEWSGROUPS, "control");
chris@1: Database.getInstance().addArticle(article);
chris@1: printStatus(240, "article cancelled");
chris@1: }
chris@1: catch(SQLException ex)
chris@1: {
chris@1: Log.msg(ex, false);
chris@1: printStatus(500, "internal server error");
chris@1: }
chris@1: }
chris@1: else
chris@1: {
chris@1: printStatus(441, "unknown Control header");
chris@1: }
chris@1: }
chris@1:
chris@1: private void supersedeMessage(Article article)
chris@1: throws IOException
chris@1: {
chris@1: try
chris@1: {
chris@1: String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
chris@1: Database.getInstance().delete(oldMsg);
chris@1: Database.getInstance().addArticle(article);
chris@1: printStatus(240, "article replaced");
chris@1: }
chris@1: catch(SQLException ex)
chris@1: {
chris@1: Log.msg(ex, false);
chris@1: printStatus(500, "internal server error");
chris@1: }
chris@1: }
chris@1:
chris@1: private void postArticle(Article article)
chris@1: throws IOException
chris@1: {
chris@1: if(article.getHeader(Headers.CONTROL)[0].length() > 0)
chris@1: {
chris@1: controlMessage(article);
chris@1: }
chris@1: else if(article.getHeader(Headers.SUPERSEDES)[0].length() > 0)
chris@1: {
chris@1: supersedeMessage(article);
chris@1: }
chris@1: else // Post the article regularily
chris@1: {
chris@1: // Try to create the article in the database or post it to
chris@1: // appropriate mailing list
chris@1: try
chris@1: {
chris@1: boolean success = false;
chris@1: String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
chris@1: for(String groupname : groupnames)
chris@1: {
chris@1: Group group = Database.getInstance().getGroup(groupname);
chris@1: if(group != null)
chris@1: {
chris@1: if(group.isMailingList() && !connection.isLocalConnection())
chris@1: {
chris@1: // Send to mailing list; the Dispatcher writes
chris@1: // statistics to database
chris@1: Dispatcher.toList(article);
chris@1: success = true;
chris@1: }
chris@1: else
chris@1: {
chris@1: // Store in database
chris@1: if(!Database.getInstance().isArticleExisting(article.getMessageID()))
chris@1: {
chris@1: Database.getInstance().addArticle(article);
chris@1:
chris@1: // Log this posting to statistics
chris@1: Stats.getInstance().mailPosted(
chris@1: article.getHeader(Headers.NEWSGROUPS)[0]);
chris@1: }
chris@1: success = true;
chris@1: }
chris@1: }
chris@1: } // end for
chris@1:
chris@1: if(success)
chris@1: {
chris@1: printStatus(240, "article posted ok");
chris@1: FeedManager.queueForPush(article);
chris@1: }
chris@1: else
chris@1: {
chris@1: printStatus(441, "newsgroup not found");
chris@1: }
chris@1: }
chris@1: catch(AddressException ex)
chris@1: {
chris@1: Log.msg(ex.getMessage(), true);
chris@1: printStatus(441, "invalid sender address");
chris@1: }
chris@1: catch(MessagingException ex)
chris@1: {
chris@1: // A MessageException is thrown when the sender email address is
chris@1: // invalid or something is wrong with the SMTP server.
chris@1: System.err.println(ex.getLocalizedMessage());
chris@1: printStatus(441, ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
chris@1: }
chris@1: catch(SQLException ex)
chris@1: {
chris@1: ex.printStackTrace();
chris@1: printStatus(500, "internal server error");
chris@1: }
chris@1: }
chris@1: }
chris@1:
chris@1: }