src/org/sonews/daemon/command/ArticleCommand.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Oct 2011 00:54:52 +0200
changeset 84 7a3514a9e4fe
parent 48 b78e77619152
child 108 fdc075324ef3
permissions -rwxr-xr-x
Drupal: funkce pro obalování textu <html><body> značkami.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package org.sonews.daemon.command;
    19 
    20 import java.io.IOException;
    21 import org.sonews.storage.Article;
    22 import org.sonews.daemon.NNTPConnection;
    23 import org.sonews.storage.Group;
    24 import org.sonews.storage.StorageBackendException;
    25 
    26 /**
    27  * Class handling the ARTICLE, BODY and HEAD commands.
    28  * @author Christian Lins
    29  * @author Dennis Schwerdel
    30  * @since n3tpd/0.1
    31  */
    32 public class ArticleCommand implements Command {
    33 
    34 	@Override
    35 	public String[] getSupportedCommandStrings() {
    36 		return new String[]{"ARTICLE", "BODY", "HEAD"};
    37 	}
    38 
    39 	@Override
    40 	public boolean hasFinished() {
    41 		return true;
    42 	}
    43 
    44 	@Override
    45 	public String impliedCapability() {
    46 		return null;
    47 	}
    48 
    49 	@Override
    50 	public boolean isStateful() {
    51 		return false;
    52 	}
    53 
    54 	// TODO: Refactor this method to reduce its complexity!
    55 	@Override
    56 	public void processLine(NNTPConnection conn, final String line, byte[] raw)
    57 			throws IOException {
    58 		final String[] command = line.split(" ");
    59 
    60 		Article article = null;
    61 		long artIndex = -1;
    62 		if (command.length == 1) {
    63 			article = conn.getCurrentArticle();
    64 			if (article == null) {
    65 				conn.println("420 no current article has been selected");
    66 				return;
    67 			}
    68 		} else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN)) {
    69 			// Message-ID
    70 			article = Article.getByMessageID(command[1]);
    71 			if (article == null) {
    72 				conn.println("430 no such article found");
    73 				return;
    74 			}
    75 		} else {
    76 			// Message Number
    77 			try {
    78 				Group currentGroup = conn.getCurrentChannel();
    79 				if (currentGroup == null) {
    80 					conn.println("400 no group selected");
    81 					return;
    82 				}
    83 
    84 				artIndex = Long.parseLong(command[1]);
    85 				article = currentGroup.getArticle(artIndex);
    86 			} catch (NumberFormatException ex) {
    87 				ex.printStackTrace();
    88 			} catch (StorageBackendException ex) {
    89 				ex.printStackTrace();
    90 			}
    91 
    92 			if (article == null) {
    93 				conn.println("423 no such article number in this group");
    94 				return;
    95 			}
    96 			conn.setCurrentArticle(article);
    97 		}
    98 
    99 		if (command[0].equalsIgnoreCase("ARTICLE")) {
   100 			conn.println("220 " + artIndex + " " + article.getMessageID()
   101 					+ " article retrieved - head and body follow");
   102 			conn.println(article.getHeaderSource());
   103 			conn.println("");
   104 			conn.println(article.getBody());
   105 			conn.println(".");
   106 		} else if (command[0].equalsIgnoreCase("BODY")) {
   107 			conn.println("222 " + artIndex + " " + article.getMessageID() + " body");
   108 			conn.println(article.getBody());
   109 			conn.println(".");
   110 		} /*
   111 		 * HEAD: This command is mandatory.
   112 		 *
   113 		 * Syntax
   114 		 *    HEAD message-id
   115 		 *    HEAD number
   116 		 *    HEAD
   117 		 *
   118 		 * Responses
   119 		 *
   120 		 * First form (message-id specified)
   121 		 *  221 0|n message-id    Headers follow (multi-line)
   122 		 *  430                   No article with that message-id
   123 		 *
   124 		 * Second form (article number specified)
   125 		 *  221 n message-id      Headers follow (multi-line)
   126 		 *  412                   No newsgroup selected
   127 		 *  423                   No article with that number
   128 		 *
   129 		 * Third form (current article number used)
   130 		 *  221 n message-id      Headers follow (multi-line)
   131 		 *  412                   No newsgroup selected
   132 		 *  420                   Current article number is invalid
   133 		 *
   134 		 * Parameters
   135 		 *  number        Requested article number
   136 		 *  n             Returned article number
   137 		 *  message-id    Article message-id
   138 		 */ else if (command[0].equalsIgnoreCase("HEAD")) {
   139 			conn.println("221 " + artIndex + " " + article.getMessageID()
   140 					+ " Headers follow (multi-line)");
   141 			conn.println(article.getHeaderSource());
   142 			conn.println(".");
   143 		}
   144 	}
   145 }