chris@0: /*
chris@0: * StarOffice News Server
chris@0: * see AUTHORS for the list of contributors
chris@0: *
chris@0: * This program is free software: you can redistribute it and/or modify
chris@0: * it under the terms of the GNU General Public License as published by
chris@0: * the Free Software Foundation, either version 3 of the License, or
chris@0: * (at your option) any later version.
chris@0: *
chris@0: * This program is distributed in the hope that it will be useful,
chris@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@0: * GNU General Public License for more details.
chris@0: *
chris@0: * You should have received a copy of the GNU General Public License
chris@0: * along with this program. If not, see .
chris@0: */
chris@0:
chris@0: package com.so.news.command;
chris@0:
chris@0: import java.io.IOException;
chris@0: import java.text.SimpleDateFormat;
chris@0: import java.util.Locale;
chris@0: import java.util.Map;
chris@0: import com.so.news.Debug;
chris@0: import com.so.news.NNTPConnection;
chris@0: import com.so.news.storage.Article;
chris@0:
chris@0: /**
chris@0: * Class handling the ARTICLE command.
chris@0: * @author Christian Lins
chris@0: * @author Dennis Schwerdel
chris@0: */
chris@0: public class ArticleCommand extends Command
chris@0: {
chris@0: public ArticleCommand(NNTPConnection connection)
chris@0: {
chris@0: super(connection);
chris@0: }
chris@0:
chris@0: public boolean process(String[] command) throws IOException
chris@0: {
chris@0: String commandName = command[0];
chris@0:
chris@0: // untested, RFC977 compliant
chris@0: Article article = null;
chris@0: if (command.length <= 1)
chris@0: {
chris@0: article = getCurrentArticle();
chris@0: if (article == null)
chris@0: {
chris@0: printStatus(420, "no current article has been selected");
chris@0: return true;
chris@0: }
chris@0: }
chris@0: else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
chris@0: {
chris@0: // Message-ID
chris@0: article = Article.getByMessageID(command[1]);
chris@0: if (article == null)
chris@0: {
chris@0: printStatus(430, "no such article found");
chris@0: return true;
chris@0: }
chris@0: }
chris@0: else
chris@0: {
chris@0: // Message Number
chris@0: try
chris@0: {
chris@0: int num = Integer.parseInt(command[1]);
chris@0: article = Article.getByNumberInGroup(connection.getCurrentGroup(), num);
chris@0: }
chris@0: catch (Exception ex)
chris@0: {
chris@0: ex.printStackTrace(Debug.getInstance().getStream());
chris@0: System.err.println(ex.getLocalizedMessage());
chris@0: }
chris@0: if (article == null)
chris@0: {
chris@0: printStatus(423, "no such article number in this group");
chris@0: return true;
chris@0: }
chris@0: setCurrentArticle(article);
chris@0: }
chris@0:
chris@0: if (commandName.equalsIgnoreCase("ARTICLE"))
chris@0: {
chris@0: printStatus(220, article.getNumberInGroup() + " " + article.getMessageID()
chris@0: + " article retrieved - head and body follow");
chris@0: Map header = article.getHeader();
chris@0: for(Map.Entry entry : header.entrySet())
chris@0: {
chris@0: if(entry.getKey().equals("Date"))
chris@0: {
chris@0: SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
chris@0: printTextPart("Date: " + sdf.format(article.getDate()));
chris@0: }
chris@0: else
chris@0: printTextPart(entry.getKey() + ": " + entry.getValue());
chris@0: }
chris@0: println("");
chris@0: printText(article.getBody());
chris@0: }
chris@0: else if (commandName.equalsIgnoreCase("HEAD"))
chris@0: {
chris@0: printStatus(500, "No longer supported! Use XOVER instead.");
chris@0: return false;
chris@0: }
chris@0: else if (commandName.equalsIgnoreCase("BODY"))
chris@0: {
chris@0: printStatus(222, article.getNumberInGroup() + " " + article.getMessageID()
chris@0: + " body");
chris@0: printText(article.getBody());
chris@0: }
chris@0: else if (commandName.equalsIgnoreCase("STAT"))
chris@0: {
chris@0: printStatus(223, article.getNumberInGroup() + " " + article.getMessageID()
chris@0: + " article retrieved - request text separately");
chris@0: }
chris@0: return true;
chris@0: }
chris@0:
chris@0: }