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.sql.SQLException; chris@1: import org.sonews.daemon.storage.Article; chris@1: import org.sonews.daemon.NNTPConnection; chris@1: import org.sonews.daemon.storage.Group; chris@1: chris@1: /** chris@1: * Class handling the ARTICLE, BODY and HEAD commands. chris@1: * @author Christian Lins chris@1: * @author Dennis Schwerdel chris@1: * @since n3tpd/0.1 chris@1: */ chris@1: public class ArticleCommand extends AbstractCommand chris@1: { chris@1: chris@1: public ArticleCommand(final NNTPConnection connection) chris@1: { chris@1: super(connection); chris@1: } chris@1: chris@1: @Override chris@1: public boolean hasFinished() chris@1: { chris@1: return true; chris@1: } chris@1: chris@1: // TODO: Refactor this method to reduce its complexity! chris@1: @Override chris@1: public void processLine(final String line) chris@1: throws IOException chris@1: { chris@1: final String[] command = line.split(" "); chris@1: chris@1: Article article = null; chris@1: long artIndex = -1; chris@1: if (command.length == 1) chris@1: { chris@1: article = getCurrentArticle(); chris@1: if (article == null) chris@1: { chris@1: printStatus(420, "no current article has been selected"); chris@1: return; chris@1: } chris@1: } chris@1: else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN)) chris@1: { chris@1: // Message-ID chris@1: article = Article.getByMessageID(command[1]); chris@1: if (article == null) chris@1: { chris@1: printStatus(430, "no such article found"); chris@1: return; chris@1: } chris@1: } chris@1: else chris@1: { chris@1: // Message Number chris@1: try chris@1: { chris@1: Group currentGroup = connection.getCurrentGroup(); chris@1: if(currentGroup == null) chris@1: { chris@1: printStatus(400, "no group selected"); chris@1: return; chris@1: } chris@1: chris@1: artIndex = Long.parseLong(command[1]); chris@1: article = Article.getByArticleNumber(artIndex, currentGroup); chris@1: } chris@1: catch(NumberFormatException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@1: catch(SQLException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@1: chris@1: if (article == null) chris@1: { chris@1: printStatus(423, "no such article number in this group"); chris@1: return; chris@1: } chris@1: setCurrentArticle(article); chris@1: } chris@1: chris@1: if(command[0].equalsIgnoreCase("ARTICLE")) chris@1: { chris@1: printStatus(220, artIndex + " " + article.getMessageID() chris@1: + " article retrieved - head and body follow"); chris@1: chris@1: println(article.getHeaderSource()); chris@1: chris@1: println(""); chris@1: println(article.getBody(), article.getBodyCharset()); chris@1: println("."); chris@1: } chris@1: else if(command[0].equalsIgnoreCase("BODY")) chris@1: { chris@1: printStatus(222, artIndex + " " + article.getMessageID() + " body"); chris@1: println(article.getBody(), article.getBodyCharset()); chris@1: println("."); chris@1: } chris@1: chris@1: /* chris@1: * HEAD: This command is mandatory. chris@1: * chris@1: * Syntax chris@1: * HEAD message-id chris@1: * HEAD number chris@1: * HEAD chris@1: * chris@1: * Responses chris@1: * chris@1: * First form (message-id specified) chris@1: * 221 0|n message-id Headers follow (multi-line) chris@1: * 430 No article with that message-id chris@1: * chris@1: * Second form (article number specified) chris@1: * 221 n message-id Headers follow (multi-line) chris@1: * 412 No newsgroup selected chris@1: * 423 No article with that number chris@1: * chris@1: * Third form (current article number used) chris@1: * 221 n message-id Headers follow (multi-line) chris@1: * 412 No newsgroup selected chris@1: * 420 Current article number is invalid chris@1: * chris@1: * Parameters chris@1: * number Requested article number chris@1: * n Returned article number chris@1: * message-id Article message-id chris@1: */ chris@1: else if(command[0].equalsIgnoreCase("HEAD")) chris@1: { chris@1: printStatus(221, artIndex + " " + article.getMessageID() chris@1: + " Headers follow (multi-line)"); chris@1: chris@1: println(article.getHeaderSource()); chris@1: println("."); chris@1: } chris@1: } chris@1: chris@1: }