org/sonews/daemon/command/ListCommand.java
author chris <chris@marvin>
Wed Jul 22 14:04:05 2009 +0200 (2009-07-22)
changeset 3 2fdc9cc89502
parent 1 6fceb66e1ad7
child 12 bb6990c0dd1a
permissions -rw-r--r--
sonews/1.0.0
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.util.List;
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 LIST command.
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 ListCommand implements Command
chris@1
    34
{
chris@1
    35
chris@3
    36
  @Override
chris@3
    37
  public String[] getSupportedCommandStrings()
chris@1
    38
  {
chris@3
    39
    return new String[]{"LIST"};
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@3
    47
chris@3
    48
  @Override
chris@3
    49
  public boolean isStateful()
chris@3
    50
  {
chris@3
    51
    return false;
chris@3
    52
  }
chris@1
    53
  
chris@1
    54
  @Override
chris@3
    55
  public void processLine(NNTPConnection conn, final String line, byte[] raw)
chris@3
    56
    throws IOException, StorageBackendException
chris@1
    57
  {
chris@1
    58
    final String[] command = line.split(" ");
chris@1
    59
    
chris@1
    60
    if (command.length >= 2)
chris@1
    61
    {
chris@1
    62
      if (command[1].equalsIgnoreCase("OVERVIEW.FMT"))
chris@1
    63
      {
chris@3
    64
        conn.println("215 information follows");
chris@3
    65
        conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
chris@3
    66
        conn.println(".");
chris@1
    67
      }
chris@1
    68
      else if (command[1].equalsIgnoreCase("NEWSGROUPS"))
chris@1
    69
      {
chris@3
    70
        conn.println("215 information follows");
chris@3
    71
        final List<Channel> list = Channel.getAll();
chris@3
    72
        for (Channel g : list)
chris@1
    73
        {
chris@3
    74
          conn.println(g.getName() + "\t" + "-");
chris@1
    75
        }
chris@3
    76
        conn.println(".");
chris@1
    77
      }
chris@1
    78
      else if (command[1].equalsIgnoreCase("SUBSCRIPTIONS"))
chris@1
    79
      {
chris@3
    80
        conn.println("215 information follows");
chris@3
    81
        conn.println(".");
chris@1
    82
      }
chris@1
    83
      else if (command[1].equalsIgnoreCase("EXTENSIONS"))
chris@1
    84
      {
chris@3
    85
        conn.println("202 Supported NNTP extensions.");
chris@3
    86
        conn.println("LISTGROUP");
chris@3
    87
        conn.println("XDAEMON");
chris@3
    88
        conn.println("XPAT");
chris@3
    89
        conn.println(".");
chris@1
    90
      }
chris@1
    91
      else
chris@1
    92
      {
chris@3
    93
        conn.println("500 unknown argument to LIST command");
chris@1
    94
      }
chris@1
    95
    }
chris@1
    96
    else
chris@1
    97
    {
chris@3
    98
      final List<Channel> groups = Channel.getAll();
chris@1
    99
      if(groups != null)
chris@1
   100
      {
chris@3
   101
        conn.println("215 list of newsgroups follows");
chris@3
   102
        for (Channel g : groups)
chris@1
   103
        {
chris@3
   104
          if(!g.isDeleted())
chris@3
   105
          {
chris@3
   106
            String writeable = g.isWriteable() ? " y" : " n";
chris@3
   107
            // Indeed first the higher article number then the lower
chris@3
   108
            conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
chris@3
   109
                + g.getFirstArticleNumber() + writeable);
chris@3
   110
          }
chris@1
   111
        }
chris@3
   112
        conn.println(".");
chris@1
   113
      }
chris@1
   114
      else
chris@1
   115
      {
chris@3
   116
        conn.println("500 server database malfunction");
chris@1
   117
      }
chris@1
   118
    }
chris@1
   119
  }
chris@1
   120
chris@1
   121
}