chris@1: /* chris@1: * SONEWS News Server chris@1: * see AUTHORS for the list of contributors chris@1: * chris@1: * This program is free software: you can redistribute it and/or modify chris@1: * it under the terms of the GNU General Public License as published by chris@1: * the Free Software Foundation, either version 3 of the License, or chris@1: * (at your option) any later version. chris@1: * chris@1: * This program is distributed in the hope that it will be useful, chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the chris@1: * GNU General Public License for more details. chris@1: * chris@1: * You should have received a copy of the GNU General Public License chris@1: * along with this program. If not, see . chris@1: */ chris@1: chris@1: package org.sonews.daemon.command; chris@1: chris@1: import java.io.IOException; chris@3: import java.util.List; chris@3: import java.util.Locale; chris@3: import java.util.regex.PatternSyntaxException; chris@1: import org.sonews.daemon.NNTPConnection; chris@3: import org.sonews.storage.StorageBackendException; chris@3: import org.sonews.storage.StorageManager; chris@3: import org.sonews.util.Pair; chris@1: chris@1: /** chris@1: *
chris@1:  *   XPAT header range| pat [pat...]
chris@1:  *
chris@1:  *   The XPAT command is used to retrieve specific headers from
chris@1:  *   specific articles, based on pattern matching on the contents of
chris@1:  *   the header. This command was first available in INN.
chris@1:  *
chris@1:  *   The required header parameter is the name of a header line (e.g.
chris@1:  *   "subject") in a news group article. See RFC-1036 for a list
chris@1:  *   of valid header lines. The required range argument may be
chris@1:  *   any of the following:
chris@1:  *               an article number
chris@1:  *               an article number followed by a dash to indicate
chris@1:  *                  all following
chris@1:  *               an article number followed by a dash followed by
chris@1:  *                  another article number
chris@1:  *
chris@1:  *   The required message-id argument indicates a specific
chris@1:  *   article. The range and message-id arguments are mutually
chris@1:  *   exclusive. At least one pattern in wildmat must be specified
chris@1:  *   as well. If there are additional arguments the are joined
chris@1:  *   together separated by a single space to form one complete
chris@1:  *   pattern. Successful responses start with a 221 response
chris@1:  *   followed by a the headers from all messages in which the
chris@1:  *   pattern matched the contents of the specified header line. This
chris@1:  *   includes an empty list. Once the output is complete, a period
chris@1:  *   is sent on a line by itself. If the optional argument is a
chris@1:  *   message-id and no such article exists, the 430 error response
chris@1:  *   is returned. A 502 response will be returned if the client only
chris@1:  *   has permission to transfer articles.
chris@1:  *
chris@1:  *   Responses
chris@1:  *
chris@1:  *       221 Header follows
chris@1:  *       430 no such article
chris@1:  *       502 no permission
chris@3:  *
chris@3:  *   Response Data:
chris@3:  *
chris@3:  *       art_nr fitting_header_value
chris@3:  * 
chris@1:  * 
chris@1: * [Source:"draft-ietf-nntp-imp-02.txt"] [Copyright: 1998 S. Barber] chris@1: * chris@1: * @author Christian Lins chris@1: * @since sonews/0.5.0 chris@1: */ chris@3: public class XPatCommand implements Command chris@1: { chris@1: cli@37: @Override cli@37: public String[] getSupportedCommandStrings() cli@37: { cli@37: return new String[] {"XPAT"}; cli@37: } chris@1: cli@37: @Override cli@37: public boolean hasFinished() cli@37: { cli@37: return true; cli@37: } cli@20: cli@37: @Override cli@37: public String impliedCapability() cli@37: { cli@37: return null; cli@37: } chris@3: cli@37: @Override cli@37: public boolean isStateful() cli@37: { cli@37: return false; cli@37: } chris@3: cli@37: @Override cli@37: public void processLine(NNTPConnection conn, final String line, byte[] raw) cli@37: throws IOException, StorageBackendException cli@37: { cli@37: if (conn.getCurrentChannel() == null) { cli@37: conn.println("430 no group selected"); cli@37: return; cli@37: } chris@3: cli@37: String[] command = line.split("\\p{Space}+"); chris@3: cli@37: // There may be multiple patterns and Thunderbird produces cli@37: // additional spaces between range and pattern cli@37: if (command.length >= 4) { cli@37: String header = command[1].toLowerCase(Locale.US); cli@37: String range = command[2]; cli@37: String pattern = command[3]; chris@3: cli@37: long start = -1; cli@37: long end = -1; cli@37: if (range.contains("-")) { cli@37: String[] rsplit = range.split("-", 2); cli@37: start = Long.parseLong(rsplit[0]); cli@37: if (rsplit[1].length() > 0) { cli@37: end = Long.parseLong(rsplit[1]); cli@37: } cli@37: } else // TODO: Handle Message-IDs cli@37: { cli@37: start = Long.parseLong(range); cli@37: } chris@1: cli@37: try { cli@37: List> heads = StorageManager.current(). cli@37: getArticleHeaders(conn.getCurrentChannel(), start, end, header, pattern); cli@37: cli@37: conn.println("221 header follows"); cli@37: for (Pair head : heads) { cli@37: conn.println(head.getA() + " " + head.getB()); cli@37: } cli@37: conn.println("."); cli@37: } catch (PatternSyntaxException ex) { cli@37: ex.printStackTrace(); cli@37: conn.println("500 invalid pattern syntax"); cli@37: } catch (StorageBackendException ex) { cli@37: ex.printStackTrace(); cli@37: conn.println("500 internal server error"); cli@37: } cli@37: } else { cli@37: conn.println("430 invalid command usage"); cli@37: } cli@37: } chris@1: }