src/org/sonews/daemon/command/ListCommand.java
author cli
Sun, 29 Aug 2010 17:43:58 +0200
changeset 36 c404a87db5b7
parent 35 ed84c8bdd87b
child 37 74139325d305
permissions -rw-r--r--
Add some checks to prevent #13 happen.
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
cli@20
    53
  public String impliedCapability()
cli@20
    54
  {
cli@20
    55
    return null;
cli@20
    56
  }
cli@20
    57
cli@20
    58
  @Override
chris@3
    59
  public boolean isStateful()
chris@3
    60
  {
chris@3
    61
    return false;
chris@3
    62
  }
chris@1
    63
  
chris@1
    64
  @Override
chris@3
    65
  public void processLine(NNTPConnection conn, final String line, byte[] raw)
chris@3
    66
    throws IOException, StorageBackendException
chris@1
    67
  {
chris@1
    68
    final String[] command = line.split(" ");
chris@1
    69
    
cli@12
    70
    if(command.length >= 2)
chris@1
    71
    {
cli@12
    72
      if(command[1].equalsIgnoreCase("OVERVIEW.FMT"))
chris@1
    73
      {
chris@3
    74
        conn.println("215 information follows");
chris@3
    75
        conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
chris@3
    76
        conn.println(".");
chris@1
    77
      }
cli@12
    78
      else if(command[1].equalsIgnoreCase("NEWSGROUPS"))
chris@1
    79
      {
chris@3
    80
        conn.println("215 information follows");
chris@3
    81
        final List<Channel> list = Channel.getAll();
chris@3
    82
        for (Channel g : list)
chris@1
    83
        {
chris@3
    84
          conn.println(g.getName() + "\t" + "-");
chris@1
    85
        }
chris@3
    86
        conn.println(".");
chris@1
    87
      }
cli@12
    88
      else if(command[1].equalsIgnoreCase("SUBSCRIPTIONS"))
chris@1
    89
      {
chris@3
    90
        conn.println("215 information follows");
chris@3
    91
        conn.println(".");
chris@1
    92
      }
cli@12
    93
      else if(command[1].equalsIgnoreCase("EXTENSIONS"))
chris@1
    94
      {
chris@3
    95
        conn.println("202 Supported NNTP extensions.");
chris@3
    96
        conn.println("LISTGROUP");
chris@3
    97
        conn.println("XDAEMON");
chris@3
    98
        conn.println("XPAT");
chris@3
    99
        conn.println(".");
chris@1
   100
      }
cli@12
   101
      else if(command[1].equalsIgnoreCase("ACTIVE"))
cli@12
   102
      {
cli@17
   103
        String  pattern  = command.length == 2
cli@17
   104
          ? null : command[2].replace("*", "\\w*");
cli@17
   105
        printGroupInfo(conn, pattern);
cli@12
   106
      }
chris@1
   107
      else
chris@1
   108
      {
chris@3
   109
        conn.println("500 unknown argument to LIST command");
chris@1
   110
      }
chris@1
   111
    }
chris@1
   112
    else
chris@1
   113
    {
cli@17
   114
      printGroupInfo(conn, null);
cli@12
   115
    }
cli@12
   116
  }
cli@12
   117
cli@17
   118
  private void printGroupInfo(NNTPConnection conn, String pattern)
cli@12
   119
    throws IOException, StorageBackendException
cli@12
   120
  {
cli@12
   121
    final List<Channel> groups = Channel.getAll();
cli@17
   122
    if(groups != null)
cli@12
   123
    {
cli@12
   124
      conn.println("215 list of newsgroups follows");
cli@17
   125
      for(Channel g : groups)
chris@1
   126
      {
cli@17
   127
        try
chris@1
   128
        {
cli@17
   129
          Matcher matcher = pattern == null ?
cli@17
   130
            null : Pattern.compile(pattern).matcher(g.getName());
cli@17
   131
          if(!g.isDeleted() &&
cli@17
   132
            (matcher == null || matcher.find()))
cli@17
   133
          {
cli@17
   134
            String writeable = g.isWriteable() ? " y" : " n";
cli@17
   135
            // Indeed first the higher article number then the lower
cli@17
   136
            conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
cli@17
   137
              + g.getFirstArticleNumber() + writeable);
cli@17
   138
          }
cli@17
   139
        }
cli@17
   140
        catch(PatternSyntaxException ex)
cli@17
   141
        {
cli@17
   142
          Log.get().info(ex.toString());
chris@1
   143
        }
chris@1
   144
      }
cli@12
   145
      conn.println(".");
cli@12
   146
    }
cli@12
   147
    else
cli@12
   148
    {
cli@17
   149
      conn.println("500 server backend malfunction");
chris@1
   150
    }
chris@1
   151
  }
chris@1
   152
chris@1
   153
}