org/sonews/daemon/command/XPatCommand.java
author cli
Sun, 29 Aug 2010 17:03:21 +0200
changeset 33 f9bf183447d1
parent 20 6ae5e4f8329b
permissions -rw-r--r--
Article(javax.mail.Message) now has a safe method to read the byte body from the given message object (fixes #16).
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@3
    22
import java.util.List;
chris@3
    23
import java.util.Locale;
chris@3
    24
import java.util.regex.PatternSyntaxException;
chris@1
    25
import org.sonews.daemon.NNTPConnection;
chris@3
    26
import org.sonews.storage.StorageBackendException;
chris@3
    27
import org.sonews.storage.StorageManager;
chris@3
    28
import org.sonews.util.Pair;
chris@1
    29
chris@1
    30
/**
chris@1
    31
 * <pre>
chris@1
    32
 *   XPAT header range|<message-id> pat [pat...]
chris@1
    33
 *
chris@1
    34
 *   The XPAT command is used to retrieve specific headers from
chris@1
    35
 *   specific articles, based on pattern matching on the contents of
chris@1
    36
 *   the header. This command was first available in INN.
chris@1
    37
 *
chris@1
    38
 *   The required header parameter is the name of a header line (e.g.
chris@1
    39
 *   "subject") in a news group article. See RFC-1036 for a list
chris@1
    40
 *   of valid header lines. The required range argument may be
chris@1
    41
 *   any of the following:
chris@1
    42
 *               an article number
chris@1
    43
 *               an article number followed by a dash to indicate
chris@1
    44
 *                  all following
chris@1
    45
 *               an article number followed by a dash followed by
chris@1
    46
 *                  another article number
chris@1
    47
 *
chris@1
    48
 *   The required message-id argument indicates a specific
chris@1
    49
 *   article. The range and message-id arguments are mutually
chris@1
    50
 *   exclusive. At least one pattern in wildmat must be specified
chris@1
    51
 *   as well. If there are additional arguments the are joined
chris@1
    52
 *   together separated by a single space to form one complete
chris@1
    53
 *   pattern. Successful responses start with a 221 response
chris@1
    54
 *   followed by a the headers from all messages in which the
chris@1
    55
 *   pattern matched the contents of the specified header line. This
chris@1
    56
 *   includes an empty list. Once the output is complete, a period
chris@1
    57
 *   is sent on a line by itself. If the optional argument is a
chris@1
    58
 *   message-id and no such article exists, the 430 error response
chris@1
    59
 *   is returned. A 502 response will be returned if the client only
chris@1
    60
 *   has permission to transfer articles.
chris@1
    61
 *
chris@1
    62
 *   Responses
chris@1
    63
 *
chris@1
    64
 *       221 Header follows
chris@1
    65
 *       430 no such article
chris@1
    66
 *       502 no permission
chris@3
    67
 *
chris@3
    68
 *   Response Data:
chris@3
    69
 *
chris@3
    70
 *       art_nr fitting_header_value
chris@3
    71
 * 
chris@1
    72
 * </pre>
chris@1
    73
 * [Source:"draft-ietf-nntp-imp-02.txt"] [Copyright: 1998 S. Barber]
chris@1
    74
 * 
chris@1
    75
 * @author Christian Lins
chris@1
    76
 * @since sonews/0.5.0
chris@1
    77
 */
chris@3
    78
public class XPatCommand implements Command
chris@1
    79
{
chris@1
    80
chris@3
    81
  @Override
chris@3
    82
  public String[] getSupportedCommandStrings()
chris@1
    83
  {
chris@3
    84
    return new String[]{"XPAT"};
chris@1
    85
  }
chris@1
    86
  
chris@1
    87
  @Override
chris@1
    88
  public boolean hasFinished()
chris@1
    89
  {
chris@1
    90
    return true;
chris@1
    91
  }
chris@1
    92
chris@1
    93
  @Override
cli@20
    94
  public String impliedCapability()
cli@20
    95
  {
cli@20
    96
    return null;
cli@20
    97
  }
cli@20
    98
cli@20
    99
  @Override
chris@3
   100
  public boolean isStateful()
chris@1
   101
  {
chris@3
   102
    return false;
chris@3
   103
  }
chris@3
   104
chris@3
   105
  @Override
chris@3
   106
  public void processLine(NNTPConnection conn, final String line, byte[] raw)
chris@3
   107
    throws IOException, StorageBackendException
chris@3
   108
  {
chris@3
   109
    if(conn.getCurrentChannel() == null)
chris@3
   110
    {
chris@3
   111
      conn.println("430 no group selected");
chris@3
   112
      return;
chris@3
   113
    }
chris@3
   114
chris@3
   115
    String[] command = line.split("\\p{Space}+");
chris@3
   116
chris@3
   117
    // There may be multiple patterns and Thunderbird produces
chris@3
   118
    // additional spaces between range and pattern
chris@3
   119
    if(command.length >= 4)
chris@3
   120
    {
chris@3
   121
      String header  = command[1].toLowerCase(Locale.US);
chris@3
   122
      String range   = command[2];
chris@3
   123
      String pattern = command[3];
chris@3
   124
chris@3
   125
      long start = -1;
chris@3
   126
      long end   = -1;
chris@3
   127
      if(range.contains("-"))
chris@3
   128
      {
chris@3
   129
        String[] rsplit = range.split("-", 2);
chris@3
   130
        start = Long.parseLong(rsplit[0]);
chris@3
   131
        if(rsplit[1].length() > 0)
chris@3
   132
        {
chris@3
   133
          end = Long.parseLong(rsplit[1]);
chris@3
   134
        }
chris@3
   135
      }
chris@3
   136
      else // TODO: Handle Message-IDs
chris@3
   137
      {
chris@3
   138
        start = Long.parseLong(range);
chris@3
   139
      }
chris@3
   140
chris@3
   141
      try
chris@3
   142
      {
chris@3
   143
        List<Pair<Long, String>> heads = StorageManager.current().
chris@3
   144
          getArticleHeaders(conn.getCurrentChannel(), start, end, header, pattern);
chris@3
   145
        
chris@3
   146
        conn.println("221 header follows");
chris@3
   147
        for(Pair<Long, String> head : heads)
chris@3
   148
        {
chris@3
   149
          conn.println(head.getA() + " " + head.getB());
chris@3
   150
        }
chris@3
   151
        conn.println(".");
chris@3
   152
      }
chris@3
   153
      catch(PatternSyntaxException ex)
chris@3
   154
      {
chris@3
   155
        ex.printStackTrace();
chris@3
   156
        conn.println("500 invalid pattern syntax");
chris@3
   157
      }
chris@3
   158
      catch(StorageBackendException ex)
chris@3
   159
      {
chris@3
   160
        ex.printStackTrace();
chris@3
   161
        conn.println("500 internal server error");
chris@3
   162
      }
chris@3
   163
    }
chris@3
   164
    else
chris@3
   165
    {
chris@3
   166
      conn.println("430 invalid command usage");
chris@3
   167
    }
chris@1
   168
  }
chris@1
   169
chris@1
   170
}