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