1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/org/sonews/daemon/command/XPatCommand.java Sun Aug 29 17:43:58 2010 +0200
1.3 @@ -0,0 +1,170 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.daemon.command;
1.23 +
1.24 +import java.io.IOException;
1.25 +import java.util.List;
1.26 +import java.util.Locale;
1.27 +import java.util.regex.PatternSyntaxException;
1.28 +import org.sonews.daemon.NNTPConnection;
1.29 +import org.sonews.storage.StorageBackendException;
1.30 +import org.sonews.storage.StorageManager;
1.31 +import org.sonews.util.Pair;
1.32 +
1.33 +/**
1.34 + * <pre>
1.35 + * XPAT header range|<message-id> pat [pat...]
1.36 + *
1.37 + * The XPAT command is used to retrieve specific headers from
1.38 + * specific articles, based on pattern matching on the contents of
1.39 + * the header. This command was first available in INN.
1.40 + *
1.41 + * The required header parameter is the name of a header line (e.g.
1.42 + * "subject") in a news group article. See RFC-1036 for a list
1.43 + * of valid header lines. The required range argument may be
1.44 + * any of the following:
1.45 + * an article number
1.46 + * an article number followed by a dash to indicate
1.47 + * all following
1.48 + * an article number followed by a dash followed by
1.49 + * another article number
1.50 + *
1.51 + * The required message-id argument indicates a specific
1.52 + * article. The range and message-id arguments are mutually
1.53 + * exclusive. At least one pattern in wildmat must be specified
1.54 + * as well. If there are additional arguments the are joined
1.55 + * together separated by a single space to form one complete
1.56 + * pattern. Successful responses start with a 221 response
1.57 + * followed by a the headers from all messages in which the
1.58 + * pattern matched the contents of the specified header line. This
1.59 + * includes an empty list. Once the output is complete, a period
1.60 + * is sent on a line by itself. If the optional argument is a
1.61 + * message-id and no such article exists, the 430 error response
1.62 + * is returned. A 502 response will be returned if the client only
1.63 + * has permission to transfer articles.
1.64 + *
1.65 + * Responses
1.66 + *
1.67 + * 221 Header follows
1.68 + * 430 no such article
1.69 + * 502 no permission
1.70 + *
1.71 + * Response Data:
1.72 + *
1.73 + * art_nr fitting_header_value
1.74 + *
1.75 + * </pre>
1.76 + * [Source:"draft-ietf-nntp-imp-02.txt"] [Copyright: 1998 S. Barber]
1.77 + *
1.78 + * @author Christian Lins
1.79 + * @since sonews/0.5.0
1.80 + */
1.81 +public class XPatCommand implements Command
1.82 +{
1.83 +
1.84 + @Override
1.85 + public String[] getSupportedCommandStrings()
1.86 + {
1.87 + return new String[]{"XPAT"};
1.88 + }
1.89 +
1.90 + @Override
1.91 + public boolean hasFinished()
1.92 + {
1.93 + return true;
1.94 + }
1.95 +
1.96 + @Override
1.97 + public String impliedCapability()
1.98 + {
1.99 + return null;
1.100 + }
1.101 +
1.102 + @Override
1.103 + public boolean isStateful()
1.104 + {
1.105 + return false;
1.106 + }
1.107 +
1.108 + @Override
1.109 + public void processLine(NNTPConnection conn, final String line, byte[] raw)
1.110 + throws IOException, StorageBackendException
1.111 + {
1.112 + if(conn.getCurrentChannel() == null)
1.113 + {
1.114 + conn.println("430 no group selected");
1.115 + return;
1.116 + }
1.117 +
1.118 + String[] command = line.split("\\p{Space}+");
1.119 +
1.120 + // There may be multiple patterns and Thunderbird produces
1.121 + // additional spaces between range and pattern
1.122 + if(command.length >= 4)
1.123 + {
1.124 + String header = command[1].toLowerCase(Locale.US);
1.125 + String range = command[2];
1.126 + String pattern = command[3];
1.127 +
1.128 + long start = -1;
1.129 + long end = -1;
1.130 + if(range.contains("-"))
1.131 + {
1.132 + String[] rsplit = range.split("-", 2);
1.133 + start = Long.parseLong(rsplit[0]);
1.134 + if(rsplit[1].length() > 0)
1.135 + {
1.136 + end = Long.parseLong(rsplit[1]);
1.137 + }
1.138 + }
1.139 + else // TODO: Handle Message-IDs
1.140 + {
1.141 + start = Long.parseLong(range);
1.142 + }
1.143 +
1.144 + try
1.145 + {
1.146 + List<Pair<Long, String>> heads = StorageManager.current().
1.147 + getArticleHeaders(conn.getCurrentChannel(), start, end, header, pattern);
1.148 +
1.149 + conn.println("221 header follows");
1.150 + for(Pair<Long, String> head : heads)
1.151 + {
1.152 + conn.println(head.getA() + " " + head.getB());
1.153 + }
1.154 + conn.println(".");
1.155 + }
1.156 + catch(PatternSyntaxException ex)
1.157 + {
1.158 + ex.printStackTrace();
1.159 + conn.println("500 invalid pattern syntax");
1.160 + }
1.161 + catch(StorageBackendException ex)
1.162 + {
1.163 + ex.printStackTrace();
1.164 + conn.println("500 internal server error");
1.165 + }
1.166 + }
1.167 + else
1.168 + {
1.169 + conn.println("430 invalid command usage");
1.170 + }
1.171 + }
1.172 +
1.173 +}