src/org/sonews/daemon/command/XPatCommand.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Oct 2011 19:50:23 +0200
changeset 91 70286a6cf338
parent 37 74139325d305
permissions -rwxr-xr-x
Drupal: text/plain – podpora obrázků a dalších vnořených elementů uvnitř odkazů.
     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 			conn.println("430 no group selected");
   111 			return;
   112 		}
   113 
   114 		String[] command = line.split("\\p{Space}+");
   115 
   116 		// There may be multiple patterns and Thunderbird produces
   117 		// additional spaces between range and pattern
   118 		if (command.length >= 4) {
   119 			String header = command[1].toLowerCase(Locale.US);
   120 			String range = command[2];
   121 			String pattern = command[3];
   122 
   123 			long start = -1;
   124 			long end = -1;
   125 			if (range.contains("-")) {
   126 				String[] rsplit = range.split("-", 2);
   127 				start = Long.parseLong(rsplit[0]);
   128 				if (rsplit[1].length() > 0) {
   129 					end = Long.parseLong(rsplit[1]);
   130 				}
   131 			} else // TODO: Handle Message-IDs
   132 			{
   133 				start = Long.parseLong(range);
   134 			}
   135 
   136 			try {
   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 					conn.println(head.getA() + " " + head.getB());
   143 				}
   144 				conn.println(".");
   145 			} catch (PatternSyntaxException ex) {
   146 				ex.printStackTrace();
   147 				conn.println("500 invalid pattern syntax");
   148 			} catch (StorageBackendException ex) {
   149 				ex.printStackTrace();
   150 				conn.println("500 internal server error");
   151 			}
   152 		} else {
   153 			conn.println("430 invalid command usage");
   154 		}
   155 	}
   156 }