Fix for #549.
3 * see AUTHORS for the list of contributors
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.
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.
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/>.
19 package org.sonews.daemon.command;
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;
32 * XPAT header range|<message-id> pat [pat...]
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.
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:
43 * an article number followed by a dash to indicate
45 * an article number followed by a dash followed by
46 * another article number
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.
70 * art_nr fitting_header_value
73 * [Source:"draft-ietf-nntp-imp-02.txt"] [Copyright: 1998 S. Barber]
75 * @author Christian Lins
78 public class XPatCommand implements Command
82 public String[] getSupportedCommandStrings()
84 return new String[]{"XPAT"};
88 public boolean hasFinished()
94 public boolean isStateful()
100 public void processLine(NNTPConnection conn, final String line, byte[] raw)
101 throws IOException, StorageBackendException
103 if(conn.getCurrentChannel() == null)
105 conn.println("430 no group selected");
109 String[] command = line.split("\\p{Space}+");
111 // There may be multiple patterns and Thunderbird produces
112 // additional spaces between range and pattern
113 if(command.length >= 4)
115 String header = command[1].toLowerCase(Locale.US);
116 String range = command[2];
117 String pattern = command[3];
121 if(range.contains("-"))
123 String[] rsplit = range.split("-", 2);
124 start = Long.parseLong(rsplit[0]);
125 if(rsplit[1].length() > 0)
127 end = Long.parseLong(rsplit[1]);
130 else // TODO: Handle Message-IDs
132 start = Long.parseLong(range);
137 List<Pair<Long, String>> heads = StorageManager.current().
138 getArticleHeaders(conn.getCurrentChannel(), start, end, header, pattern);
140 conn.println("221 header follows");
141 for(Pair<Long, String> head : heads)
143 conn.println(head.getA() + " " + head.getB());
147 catch(PatternSyntaxException ex)
149 ex.printStackTrace();
150 conn.println("500 invalid pattern syntax");
152 catch(StorageBackendException ex)
154 ex.printStackTrace();
155 conn.println("500 internal server error");
160 conn.println("430 invalid command usage");