src/org/sonews/daemon/command/PostCommand.java
author František Kučera <franta-hg@frantovo.cz>
Sun Nov 06 00:08:05 2011 +0100 (2011-11-06)
changeset 116 4ddc1020a154
parent 113 a059aecd1794
child 117 79ce65d63cce
permissions -rwxr-xr-x
Podpora Markdown syntaxe
na začátek odesílané zprávy stačí přidat:
#!markdown
a zpráva se pak prožene Markdown procesorem (spouštěno přes sudo pod jiným uživatelem)
a pak teprve přes standardní XSL transformaci.

1) Vytvořit uživatele a skupinu markdown

2) Do /etc/sudoers přidat:
Cmnd_Alias MARKDOWN = /usr/bin/markdown
%markdown ALL = (%markdown) NOPASSWD : MARKDOWN

3) Uivatele, pod kterým běží NNTP démon, přidat do skupiny markdown
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;
franta-hg@115
    40
import org.sonews.util.io.SMTPInputStream;
chris@1
    41
chris@1
    42
/**
chris@1
    43
 * Implementation of the POST command. This command requires multiple lines
chris@1
    44
 * from the client, so the handling of asynchronous reading is a little tricky
chris@1
    45
 * to handle.
chris@1
    46
 * @author Christian Lins
chris@1
    47
 * @since sonews/0.5.0
chris@1
    48
 */
cli@42
    49
public class PostCommand implements Command {
chris@3
    50
cli@37
    51
	private final Article article = new Article();
cli@37
    52
	private int lineCount = 0;
cli@37
    53
	private long bodySize = 0;
cli@37
    54
	private InternetHeaders headers = null;
cli@37
    55
	private long maxBodySize =
cli@42
    56
			Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
cli@37
    57
	private PostState state = PostState.WaitForLineOne;
cli@37
    58
	private final ByteArrayOutputStream bufBody = new ByteArrayOutputStream();
cli@37
    59
	private final StringBuilder strHead = new StringBuilder();
chris@1
    60
cli@37
    61
	@Override
cli@42
    62
	public String[] getSupportedCommandStrings() {
cli@42
    63
		return new String[]{"POST"};
cli@37
    64
	}
chris@1
    65
cli@37
    66
	@Override
cli@42
    67
	public boolean hasFinished() {
cli@37
    68
		return this.state == PostState.Finished;
cli@37
    69
	}
cli@20
    70
cli@37
    71
	@Override
cli@42
    72
	public String impliedCapability() {
cli@37
    73
		return null;
cli@37
    74
	}
chris@3
    75
cli@37
    76
	@Override
cli@42
    77
	public boolean isStateful() {
cli@37
    78
		return true;
cli@37
    79
	}
chris@1
    80
cli@37
    81
	/**
cli@37
    82
	 * Process the given line String. line.trim() was called by NNTPConnection.
cli@37
    83
	 * @param line
cli@37
    84
	 * @throws java.io.IOException
cli@37
    85
	 * @throws java.sql.SQLException
cli@37
    86
	 */
cli@37
    87
	@Override // TODO: Refactor this method to reduce complexity!
cli@37
    88
	public void processLine(NNTPConnection conn, String line, byte[] raw)
cli@42
    89
			throws IOException, StorageBackendException {
cli@37
    90
		switch (state) {
cli@37
    91
			case WaitForLineOne: {
cli@37
    92
				if (line.equalsIgnoreCase("POST")) {
cli@37
    93
					conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
cli@37
    94
					state = PostState.ReadingHeaders;
cli@37
    95
				} else {
cli@37
    96
					conn.println("500 invalid command usage");
cli@37
    97
				}
cli@37
    98
				break;
cli@37
    99
			}
cli@37
   100
			case ReadingHeaders: {
cli@37
   101
				strHead.append(line);
cli@37
   102
				strHead.append(NNTPConnection.NEWLINE);
chris@1
   103
cli@37
   104
				if ("".equals(line) || ".".equals(line)) {
cli@37
   105
					// we finally met the blank line
cli@37
   106
					// separating headers from body
franta-hg@115
   107
					// WTF: "."
chris@3
   108
cli@37
   109
					try {
cli@37
   110
						// Parse the header using the InternetHeader class from JavaMail API
cli@37
   111
						headers = new InternetHeaders(
cli@42
   112
								new ByteArrayInputStream(strHead.toString().trim().getBytes(conn.getCurrentCharset())));
cli@18
   113
cli@37
   114
						// add the header entries for the article
cli@37
   115
						article.setHeaders(headers);
cli@50
   116
					} catch (MessagingException ex) {
cli@50
   117
						Log.get().log(Level.INFO, ex.getLocalizedMessage(), ex);
cli@37
   118
						conn.println("500 posting failed - invalid header");
cli@37
   119
						state = PostState.Finished;
cli@37
   120
						break;
cli@37
   121
					}
chris@1
   122
cli@37
   123
					// Change charset for reading body;
cli@37
   124
					// for multipart messages UTF-8 is returned
cli@37
   125
					//conn.setCurrentCharset(article.getBodyCharset());
chris@1
   126
cli@37
   127
					state = PostState.ReadingBody;
chris@1
   128
franta-hg@115
   129
					// WTF: do we need articles without bodies?
cli@37
   130
					if (".".equals(line)) {
cli@37
   131
						// Post an article without body
cli@37
   132
						postArticle(conn, article);
cli@37
   133
						state = PostState.Finished;
cli@37
   134
					}
cli@37
   135
				}
cli@37
   136
				break;
cli@37
   137
			}
cli@37
   138
			case ReadingBody: {
cli@37
   139
				if (".".equals(line)) {
cli@37
   140
					// Set some headers needed for Over command
cli@37
   141
					headers.setHeader(Headers.LINES, Integer.toString(lineCount));
cli@37
   142
					headers.setHeader(Headers.BYTES, Long.toString(bodySize));
cli@37
   143
franta-hg@115
   144
					byte[] body = unescapeDots(bufBody.toByteArray());
cli@37
   145
					if (body.length >= 2) {
cli@37
   146
						// Remove trailing CRLF
cli@37
   147
						body = Arrays.copyOf(body, body.length - 2);
cli@37
   148
					}
cli@37
   149
					article.setBody(body); // set the article body
cli@37
   150
cli@37
   151
					postArticle(conn, article);
cli@37
   152
					state = PostState.Finished;
cli@37
   153
				} else {
cli@37
   154
					bodySize += line.length() + 1;
cli@37
   155
					lineCount++;
cli@37
   156
cli@37
   157
					// Add line to body buffer
cli@37
   158
					bufBody.write(raw, 0, raw.length);
cli@37
   159
					bufBody.write(NNTPConnection.NEWLINE.getBytes());
cli@37
   160
cli@37
   161
					if (bodySize > maxBodySize) {
cli@37
   162
						conn.println("500 article is too long");
cli@37
   163
						state = PostState.Finished;
cli@37
   164
						break;
cli@37
   165
					}
cli@37
   166
				}
cli@37
   167
				break;
cli@37
   168
			}
cli@37
   169
			default: {
cli@37
   170
				// Should never happen
cli@37
   171
				Log.get().severe("PostCommand::processLine(): already finished...");
cli@37
   172
			}
cli@37
   173
		}
cli@37
   174
	}
cli@37
   175
cli@37
   176
	/**
cli@37
   177
	 * Article is a control message and needs special handling.
cli@37
   178
	 * @param article
cli@37
   179
	 */
cli@37
   180
	private void controlMessage(NNTPConnection conn, Article article)
cli@42
   181
			throws IOException {
cli@37
   182
		String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
cli@37
   183
		if (ctrl.length == 2) // "cancel <mid>"
cli@37
   184
		{
cli@37
   185
			try {
cli@37
   186
				StorageManager.current().delete(ctrl[1]);
cli@37
   187
cli@37
   188
				// Move cancel message to "control" group
cli@37
   189
				article.setHeader(Headers.NEWSGROUPS, "control");
cli@37
   190
				StorageManager.current().addArticle(article);
cli@37
   191
				conn.println("240 article cancelled");
cli@37
   192
			} catch (StorageBackendException ex) {
cli@37
   193
				Log.get().severe(ex.toString());
cli@37
   194
				conn.println("500 internal server error");
cli@37
   195
			}
cli@37
   196
		} else {
cli@37
   197
			conn.println("441 unknown control header");
cli@37
   198
		}
cli@37
   199
	}
cli@37
   200
cli@37
   201
	private void supersedeMessage(NNTPConnection conn, Article article)
cli@42
   202
			throws IOException {
cli@37
   203
		try {
cli@37
   204
			String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
cli@37
   205
			StorageManager.current().delete(oldMsg);
cli@37
   206
			StorageManager.current().addArticle(article);
cli@37
   207
			conn.println("240 article replaced");
cli@37
   208
		} catch (StorageBackendException ex) {
cli@37
   209
			Log.get().severe(ex.toString());
cli@37
   210
			conn.println("500 internal server error");
cli@37
   211
		}
cli@37
   212
	}
cli@37
   213
cli@37
   214
	private void postArticle(NNTPConnection conn, Article article)
cli@42
   215
			throws IOException {
franta-hg@113
   216
		if (conn.getUser() != null && conn.getUser().isAuthenticated()) {
franta-hg@113
   217
			article.setAuthenticatedUser(conn.getUser().getUserName());
franta-hg@101
   218
		}
franta-hg@115
   219
cli@37
   220
		if (article.getHeader(Headers.CONTROL)[0].length() > 0) {
cli@37
   221
			controlMessage(conn, article);
cli@37
   222
		} else if (article.getHeader(Headers.SUPERSEDES)[0].length() > 0) {
cli@37
   223
			supersedeMessage(conn, article);
cli@50
   224
		} else { // Post the article regularily
cli@37
   225
			// Circle check; note that Path can already contain the hostname here
cli@37
   226
			String host = Config.inst().get(Config.HOSTNAME, "localhost");
cli@37
   227
			if (article.getHeader(Headers.PATH)[0].indexOf(host + "!", 1) > 0) {
franta-hg@101
   228
				Log.get().log(Level.INFO, "{0} skipped for host {1}", new Object[]{article.getMessageID(), host});
cli@37
   229
				conn.println("441 I know this article already");
cli@37
   230
				return;
cli@37
   231
			}
cli@37
   232
cli@37
   233
			// Try to create the article in the database or post it to
cli@37
   234
			// appropriate mailing list
cli@37
   235
			try {
cli@37
   236
				boolean success = false;
cli@37
   237
				String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
cli@37
   238
				for (String groupname : groupnames) {
cli@37
   239
					Group group = StorageManager.current().getGroup(groupname);
cli@37
   240
					if (group != null && !group.isDeleted()) {
cli@37
   241
						if (group.isMailingList() && !conn.isLocalConnection()) {
cli@37
   242
							// Send to mailing list; the Dispatcher writes
cli@37
   243
							// statistics to database
cli@50
   244
							success = Dispatcher.toList(article, group.getName());
cli@37
   245
						} else {
cli@37
   246
							// Store in database
cli@37
   247
							if (!StorageManager.current().isArticleExisting(article.getMessageID())) {
cli@37
   248
								StorageManager.current().addArticle(article);
cli@37
   249
cli@37
   250
								// Log this posting to statistics
cli@37
   251
								Stats.getInstance().mailPosted(
cli@42
   252
										article.getHeader(Headers.NEWSGROUPS)[0]);
cli@37
   253
							}
cli@37
   254
							success = true;
cli@37
   255
						}
cli@37
   256
					}
cli@37
   257
				} // end for
cli@37
   258
cli@37
   259
				if (success) {
cli@37
   260
					conn.println("240 article posted ok");
cli@37
   261
					FeedManager.queueForPush(article);
cli@37
   262
				} else {
cli@50
   263
					conn.println("441 newsgroup not found or configuration error");
cli@37
   264
				}
cli@37
   265
			} catch (AddressException ex) {
cli@37
   266
				Log.get().warning(ex.getMessage());
cli@37
   267
				conn.println("441 invalid sender address");
cli@37
   268
			} catch (MessagingException ex) {
cli@37
   269
				// A MessageException is thrown when the sender email address is
cli@37
   270
				// invalid or something is wrong with the SMTP server.
cli@37
   271
				System.err.println(ex.getLocalizedMessage());
cli@37
   272
				conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
cli@37
   273
			} catch (StorageBackendException ex) {
cli@37
   274
				ex.printStackTrace();
cli@37
   275
				conn.println("500 internal server error");
cli@37
   276
			}
cli@37
   277
		}
cli@37
   278
	}
franta-hg@115
   279
franta-hg@115
   280
	/**
franta-hg@115
   281
	 * TODO: rework, integrate into NNTPConnection
franta-hg@115
   282
	 * 
franta-hg@115
   283
	 * @param body message body with doubled dots
franta-hg@115
   284
	 * @return message body with unescaped dots (.. → .)
franta-hg@115
   285
	 */
franta-hg@115
   286
	private static byte[] unescapeDots(byte[] body) throws IOException {
franta-hg@115
   287
		byte[] result = new byte[body.length];
franta-hg@115
   288
		int resultLength = 0;
franta-hg@115
   289
franta-hg@115
   290
		ByteArrayInputStream escapedInput = new ByteArrayInputStream(body);
franta-hg@115
   291
		SMTPInputStream unescapedInput = new SMTPInputStream(escapedInput);
franta-hg@115
   292
franta-hg@115
   293
		int ch = unescapedInput.read();
franta-hg@115
   294
		while (ch >= 0) {
franta-hg@115
   295
			result[resultLength] = (byte) ch;
franta-hg@115
   296
			resultLength++;
franta-hg@115
   297
			ch = unescapedInput.read();
franta-hg@115
   298
		}
franta-hg@115
   299
franta-hg@115
   300
		return Arrays.copyOfRange(result, 0, resultLength);
franta-hg@115
   301
	}
chris@1
   302
}