org/sonews/daemon/command/ListCommand.java
author cli
Thu, 20 Aug 2009 16:57:38 +0200
changeset 14 efce4ec25564
parent 12 bb6990c0dd1a
child 17 4ae6ada7ea23
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@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
    
cli@12
    60
    if(command.length >= 2)
chris@1
    61
    {
cli@12
    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
      }
cli@12
    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
      }
cli@12
    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
      }
cli@12
    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
      }
cli@12
    91
      else if(command[1].equalsIgnoreCase("ACTIVE"))
cli@12
    92
      {
cli@12
    93
        // TODO: Implement wildcards for LIST ACTIVE
cli@12
    94
        printGroupInfo(conn);
cli@12
    95
      }
chris@1
    96
      else
chris@1
    97
      {
chris@3
    98
        conn.println("500 unknown argument to LIST command");
chris@1
    99
      }
chris@1
   100
    }
chris@1
   101
    else
chris@1
   102
    {
cli@12
   103
      printGroupInfo(conn);
cli@12
   104
    }
cli@12
   105
  }
cli@12
   106
cli@12
   107
  private void printGroupInfo(NNTPConnection conn)
cli@12
   108
    throws IOException, StorageBackendException
cli@12
   109
  {
cli@12
   110
    final List<Channel> groups = Channel.getAll();
cli@12
   111
    if (groups != null)
cli@12
   112
    {
cli@12
   113
      conn.println("215 list of newsgroups follows");
cli@12
   114
      for (Channel g : groups)
chris@1
   115
      {
cli@12
   116
        if (!g.isDeleted())
chris@1
   117
        {
cli@12
   118
          String writeable = g.isWriteable() ? " y" : " n";
cli@12
   119
          // Indeed first the higher article number then the lower
cli@12
   120
          conn.println(g.getName() + " " + g.getLastArticleNumber() + " " + g.getFirstArticleNumber() + writeable);
chris@1
   121
        }
chris@1
   122
      }
cli@12
   123
      conn.println(".");
cli@12
   124
    }
cli@12
   125
    else
cli@12
   126
    {
cli@12
   127
      conn.println("500 server database malfunction");
chris@1
   128
    }
chris@1
   129
  }
chris@1
   130
chris@1
   131
}