src/org/sonews/mlgw/Dispatcher.java
changeset 50 0bf10add82d9
parent 37 74139325d305
child 58 b2df305a13ce
     1.1 --- a/src/org/sonews/mlgw/Dispatcher.java	Sun Sep 11 17:01:19 2011 +0200
     1.2 +++ b/src/org/sonews/mlgw/Dispatcher.java	Sun Sep 11 17:36:47 2011 +0200
     1.3 @@ -15,12 +15,12 @@
     1.4   *   You should have received a copy of the GNU General Public License
     1.5   *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     1.6   */
     1.7 -
     1.8  package org.sonews.mlgw;
     1.9  
    1.10  import java.io.IOException;
    1.11  import java.util.ArrayList;
    1.12  import java.util.List;
    1.13 +import java.util.logging.Level;
    1.14  import java.util.regex.Matcher;
    1.15  import java.util.regex.Pattern;
    1.16  import javax.mail.Address;
    1.17 @@ -43,19 +43,16 @@
    1.18   * @author Christian Lins
    1.19   * @since sonews/0.5.0
    1.20   */
    1.21 -public class Dispatcher
    1.22 -{
    1.23 +public class Dispatcher {
    1.24  
    1.25 -	static class PasswordAuthenticator extends Authenticator
    1.26 -	{
    1.27 +	static class PasswordAuthenticator extends Authenticator {
    1.28  
    1.29  		@Override
    1.30 -		public PasswordAuthentication getPasswordAuthentication()
    1.31 -		{
    1.32 +		public PasswordAuthentication getPasswordAuthentication() {
    1.33  			final String username =
    1.34 -				Config.inst().get(Config.MLSEND_USER, "user");
    1.35 +					Config.inst().get(Config.MLSEND_USER, "user");
    1.36  			final String password =
    1.37 -				Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
    1.38 +					Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
    1.39  
    1.40  			return new PasswordAuthentication(username, password);
    1.41  		}
    1.42 @@ -66,8 +63,7 @@
    1.43  	 * @param listPostValue
    1.44  	 * @return The matching email address or null
    1.45  	 */
    1.46 -	private static String chunkListPost(String listPostValue)
    1.47 -	{
    1.48 +	private static String chunkListPost(String listPostValue) {
    1.49  		// listPostValue is of form "<mailto:dev@openoffice.org>"
    1.50  		Pattern mailPattern = Pattern.compile("(\\w+[-|.])*\\w+@(\\w+.)+\\w+");
    1.51  		Matcher mailMatcher = mailPattern.matcher(listPostValue);
    1.52 @@ -87,8 +83,7 @@
    1.53  	 * @return null or fitting group name for the given message.
    1.54  	 */
    1.55  	private static List<String> getGroupFor(final Message msg, final boolean fallback)
    1.56 -		throws MessagingException, StorageBackendException
    1.57 -	{
    1.58 +			throws MessagingException, StorageBackendException {
    1.59  		List<String> groups = null;
    1.60  
    1.61  		// Is there a List-Post header?
    1.62 @@ -101,13 +96,16 @@
    1.63  		}
    1.64  
    1.65  		if (listPost != null && listPost.length > 0
    1.66 -			&& !"".equals(listPost[0]) && chunkListPost(listPost[0]) != null) {
    1.67 +				&& !"".equals(listPost[0]) && chunkListPost(listPost[0]) != null) {
    1.68  			// listPost[0] is of form "<mailto:dev@openoffice.org>"
    1.69  			listPost[0] = chunkListPost(listPost[0]);
    1.70  			listPostAddr = new InternetAddress(listPost[0], false);
    1.71  			groups = StorageManager.current().getGroupsForList(listPostAddr.getAddress());
    1.72  		} else if (fallback) {
    1.73 -			Log.get().info("Using fallback recipient discovery for: " + msg.getSubject());
    1.74 +			StringBuilder strBuf = new StringBuilder();
    1.75 +			strBuf.append("Using fallback recipient discovery for: ");
    1.76 +			strBuf.append(msg.getSubject());
    1.77 +			Log.get().info(strBuf.toString());
    1.78  			groups = new ArrayList<String>();
    1.79  			// Fallback to TO/CC/BCC addresses
    1.80  			Address[] to = msg.getAllRecipients();
    1.81 @@ -131,8 +129,7 @@
    1.82  	 * crosspostings in different mailing lists.
    1.83  	 * @param msg
    1.84  	 */
    1.85 -	public static boolean toGroup(final Message msg)
    1.86 -	{
    1.87 +	public static boolean toGroup(final Message msg) {
    1.88  		if (msg == null) {
    1.89  			throw new IllegalArgumentException("Argument 'msg' must not be null!");
    1.90  		}
    1.91 @@ -144,7 +141,7 @@
    1.92  
    1.93  			// Check if this mail is already existing the storage
    1.94  			boolean updateReq =
    1.95 -				StorageManager.current().isArticleExisting(article.getMessageID());
    1.96 +					StorageManager.current().isArticleExisting(article.getMessageID());
    1.97  
    1.98  			List<String> newsgroups = getGroupFor(msg, !updateReq);
    1.99  			List<String> oldgroups = new ArrayList<String>();
   1.100 @@ -170,7 +167,11 @@
   1.101  						groups.append(',');
   1.102  					}
   1.103  				}
   1.104 -				Log.get().info("Posting to group " + groups.toString());
   1.105 +
   1.106 +				StringBuilder strBuf = new StringBuilder();
   1.107 +				strBuf.append("Posting to group ");
   1.108 +				strBuf.append(groups.toString());
   1.109 +				Log.get().info(strBuf.toString());
   1.110  
   1.111  				article.setGroup(groups.toString());
   1.112  				//article.removeHeader(Headers.REPLY_TO);
   1.113 @@ -179,15 +180,15 @@
   1.114  				// Write article to database
   1.115  				if (updateReq) {
   1.116  					Log.get().info("Updating " + article.getMessageID()
   1.117 -						+ " with additional groups");
   1.118 +							+ " with additional groups");
   1.119  					StorageManager.current().delete(article.getMessageID());
   1.120  					StorageManager.current().addArticle(article);
   1.121  				} else {
   1.122  					Log.get().info("Gatewaying " + article.getMessageID() + " to "
   1.123 -						+ article.getHeader(Headers.NEWSGROUPS)[0]);
   1.124 +							+ article.getHeader(Headers.NEWSGROUPS)[0]);
   1.125  					StorageManager.current().addArticle(article);
   1.126  					Stats.getInstance().mailGatewayed(
   1.127 -						article.getHeader(Headers.NEWSGROUPS)[0]);
   1.128 +							article.getHeader(Headers.NEWSGROUPS)[0]);
   1.129  				}
   1.130  				posted = true;
   1.131  			} else {
   1.132 @@ -196,12 +197,13 @@
   1.133  					buf.append(' ');
   1.134  					buf.append(toa.toString());
   1.135  				}
   1.136 -				buf.append(" " + article.getHeader(Headers.LIST_POST)[0]);
   1.137 +				buf.append(" ");
   1.138 +				buf.append(article.getHeader(Headers.LIST_POST)[0]);
   1.139  				Log.get().warning("No group for" + buf.toString());
   1.140  			}
   1.141  			return posted;
   1.142  		} catch (Exception ex) {
   1.143 -			ex.printStackTrace();
   1.144 +			Log.get().log(Level.WARNING, ex.getLocalizedMessage(), ex);
   1.145  			return false;
   1.146  		}
   1.147  	}
   1.148 @@ -211,15 +213,17 @@
   1.149  	 * This method MAY be called several times by PostCommand for the same
   1.150  	 * article.
   1.151  	 */
   1.152 -	public static void toList(Article article, String group)
   1.153 -		throws IOException, MessagingException, StorageBackendException
   1.154 -	{
   1.155 +	public static boolean toList(Article article, String group)
   1.156 +			throws IOException, MessagingException, StorageBackendException {
   1.157  		// Get mailing lists for the group of this article
   1.158  		List<String> rcptAddresses = StorageManager.current().getListsForGroup(group);
   1.159  
   1.160 -		if (rcptAddresses == null || rcptAddresses.size() == 0) {
   1.161 -			Log.get().warning("No ML-address for " + group + " found.");
   1.162 -			return;
   1.163 +		if (rcptAddresses == null || rcptAddresses.isEmpty()) {
   1.164 +			StringBuilder strBuf = new StringBuilder();
   1.165 +			strBuf.append("No ML address found for group ");
   1.166 +			strBuf.append(group);
   1.167 +			Log.get().warning(strBuf.toString());
   1.168 +			return false;
   1.169  		}
   1.170  
   1.171  		for (String rcptAddress : rcptAddresses) {
   1.172 @@ -229,7 +233,7 @@
   1.173  			String smtpUser = Config.inst().get(Config.MLSEND_USER, "user");
   1.174  			String smtpPw = Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
   1.175  			String smtpFrom = Config.inst().get(
   1.176 -				Config.MLSEND_ADDRESS, article.getHeader(Headers.FROM)[0]);
   1.177 +					Config.MLSEND_ADDRESS, article.getHeader(Headers.FROM)[0]);
   1.178  
   1.179  			// TODO: Make Article cloneable()
   1.180  			article.getMessageID(); // Make sure an ID is existing
   1.181 @@ -251,8 +255,9 @@
   1.182  
   1.183  			Stats.getInstance().mailGatewayed(group);
   1.184  			Log.get().info("MLGateway: Mail " + article.getHeader("Subject")[0]
   1.185 -				+ " was delivered to " + rcptAddress + ".");
   1.186 +					+ " was delivered to " + rcptAddress + ".");
   1.187  		}
   1.188 +		return true;
   1.189  	}
   1.190  
   1.191  	/**
   1.192 @@ -262,8 +267,7 @@
   1.193  	 * @throws javax.mail.MessagingException
   1.194  	 */
   1.195  	private static void rewriteSenderAddress(Article msg)
   1.196 -		throws MessagingException
   1.197 -	{
   1.198 +			throws MessagingException {
   1.199  		String mlAddress = Config.inst().get(Config.MLSEND_ADDRESS, null);
   1.200  
   1.201  		if (mlAddress != null) {