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@3: import org.sonews.storage.Article; chris@1: import org.sonews.daemon.NNTPConnection; chris@3: import org.sonews.storage.Channel; chris@3: import org.sonews.storage.StorageBackendException; 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@3: public class ArticleCommand implements Command chris@1: { chris@3: chris@3: @Override chris@3: public String[] getSupportedCommandStrings() chris@1: { chris@3: return new String[] {"ARTICLE", "BODY", "HEAD"}; chris@1: } chris@1: chris@1: @Override chris@1: public boolean hasFinished() chris@1: { chris@1: return true; chris@1: } chris@1: chris@3: @Override chris@3: public boolean isStateful() chris@3: { chris@3: return false; chris@3: } chris@3: chris@1: // TODO: Refactor this method to reduce its complexity! chris@1: @Override chris@3: public void processLine(NNTPConnection conn, final String line, byte[] raw) 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@3: article = conn.getCurrentArticle(); chris@1: if (article == null) chris@1: { chris@3: conn.println("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@3: conn.println("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@3: Channel currentGroup = conn.getCurrentChannel(); chris@1: if(currentGroup == null) chris@1: { chris@3: conn.println("400 no group selected"); chris@1: return; chris@1: } chris@1: chris@1: artIndex = Long.parseLong(command[1]); chris@3: article = currentGroup.getArticle(artIndex); chris@1: } chris@1: catch(NumberFormatException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@3: catch(StorageBackendException ex) chris@1: { chris@1: ex.printStackTrace(); chris@1: } chris@1: chris@1: if (article == null) chris@1: { chris@3: conn.println("423 no such article number in this group"); chris@1: return; chris@1: } chris@3: conn.setCurrentArticle(article); chris@1: } chris@1: chris@1: if(command[0].equalsIgnoreCase("ARTICLE")) chris@1: { chris@3: conn.println("220 " + artIndex + " " + article.getMessageID() chris@1: + " article retrieved - head and body follow"); chris@3: conn.println(article.getHeaderSource()); chris@3: conn.println(""); chris@3: conn.println(article.getBody()); chris@3: conn.println("."); chris@1: } chris@1: else if(command[0].equalsIgnoreCase("BODY")) chris@1: { chris@3: conn.println("222 " + artIndex + " " + article.getMessageID() + " body"); chris@3: conn.println(article.getBody()); chris@3: conn.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@3: conn.println("221 " + artIndex + " " + article.getMessageID() chris@1: + " Headers follow (multi-line)"); chris@3: conn.println(article.getHeaderSource()); chris@3: conn.println("."); chris@1: } chris@1: } chris@1: chris@1: }