PullFeeder sends an addition "MODE READER" to peers.
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.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.nio.charset.Charset;
25 import java.nio.charset.IllegalCharsetNameException;
26 import java.nio.charset.UnsupportedCharsetException;
27 import java.sql.SQLException;
28 import java.util.Arrays;
29 import java.util.Locale;
30 import javax.mail.MessagingException;
31 import javax.mail.internet.AddressException;
32 import javax.mail.internet.InternetHeaders;
33 import org.sonews.config.Config;
34 import org.sonews.util.Log;
35 import org.sonews.mlgw.Dispatcher;
36 import org.sonews.storage.Article;
37 import org.sonews.storage.Group;
38 import org.sonews.daemon.NNTPConnection;
39 import org.sonews.storage.Headers;
40 import org.sonews.storage.StorageBackendException;
41 import org.sonews.storage.StorageManager;
42 import org.sonews.feed.FeedManager;
43 import org.sonews.util.Stats;
46 * Implementation of the POST command. This command requires multiple lines
47 * from the client, so the handling of asynchronous reading is a little tricky
49 * @author Christian Lins
52 public class PostCommand implements Command
55 private final Article article = new Article();
56 private int lineCount = 0;
57 private long bodySize = 0;
58 private InternetHeaders headers = null;
59 private long maxBodySize =
60 Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
61 private PostState state = PostState.WaitForLineOne;
62 private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream();
63 private final StringBuilder strHead = new StringBuilder();
66 public String[] getSupportedCommandStrings()
68 return new String[]{"POST"};
72 public boolean hasFinished()
74 return this.state == PostState.Finished;
78 public boolean isStateful()
84 * Process the given line String. line.trim() was called by NNTPConnection.
86 * @throws java.io.IOException
87 * @throws java.sql.SQLException
89 @Override // TODO: Refactor this method to reduce complexity!
90 public void processLine(NNTPConnection conn, String line, byte[] raw)
91 throws IOException, StorageBackendException
97 if(line.equalsIgnoreCase("POST"))
99 conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
100 state = PostState.ReadingHeaders;
104 conn.println("500 invalid command usage");
110 strHead.append(line);
111 strHead.append(NNTPConnection.NEWLINE);
113 if("".equals(line) || ".".equals(line))
115 // we finally met the blank line
116 // separating headers from body
120 // Parse the header using the InternetHeader class from JavaMail API
121 headers = new InternetHeaders(
122 new ByteArrayInputStream(strHead.toString().trim()
123 .getBytes(conn.getCurrentCharset())));
125 // add the header entries for the article
126 article.setHeaders(headers);
128 catch (MessagingException e)
131 conn.println("500 posting failed - invalid header");
132 state = PostState.Finished;
136 // Change charset for reading body;
137 // for multipart messages UTF-8 is returned
138 //conn.setCurrentCharset(article.getBodyCharset());
140 state = PostState.ReadingBody;
144 // Post an article without body
145 postArticle(conn, article);
146 state = PostState.Finished;
155 // Set some headers needed for Over command
156 headers.setHeader(Headers.LINES, Integer.toString(lineCount));
157 headers.setHeader(Headers.BYTES, Long.toString(bodySize));
159 byte[] body = bufBody.toByteArray();
162 // Remove trailing CRLF
163 body = Arrays.copyOf(body, body.length - 2);
165 article.setBody(body); // set the article body
167 postArticle(conn, article);
168 state = PostState.Finished;
172 bodySize += line.length() + 1;
175 // Add line to body buffer
176 bufBody.write(raw, 0, raw.length);
177 bufBody.write(NNTPConnection.NEWLINE.getBytes());
179 if(bodySize > maxBodySize)
181 conn.println("500 article is too long");
182 state = PostState.Finished;
186 // Check if this message is a MIME-multipart message and needs a
190 line = line.toLowerCase(Locale.ENGLISH);
191 if(line.startsWith(Headers.CONTENT_TYPE))
193 int idxStart = line.indexOf("charset=") + "charset=".length();
194 int idxEnd = line.indexOf(";", idxStart);
197 idxEnd = line.length();
202 String charsetName = line.substring(idxStart, idxEnd);
203 if(charsetName.length() > 0 && charsetName.charAt(0) == '"')
205 charsetName = charsetName.substring(1, charsetName.length() - 1);
210 conn.setCurrentCharset(Charset.forName(charsetName));
212 catch(IllegalCharsetNameException ex)
214 Log.msg("PostCommand: " + ex, false);
216 catch(UnsupportedCharsetException ex)
218 Log.msg("PostCommand: " + ex, false);
220 } // if(idxStart > 0)
225 ex.printStackTrace();
232 // Should never happen
233 Log.msg("PostCommand::processLine(): already finished...", false);
239 * Article is a control message and needs special handling.
242 private void controlMessage(NNTPConnection conn, Article article)
245 String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
246 if(ctrl.length == 2) // "cancel <mid>"
250 StorageManager.current().delete(ctrl[1]);
252 // Move cancel message to "control" group
253 article.setHeader(Headers.NEWSGROUPS, "control");
254 StorageManager.current().addArticle(article);
255 conn.println("240 article cancelled");
257 catch(StorageBackendException ex)
260 conn.println("500 internal server error");
265 conn.println("441 unknown control header");
269 private void supersedeMessage(NNTPConnection conn, Article article)
274 String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
275 StorageManager.current().delete(oldMsg);
276 StorageManager.current().addArticle(article);
277 conn.println("240 article replaced");
279 catch(StorageBackendException ex)
282 conn.println("500 internal server error");
286 private void postArticle(NNTPConnection conn, Article article)
289 if(article.getHeader(Headers.CONTROL)[0].length() > 0)
291 controlMessage(conn, article);
293 else if(article.getHeader(Headers.SUPERSEDES)[0].length() > 0)
295 supersedeMessage(conn, article);
297 else // Post the article regularily
299 // Try to create the article in the database or post it to
300 // appropriate mailing list
303 boolean success = false;
304 String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
305 for(String groupname : groupnames)
307 Group group = StorageManager.current().getGroup(groupname);
308 if(group != null && !group.isDeleted())
310 if(group.isMailingList() && !conn.isLocalConnection())
312 // Send to mailing list; the Dispatcher writes
313 // statistics to database
314 Dispatcher.toList(article);
320 if(!StorageManager.current().isArticleExisting(article.getMessageID()))
322 StorageManager.current().addArticle(article);
324 // Log this posting to statistics
325 Stats.getInstance().mailPosted(
326 article.getHeader(Headers.NEWSGROUPS)[0]);
335 conn.println("240 article posted ok");
336 FeedManager.queueForPush(article);
340 conn.println("441 newsgroup not found");
343 catch(AddressException ex)
345 Log.msg(ex.getMessage(), true);
346 conn.println("441 invalid sender address");
348 catch(MessagingException ex)
350 // A MessageException is thrown when the sender email address is
351 // invalid or something is wrong with the SMTP server.
352 System.err.println(ex.getLocalizedMessage());
353 conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
355 catch(StorageBackendException ex)
357 ex.printStackTrace();
358 conn.println("500 internal server error");