org/sonews/mlgw/MailPoller.java
author chris <chris@marvin>
Wed, 01 Jul 2009 10:48:22 +0200
changeset 2 1090e2141798
parent 1 6fceb66e1ad7
child 3 2fdc9cc89502
permissions -rw-r--r--
sonews/0.5.1 fixes merged
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.mlgw;
    20 
    21 import java.util.Properties;
    22 import javax.mail.AuthenticationFailedException;
    23 import javax.mail.Authenticator;
    24 import javax.mail.Flags.Flag;
    25 import javax.mail.Folder;
    26 import javax.mail.Message;
    27 import javax.mail.MessagingException;
    28 import javax.mail.NoSuchProviderException;
    29 import javax.mail.PasswordAuthentication;
    30 import javax.mail.Session;
    31 import javax.mail.Store;
    32 import org.sonews.daemon.Config;
    33 import org.sonews.daemon.AbstractDaemon;
    34 import org.sonews.util.Log;
    35 import org.sonews.util.Stats;
    36 
    37 /**
    38  * Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
    39  * @author Christian Lins
    40  * @since sonews/0.5.0
    41  */
    42 public class MailPoller extends AbstractDaemon
    43 {
    44 
    45   static class PasswordAuthenticator extends Authenticator
    46   {
    47     
    48     @Override
    49     public PasswordAuthentication getPasswordAuthentication()
    50     {
    51       final String username = 
    52         Config.getInstance().get(Config.MLPOLL_USER, "user");
    53       final String password = 
    54         Config.getInstance().get(Config.MLPOLL_PASSWORD, "mysecret");
    55 
    56       return new PasswordAuthentication(username, password);
    57     }
    58     
    59   }
    60   
    61   @Override
    62   public void run()
    63   {
    64     Log.msg("Starting Mailinglist Poller...", false);
    65     int errors = 0;
    66     while(isRunning() && errors < 5)
    67     {
    68       try
    69       {
    70         // Wait some time between runs. At the beginning has advantages,
    71         // because the wait is not skipped if an exception occurs.
    72         Thread.sleep(60000 * (errors + 1)); // one minute * errors
    73         
    74         final String host     = 
    75           Config.getInstance().get(Config.MLPOLL_HOST, "samplehost");
    76         final String username = 
    77           Config.getInstance().get(Config.MLPOLL_USER, "user");
    78         final String password = 
    79           Config.getInstance().get(Config.MLPOLL_PASSWORD, "mysecret");
    80         
    81         Stats.getInstance().mlgwRunStart();
    82         
    83         // Create empty properties
    84         Properties props = System.getProperties();
    85         props.put("mail.pop3.host", host);
    86 
    87         // Get session
    88         Session session = Session.getInstance(props);
    89 
    90         // Get the store
    91         Store store = session.getStore("pop3");
    92         store.connect(host, 110, username, password);
    93 
    94         // Get folder
    95         Folder folder = store.getFolder("INBOX");
    96         folder.open(Folder.READ_WRITE);
    97 
    98         // Get directory
    99         Message[] messages = folder.getMessages();
   100 
   101         // Dispatch messages and delete it afterwards on the inbox
   102         for(Message message : messages)
   103         {
   104           String subject = message.getSubject();
   105           System.out.println("MLGateway: message with subject \"" + subject + "\" received.");
   106           if(Dispatcher.toGroup(message)
   107             || Config.getInstance().get(Config.MLPOLL_DELETEUNKNOWN, false))
   108           {
   109             // Delete the message
   110             message.setFlag(Flag.DELETED, true);
   111           }
   112         }
   113 
   114         // Close connection 
   115         folder.close(true); // true to expunge deleted messages
   116         store.close();
   117         errors = 0;
   118         
   119         Stats.getInstance().mlgwRunEnd();
   120       }
   121       catch(NoSuchProviderException ex)
   122       {
   123         Log.msg(ex.toString(), false);
   124         shutdown();
   125       }
   126       catch(AuthenticationFailedException ex)
   127       {
   128         // AuthentificationFailedException may be thrown if credentials are
   129         // bad or if the Mailbox is in use (locked).
   130         ex.printStackTrace();
   131         errors++;
   132       }
   133       catch(InterruptedException ex)
   134       {
   135         System.out.println("sonews: " + this + " returns.");
   136         return;
   137       }
   138       catch(MessagingException ex)
   139       {
   140         ex.printStackTrace();
   141         errors++;
   142       }
   143       catch(Exception ex)
   144       {
   145         ex.printStackTrace();
   146         errors++;
   147       }
   148     }
   149     Log.msg("MailPoller exited.", false);
   150   }
   151   
   152 }