src/org/sonews/mlgw/MailPoller.java
author cli
Wed, 04 May 2011 18:06:24 +0200
changeset 40 f8894df2d079
parent 37 74139325d305
permissions -rwxr-xr-x
Refactoring.
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
cli@37
    45
	static class PasswordAuthenticator extends Authenticator
cli@37
    46
	{
chris@1
    47
cli@37
    48
		@Override
cli@37
    49
		public PasswordAuthentication getPasswordAuthentication()
cli@37
    50
		{
cli@37
    51
			final String username =
cli@37
    52
				Config.inst().get(Config.MLPOLL_USER, "user");
cli@37
    53
			final String password =
cli@37
    54
				Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
chris@1
    55
cli@37
    56
			return new PasswordAuthentication(username, password);
cli@37
    57
		}
cli@37
    58
	}
chris@1
    59
cli@37
    60
	@Override
cli@37
    61
	public void run()
cli@37
    62
	{
cli@37
    63
		Log.get().info("Starting Mailinglist Poller...");
cli@37
    64
		int errors = 0;
cli@37
    65
		while (isRunning()) {
cli@37
    66
			try {
cli@37
    67
				// Wait some time between runs. At the beginning has advantages,
cli@37
    68
				// because the wait is not skipped if an exception occurs.
cli@37
    69
				Thread.sleep(60000 * (errors + 1)); // one minute * errors
chris@1
    70
cli@37
    71
				final String host =
cli@37
    72
					Config.inst().get(Config.MLPOLL_HOST, "samplehost");
cli@37
    73
				final String username =
cli@37
    74
					Config.inst().get(Config.MLPOLL_USER, "user");
cli@37
    75
				final String password =
cli@37
    76
					Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
chris@1
    77
cli@37
    78
				Stats.getInstance().mlgwRunStart();
chris@1
    79
cli@37
    80
				// Create empty properties
cli@37
    81
				Properties props = System.getProperties();
cli@37
    82
				props.put("mail.pop3.host", host);
cli@37
    83
				props.put("mail.mime.address.strict", "false");
chris@1
    84
cli@37
    85
				// Get session
cli@37
    86
				Session session = Session.getInstance(props);
cli@37
    87
cli@37
    88
				// Get the store
cli@37
    89
				Store store = session.getStore("pop3");
cli@37
    90
				store.connect(host, 110, username, password);
cli@37
    91
cli@37
    92
				// Get folder
cli@37
    93
				Folder folder = store.getFolder("INBOX");
cli@37
    94
				folder.open(Folder.READ_WRITE);
cli@37
    95
cli@37
    96
				// Get directory
cli@37
    97
				Message[] messages = folder.getMessages();
cli@37
    98
cli@37
    99
				// Dispatch messages and delete it afterwards on the inbox
cli@37
   100
				for (Message message : messages) {
cli@37
   101
					if (Dispatcher.toGroup(message)
cli@37
   102
						|| Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false)) {
cli@37
   103
						// Delete the message
cli@37
   104
						message.setFlag(Flag.DELETED, true);
cli@37
   105
					}
cli@37
   106
				}
cli@37
   107
cli@37
   108
				// Close connection
cli@37
   109
				folder.close(true); // true to expunge deleted messages
cli@37
   110
				store.close();
cli@37
   111
				errors = 0;
cli@37
   112
cli@37
   113
				Stats.getInstance().mlgwRunEnd();
cli@37
   114
			} catch (NoSuchProviderException ex) {
cli@37
   115
				Log.get().severe(ex.toString());
cli@37
   116
				shutdown();
cli@37
   117
			} catch (AuthenticationFailedException ex) {
cli@37
   118
				// AuthentificationFailedException may be thrown if credentials are
cli@37
   119
				// bad or if the Mailbox is in use (locked).
cli@37
   120
				ex.printStackTrace();
cli@37
   121
				errors = errors < 5 ? errors + 1 : errors;
cli@37
   122
			} catch (InterruptedException ex) {
cli@37
   123
				System.out.println("sonews: " + this + " returns: " + ex);
cli@37
   124
				return;
cli@37
   125
			} catch (MessagingException ex) {
cli@37
   126
				ex.printStackTrace();
cli@37
   127
				errors = errors < 5 ? errors + 1 : errors;
cli@37
   128
			} catch (Exception ex) {
cli@37
   129
				ex.printStackTrace();
cli@37
   130
				errors = errors < 5 ? errors + 1 : errors;
cli@37
   131
			}
cli@37
   132
		}
cli@37
   133
		Log.get().severe("MailPoller exited.");
cli@37
   134
	}
chris@1
   135
}