src/org/sonews/daemon/command/XPatCommand.java
author cli
Sun Aug 29 17:28:58 2010 +0200 (2010-08-29)
changeset 35 ed84c8bdd87b
parent 20 org/sonews/daemon/command/XPatCommand.java@6ae5e4f8329b
child 37 74139325d305
permissions -rw-r--r--
Moving source files into src/-subdir.
     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 String impliedCapability()
    95   {
    96     return null;
    97   }
    98 
    99   @Override
   100   public boolean isStateful()
   101   {
   102     return false;
   103   }
   104 
   105   @Override
   106   public void processLine(NNTPConnection conn, final String line, byte[] raw)
   107     throws IOException, StorageBackendException
   108   {
   109     if(conn.getCurrentChannel() == null)
   110     {
   111       conn.println("430 no group selected");
   112       return;
   113     }
   114 
   115     String[] command = line.split("\\p{Space}+");
   116 
   117     // There may be multiple patterns and Thunderbird produces
   118     // additional spaces between range and pattern
   119     if(command.length >= 4)
   120     {
   121       String header  = command[1].toLowerCase(Locale.US);
   122       String range   = command[2];
   123       String pattern = command[3];
   124 
   125       long start = -1;
   126       long end   = -1;
   127       if(range.contains("-"))
   128       {
   129         String[] rsplit = range.split("-", 2);
   130         start = Long.parseLong(rsplit[0]);
   131         if(rsplit[1].length() > 0)
   132         {
   133           end = Long.parseLong(rsplit[1]);
   134         }
   135       }
   136       else // TODO: Handle Message-IDs
   137       {
   138         start = Long.parseLong(range);
   139       }
   140 
   141       try
   142       {
   143         List<Pair<Long, String>> heads = StorageManager.current().
   144           getArticleHeaders(conn.getCurrentChannel(), start, end, header, pattern);
   145         
   146         conn.println("221 header follows");
   147         for(Pair<Long, String> head : heads)
   148         {
   149           conn.println(head.getA() + " " + head.getB());
   150         }
   151         conn.println(".");
   152       }
   153       catch(PatternSyntaxException ex)
   154       {
   155         ex.printStackTrace();
   156         conn.println("500 invalid pattern syntax");
   157       }
   158       catch(StorageBackendException ex)
   159       {
   160         ex.printStackTrace();
   161         conn.println("500 internal server error");
   162       }
   163     }
   164     else
   165     {
   166       conn.println("430 invalid command usage");
   167     }
   168   }
   169 
   170 }