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