1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/daemon/command/ArticleCommand.java Sun Aug 29 17:43:58 2010 +0200
1.3 @@ -0,0 +1,174 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.daemon.command;
1.23 +
1.24 +import java.io.IOException;
1.25 +import org.sonews.storage.Article;
1.26 +import org.sonews.daemon.NNTPConnection;
1.27 +import org.sonews.storage.Channel;
1.28 +import org.sonews.storage.StorageBackendException;
1.29 +
1.30 +/**
1.31 + * Class handling the ARTICLE, BODY and HEAD commands.
1.32 + * @author Christian Lins
1.33 + * @author Dennis Schwerdel
1.34 + * @since n3tpd/0.1
1.35 + */
1.36 +public class ArticleCommand implements Command
1.37 +{
1.38 +
1.39 + @Override
1.40 + public String[] getSupportedCommandStrings()
1.41 + {
1.42 + return new String[] {"ARTICLE", "BODY", "HEAD"};
1.43 + }
1.44 +
1.45 + @Override
1.46 + public boolean hasFinished()
1.47 + {
1.48 + return true;
1.49 + }
1.50 +
1.51 + @Override
1.52 + public String impliedCapability()
1.53 + {
1.54 + return null;
1.55 + }
1.56 +
1.57 + @Override
1.58 + public boolean isStateful()
1.59 + {
1.60 + return false;
1.61 + }
1.62 +
1.63 + // TODO: Refactor this method to reduce its complexity!
1.64 + @Override
1.65 + public void processLine(NNTPConnection conn, final String line, byte[] raw)
1.66 + throws IOException
1.67 + {
1.68 + final String[] command = line.split(" ");
1.69 +
1.70 + Article article = null;
1.71 + long artIndex = -1;
1.72 + if (command.length == 1)
1.73 + {
1.74 + article = conn.getCurrentArticle();
1.75 + if (article == null)
1.76 + {
1.77 + conn.println("420 no current article has been selected");
1.78 + return;
1.79 + }
1.80 + }
1.81 + else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
1.82 + {
1.83 + // Message-ID
1.84 + article = Article.getByMessageID(command[1]);
1.85 + if (article == null)
1.86 + {
1.87 + conn.println("430 no such article found");
1.88 + return;
1.89 + }
1.90 + }
1.91 + else
1.92 + {
1.93 + // Message Number
1.94 + try
1.95 + {
1.96 + Channel currentGroup = conn.getCurrentChannel();
1.97 + if(currentGroup == null)
1.98 + {
1.99 + conn.println("400 no group selected");
1.100 + return;
1.101 + }
1.102 +
1.103 + artIndex = Long.parseLong(command[1]);
1.104 + article = currentGroup.getArticle(artIndex);
1.105 + }
1.106 + catch(NumberFormatException ex)
1.107 + {
1.108 + ex.printStackTrace();
1.109 + }
1.110 + catch(StorageBackendException ex)
1.111 + {
1.112 + ex.printStackTrace();
1.113 + }
1.114 +
1.115 + if (article == null)
1.116 + {
1.117 + conn.println("423 no such article number in this group");
1.118 + return;
1.119 + }
1.120 + conn.setCurrentArticle(article);
1.121 + }
1.122 +
1.123 + if(command[0].equalsIgnoreCase("ARTICLE"))
1.124 + {
1.125 + conn.println("220 " + artIndex + " " + article.getMessageID()
1.126 + + " article retrieved - head and body follow");
1.127 + conn.println(article.getHeaderSource());
1.128 + conn.println("");
1.129 + conn.println(article.getBody());
1.130 + conn.println(".");
1.131 + }
1.132 + else if(command[0].equalsIgnoreCase("BODY"))
1.133 + {
1.134 + conn.println("222 " + artIndex + " " + article.getMessageID() + " body");
1.135 + conn.println(article.getBody());
1.136 + conn.println(".");
1.137 + }
1.138 +
1.139 + /*
1.140 + * HEAD: This command is mandatory.
1.141 + *
1.142 + * Syntax
1.143 + * HEAD message-id
1.144 + * HEAD number
1.145 + * HEAD
1.146 + *
1.147 + * Responses
1.148 + *
1.149 + * First form (message-id specified)
1.150 + * 221 0|n message-id Headers follow (multi-line)
1.151 + * 430 No article with that message-id
1.152 + *
1.153 + * Second form (article number specified)
1.154 + * 221 n message-id Headers follow (multi-line)
1.155 + * 412 No newsgroup selected
1.156 + * 423 No article with that number
1.157 + *
1.158 + * Third form (current article number used)
1.159 + * 221 n message-id Headers follow (multi-line)
1.160 + * 412 No newsgroup selected
1.161 + * 420 Current article number is invalid
1.162 + *
1.163 + * Parameters
1.164 + * number Requested article number
1.165 + * n Returned article number
1.166 + * message-id Article message-id
1.167 + */
1.168 + else if(command[0].equalsIgnoreCase("HEAD"))
1.169 + {
1.170 + conn.println("221 " + artIndex + " " + article.getMessageID()
1.171 + + " Headers follow (multi-line)");
1.172 + conn.println(article.getHeaderSource());
1.173 + conn.println(".");
1.174 + }
1.175 + }
1.176 +
1.177 +}