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