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