org/sonews/daemon/command/ArticleCommand.java
author chris <chris@marvin>
Fri, 26 Jun 2009 16:48:50 +0200
changeset 1 6fceb66e1ad7
child 3 2fdc9cc89502
permissions -rw-r--r--
Hooray... sonews/0.5.0 final

HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Remove all lines to abort the collapse operation.
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@1
    22
import java.sql.SQLException;
chris@1
    23
import org.sonews.daemon.storage.Article;
chris@1
    24
import org.sonews.daemon.NNTPConnection;
chris@1
    25
import org.sonews.daemon.storage.Group;
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@1
    33
public class ArticleCommand extends AbstractCommand
chris@1
    34
{
chris@1
    35
  
chris@1
    36
  public ArticleCommand(final NNTPConnection connection)
chris@1
    37
  {
chris@1
    38
    super(connection);
chris@1
    39
  }
chris@1
    40
chris@1
    41
  @Override
chris@1
    42
  public boolean hasFinished()
chris@1
    43
  {
chris@1
    44
    return true;
chris@1
    45
  }
chris@1
    46
chris@1
    47
  // TODO: Refactor this method to reduce its complexity!
chris@1
    48
  @Override
chris@1
    49
  public void processLine(final String line)
chris@1
    50
    throws IOException
chris@1
    51
  {
chris@1
    52
    final String[] command = line.split(" ");
chris@1
    53
    
chris@1
    54
    Article article  = null;
chris@1
    55
    long    artIndex = -1;
chris@1
    56
    if (command.length == 1)
chris@1
    57
    {
chris@1
    58
      article = getCurrentArticle();
chris@1
    59
      if (article == null)
chris@1
    60
      {
chris@1
    61
        printStatus(420, "no current article has been selected");
chris@1
    62
        return;
chris@1
    63
      }
chris@1
    64
    }
chris@1
    65
    else if (command[1].matches(NNTPConnection.MESSAGE_ID_PATTERN))
chris@1
    66
    {
chris@1
    67
      // Message-ID
chris@1
    68
      article = Article.getByMessageID(command[1]);
chris@1
    69
      if (article == null)
chris@1
    70
      {
chris@1
    71
        printStatus(430, "no such article found");
chris@1
    72
        return;
chris@1
    73
      }
chris@1
    74
    }
chris@1
    75
    else
chris@1
    76
    {
chris@1
    77
      // Message Number
chris@1
    78
      try
chris@1
    79
      {
chris@1
    80
        Group currentGroup = connection.getCurrentGroup();
chris@1
    81
        if(currentGroup == null)
chris@1
    82
        {
chris@1
    83
          printStatus(400, "no group selected");
chris@1
    84
          return;
chris@1
    85
        }
chris@1
    86
        
chris@1
    87
        artIndex = Long.parseLong(command[1]);
chris@1
    88
        article  = Article.getByArticleNumber(artIndex, currentGroup);
chris@1
    89
      }
chris@1
    90
      catch(NumberFormatException ex)
chris@1
    91
      {
chris@1
    92
        ex.printStackTrace();
chris@1
    93
      }
chris@1
    94
      catch(SQLException ex)
chris@1
    95
      {
chris@1
    96
        ex.printStackTrace();
chris@1
    97
      }
chris@1
    98
chris@1
    99
      if (article == null)
chris@1
   100
      {
chris@1
   101
        printStatus(423, "no such article number in this group");
chris@1
   102
        return;
chris@1
   103
      }
chris@1
   104
      setCurrentArticle(article);
chris@1
   105
    }
chris@1
   106
chris@1
   107
    if(command[0].equalsIgnoreCase("ARTICLE"))
chris@1
   108
    {
chris@1
   109
      printStatus(220, artIndex + " " + article.getMessageID()
chris@1
   110
          + " article retrieved - head and body follow");
chris@1
   111
      
chris@1
   112
      println(article.getHeaderSource());
chris@1
   113
      
chris@1
   114
      println("");
chris@1
   115
      println(article.getBody(), article.getBodyCharset());
chris@1
   116
      println(".");
chris@1
   117
    }
chris@1
   118
    else if(command[0].equalsIgnoreCase("BODY"))
chris@1
   119
    {
chris@1
   120
      printStatus(222, artIndex + " " + article.getMessageID() + " body");
chris@1
   121
      println(article.getBody(), article.getBodyCharset());
chris@1
   122
      println(".");
chris@1
   123
    }
chris@1
   124
    
chris@1
   125
    /*
chris@1
   126
     * HEAD: This command is mandatory.
chris@1
   127
     *
chris@1
   128
     * Syntax
chris@1
   129
     *    HEAD message-id
chris@1
   130
     *    HEAD number
chris@1
   131
     *    HEAD
chris@1
   132
     *
chris@1
   133
     * Responses
chris@1
   134
     *
chris@1
   135
     * First form (message-id specified)
chris@1
   136
     *  221 0|n message-id    Headers follow (multi-line)
chris@1
   137
     *  430                   No article with that message-id
chris@1
   138
     *
chris@1
   139
     * Second form (article number specified)
chris@1
   140
     *  221 n message-id      Headers follow (multi-line)
chris@1
   141
     *  412                   No newsgroup selected
chris@1
   142
     *  423                   No article with that number
chris@1
   143
     *
chris@1
   144
     * Third form (current article number used)
chris@1
   145
     *  221 n message-id      Headers follow (multi-line)
chris@1
   146
     *  412                   No newsgroup selected
chris@1
   147
     *  420                   Current article number is invalid
chris@1
   148
     *
chris@1
   149
     * Parameters
chris@1
   150
     *  number        Requested article number
chris@1
   151
     *  n             Returned article number
chris@1
   152
     *  message-id    Article message-id
chris@1
   153
     */
chris@1
   154
    else if(command[0].equalsIgnoreCase("HEAD"))
chris@1
   155
    {
chris@1
   156
      printStatus(221, artIndex + " " + article.getMessageID()
chris@1
   157
          + " Headers follow (multi-line)");
chris@1
   158
      
chris@1
   159
      println(article.getHeaderSource());
chris@1
   160
      println(".");
chris@1
   161
    }
chris@1
   162
  }  
chris@1
   163
  
chris@1
   164
}