org/sonews/daemon/command/ListCommand.java
author chris <chris@marvin>
Wed, 22 Jul 2009 14:04:05 +0200
changeset 3 2fdc9cc89502
parent 1 6fceb66e1ad7
child 12 bb6990c0dd1a
permissions -rw-r--r--
sonews/1.0.0
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon.command;
    20 
    21 import java.io.IOException;
    22 import java.util.List;
    23 import org.sonews.daemon.NNTPConnection;
    24 import org.sonews.storage.Channel;
    25 import org.sonews.storage.StorageBackendException;
    26 
    27 /**
    28  * Class handling the LIST command.
    29  * @author Christian Lins
    30  * @author Dennis Schwerdel
    31  * @since n3tpd/0.1
    32  */
    33 public class ListCommand implements Command
    34 {
    35 
    36   @Override
    37   public String[] getSupportedCommandStrings()
    38   {
    39     return new String[]{"LIST"};
    40   }
    41 
    42   @Override
    43   public boolean hasFinished()
    44   {
    45     return true;
    46   }
    47 
    48   @Override
    49   public boolean isStateful()
    50   {
    51     return false;
    52   }
    53   
    54   @Override
    55   public void processLine(NNTPConnection conn, final String line, byte[] raw)
    56     throws IOException, StorageBackendException
    57   {
    58     final String[] command = line.split(" ");
    59     
    60     if (command.length >= 2)
    61     {
    62       if (command[1].equalsIgnoreCase("OVERVIEW.FMT"))
    63       {
    64         conn.println("215 information follows");
    65         conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
    66         conn.println(".");
    67       }
    68       else if (command[1].equalsIgnoreCase("NEWSGROUPS"))
    69       {
    70         conn.println("215 information follows");
    71         final List<Channel> list = Channel.getAll();
    72         for (Channel g : list)
    73         {
    74           conn.println(g.getName() + "\t" + "-");
    75         }
    76         conn.println(".");
    77       }
    78       else if (command[1].equalsIgnoreCase("SUBSCRIPTIONS"))
    79       {
    80         conn.println("215 information follows");
    81         conn.println(".");
    82       }
    83       else if (command[1].equalsIgnoreCase("EXTENSIONS"))
    84       {
    85         conn.println("202 Supported NNTP extensions.");
    86         conn.println("LISTGROUP");
    87         conn.println("XDAEMON");
    88         conn.println("XPAT");
    89         conn.println(".");
    90       }
    91       else
    92       {
    93         conn.println("500 unknown argument to LIST command");
    94       }
    95     }
    96     else
    97     {
    98       final List<Channel> groups = Channel.getAll();
    99       if(groups != null)
   100       {
   101         conn.println("215 list of newsgroups follows");
   102         for (Channel g : groups)
   103         {
   104           if(!g.isDeleted())
   105           {
   106             String writeable = g.isWriteable() ? " y" : " n";
   107             // Indeed first the higher article number then the lower
   108             conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
   109                 + g.getFirstArticleNumber() + writeable);
   110           }
   111         }
   112         conn.println(".");
   113       }
   114       else
   115       {
   116         conn.println("500 server database malfunction");
   117       }
   118     }
   119   }
   120 
   121 }