org/sonews/daemon/command/ArticleCommand.java
author cli
Thu Aug 20 16:57:38 2009 +0200 (2009-08-20)
changeset 14 efce4ec25564
parent 1 6fceb66e1ad7
child 20 6ae5e4f8329b
permissions -rw-r--r--
Fix #548: API change; changed parameter type of Storage.getGroupsForList()
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
chris@3
    49
  public boolean isStateful()
chris@3
    50
  {
chris@3
    51
    return false;
chris@3
    52
  }
chris@3
    53
chris@1
    54
  // TODO: Refactor this method to reduce its complexity!
chris@1
    55
  @Override
chris@3
    56
  public void processLine(NNTPConnection conn, final String line, byte[] raw)
chris@1
    57
    throws IOException
chris@1
    58
  {
chris@1
    59
    final String[] command = line.split(" ");
chris@1
    60
    
chris@1
    61
    Article article  = null;
chris@1
    62
    long    artIndex = -1;
chris@1
    63
    if (command.length == 1)
chris@1
    64
    {
chris@3
    65
      article = conn.getCurrentArticle();
chris@1
    66
      if (article == null)
chris@1
    67
      {
chris@3
    68
        conn.println("420 no current article has been selected");
chris@1
    69
        return;
chris@1
    70
      }
chris@1
    71
    }
chris@1
    72
    else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
chris@1
    73
    {
chris@1
    74
      // Message-ID
chris@1
    75
      article = Article.getByMessageID(command[1]);
chris@1
    76
      if (article == null)
chris@1
    77
      {
chris@3
    78
        conn.println("430 no such article found");
chris@1
    79
        return;
chris@1
    80
      }
chris@1
    81
    }
chris@1
    82
    else
chris@1
    83
    {
chris@1
    84
      // Message Number
chris@1
    85
      try
chris@1
    86
      {
chris@3
    87
        Channel currentGroup = conn.getCurrentChannel();
chris@1
    88
        if(currentGroup == null)
chris@1
    89
        {
chris@3
    90
          conn.println("400 no group selected");
chris@1
    91
          return;
chris@1
    92
        }
chris@1
    93
        
chris@1
    94
        artIndex = Long.parseLong(command[1]);
chris@3
    95
        article  = currentGroup.getArticle(artIndex);
chris@1
    96
      }
chris@1
    97
      catch(NumberFormatException ex)
chris@1
    98
      {
chris@1
    99
        ex.printStackTrace();
chris@1
   100
      }
chris@3
   101
      catch(StorageBackendException ex)
chris@1
   102
      {
chris@1
   103
        ex.printStackTrace();
chris@1
   104
      }
chris@1
   105
chris@1
   106
      if (article == null)
chris@1
   107
      {
chris@3
   108
        conn.println("423 no such article number in this group");
chris@1
   109
        return;
chris@1
   110
      }
chris@3
   111
      conn.setCurrentArticle(article);
chris@1
   112
    }
chris@1
   113
chris@1
   114
    if(command[0].equalsIgnoreCase("ARTICLE"))
chris@1
   115
    {
chris@3
   116
      conn.println("220 " + artIndex + " " + article.getMessageID()
chris@1
   117
          + " article retrieved - head and body follow");
chris@3
   118
      conn.println(article.getHeaderSource());
chris@3
   119
      conn.println("");
chris@3
   120
      conn.println(article.getBody());
chris@3
   121
      conn.println(".");
chris@1
   122
    }
chris@1
   123
    else if(command[0].equalsIgnoreCase("BODY"))
chris@1
   124
    {
chris@3
   125
      conn.println("222 " + artIndex + " " + article.getMessageID() + " body");
chris@3
   126
      conn.println(article.getBody());
chris@3
   127
      conn.println(".");
chris@1
   128
    }
chris@1
   129
    
chris@1
   130
    /*
chris@1
   131
     * HEAD: This command is mandatory.
chris@1
   132
     *
chris@1
   133
     * Syntax
chris@1
   134
     *    HEAD message-id
chris@1
   135
     *    HEAD number
chris@1
   136
     *    HEAD
chris@1
   137
     *
chris@1
   138
     * Responses
chris@1
   139
     *
chris@1
   140
     * First form (message-id specified)
chris@1
   141
     *  221 0|n message-id    Headers follow (multi-line)
chris@1
   142
     *  430                   No article with that message-id
chris@1
   143
     *
chris@1
   144
     * Second form (article number specified)
chris@1
   145
     *  221 n message-id      Headers follow (multi-line)
chris@1
   146
     *  412                   No newsgroup selected
chris@1
   147
     *  423                   No article with that number
chris@1
   148
     *
chris@1
   149
     * Third form (current article number used)
chris@1
   150
     *  221 n message-id      Headers follow (multi-line)
chris@1
   151
     *  412                   No newsgroup selected
chris@1
   152
     *  420                   Current article number is invalid
chris@1
   153
     *
chris@1
   154
     * Parameters
chris@1
   155
     *  number        Requested article number
chris@1
   156
     *  n             Returned article number
chris@1
   157
     *  message-id    Article message-id
chris@1
   158
     */
chris@1
   159
    else if(command[0].equalsIgnoreCase("HEAD"))
chris@1
   160
    {
chris@3
   161
      conn.println("221 " + artIndex + " " + article.getMessageID()
chris@1
   162
          + " Headers follow (multi-line)");
chris@3
   163
      conn.println(article.getHeaderSource());
chris@3
   164
      conn.println(".");
chris@1
   165
    }
chris@1
   166
  }  
chris@1
   167
  
chris@1
   168
}