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