org/sonews/daemon/command/ListCommand.java
author cli
Thu, 20 Aug 2009 22:18:45 +0200
changeset 17 4ae6ada7ea23
parent 12 bb6990c0dd1a
child 20 6ae5e4f8329b
permissions -rw-r--r--
#544 fixed.
     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 java.util.regex.Matcher;
    24 import java.util.regex.Pattern;
    25 import java.util.regex.PatternSyntaxException;
    26 import org.sonews.daemon.NNTPConnection;
    27 import org.sonews.storage.Channel;
    28 import org.sonews.storage.StorageBackendException;
    29 import org.sonews.util.Log;
    30 
    31 /**
    32  * Class handling the LIST command.
    33  * @author Christian Lins
    34  * @author Dennis Schwerdel
    35  * @since n3tpd/0.1
    36  */
    37 public class ListCommand implements Command
    38 {
    39 
    40   @Override
    41   public String[] getSupportedCommandStrings()
    42   {
    43     return new String[]{"LIST"};
    44   }
    45 
    46   @Override
    47   public boolean hasFinished()
    48   {
    49     return true;
    50   }
    51 
    52   @Override
    53   public boolean isStateful()
    54   {
    55     return false;
    56   }
    57   
    58   @Override
    59   public void processLine(NNTPConnection conn, final String line, byte[] raw)
    60     throws IOException, StorageBackendException
    61   {
    62     final String[] command = line.split(" ");
    63     
    64     if(command.length >= 2)
    65     {
    66       if(command[1].equalsIgnoreCase("OVERVIEW.FMT"))
    67       {
    68         conn.println("215 information follows");
    69         conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
    70         conn.println(".");
    71       }
    72       else if(command[1].equalsIgnoreCase("NEWSGROUPS"))
    73       {
    74         conn.println("215 information follows");
    75         final List<Channel> list = Channel.getAll();
    76         for (Channel g : list)
    77         {
    78           conn.println(g.getName() + "\t" + "-");
    79         }
    80         conn.println(".");
    81       }
    82       else if(command[1].equalsIgnoreCase("SUBSCRIPTIONS"))
    83       {
    84         conn.println("215 information follows");
    85         conn.println(".");
    86       }
    87       else if(command[1].equalsIgnoreCase("EXTENSIONS"))
    88       {
    89         conn.println("202 Supported NNTP extensions.");
    90         conn.println("LISTGROUP");
    91         conn.println("XDAEMON");
    92         conn.println("XPAT");
    93         conn.println(".");
    94       }
    95       else if(command[1].equalsIgnoreCase("ACTIVE"))
    96       {
    97         String  pattern  = command.length == 2
    98           ? null : command[2].replace("*", "\\w*");
    99         printGroupInfo(conn, pattern);
   100       }
   101       else
   102       {
   103         conn.println("500 unknown argument to LIST command");
   104       }
   105     }
   106     else
   107     {
   108       printGroupInfo(conn, null);
   109     }
   110   }
   111 
   112   private void printGroupInfo(NNTPConnection conn, String pattern)
   113     throws IOException, StorageBackendException
   114   {
   115     final List<Channel> groups = Channel.getAll();
   116     if(groups != null)
   117     {
   118       conn.println("215 list of newsgroups follows");
   119       for(Channel g : groups)
   120       {
   121         try
   122         {
   123           Matcher matcher = pattern == null ?
   124             null : Pattern.compile(pattern).matcher(g.getName());
   125           if(!g.isDeleted() &&
   126             (matcher == null || matcher.find()))
   127           {
   128             String writeable = g.isWriteable() ? " y" : " n";
   129             // Indeed first the higher article number then the lower
   130             conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
   131               + g.getFirstArticleNumber() + writeable);
   132           }
   133         }
   134         catch(PatternSyntaxException ex)
   135         {
   136           Log.get().info(ex.toString());
   137         }
   138       }
   139       conn.println(".");
   140     }
   141     else
   142     {
   143       conn.println("500 server backend malfunction");
   144     }
   145   }
   146 
   147 }