src/org/sonews/daemon/command/ListCommand.java
changeset 35 ed84c8bdd87b
parent 20 6ae5e4f8329b
child 37 74139325d305
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/daemon/command/ListCommand.java	Sun Aug 29 17:28:58 2010 +0200
     1.3 @@ -0,0 +1,153 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.daemon.command;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.util.List;
    1.26 +import java.util.regex.Matcher;
    1.27 +import java.util.regex.Pattern;
    1.28 +import java.util.regex.PatternSyntaxException;
    1.29 +import org.sonews.daemon.NNTPConnection;
    1.30 +import org.sonews.storage.Channel;
    1.31 +import org.sonews.storage.StorageBackendException;
    1.32 +import org.sonews.util.Log;
    1.33 +
    1.34 +/**
    1.35 + * Class handling the LIST command.
    1.36 + * @author Christian Lins
    1.37 + * @author Dennis Schwerdel
    1.38 + * @since n3tpd/0.1
    1.39 + */
    1.40 +public class ListCommand implements Command
    1.41 +{
    1.42 +
    1.43 +  @Override
    1.44 +  public String[] getSupportedCommandStrings()
    1.45 +  {
    1.46 +    return new String[]{"LIST"};
    1.47 +  }
    1.48 +
    1.49 +  @Override
    1.50 +  public boolean hasFinished()
    1.51 +  {
    1.52 +    return true;
    1.53 +  }
    1.54 +
    1.55 +  @Override
    1.56 +  public String impliedCapability()
    1.57 +  {
    1.58 +    return null;
    1.59 +  }
    1.60 +
    1.61 +  @Override
    1.62 +  public boolean isStateful()
    1.63 +  {
    1.64 +    return false;
    1.65 +  }
    1.66 +  
    1.67 +  @Override
    1.68 +  public void processLine(NNTPConnection conn, final String line, byte[] raw)
    1.69 +    throws IOException, StorageBackendException
    1.70 +  {
    1.71 +    final String[] command = line.split(" ");
    1.72 +    
    1.73 +    if(command.length >= 2)
    1.74 +    {
    1.75 +      if(command[1].equalsIgnoreCase("OVERVIEW.FMT"))
    1.76 +      {
    1.77 +        conn.println("215 information follows");
    1.78 +        conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
    1.79 +        conn.println(".");
    1.80 +      }
    1.81 +      else if(command[1].equalsIgnoreCase("NEWSGROUPS"))
    1.82 +      {
    1.83 +        conn.println("215 information follows");
    1.84 +        final List<Channel> list = Channel.getAll();
    1.85 +        for (Channel g : list)
    1.86 +        {
    1.87 +          conn.println(g.getName() + "\t" + "-");
    1.88 +        }
    1.89 +        conn.println(".");
    1.90 +      }
    1.91 +      else if(command[1].equalsIgnoreCase("SUBSCRIPTIONS"))
    1.92 +      {
    1.93 +        conn.println("215 information follows");
    1.94 +        conn.println(".");
    1.95 +      }
    1.96 +      else if(command[1].equalsIgnoreCase("EXTENSIONS"))
    1.97 +      {
    1.98 +        conn.println("202 Supported NNTP extensions.");
    1.99 +        conn.println("LISTGROUP");
   1.100 +        conn.println("XDAEMON");
   1.101 +        conn.println("XPAT");
   1.102 +        conn.println(".");
   1.103 +      }
   1.104 +      else if(command[1].equalsIgnoreCase("ACTIVE"))
   1.105 +      {
   1.106 +        String  pattern  = command.length == 2
   1.107 +          ? null : command[2].replace("*", "\\w*");
   1.108 +        printGroupInfo(conn, pattern);
   1.109 +      }
   1.110 +      else
   1.111 +      {
   1.112 +        conn.println("500 unknown argument to LIST command");
   1.113 +      }
   1.114 +    }
   1.115 +    else
   1.116 +    {
   1.117 +      printGroupInfo(conn, null);
   1.118 +    }
   1.119 +  }
   1.120 +
   1.121 +  private void printGroupInfo(NNTPConnection conn, String pattern)
   1.122 +    throws IOException, StorageBackendException
   1.123 +  {
   1.124 +    final List<Channel> groups = Channel.getAll();
   1.125 +    if(groups != null)
   1.126 +    {
   1.127 +      conn.println("215 list of newsgroups follows");
   1.128 +      for(Channel g : groups)
   1.129 +      {
   1.130 +        try
   1.131 +        {
   1.132 +          Matcher matcher = pattern == null ?
   1.133 +            null : Pattern.compile(pattern).matcher(g.getName());
   1.134 +          if(!g.isDeleted() &&
   1.135 +            (matcher == null || matcher.find()))
   1.136 +          {
   1.137 +            String writeable = g.isWriteable() ? " y" : " n";
   1.138 +            // Indeed first the higher article number then the lower
   1.139 +            conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
   1.140 +              + g.getFirstArticleNumber() + writeable);
   1.141 +          }
   1.142 +        }
   1.143 +        catch(PatternSyntaxException ex)
   1.144 +        {
   1.145 +          Log.get().info(ex.toString());
   1.146 +        }
   1.147 +      }
   1.148 +      conn.println(".");
   1.149 +    }
   1.150 +    else
   1.151 +    {
   1.152 +      conn.println("500 server backend malfunction");
   1.153 +    }
   1.154 +  }
   1.155 +
   1.156 +}