chris@1: /*
chris@1:  *   SONEWS News Server
chris@1:  *   see AUTHORS for the list of contributors
chris@1:  *
chris@1:  *   This program is free software: you can redistribute it and/or modify
chris@1:  *   it under the terms of the GNU General Public License as published by
chris@1:  *   the Free Software Foundation, either version 3 of the License, or
chris@1:  *   (at your option) any later version.
chris@1:  *
chris@1:  *   This program is distributed in the hope that it will be useful,
chris@1:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1:  *   GNU General Public License for more details.
chris@1:  *
chris@1:  *   You should have received a copy of the GNU General Public License
chris@1:  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1:  */
chris@1: 
chris@1: package org.sonews.mlgw;
chris@1: 
chris@1: import java.util.Properties;
chris@1: import javax.mail.AuthenticationFailedException;
chris@1: import javax.mail.Authenticator;
chris@1: import javax.mail.Flags.Flag;
chris@1: import javax.mail.Folder;
chris@1: import javax.mail.Message;
chris@1: import javax.mail.MessagingException;
chris@1: import javax.mail.NoSuchProviderException;
chris@1: import javax.mail.PasswordAuthentication;
chris@1: import javax.mail.Session;
chris@1: import javax.mail.Store;
chris@3: import org.sonews.config.Config;
chris@1: import org.sonews.daemon.AbstractDaemon;
chris@1: import org.sonews.util.Log;
chris@1: import org.sonews.util.Stats;
chris@1: 
chris@1: /**
chris@1:  * Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
chris@1:  * @author Christian Lins
chris@1:  * @since sonews/0.5.0
chris@1:  */
chris@1: public class MailPoller extends AbstractDaemon
chris@1: {
chris@1: 
chris@1:   static class PasswordAuthenticator extends Authenticator
chris@1:   {
chris@1:     
chris@1:     @Override
chris@1:     public PasswordAuthentication getPasswordAuthentication()
chris@1:     {
chris@1:       final String username = 
chris@3:         Config.inst().get(Config.MLPOLL_USER, "user");
chris@1:       final String password = 
chris@3:         Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
chris@1: 
chris@1:       return new PasswordAuthentication(username, password);
chris@1:     }
chris@1:     
chris@1:   }
chris@1:   
chris@1:   @Override
chris@1:   public void run()
chris@1:   {
cli@16:     Log.get().info("Starting Mailinglist Poller...");
chris@1:     int errors = 0;
cli@27:     while(isRunning())
chris@1:     {
chris@1:       try
chris@1:       {
chris@1:         // Wait some time between runs. At the beginning has advantages,
chris@1:         // because the wait is not skipped if an exception occurs.
chris@1:         Thread.sleep(60000 * (errors + 1)); // one minute * errors
chris@1:         
chris@1:         final String host     = 
chris@3:           Config.inst().get(Config.MLPOLL_HOST, "samplehost");
chris@1:         final String username = 
chris@3:           Config.inst().get(Config.MLPOLL_USER, "user");
chris@1:         final String password = 
chris@3:           Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
chris@1:         
chris@1:         Stats.getInstance().mlgwRunStart();
chris@1:         
chris@1:         // Create empty properties
chris@1:         Properties props = System.getProperties();
chris@1:         props.put("mail.pop3.host", host);
cli@8:         props.put("mail.mime.address.strict", "false");
chris@1: 
chris@1:         // Get session
chris@1:         Session session = Session.getInstance(props);
chris@1: 
chris@1:         // Get the store
chris@1:         Store store = session.getStore("pop3");
chris@1:         store.connect(host, 110, username, password);
chris@1: 
chris@1:         // Get folder
chris@1:         Folder folder = store.getFolder("INBOX");
chris@1:         folder.open(Folder.READ_WRITE);
chris@1: 
chris@1:         // Get directory
chris@1:         Message[] messages = folder.getMessages();
chris@1: 
chris@1:         // Dispatch messages and delete it afterwards on the inbox
chris@1:         for(Message message : messages)
chris@1:         {
chris@1:           if(Dispatcher.toGroup(message)
chris@3:             || Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false))
chris@1:           {
chris@1:             // Delete the message
chris@1:             message.setFlag(Flag.DELETED, true);
chris@1:           }
chris@1:         }
chris@1: 
chris@1:         // Close connection 
chris@1:         folder.close(true); // true to expunge deleted messages
chris@1:         store.close();
chris@1:         errors = 0;
chris@1:         
chris@1:         Stats.getInstance().mlgwRunEnd();
chris@1:       }
chris@1:       catch(NoSuchProviderException ex)
chris@1:       {
cli@16:         Log.get().severe(ex.toString());
chris@1:         shutdown();
chris@1:       }
chris@1:       catch(AuthenticationFailedException ex)
chris@1:       {
chris@1:         // AuthentificationFailedException may be thrown if credentials are
chris@1:         // bad or if the Mailbox is in use (locked).
chris@1:         ex.printStackTrace();
cli@27:         errors = errors < 5 ? errors + 1 : errors;
chris@1:       }
chris@1:       catch(InterruptedException ex)
chris@1:       {
chris@3:         System.out.println("sonews: " + this + " returns: " + ex);
chris@1:         return;
chris@1:       }
chris@1:       catch(MessagingException ex)
chris@1:       {
chris@1:         ex.printStackTrace();
cli@27:         errors = errors < 5 ? errors + 1 : errors;
chris@1:       }
chris@1:       catch(Exception ex)
chris@1:       {
chris@1:         ex.printStackTrace();
cli@27:         errors = errors < 5 ? errors + 1 : errors;
chris@1:       }
chris@1:     }
cli@16:     Log.get().severe("MailPoller exited.");
chris@1:   }
chris@1:   
chris@1: }