org/sonews/mlgw/MailPoller.java
author cli
Thu, 06 Aug 2009 18:41:34 +0200
changeset 6 dcc7e491fbc9
parent 3 2fdc9cc89502
child 8 b62fe6ed39d3
permissions -rw-r--r--
Removing doc subdir which resides now in sonews-doc repository.
     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.Address;
    23 import javax.mail.AuthenticationFailedException;
    24 import javax.mail.Authenticator;
    25 import javax.mail.Flags.Flag;
    26 import javax.mail.Folder;
    27 import javax.mail.Message;
    28 import javax.mail.MessagingException;
    29 import javax.mail.NoSuchProviderException;
    30 import javax.mail.PasswordAuthentication;
    31 import javax.mail.Session;
    32 import javax.mail.Store;
    33 import org.sonews.config.Config;
    34 import org.sonews.daemon.AbstractDaemon;
    35 import org.sonews.util.Log;
    36 import org.sonews.util.Stats;
    37 
    38 /**
    39  * Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
    40  * @author Christian Lins
    41  * @since sonews/0.5.0
    42  */
    43 public class MailPoller extends AbstractDaemon
    44 {
    45 
    46   static class PasswordAuthenticator extends Authenticator
    47   {
    48     
    49     @Override
    50     public PasswordAuthentication getPasswordAuthentication()
    51     {
    52       final String username = 
    53         Config.inst().get(Config.MLPOLL_USER, "user");
    54       final String password = 
    55         Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    56 
    57       return new PasswordAuthentication(username, password);
    58     }
    59     
    60   }
    61   
    62   @Override
    63   public void run()
    64   {
    65     Log.msg("Starting Mailinglist Poller...", false);
    66     int errors = 0;
    67     while(isRunning() && errors < 5)
    68     {
    69       try
    70       {
    71         // Wait some time between runs. At the beginning has advantages,
    72         // because the wait is not skipped if an exception occurs.
    73         Thread.sleep(60000 * (errors + 1)); // one minute * errors
    74         
    75         final String host     = 
    76           Config.inst().get(Config.MLPOLL_HOST, "samplehost");
    77         final String username = 
    78           Config.inst().get(Config.MLPOLL_USER, "user");
    79         final String password = 
    80           Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
    81         
    82         Stats.getInstance().mlgwRunStart();
    83         
    84         // Create empty properties
    85         Properties props = System.getProperties();
    86         props.put("mail.pop3.host", host);
    87 
    88         // Get session
    89         Session session = Session.getInstance(props);
    90 
    91         // Get the store
    92         Store store = session.getStore("pop3");
    93         store.connect(host, 110, username, password);
    94 
    95         // Get folder
    96         Folder folder = store.getFolder("INBOX");
    97         folder.open(Folder.READ_WRITE);
    98 
    99         // Get directory
   100         Message[] messages = folder.getMessages();
   101 
   102         // Dispatch messages and delete it afterwards on the inbox
   103         for(Message message : messages)
   104         {
   105           if(Dispatcher.toGroup(message)
   106             || Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false))
   107           {
   108             // Delete the message
   109             message.setFlag(Flag.DELETED, true);
   110           }
   111         }
   112 
   113         // Close connection 
   114         folder.close(true); // true to expunge deleted messages
   115         store.close();
   116         errors = 0;
   117         
   118         Stats.getInstance().mlgwRunEnd();
   119       }
   120       catch(NoSuchProviderException ex)
   121       {
   122         Log.msg(ex.toString(), false);
   123         shutdown();
   124       }
   125       catch(AuthenticationFailedException ex)
   126       {
   127         // AuthentificationFailedException may be thrown if credentials are
   128         // bad or if the Mailbox is in use (locked).
   129         ex.printStackTrace();
   130         errors++;
   131       }
   132       catch(InterruptedException ex)
   133       {
   134         System.out.println("sonews: " + this + " returns: " + ex);
   135         return;
   136       }
   137       catch(MessagingException ex)
   138       {
   139         ex.printStackTrace();
   140         errors++;
   141       }
   142       catch(Exception ex)
   143       {
   144         ex.printStackTrace();
   145         errors++;
   146       }
   147     }
   148     Log.msg("MailPoller exited.", false);
   149   }
   150   
   151 }