src/org/sonews/mlgw/Dispatcher.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 08 Oct 2011 23:32:18 +0200
changeset 63 d883d4ab7b9d
parent 58 b2df305a13ce
permissions -rwxr-xr-x
Drupal: kostry tříd
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.mlgw;
chris@1
    19
chris@1
    20
import java.io.IOException;
chris@1
    21
import java.util.ArrayList;
chris@1
    22
import java.util.List;
cli@50
    23
import java.util.logging.Level;
cli@12
    24
import java.util.regex.Matcher;
cli@12
    25
import java.util.regex.Pattern;
chris@1
    26
import javax.mail.Address;
chris@1
    27
import javax.mail.Authenticator;
chris@1
    28
import javax.mail.Message;
chris@1
    29
import javax.mail.MessagingException;
chris@1
    30
import javax.mail.PasswordAuthentication;
chris@1
    31
import javax.mail.internet.InternetAddress;
chris@3
    32
import org.sonews.config.Config;
chris@3
    33
import org.sonews.storage.Article;
cli@12
    34
import org.sonews.storage.Group;
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.util.Log;
chris@1
    39
import org.sonews.util.Stats;
chris@1
    40
chris@1
    41
/**
cli@12
    42
 * Dispatches messages from mailing list to newsserver or vice versa.
chris@1
    43
 * @author Christian Lins
chris@1
    44
 * @since sonews/0.5.0
chris@1
    45
 */
cli@50
    46
public class Dispatcher {
chris@1
    47
cli@50
    48
	static class PasswordAuthenticator extends Authenticator {
chris@1
    49
cli@37
    50
		@Override
cli@50
    51
		public PasswordAuthentication getPasswordAuthentication() {
cli@37
    52
			final String username =
cli@50
    53
					Config.inst().get(Config.MLSEND_USER, "user");
cli@37
    54
			final String password =
cli@50
    55
					Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
cli@12
    56
cli@37
    57
			return new PasswordAuthentication(username, password);
cli@37
    58
		}
cli@37
    59
	}
cli@12
    60
cli@37
    61
	/**
cli@37
    62
	 * Chunks out the email address of the full List-Post header field.
cli@37
    63
	 * @param listPostValue
cli@37
    64
	 * @return The matching email address or null
cli@37
    65
	 */
cli@50
    66
	private static String chunkListPost(String listPostValue) {
cli@37
    67
		// listPostValue is of form "<mailto:dev@openoffice.org>"
cli@37
    68
		Pattern mailPattern = Pattern.compile("(\\w+[-|.])*\\w+@(\\w+.)+\\w+");
cli@37
    69
		Matcher mailMatcher = mailPattern.matcher(listPostValue);
cli@37
    70
		if (mailMatcher.find()) {
cli@37
    71
			return listPostValue.substring(mailMatcher.start(), mailMatcher.end());
cli@37
    72
		} else {
cli@37
    73
			return null;
cli@37
    74
		}
cli@37
    75
	}
cli@12
    76
cli@37
    77
	/**
cli@37
    78
	 * This method inspects the header of the given message, trying
cli@37
    79
	 * to find the most appropriate recipient.
cli@37
    80
	 * @param msg
cli@37
    81
	 * @param fallback If this is false only List-Post and X-List-Post headers
cli@37
    82
	 *                 are examined.
cli@37
    83
	 * @return null or fitting group name for the given message.
cli@37
    84
	 */
cli@37
    85
	private static List<String> getGroupFor(final Message msg, final boolean fallback)
cli@50
    86
			throws MessagingException, StorageBackendException {
cli@37
    87
		List<String> groups = null;
cli@12
    88
cli@37
    89
		// Is there a List-Post header?
cli@37
    90
		String[] listPost = msg.getHeader(Headers.LIST_POST);
cli@37
    91
		InternetAddress listPostAddr;
cli@12
    92
cli@37
    93
		if (listPost == null || listPost.length == 0 || "".equals(listPost[0])) {
cli@37
    94
			// Is there a X-List-Post header?
cli@37
    95
			listPost = msg.getHeader(Headers.X_LIST_POST);
cli@37
    96
		}
cli@37
    97
cli@37
    98
		if (listPost != null && listPost.length > 0
cli@50
    99
				&& !"".equals(listPost[0]) && chunkListPost(listPost[0]) != null) {
cli@37
   100
			// listPost[0] is of form "<mailto:dev@openoffice.org>"
cli@37
   101
			listPost[0] = chunkListPost(listPost[0]);
cli@37
   102
			listPostAddr = new InternetAddress(listPost[0], false);
cli@37
   103
			groups = StorageManager.current().getGroupsForList(listPostAddr.getAddress());
cli@37
   104
		} else if (fallback) {
cli@50
   105
			StringBuilder strBuf = new StringBuilder();
cli@50
   106
			strBuf.append("Using fallback recipient discovery for: ");
cli@50
   107
			strBuf.append(msg.getSubject());
cli@50
   108
			Log.get().info(strBuf.toString());
cli@37
   109
			groups = new ArrayList<String>();
cli@37
   110
			// Fallback to TO/CC/BCC addresses
cli@37
   111
			Address[] to = msg.getAllRecipients();
cli@37
   112
			for (Address toa : to) // Address can have '<' '>' around
cli@37
   113
			{
cli@37
   114
				if (toa instanceof InternetAddress) {
cli@37
   115
					List<String> g = StorageManager.current().getGroupsForList(((InternetAddress) toa).getAddress());
cli@37
   116
					groups.addAll(g);
cli@37
   117
				}
cli@37
   118
			}
cli@37
   119
		}
cli@37
   120
cli@37
   121
		return groups;
cli@37
   122
	}
cli@37
   123
cli@37
   124
	/**
cli@37
   125
	 * Posts a message that was received from a mailing list to the
cli@37
   126
	 * appropriate newsgroup.
cli@37
   127
	 * If the message already exists in the storage, this message checks
cli@37
   128
	 * if it must be posted in an additional group. This can happen for
cli@37
   129
	 * crosspostings in different mailing lists.
cli@37
   130
	 * @param msg
cli@37
   131
	 */
cli@50
   132
	public static boolean toGroup(final Message msg) {
cli@37
   133
		if (msg == null) {
cli@37
   134
			throw new IllegalArgumentException("Argument 'msg' must not be null!");
cli@37
   135
		}
cli@36
   136
cli@37
   137
		try {
cli@37
   138
			// Create new Article object
cli@37
   139
			Article article = new Article(msg);
cli@37
   140
			boolean posted = false;
cli@12
   141
cli@37
   142
			// Check if this mail is already existing the storage
cli@37
   143
			boolean updateReq =
cli@50
   144
					StorageManager.current().isArticleExisting(article.getMessageID());
cli@12
   145
cli@37
   146
			List<String> newsgroups = getGroupFor(msg, !updateReq);
cli@37
   147
			List<String> oldgroups = new ArrayList<String>();
cli@37
   148
			if (updateReq) {
cli@37
   149
				// Check for duplicate entries of the same group
cli@37
   150
				Article oldArticle = StorageManager.current().getArticle(article.getMessageID());
cli@37
   151
				if (oldArticle != null) {
cli@37
   152
					List<Group> oldGroups = oldArticle.getGroups();
cli@37
   153
					for (Group oldGroup : oldGroups) {
cli@37
   154
						if (!newsgroups.contains(oldGroup.getName())) {
cli@37
   155
							oldgroups.add(oldGroup.getName());
cli@37
   156
						}
cli@37
   157
					}
cli@37
   158
				}
cli@37
   159
			}
cli@37
   160
cli@37
   161
			if (newsgroups.size() > 0) {
cli@37
   162
				newsgroups.addAll(oldgroups);
cli@37
   163
				StringBuilder groups = new StringBuilder();
cli@37
   164
				for (int n = 0; n < newsgroups.size(); n++) {
cli@37
   165
					groups.append(newsgroups.get(n));
cli@37
   166
					if (n + 1 != newsgroups.size()) {
cli@37
   167
						groups.append(',');
cli@37
   168
					}
cli@37
   169
				}
cli@50
   170
cli@50
   171
				StringBuilder strBuf = new StringBuilder();
cli@50
   172
				strBuf.append("Posting to group ");
cli@50
   173
				strBuf.append(groups.toString());
cli@50
   174
				Log.get().info(strBuf.toString());
cli@37
   175
cli@37
   176
				article.setGroup(groups.toString());
cli@37
   177
				//article.removeHeader(Headers.REPLY_TO);
cli@37
   178
				//article.removeHeader(Headers.TO);
cli@37
   179
cli@37
   180
				// Write article to database
cli@37
   181
				if (updateReq) {
cli@37
   182
					Log.get().info("Updating " + article.getMessageID()
cli@50
   183
							+ " with additional groups");
cli@37
   184
					StorageManager.current().delete(article.getMessageID());
cli@37
   185
					StorageManager.current().addArticle(article);
cli@37
   186
				} else {
cli@37
   187
					Log.get().info("Gatewaying " + article.getMessageID() + " to "
cli@50
   188
							+ article.getHeader(Headers.NEWSGROUPS)[0]);
cli@37
   189
					StorageManager.current().addArticle(article);
cli@37
   190
					Stats.getInstance().mailGatewayed(
cli@50
   191
							article.getHeader(Headers.NEWSGROUPS)[0]);
cli@37
   192
				}
cli@37
   193
				posted = true;
cli@37
   194
			} else {
cli@37
   195
				StringBuilder buf = new StringBuilder();
cli@37
   196
				for (Address toa : msg.getAllRecipients()) {
cli@37
   197
					buf.append(' ');
cli@37
   198
					buf.append(toa.toString());
cli@37
   199
				}
cli@50
   200
				buf.append(" ");
cli@50
   201
				buf.append(article.getHeader(Headers.LIST_POST)[0]);
cli@37
   202
				Log.get().warning("No group for" + buf.toString());
cli@37
   203
			}
cli@37
   204
			return posted;
cli@37
   205
		} catch (Exception ex) {
cli@50
   206
			Log.get().log(Level.WARNING, ex.getLocalizedMessage(), ex);
cli@37
   207
			return false;
cli@36
   208
		}
cli@37
   209
	}
chris@3
   210
cli@37
   211
	/**
cli@37
   212
	 * Mails a message received through NNTP to the appropriate mailing list.
cli@37
   213
	 * This method MAY be called several times by PostCommand for the same
cli@37
   214
	 * article.
cli@37
   215
	 */
cli@50
   216
	public static boolean toList(Article article, String group)
cli@50
   217
			throws IOException, MessagingException, StorageBackendException {
cli@37
   218
		// Get mailing lists for the group of this article
cli@37
   219
		List<String> rcptAddresses = StorageManager.current().getListsForGroup(group);
cli@12
   220
cli@50
   221
		if (rcptAddresses == null || rcptAddresses.isEmpty()) {
cli@50
   222
			StringBuilder strBuf = new StringBuilder();
cli@50
   223
			strBuf.append("No ML address found for group ");
cli@50
   224
			strBuf.append(group);
cli@50
   225
			Log.get().warning(strBuf.toString());
cli@50
   226
			return false;
cli@37
   227
		}
cli@12
   228
cli@37
   229
		for (String rcptAddress : rcptAddresses) {
cli@37
   230
			// Compose message and send it via given SMTP-Host
cli@37
   231
			String smtpHost = Config.inst().get(Config.MLSEND_HOST, "localhost");
cli@37
   232
			int smtpPort = Config.inst().get(Config.MLSEND_PORT, 25);
cli@37
   233
			String smtpUser = Config.inst().get(Config.MLSEND_USER, "user");
cli@37
   234
			String smtpPw = Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
cli@37
   235
			String smtpFrom = Config.inst().get(
cli@50
   236
					Config.MLSEND_ADDRESS, article.getHeader(Headers.FROM)[0]);
cli@12
   237
cli@37
   238
			// TODO: Make Article cloneable()
cli@37
   239
			article.getMessageID(); // Make sure an ID is existing
cli@37
   240
			article.removeHeader(Headers.NEWSGROUPS);
cli@37
   241
			article.removeHeader(Headers.PATH);
cli@37
   242
			article.removeHeader(Headers.LINES);
cli@37
   243
			article.removeHeader(Headers.BYTES);
chris@1
   244
cli@37
   245
			article.setHeader("To", rcptAddress);
cli@37
   246
			//article.setHeader("Reply-To", listAddress);
chris@1
   247
cli@37
   248
			if (Config.inst().get(Config.MLSEND_RW_SENDER, false)) {
cli@37
   249
				rewriteSenderAddress(article); // Set the SENDER address
cli@37
   250
			}
chris@1
   251
cli@37
   252
			SMTPTransport smtpTransport = new SMTPTransport(smtpHost, smtpPort);
cli@58
   253
			smtpTransport.login();
cli@37
   254
			smtpTransport.send(article, smtpFrom, rcptAddress);
cli@37
   255
			smtpTransport.close();
chris@1
   256
cli@37
   257
			Stats.getInstance().mailGatewayed(group);
cli@37
   258
			Log.get().info("MLGateway: Mail " + article.getHeader("Subject")[0]
cli@50
   259
					+ " was delivered to " + rcptAddress + ".");
cli@37
   260
		}
cli@50
   261
		return true;
cli@37
   262
	}
chris@1
   263
cli@37
   264
	/**
cli@37
   265
	 * Sets the SENDER header of the given MimeMessage. This might be necessary
cli@37
   266
	 * for moderated groups that does not allow the "normal" FROM sender.
cli@37
   267
	 * @param msg
cli@37
   268
	 * @throws javax.mail.MessagingException
cli@37
   269
	 */
cli@37
   270
	private static void rewriteSenderAddress(Article msg)
cli@50
   271
			throws MessagingException {
cli@37
   272
		String mlAddress = Config.inst().get(Config.MLSEND_ADDRESS, null);
chris@1
   273
cli@37
   274
		if (mlAddress != null) {
cli@37
   275
			msg.setHeader(Headers.SENDER, mlAddress);
cli@37
   276
		} else {
cli@37
   277
			throw new MessagingException("Cannot rewrite SENDER header!");
cli@37
   278
		}
cli@37
   279
	}
chris@1
   280
}