chris@1
|
1 |
/*
|
chris@1
|
2 |
* SONEWS News Server
|
chris@1
|
3 |
* see AUTHORS for the list of contributors
|
chris@1
|
4 |
*
|
chris@1
|
5 |
* This program is free software: you can redistribute it and/or modify
|
chris@1
|
6 |
* it under the terms of the GNU General Public License as published by
|
chris@1
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
chris@1
|
8 |
* (at your option) any later version.
|
chris@1
|
9 |
*
|
chris@1
|
10 |
* This program is distributed in the hope that it will be useful,
|
chris@1
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
chris@1
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
chris@1
|
13 |
* GNU General Public License for more details.
|
chris@1
|
14 |
*
|
chris@1
|
15 |
* You should have received a copy of the GNU General Public License
|
chris@1
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
chris@1
|
17 |
*/
|
chris@1
|
18 |
package org.sonews.daemon.command;
|
chris@1
|
19 |
|
chris@1
|
20 |
import java.io.IOException;
|
chris@1
|
21 |
import java.io.ByteArrayInputStream;
|
chris@3
|
22 |
import java.io.ByteArrayOutputStream;
|
chris@1
|
23 |
import java.sql.SQLException;
|
chris@3
|
24 |
import java.util.Arrays;
|
cli@50
|
25 |
import java.util.logging.Level;
|
chris@1
|
26 |
import javax.mail.MessagingException;
|
chris@1
|
27 |
import javax.mail.internet.AddressException;
|
chris@1
|
28 |
import javax.mail.internet.InternetHeaders;
|
chris@3
|
29 |
import org.sonews.config.Config;
|
chris@1
|
30 |
import org.sonews.util.Log;
|
chris@1
|
31 |
import org.sonews.mlgw.Dispatcher;
|
chris@3
|
32 |
import org.sonews.storage.Article;
|
chris@3
|
33 |
import org.sonews.storage.Group;
|
chris@1
|
34 |
import org.sonews.daemon.NNTPConnection;
|
chris@3
|
35 |
import org.sonews.storage.Headers;
|
chris@3
|
36 |
import org.sonews.storage.StorageBackendException;
|
chris@3
|
37 |
import org.sonews.storage.StorageManager;
|
chris@1
|
38 |
import org.sonews.feed.FeedManager;
|
chris@1
|
39 |
import org.sonews.util.Stats;
|
chris@1
|
40 |
|
chris@1
|
41 |
/**
|
chris@1
|
42 |
* Implementation of the POST command. This command requires multiple lines
|
chris@1
|
43 |
* from the client, so the handling of asynchronous reading is a little tricky
|
chris@1
|
44 |
* to handle.
|
chris@1
|
45 |
* @author Christian Lins
|
chris@1
|
46 |
* @since sonews/0.5.0
|
chris@1
|
47 |
*/
|
cli@42
|
48 |
public class PostCommand implements Command {
|
chris@3
|
49 |
|
cli@37
|
50 |
private final Article article = new Article();
|
cli@37
|
51 |
private int lineCount = 0;
|
cli@37
|
52 |
private long bodySize = 0;
|
cli@37
|
53 |
private InternetHeaders headers = null;
|
cli@37
|
54 |
private long maxBodySize =
|
cli@42
|
55 |
Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
|
cli@37
|
56 |
private PostState state = PostState.WaitForLineOne;
|
cli@37
|
57 |
private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream();
|
cli@37
|
58 |
private final StringBuilder strHead = new StringBuilder();
|
chris@1
|
59 |
|
cli@37
|
60 |
@Override
|
cli@42
|
61 |
public String[] getSupportedCommandStrings() {
|
cli@42
|
62 |
return new String[]{"POST"};
|
cli@37
|
63 |
}
|
chris@1
|
64 |
|
cli@37
|
65 |
@Override
|
cli@42
|
66 |
public boolean hasFinished() {
|
cli@37
|
67 |
return this.state == PostState.Finished;
|
cli@37
|
68 |
}
|
cli@20
|
69 |
|
cli@37
|
70 |
@Override
|
cli@42
|
71 |
public String impliedCapability() {
|
cli@37
|
72 |
return null;
|
cli@37
|
73 |
}
|
chris@3
|
74 |
|
cli@37
|
75 |
@Override
|
cli@42
|
76 |
public boolean isStateful() {
|
cli@37
|
77 |
return true;
|
cli@37
|
78 |
}
|
chris@1
|
79 |
|
cli@37
|
80 |
/**
|
cli@37
|
81 |
* Process the given line String. line.trim() was called by NNTPConnection.
|
cli@37
|
82 |
* @param line
|
cli@37
|
83 |
* @throws java.io.IOException
|
cli@37
|
84 |
* @throws java.sql.SQLException
|
cli@37
|
85 |
*/
|
cli@37
|
86 |
@Override // TODO: Refactor this method to reduce complexity!
|
cli@37
|
87 |
public void processLine(NNTPConnection conn, String line, byte[] raw)
|
cli@42
|
88 |
throws IOException, StorageBackendException {
|
cli@37
|
89 |
switch (state) {
|
cli@37
|
90 |
case WaitForLineOne: {
|
cli@37
|
91 |
if (line.equalsIgnoreCase("POST")) {
|
cli@37
|
92 |
conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
|
cli@37
|
93 |
state = PostState.ReadingHeaders;
|
cli@37
|
94 |
} else {
|
cli@37
|
95 |
conn.println("500 invalid command usage");
|
cli@37
|
96 |
}
|
cli@37
|
97 |
break;
|
cli@37
|
98 |
}
|
cli@37
|
99 |
case ReadingHeaders: {
|
cli@37
|
100 |
strHead.append(line);
|
cli@37
|
101 |
strHead.append(NNTPConnection.NEWLINE);
|
chris@1
|
102 |
|
cli@37
|
103 |
if ("".equals(line) || ".".equals(line)) {
|
cli@37
|
104 |
// we finally met the blank line
|
cli@37
|
105 |
// separating headers from body
|
chris@3
|
106 |
|
cli@37
|
107 |
try {
|
cli@37
|
108 |
// Parse the header using the InternetHeader class from JavaMail API
|
cli@37
|
109 |
headers = new InternetHeaders(
|
cli@42
|
110 |
new ByteArrayInputStream(strHead.toString().trim().getBytes(conn.getCurrentCharset())));
|
cli@18
|
111 |
|
cli@37
|
112 |
// add the header entries for the article
|
cli@37
|
113 |
article.setHeaders(headers);
|
cli@50
|
114 |
} catch (MessagingException ex) {
|
cli@50
|
115 |
Log.get().log(Level.INFO, ex.getLocalizedMessage(), ex);
|
cli@37
|
116 |
conn.println("500 posting failed - invalid header");
|
cli@37
|
117 |
state = PostState.Finished;
|
cli@37
|
118 |
break;
|
cli@37
|
119 |
}
|
chris@1
|
120 |
|
cli@37
|
121 |
// Change charset for reading body;
|
cli@37
|
122 |
// for multipart messages UTF-8 is returned
|
cli@37
|
123 |
//conn.setCurrentCharset(article.getBodyCharset());
|
chris@1
|
124 |
|
cli@37
|
125 |
state = PostState.ReadingBody;
|
chris@1
|
126 |
|
cli@37
|
127 |
if (".".equals(line)) {
|
cli@37
|
128 |
// Post an article without body
|
cli@37
|
129 |
postArticle(conn, article);
|
cli@37
|
130 |
state = PostState.Finished;
|
cli@37
|
131 |
}
|
cli@37
|
132 |
}
|
cli@37
|
133 |
break;
|
cli@37
|
134 |
}
|
cli@37
|
135 |
case ReadingBody: {
|
cli@37
|
136 |
if (".".equals(line)) {
|
cli@37
|
137 |
// Set some headers needed for Over command
|
cli@37
|
138 |
headers.setHeader(Headers.LINES, Integer.toString(lineCount));
|
cli@37
|
139 |
headers.setHeader(Headers.BYTES, Long.toString(bodySize));
|
cli@37
|
140 |
|
cli@37
|
141 |
byte[] body = bufBody.toByteArray();
|
cli@37
|
142 |
if (body.length >= 2) {
|
cli@37
|
143 |
// Remove trailing CRLF
|
cli@37
|
144 |
body = Arrays.copyOf(body, body.length - 2);
|
cli@37
|
145 |
}
|
cli@37
|
146 |
article.setBody(body); // set the article body
|
cli@37
|
147 |
|
cli@37
|
148 |
postArticle(conn, article);
|
cli@37
|
149 |
state = PostState.Finished;
|
cli@37
|
150 |
} else {
|
cli@37
|
151 |
bodySize += line.length() + 1;
|
cli@37
|
152 |
lineCount++;
|
cli@37
|
153 |
|
cli@37
|
154 |
// Add line to body buffer
|
cli@37
|
155 |
bufBody.write(raw, 0, raw.length);
|
cli@37
|
156 |
bufBody.write(NNTPConnection.NEWLINE.getBytes());
|
cli@37
|
157 |
|
cli@37
|
158 |
if (bodySize > maxBodySize) {
|
cli@37
|
159 |
conn.println("500 article is too long");
|
cli@37
|
160 |
state = PostState.Finished;
|
cli@37
|
161 |
break;
|
cli@37
|
162 |
}
|
cli@37
|
163 |
}
|
cli@37
|
164 |
break;
|
cli@37
|
165 |
}
|
cli@37
|
166 |
default: {
|
cli@37
|
167 |
// Should never happen
|
cli@37
|
168 |
Log.get().severe("PostCommand::processLine(): already finished...");
|
cli@37
|
169 |
}
|
cli@37
|
170 |
}
|
cli@37
|
171 |
}
|
cli@37
|
172 |
|
cli@37
|
173 |
/**
|
cli@37
|
174 |
* Article is a control message and needs special handling.
|
cli@37
|
175 |
* @param article
|
cli@37
|
176 |
*/
|
cli@37
|
177 |
private void controlMessage(NNTPConnection conn, Article article)
|
cli@42
|
178 |
throws IOException {
|
cli@37
|
179 |
String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
|
cli@37
|
180 |
if (ctrl.length == 2) // "cancel <mid>"
|
cli@37
|
181 |
{
|
cli@37
|
182 |
try {
|
cli@37
|
183 |
StorageManager.current().delete(ctrl[1]);
|
cli@37
|
184 |
|
cli@37
|
185 |
// Move cancel message to "control" group
|
cli@37
|
186 |
article.setHeader(Headers.NEWSGROUPS, "control");
|
cli@37
|
187 |
StorageManager.current().addArticle(article);
|
cli@37
|
188 |
conn.println("240 article cancelled");
|
cli@37
|
189 |
} catch (StorageBackendException ex) {
|
cli@37
|
190 |
Log.get().severe(ex.toString());
|
cli@37
|
191 |
conn.println("500 internal server error");
|
cli@37
|
192 |
}
|
cli@37
|
193 |
} else {
|
cli@37
|
194 |
conn.println("441 unknown control header");
|
cli@37
|
195 |
}
|
cli@37
|
196 |
}
|
cli@37
|
197 |
|
cli@37
|
198 |
private void supersedeMessage(NNTPConnection conn, Article article)
|
cli@42
|
199 |
throws IOException {
|
cli@37
|
200 |
try {
|
cli@37
|
201 |
String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
|
cli@37
|
202 |
StorageManager.current().delete(oldMsg);
|
cli@37
|
203 |
StorageManager.current().addArticle(article);
|
cli@37
|
204 |
conn.println("240 article replaced");
|
cli@37
|
205 |
} catch (StorageBackendException ex) {
|
cli@37
|
206 |
Log.get().severe(ex.toString());
|
cli@37
|
207 |
conn.println("500 internal server error");
|
cli@37
|
208 |
}
|
cli@37
|
209 |
}
|
cli@37
|
210 |
|
cli@37
|
211 |
private void postArticle(NNTPConnection conn, Article article)
|
cli@42
|
212 |
throws IOException {
|
cli@37
|
213 |
if (article.getHeader(Headers.CONTROL)[0].length() > 0) {
|
cli@37
|
214 |
controlMessage(conn, article);
|
cli@37
|
215 |
} else if (article.getHeader(Headers.SUPERSEDES)[0].length() > 0) {
|
cli@37
|
216 |
supersedeMessage(conn, article);
|
cli@50
|
217 |
} else { // Post the article regularily
|
cli@37
|
218 |
// Circle check; note that Path can already contain the hostname here
|
cli@37
|
219 |
String host = Config.inst().get(Config.HOSTNAME, "localhost");
|
cli@37
|
220 |
if (article.getHeader(Headers.PATH)[0].indexOf(host + "!", 1) > 0) {
|
cli@37
|
221 |
Log.get().info(article.getMessageID() + " skipped for host " + host);
|
cli@37
|
222 |
conn.println("441 I know this article already");
|
cli@37
|
223 |
return;
|
cli@37
|
224 |
}
|
cli@37
|
225 |
|
cli@37
|
226 |
// Try to create the article in the database or post it to
|
cli@37
|
227 |
// appropriate mailing list
|
cli@37
|
228 |
try {
|
cli@37
|
229 |
boolean success = false;
|
cli@37
|
230 |
String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
|
cli@37
|
231 |
for (String groupname : groupnames) {
|
cli@37
|
232 |
Group group = StorageManager.current().getGroup(groupname);
|
cli@37
|
233 |
if (group != null && !group.isDeleted()) {
|
cli@37
|
234 |
if (group.isMailingList() && !conn.isLocalConnection()) {
|
cli@37
|
235 |
// Send to mailing list; the Dispatcher writes
|
cli@37
|
236 |
// statistics to database
|
cli@50
|
237 |
success = Dispatcher.toList(article, group.getName());
|
cli@37
|
238 |
} else {
|
cli@37
|
239 |
// Store in database
|
cli@37
|
240 |
if (!StorageManager.current().isArticleExisting(article.getMessageID())) {
|
cli@37
|
241 |
StorageManager.current().addArticle(article);
|
cli@37
|
242 |
|
cli@37
|
243 |
// Log this posting to statistics
|
cli@37
|
244 |
Stats.getInstance().mailPosted(
|
cli@42
|
245 |
article.getHeader(Headers.NEWSGROUPS)[0]);
|
cli@37
|
246 |
}
|
cli@37
|
247 |
success = true;
|
cli@37
|
248 |
}
|
cli@37
|
249 |
}
|
cli@37
|
250 |
} // end for
|
cli@37
|
251 |
|
cli@37
|
252 |
if (success) {
|
cli@37
|
253 |
conn.println("240 article posted ok");
|
cli@37
|
254 |
FeedManager.queueForPush(article);
|
cli@37
|
255 |
} else {
|
cli@50
|
256 |
conn.println("441 newsgroup not found or configuration error");
|
cli@37
|
257 |
}
|
cli@37
|
258 |
} catch (AddressException ex) {
|
cli@37
|
259 |
Log.get().warning(ex.getMessage());
|
cli@37
|
260 |
conn.println("441 invalid sender address");
|
cli@37
|
261 |
} catch (MessagingException ex) {
|
cli@37
|
262 |
// A MessageException is thrown when the sender email address is
|
cli@37
|
263 |
// invalid or something is wrong with the SMTP server.
|
cli@37
|
264 |
System.err.println(ex.getLocalizedMessage());
|
cli@37
|
265 |
conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
|
cli@37
|
266 |
} catch (StorageBackendException ex) {
|
cli@37
|
267 |
ex.printStackTrace();
|
cli@37
|
268 |
conn.println("500 internal server error");
|
cli@37
|
269 |
}
|
cli@37
|
270 |
}
|
cli@37
|
271 |
}
|
chris@1
|
272 |
}
|