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