chris@1: /*
chris@1:  *   SONEWS News Server
chris@1:  *   see AUTHORS for the list of contributors
chris@1:  *
chris@1:  *   This program is free software: you can redistribute it and/or modify
chris@1:  *   it under the terms of the GNU General Public License as published by
chris@1:  *   the Free Software Foundation, either version 3 of the License, or
chris@1:  *   (at your option) any later version.
chris@1:  *
chris@1:  *   This program is distributed in the hope that it will be useful,
chris@1:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1:  *   GNU General Public License for more details.
chris@1:  *
chris@1:  *   You should have received a copy of the GNU General Public License
chris@1:  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1:  */
chris@1: 
chris@1: package org.sonews.mlgw;
chris@1: 
chris@1: import java.util.Properties;
chris@1: import javax.mail.AuthenticationFailedException;
chris@1: import javax.mail.Authenticator;
chris@1: import javax.mail.Flags.Flag;
chris@1: import javax.mail.Folder;
chris@1: import javax.mail.Message;
chris@1: import javax.mail.MessagingException;
chris@1: import javax.mail.NoSuchProviderException;
chris@1: import javax.mail.PasswordAuthentication;
chris@1: import javax.mail.Session;
chris@1: import javax.mail.Store;
chris@3: import org.sonews.config.Config;
chris@1: import org.sonews.daemon.AbstractDaemon;
chris@1: import org.sonews.util.Log;
chris@1: import org.sonews.util.Stats;
chris@1: 
chris@1: /**
chris@1:  * Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
chris@1:  * @author Christian Lins
chris@1:  * @since sonews/0.5.0
chris@1:  */
chris@1: public class MailPoller extends AbstractDaemon
chris@1: {
chris@1: 
cli@37: 	static class PasswordAuthenticator extends Authenticator
cli@37: 	{
chris@1: 
cli@37: 		@Override
cli@37: 		public PasswordAuthentication getPasswordAuthentication()
cli@37: 		{
cli@37: 			final String username =
cli@37: 				Config.inst().get(Config.MLPOLL_USER, "user");
cli@37: 			final String password =
cli@37: 				Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
chris@1: 
cli@37: 			return new PasswordAuthentication(username, password);
cli@37: 		}
cli@37: 	}
chris@1: 
cli@37: 	@Override
cli@37: 	public void run()
cli@37: 	{
cli@37: 		Log.get().info("Starting Mailinglist Poller...");
cli@37: 		int errors = 0;
cli@37: 		while (isRunning()) {
cli@37: 			try {
cli@37: 				// Wait some time between runs. At the beginning has advantages,
cli@37: 				// because the wait is not skipped if an exception occurs.
cli@37: 				Thread.sleep(60000 * (errors + 1)); // one minute * errors
chris@1: 
cli@37: 				final String host =
cli@37: 					Config.inst().get(Config.MLPOLL_HOST, "samplehost");
cli@37: 				final String username =
cli@37: 					Config.inst().get(Config.MLPOLL_USER, "user");
cli@37: 				final String password =
cli@37: 					Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
chris@1: 
cli@37: 				Stats.getInstance().mlgwRunStart();
chris@1: 
cli@37: 				// Create empty properties
cli@37: 				Properties props = System.getProperties();
cli@37: 				props.put("mail.pop3.host", host);
cli@37: 				props.put("mail.mime.address.strict", "false");
chris@1: 
cli@37: 				// Get session
cli@37: 				Session session = Session.getInstance(props);
cli@37: 
cli@37: 				// Get the store
cli@37: 				Store store = session.getStore("pop3");
cli@37: 				store.connect(host, 110, username, password);
cli@37: 
cli@37: 				// Get folder
cli@37: 				Folder folder = store.getFolder("INBOX");
cli@37: 				folder.open(Folder.READ_WRITE);
cli@37: 
cli@37: 				// Get directory
cli@37: 				Message[] messages = folder.getMessages();
cli@37: 
cli@37: 				// Dispatch messages and delete it afterwards on the inbox
cli@37: 				for (Message message : messages) {
cli@37: 					if (Dispatcher.toGroup(message)
cli@37: 						|| Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false)) {
cli@37: 						// Delete the message
cli@37: 						message.setFlag(Flag.DELETED, true);
cli@37: 					}
cli@37: 				}
cli@37: 
cli@37: 				// Close connection
cli@37: 				folder.close(true); // true to expunge deleted messages
cli@37: 				store.close();
cli@37: 				errors = 0;
cli@37: 
cli@37: 				Stats.getInstance().mlgwRunEnd();
cli@37: 			} catch (NoSuchProviderException ex) {
cli@37: 				Log.get().severe(ex.toString());
cli@37: 				shutdown();
cli@37: 			} catch (AuthenticationFailedException ex) {
cli@37: 				// AuthentificationFailedException may be thrown if credentials are
cli@37: 				// bad or if the Mailbox is in use (locked).
cli@37: 				ex.printStackTrace();
cli@37: 				errors = errors < 5 ? errors + 1 : errors;
cli@37: 			} catch (InterruptedException ex) {
cli@37: 				System.out.println("sonews: " + this + " returns: " + ex);
cli@37: 				return;
cli@37: 			} catch (MessagingException ex) {
cli@37: 				ex.printStackTrace();
cli@37: 				errors = errors < 5 ? errors + 1 : errors;
cli@37: 			} catch (Exception ex) {
cli@37: 				ex.printStackTrace();
cli@37: 				errors = errors < 5 ? errors + 1 : errors;
cli@37: 			}
cli@37: 		}
cli@37: 		Log.get().severe("MailPoller exited.");
cli@37: 	}
chris@1: }