src/org/sonews/mlgw/MailPoller.java
changeset 35 ed84c8bdd87b
parent 27 d879bab39600
child 37 74139325d305
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/org/sonews/mlgw/MailPoller.java	Sun Aug 29 17:28:58 2010 +0200
     1.3 @@ -0,0 +1,151 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.mlgw;
    1.23 +
    1.24 +import java.util.Properties;
    1.25 +import javax.mail.AuthenticationFailedException;
    1.26 +import javax.mail.Authenticator;
    1.27 +import javax.mail.Flags.Flag;
    1.28 +import javax.mail.Folder;
    1.29 +import javax.mail.Message;
    1.30 +import javax.mail.MessagingException;
    1.31 +import javax.mail.NoSuchProviderException;
    1.32 +import javax.mail.PasswordAuthentication;
    1.33 +import javax.mail.Session;
    1.34 +import javax.mail.Store;
    1.35 +import org.sonews.config.Config;
    1.36 +import org.sonews.daemon.AbstractDaemon;
    1.37 +import org.sonews.util.Log;
    1.38 +import org.sonews.util.Stats;
    1.39 +
    1.40 +/**
    1.41 + * Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
    1.42 + * @author Christian Lins
    1.43 + * @since sonews/0.5.0
    1.44 + */
    1.45 +public class MailPoller extends AbstractDaemon
    1.46 +{
    1.47 +
    1.48 +  static class PasswordAuthenticator extends Authenticator
    1.49 +  {
    1.50 +    
    1.51 +    @Override
    1.52 +    public PasswordAuthentication getPasswordAuthentication()
    1.53 +    {
    1.54 +      final String username = 
    1.55 +        Config.inst().get(Config.MLPOLL_USER, "user");
    1.56 +      final String password = 
    1.57 +        Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    1.58 +
    1.59 +      return new PasswordAuthentication(username, password);
    1.60 +    }
    1.61 +    
    1.62 +  }
    1.63 +  
    1.64 +  @Override
    1.65 +  public void run()
    1.66 +  {
    1.67 +    Log.get().info("Starting Mailinglist Poller...");
    1.68 +    int errors = 0;
    1.69 +    while(isRunning())
    1.70 +    {
    1.71 +      try
    1.72 +      {
    1.73 +        // Wait some time between runs. At the beginning has advantages,
    1.74 +        // because the wait is not skipped if an exception occurs.
    1.75 +        Thread.sleep(60000 * (errors + 1)); // one minute * errors
    1.76 +        
    1.77 +        final String host     = 
    1.78 +          Config.inst().get(Config.MLPOLL_HOST, "samplehost");
    1.79 +        final String username = 
    1.80 +          Config.inst().get(Config.MLPOLL_USER, "user");
    1.81 +        final String password = 
    1.82 +          Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    1.83 +        
    1.84 +        Stats.getInstance().mlgwRunStart();
    1.85 +        
    1.86 +        // Create empty properties
    1.87 +        Properties props = System.getProperties();
    1.88 +        props.put("mail.pop3.host", host);
    1.89 +        props.put("mail.mime.address.strict", "false");
    1.90 +
    1.91 +        // Get session
    1.92 +        Session session = Session.getInstance(props);
    1.93 +
    1.94 +        // Get the store
    1.95 +        Store store = session.getStore("pop3");
    1.96 +        store.connect(host, 110, username, password);
    1.97 +
    1.98 +        // Get folder
    1.99 +        Folder folder = store.getFolder("INBOX");
   1.100 +        folder.open(Folder.READ_WRITE);
   1.101 +
   1.102 +        // Get directory
   1.103 +        Message[] messages = folder.getMessages();
   1.104 +
   1.105 +        // Dispatch messages and delete it afterwards on the inbox
   1.106 +        for(Message message : messages)
   1.107 +        {
   1.108 +          if(Dispatcher.toGroup(message)
   1.109 +            || Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false))
   1.110 +          {
   1.111 +            // Delete the message
   1.112 +            message.setFlag(Flag.DELETED, true);
   1.113 +          }
   1.114 +        }
   1.115 +
   1.116 +        // Close connection 
   1.117 +        folder.close(true); // true to expunge deleted messages
   1.118 +        store.close();
   1.119 +        errors = 0;
   1.120 +        
   1.121 +        Stats.getInstance().mlgwRunEnd();
   1.122 +      }
   1.123 +      catch(NoSuchProviderException ex)
   1.124 +      {
   1.125 +        Log.get().severe(ex.toString());
   1.126 +        shutdown();
   1.127 +      }
   1.128 +      catch(AuthenticationFailedException ex)
   1.129 +      {
   1.130 +        // AuthentificationFailedException may be thrown if credentials are
   1.131 +        // bad or if the Mailbox is in use (locked).
   1.132 +        ex.printStackTrace();
   1.133 +        errors = errors < 5 ? errors + 1 : errors;
   1.134 +      }
   1.135 +      catch(InterruptedException ex)
   1.136 +      {
   1.137 +        System.out.println("sonews: " + this + " returns: " + ex);
   1.138 +        return;
   1.139 +      }
   1.140 +      catch(MessagingException ex)
   1.141 +      {
   1.142 +        ex.printStackTrace();
   1.143 +        errors = errors < 5 ? errors + 1 : errors;
   1.144 +      }
   1.145 +      catch(Exception ex)
   1.146 +      {
   1.147 +        ex.printStackTrace();
   1.148 +        errors = errors < 5 ? errors + 1 : errors;
   1.149 +      }
   1.150 +    }
   1.151 +    Log.get().severe("MailPoller exited.");
   1.152 +  }
   1.153 +  
   1.154 +}