org/sonews/daemon/command/ArticleCommand.java
author cli
Wed, 26 Aug 2009 10:47:51 +0200
changeset 22 2541bdb54cb2
parent 20 6ae5e4f8329b
permissions -rw-r--r--
Not longer required to restart server when changing peering settings (#547).
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
chris@1
    19
package org.sonews.daemon.command;
chris@1
    20
chris@1
    21
import java.io.IOException;
chris@3
    22
import org.sonews.storage.Article;
chris@1
    23
import org.sonews.daemon.NNTPConnection;
chris@3
    24
import org.sonews.storage.Channel;
chris@3
    25
import org.sonews.storage.StorageBackendException;
chris@1
    26
chris@1
    27
/**
chris@1
    28
 * Class handling the ARTICLE, BODY and HEAD commands.
chris@1
    29
 * @author Christian Lins
chris@1
    30
 * @author Dennis Schwerdel
chris@1
    31
 * @since n3tpd/0.1
chris@1
    32
 */
chris@3
    33
public class ArticleCommand implements Command
chris@1
    34
{
chris@3
    35
chris@3
    36
  @Override
chris@3
    37
  public String[] getSupportedCommandStrings()
chris@1
    38
  {
chris@3
    39
    return new String[] {"ARTICLE", "BODY", "HEAD"};
chris@1
    40
  }
chris@1
    41
chris@1
    42
  @Override
chris@1
    43
  public boolean hasFinished()
chris@1
    44
  {
chris@1
    45
    return true;
chris@1
    46
  }
chris@1
    47
chris@3
    48
  @Override
cli@20
    49
  public String impliedCapability()
cli@20
    50
  {
cli@20
    51
    return null;
cli@20
    52
  }
cli@20
    53
cli@20
    54
  @Override
chris@3
    55
  public boolean isStateful()
chris@3
    56
  {
chris@3
    57
    return false;
chris@3
    58
  }
chris@3
    59
chris@1
    60
  // TODO: Refactor this method to reduce its complexity!
chris@1
    61
  @Override
chris@3
    62
  public void processLine(NNTPConnection conn, final String line, byte[] raw)
chris@1
    63
    throws IOException
chris@1
    64
  {
chris@1
    65
    final String[] command = line.split(" ");
chris@1
    66
    
chris@1
    67
    Article article  = null;
chris@1
    68
    long    artIndex = -1;
chris@1
    69
    if (command.length == 1)
chris@1
    70
    {
chris@3
    71
      article = conn.getCurrentArticle();
chris@1
    72
      if (article == null)
chris@1
    73
      {
chris@3
    74
        conn.println("420 no current article has been selected");
chris@1
    75
        return;
chris@1
    76
      }
chris@1
    77
    }
chris@1
    78
    else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
chris@1
    79
    {
chris@1
    80
      // Message-ID
chris@1
    81
      article = Article.getByMessageID(command[1]);
chris@1
    82
      if (article == null)
chris@1
    83
      {
chris@3
    84
        conn.println("430 no such article found");
chris@1
    85
        return;
chris@1
    86
      }
chris@1
    87
    }
chris@1
    88
    else
chris@1
    89
    {
chris@1
    90
      // Message Number
chris@1
    91
      try
chris@1
    92
      {
chris@3
    93
        Channel currentGroup = conn.getCurrentChannel();
chris@1
    94
        if(currentGroup == null)
chris@1
    95
        {
chris@3
    96
          conn.println("400 no group selected");
chris@1
    97
          return;
chris@1
    98
        }
chris@1
    99
        
chris@1
   100
        artIndex = Long.parseLong(command[1]);
chris@3
   101
        article  = currentGroup.getArticle(artIndex);
chris@1
   102
      }
chris@1
   103
      catch(NumberFormatException ex)
chris@1
   104
      {
chris@1
   105
        ex.printStackTrace();
chris@1
   106
      }
chris@3
   107
      catch(StorageBackendException ex)
chris@1
   108
      {
chris@1
   109
        ex.printStackTrace();
chris@1
   110
      }
chris@1
   111
chris@1
   112
      if (article == null)
chris@1
   113
      {
chris@3
   114
        conn.println("423 no such article number in this group");
chris@1
   115
        return;
chris@1
   116
      }
chris@3
   117
      conn.setCurrentArticle(article);
chris@1
   118
    }
chris@1
   119
chris@1
   120
    if(command[0].equalsIgnoreCase("ARTICLE"))
chris@1
   121
    {
chris@3
   122
      conn.println("220 " + artIndex + " " + article.getMessageID()
chris@1
   123
          + " article retrieved - head and body follow");
chris@3
   124
      conn.println(article.getHeaderSource());
chris@3
   125
      conn.println("");
chris@3
   126
      conn.println(article.getBody());
chris@3
   127
      conn.println(".");
chris@1
   128
    }
chris@1
   129
    else if(command[0].equalsIgnoreCase("BODY"))
chris@1
   130
    {
chris@3
   131
      conn.println("222 " + artIndex + " " + article.getMessageID() + " body");
chris@3
   132
      conn.println(article.getBody());
chris@3
   133
      conn.println(".");
chris@1
   134
    }
chris@1
   135
    
chris@1
   136
    /*
chris@1
   137
     * HEAD: This command is mandatory.
chris@1
   138
     *
chris@1
   139
     * Syntax
chris@1
   140
     *    HEAD message-id
chris@1
   141
     *    HEAD number
chris@1
   142
     *    HEAD
chris@1
   143
     *
chris@1
   144
     * Responses
chris@1
   145
     *
chris@1
   146
     * First form (message-id specified)
chris@1
   147
     *  221 0|n message-id    Headers follow (multi-line)
chris@1
   148
     *  430                   No article with that message-id
chris@1
   149
     *
chris@1
   150
     * Second form (article number specified)
chris@1
   151
     *  221 n message-id      Headers follow (multi-line)
chris@1
   152
     *  412                   No newsgroup selected
chris@1
   153
     *  423                   No article with that number
chris@1
   154
     *
chris@1
   155
     * Third form (current article number used)
chris@1
   156
     *  221 n message-id      Headers follow (multi-line)
chris@1
   157
     *  412                   No newsgroup selected
chris@1
   158
     *  420                   Current article number is invalid
chris@1
   159
     *
chris@1
   160
     * Parameters
chris@1
   161
     *  number        Requested article number
chris@1
   162
     *  n             Returned article number
chris@1
   163
     *  message-id    Article message-id
chris@1
   164
     */
chris@1
   165
    else if(command[0].equalsIgnoreCase("HEAD"))
chris@1
   166
    {
chris@3
   167
      conn.println("221 " + artIndex + " " + article.getMessageID()
chris@1
   168
          + " Headers follow (multi-line)");
chris@3
   169
      conn.println(article.getHeaderSource());
chris@3
   170
      conn.println(".");
chris@1
   171
    }
chris@1
   172
  }  
chris@1
   173
  
chris@1
   174
}