org/sonews/mlgw/Dispatcher.java
changeset 3 2fdc9cc89502
parent 1 6fceb66e1ad7
child 12 bb6990c0dd1a
     1.1 --- a/org/sonews/mlgw/Dispatcher.java	Wed Jul 01 10:48:22 2009 +0200
     1.2 +++ b/org/sonews/mlgw/Dispatcher.java	Wed Jul 22 14:04:05 2009 +0200
     1.3 @@ -19,24 +19,19 @@
     1.4  package org.sonews.mlgw;
     1.5  
     1.6  import java.io.IOException;
     1.7 -import org.sonews.daemon.Config;
     1.8 -import org.sonews.daemon.storage.Article;
     1.9 -import org.sonews.util.io.ArticleInputStream;
    1.10 -import org.sonews.daemon.storage.Database;
    1.11 -import java.sql.SQLException;
    1.12  import java.util.ArrayList;
    1.13  import java.util.List;
    1.14 -import java.util.Properties;
    1.15  import javax.mail.Address;
    1.16  import javax.mail.Authenticator;
    1.17  import javax.mail.Message;
    1.18  import javax.mail.MessagingException;
    1.19  import javax.mail.PasswordAuthentication;
    1.20 -import javax.mail.Session;
    1.21 -import javax.mail.Transport;
    1.22  import javax.mail.internet.InternetAddress;
    1.23 -import javax.mail.internet.MimeMessage;
    1.24 -import org.sonews.daemon.storage.Headers;
    1.25 +import org.sonews.config.Config;
    1.26 +import org.sonews.storage.Article;
    1.27 +import org.sonews.storage.Headers;
    1.28 +import org.sonews.storage.StorageBackendException;
    1.29 +import org.sonews.storage.StorageManager;
    1.30  import org.sonews.util.Log;
    1.31  import org.sonews.util.Stats;
    1.32  
    1.33 @@ -55,9 +50,9 @@
    1.34      public PasswordAuthentication getPasswordAuthentication()
    1.35      {
    1.36        final String username = 
    1.37 -        Config.getInstance().get(Config.MLSEND_USER, "user");
    1.38 +        Config.inst().get(Config.MLSEND_USER, "user");
    1.39        final String password = 
    1.40 -        Config.getInstance().get(Config.MLSEND_PASSWORD, "mysecret");
    1.41 +        Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
    1.42  
    1.43        return new PasswordAuthentication(username, password);
    1.44      }
    1.45 @@ -76,48 +71,71 @@
    1.46        Address[] to = msg.getAllRecipients(); // includes TO/CC/BCC
    1.47        if(to == null || to.length <= 0)
    1.48        {
    1.49 -        Log.msg("Skipping message because no receipient!", true);
    1.50 +        to = msg.getReplyTo();
    1.51 +      }
    1.52 +
    1.53 +      if(to == null || to.length <= 0)
    1.54 +      {
    1.55 +        Log.msg("Skipping message because no recipient!", false);
    1.56          return false;
    1.57        }
    1.58        else
    1.59        {
    1.60 -        boolean posted = false;
    1.61 -        for(Address toa : to) // Address can have '<' '>' around
    1.62 +        boolean      posted     = false;
    1.63 +        List<String> newsgroups = new ArrayList<String>();
    1.64 +
    1.65 +        for (Address toa : to) // Address can have '<' '>' around
    1.66          {
    1.67 -          if(!(toa instanceof InternetAddress))
    1.68 +          if (toa instanceof InternetAddress)
    1.69            {
    1.70 -            continue;
    1.71 +            List<String> groups = StorageManager.current()
    1.72 +              .getGroupsForList((InternetAddress)toa);
    1.73 +            newsgroups.addAll(groups);
    1.74            }
    1.75 -          String group = Database.getInstance()
    1.76 -            .getGroupForList((InternetAddress)toa);
    1.77 -          if(group != null)
    1.78 +        }
    1.79 +
    1.80 +        if (newsgroups.size() > 0)
    1.81 +        {
    1.82 +          StringBuilder groups = new StringBuilder();
    1.83 +          for(int n = 0; n < newsgroups.size(); n++)
    1.84            {
    1.85 -            Log.msg("Posting to group " + group, true);
    1.86 +            groups.append(newsgroups.get(n));
    1.87 +            if(n + 1 != newsgroups.size())
    1.88 +            {
    1.89 +              groups.append(',');
    1.90 +            }
    1.91 +          }
    1.92 +          Log.msg("Posting to group " + groups.toString(), true);
    1.93  
    1.94 -            // Create new Article object
    1.95 -            Article article = new Article(msg);
    1.96 -            article.setGroup(group);
    1.97 -            
    1.98 -            // Write article to database
    1.99 -            if(!Database.getInstance().isArticleExisting(article.getMessageID()))
   1.100 -            {
   1.101 -              Database.getInstance().addArticle(article);
   1.102 -              Stats.getInstance().mailGatewayed(
   1.103 -                article.getHeader(Headers.NEWSGROUPS)[0]);
   1.104 -            }
   1.105 -            else
   1.106 -            {
   1.107 -              Log.msg("Article " + article.getMessageID() + " already existing.", true);
   1.108 -              // TODO: It may be possible that a ML mail is posted to several
   1.109 -              // ML addresses...
   1.110 -            }
   1.111 -            posted = true;
   1.112 +          // Create new Article object
   1.113 +          Article article = new Article(msg);
   1.114 +          article.setGroup(groups.toString());
   1.115 +          article.removeHeader(Headers.REPLY_TO);
   1.116 +          article.removeHeader(Headers.TO);
   1.117 +
   1.118 +          // Write article to database
   1.119 +          if(!StorageManager.current().isArticleExisting(article.getMessageID()))
   1.120 +          {
   1.121 +            StorageManager.current().addArticle(article);
   1.122 +            Stats.getInstance().mailGatewayed(
   1.123 +              article.getHeader(Headers.NEWSGROUPS)[0]);
   1.124            }
   1.125            else
   1.126            {
   1.127 -            Log.msg("No group for " + toa, true);
   1.128 +            Log.msg("Article " + article.getMessageID() + " already existing.", true);
   1.129            }
   1.130 -        } // end for
   1.131 +          posted = true;
   1.132 +        }
   1.133 +        else
   1.134 +        {
   1.135 +          StringBuilder buf = new StringBuilder();
   1.136 +          for(Address toa : to)
   1.137 +          {
   1.138 +            buf.append(' ');
   1.139 +            buf.append(toa.toString());
   1.140 +          }
   1.141 +          Log.msg("No group for" + buf.toString(), false);
   1.142 +        }
   1.143          return posted;
   1.144        }
   1.145      }
   1.146 @@ -132,7 +150,7 @@
   1.147     * Mails a message received through NNTP to the appropriate mailing list.
   1.148     */
   1.149    public static void toList(Article article)
   1.150 -    throws IOException, MessagingException, SQLException
   1.151 +    throws IOException, MessagingException, StorageBackendException
   1.152    {
   1.153      // Get mailing lists for the group of this article
   1.154      List<String> listAddresses = new ArrayList<String>();
   1.155 @@ -140,7 +158,7 @@
   1.156      
   1.157      for(String groupname : groupnames)
   1.158      {
   1.159 -      String listAddress = Database.getInstance().getListForGroup(groupname);
   1.160 +      String listAddress = StorageManager.current().getListForGroup(groupname);
   1.161        if(listAddress != null)
   1.162        {
   1.163          listAddresses.add(listAddress);
   1.164 @@ -150,53 +168,34 @@
   1.165      for(String listAddress : listAddresses)
   1.166      {
   1.167        // Compose message and send it via given SMTP-Host
   1.168 -      String smtpHost = Config.getInstance().get(Config.MLSEND_HOST, "localhost");
   1.169 -      int    smtpPort = Config.getInstance().get(Config.MLSEND_PORT, 25);
   1.170 -      String smtpUser = Config.getInstance().get(Config.MLSEND_USER, "user");
   1.171 -      String smtpPw   = Config.getInstance().get(Config.MLSEND_PASSWORD, "mysecret");
   1.172 +      String smtpHost = Config.inst().get(Config.MLSEND_HOST, "localhost");
   1.173 +      int    smtpPort = Config.inst().get(Config.MLSEND_PORT, 25);
   1.174 +      String smtpUser = Config.inst().get(Config.MLSEND_USER, "user");
   1.175 +      String smtpPw   = Config.inst().get(Config.MLSEND_PASSWORD, "mysecret");
   1.176 +      String smtpFrom = Config.inst().get(
   1.177 +          Config.MLSEND_ADDRESS, article.getHeader(Headers.FROM)[0]);
   1.178  
   1.179 -      Properties props    = System.getProperties();
   1.180 -      props.put("mail.smtp.localhost", 
   1.181 -        Config.getInstance().get(Config.HOSTNAME, "localhost"));
   1.182 -      props.put("mail.smtp.from",  // Used for MAIL FROM command
   1.183 -        Config.getInstance().get(
   1.184 -          Config.MLSEND_ADDRESS, article.getHeader(Headers.FROM)[0]));
   1.185 -      props.put("mail.smtp.host", smtpHost);
   1.186 -      props.put("mail.smtp.port", smtpPort);
   1.187 -      props.put("mail.smtp.auth", "true");
   1.188 +      // TODO: Make Article cloneable()
   1.189 +      String group = article.getHeader(Headers.NEWSGROUPS)[0];
   1.190 +      article.getMessageID(); // Make sure an ID is existing
   1.191 +      article.removeHeader(Headers.NEWSGROUPS);
   1.192 +      article.removeHeader(Headers.PATH);
   1.193 +      article.removeHeader(Headers.LINES);
   1.194 +      article.removeHeader(Headers.BYTES);
   1.195  
   1.196 -      Address[] address = new Address[1];
   1.197 -      address[0] = new InternetAddress(listAddress);
   1.198 +      article.setHeader("To", listAddress);
   1.199 +      article.setHeader("Reply-To", listAddress);
   1.200  
   1.201 -      ArticleInputStream in = new ArticleInputStream(article);
   1.202 -      Session session = Session.getDefaultInstance(props, new PasswordAuthenticator());
   1.203 -      MimeMessage msg = new MimeMessage(session, in);
   1.204 -      msg.setRecipient(Message.RecipientType.TO, address[0]);
   1.205 -      msg.setReplyTo(address);
   1.206 -      msg.removeHeader(Headers.NEWSGROUPS);
   1.207 -      msg.removeHeader(Headers.PATH);
   1.208 -      msg.removeHeader(Headers.LINES);
   1.209 -      msg.removeHeader(Headers.BYTES);
   1.210 -      
   1.211 -      if(Config.getInstance().get(Config.MLSEND_RW_SENDER, false))
   1.212 +      if(Config.inst().get(Config.MLSEND_RW_SENDER, false))
   1.213        {
   1.214 -        rewriteSenderAddress(msg); // Set the SENDER address
   1.215 +        rewriteSenderAddress(article); // Set the SENDER address
   1.216        }
   1.217 -      
   1.218 -      if(Config.getInstance().get(Config.MLSEND_RW_FROM, false))
   1.219 -      {
   1.220 -        rewriteFromAddress(msg);   // Set the FROM address
   1.221 -      }
   1.222 -      
   1.223 -      msg.saveChanges();
   1.224  
   1.225 -      // Send the mail
   1.226 -      Transport transport = session.getTransport("smtp");
   1.227 -      transport.connect(smtpHost, smtpPort, smtpUser, smtpPw);
   1.228 -      transport.sendMessage(msg, msg.getAllRecipients());
   1.229 -      transport.close();
   1.230 +      SMTPTransport smtpTransport = new SMTPTransport(smtpHost, smtpPort);
   1.231 +      smtpTransport.send(article, smtpFrom, listAddress);
   1.232 +      smtpTransport.close();
   1.233  
   1.234 -      Stats.getInstance().mailGatewayed(article.getHeader(Headers.NEWSGROUPS)[0]);
   1.235 +      Stats.getInstance().mailGatewayed(group);
   1.236        Log.msg("MLGateway: Mail " + article.getHeader("Subject")[0] 
   1.237          + " was delivered to " + listAddress + ".", true);
   1.238      }
   1.239 @@ -208,14 +207,14 @@
   1.240     * @param msg
   1.241     * @throws javax.mail.MessagingException
   1.242     */
   1.243 -  private static void rewriteSenderAddress(MimeMessage msg)
   1.244 +  private static void rewriteSenderAddress(Article msg)
   1.245      throws MessagingException
   1.246    {
   1.247 -    String mlAddress = Config.getInstance().get(Config.MLSEND_ADDRESS, null);
   1.248 +    String mlAddress = Config.inst().get(Config.MLSEND_ADDRESS, null);
   1.249  
   1.250      if(mlAddress != null)
   1.251      {
   1.252 -      msg.setSender(new InternetAddress(mlAddress));
   1.253 +      msg.setHeader(Headers.SENDER, mlAddress);
   1.254      }
   1.255      else
   1.256      {
   1.257 @@ -223,29 +222,4 @@
   1.258      }
   1.259    }
   1.260    
   1.261 -  /**
   1.262 -   * Sets the FROM header of the given MimeMessage. This might be necessary
   1.263 -   * for moderated groups that does not allow the "normal" FROM sender.
   1.264 -   * @param msg
   1.265 -   * @throws javax.mail.MessagingException
   1.266 -   */
   1.267 -  private static void rewriteFromAddress(MimeMessage msg)
   1.268 -    throws MessagingException
   1.269 -  {
   1.270 -    Address[] froms  = msg.getFrom();
   1.271 -    String mlAddress = Config.getInstance().get(Config.MLSEND_ADDRESS, null);
   1.272 -
   1.273 -    if(froms.length > 0 && froms[0] instanceof InternetAddress 
   1.274 -      && mlAddress != null)
   1.275 -    {
   1.276 -      InternetAddress from = (InternetAddress)froms[0];
   1.277 -      from.setAddress(mlAddress);
   1.278 -      msg.setFrom(from);
   1.279 -    }
   1.280 -    else
   1.281 -    {
   1.282 -      throw new MessagingException("Cannot rewrite FROM header!");
   1.283 -    }    
   1.284 -  }
   1.285 -  
   1.286  }