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