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: import java.io.ByteArrayInputStream;
chris@3: import java.io.ByteArrayOutputStream;
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@3: import java.util.Arrays;
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@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;
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@3: public class PostCommand implements Command
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@3: Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
chris@1: private PostState state = PostState.WaitForLineOne;
chris@3: private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream();
chris@3: private final StringBuilder strHead = new StringBuilder();
chris@3:
chris@3: @Override
chris@3: public String[] getSupportedCommandStrings()
chris@1: {
chris@3: return new String[]{"POST"};
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@3: @Override
chris@3: public boolean isStateful()
chris@3: {
chris@3: return true;
chris@3: }
chris@3:
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@3: public void processLine(NNTPConnection conn, String line, byte[] raw)
chris@3: throws IOException, StorageBackendException
chris@1: {
chris@1: switch(state)
chris@1: {
chris@1: case WaitForLineOne:
chris@1: {
chris@1: if(line.equalsIgnoreCase("POST"))
chris@1: {
chris@3: conn.println("340 send article to be posted. End with .");
chris@1: state = PostState.ReadingHeaders;
chris@1: }
chris@1: else
chris@1: {
chris@3: conn.println("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@3: .getBytes(conn.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@3: conn.println("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@3: //conn.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@3: postArticle(conn, 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@3:
chris@3: byte[] body = bufBody.toByteArray();
chris@3: if(body.length >= 2)
chris@3: {
chris@3: // Remove trailing CRLF
chris@3: body = Arrays.copyOf(body, body.length - 2);
chris@3: }
chris@3: article.setBody(body); // set the article body
chris@1:
chris@3: postArticle(conn, 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@3: bufBody.write(raw, 0, raw.length);
chris@3: bufBody.write(NNTPConnection.NEWLINE.getBytes());
chris@1:
chris@1: if(bodySize > maxBodySize)
chris@1: {
chris@3: conn.println("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@3: /*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@3: conn.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@3: }*/
chris@1: }
chris@1: break;
chris@1: }
chris@1: default:
chris@3: {
chris@3: // Should never happen
chris@1: Log.msg("PostCommand::processLine(): already finished...", false);
chris@3: }
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@3: private void controlMessage(NNTPConnection conn, 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@3: StorageManager.current().delete(ctrl[1]);
chris@1:
chris@1: // Move cancel message to "control" group
chris@1: article.setHeader(Headers.NEWSGROUPS, "control");
chris@3: StorageManager.current().addArticle(article);
chris@3: conn.println("240 article cancelled");
chris@1: }
chris@3: catch(StorageBackendException ex)
chris@1: {
chris@1: Log.msg(ex, false);
chris@3: conn.println("500 internal server error");
chris@1: }
chris@1: }
chris@1: else
chris@1: {
chris@3: conn.println("441 unknown control header");
chris@1: }
chris@1: }
chris@1:
chris@3: private void supersedeMessage(NNTPConnection conn, Article article)
chris@1: throws IOException
chris@1: {
chris@1: try
chris@1: {
chris@1: String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
chris@3: StorageManager.current().delete(oldMsg);
chris@3: StorageManager.current().addArticle(article);
chris@3: conn.println("240 article replaced");
chris@1: }
chris@3: catch(StorageBackendException ex)
chris@1: {
chris@1: Log.msg(ex, false);
chris@3: conn.println("500 internal server error");
chris@1: }
chris@1: }
chris@1:
chris@3: private void postArticle(NNTPConnection conn, Article article)
chris@1: throws IOException
chris@1: {
chris@1: if(article.getHeader(Headers.CONTROL)[0].length() > 0)
chris@1: {
chris@3: controlMessage(conn, article);
chris@1: }
chris@1: else if(article.getHeader(Headers.SUPERSEDES)[0].length() > 0)
chris@1: {
chris@3: supersedeMessage(conn, 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@3: Group group = StorageManager.current().getGroup(groupname);
chris@3: if(group != null && !group.isDeleted())
chris@1: {
chris@3: if(group.isMailingList() && !conn.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@3: if(!StorageManager.current().isArticleExisting(article.getMessageID()))
chris@1: {
chris@3: StorageManager.current().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@3: conn.println("240 article posted ok");
chris@1: FeedManager.queueForPush(article);
chris@1: }
chris@1: else
chris@1: {
chris@3: conn.println("441 newsgroup not found");
chris@1: }
chris@1: }
chris@1: catch(AddressException ex)
chris@1: {
chris@1: Log.msg(ex.getMessage(), true);
chris@3: conn.println("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@3: conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
chris@1: }
chris@3: catch(StorageBackendException ex)
chris@1: {
chris@1: ex.printStackTrace();
chris@3: conn.println("500 internal server error");
chris@1: }
chris@1: }
chris@1: }
chris@1:
chris@1: }