1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/org/sonews/mlgw/MailPoller.java Fri Jun 26 16:48:50 2009 +0200
1.3 @@ -0,0 +1,152 @@
1.4 +/*
1.5 + * SONEWS News Server
1.6 + * see AUTHORS for the list of contributors
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, either version 3 of the License, or
1.11 + * (at your option) any later version.
1.12 + *
1.13 + * This program is distributed in the hope that it will be useful,
1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 + * GNU General Public License for more details.
1.17 + *
1.18 + * You should have received a copy of the GNU General Public License
1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 + */
1.21 +
1.22 +package org.sonews.mlgw;
1.23 +
1.24 +import java.util.Properties;
1.25 +import javax.mail.AuthenticationFailedException;
1.26 +import javax.mail.Authenticator;
1.27 +import javax.mail.Flags.Flag;
1.28 +import javax.mail.Folder;
1.29 +import javax.mail.Message;
1.30 +import javax.mail.MessagingException;
1.31 +import javax.mail.NoSuchProviderException;
1.32 +import javax.mail.PasswordAuthentication;
1.33 +import javax.mail.Session;
1.34 +import javax.mail.Store;
1.35 +import org.sonews.daemon.Config;
1.36 +import org.sonews.daemon.AbstractDaemon;
1.37 +import org.sonews.util.Log;
1.38 +import org.sonews.util.Stats;
1.39 +
1.40 +/**
1.41 + * Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
1.42 + * @author Christian Lins
1.43 + * @since sonews/0.5.0
1.44 + */
1.45 +public class MailPoller extends AbstractDaemon
1.46 +{
1.47 +
1.48 + static class PasswordAuthenticator extends Authenticator
1.49 + {
1.50 +
1.51 + @Override
1.52 + public PasswordAuthentication getPasswordAuthentication()
1.53 + {
1.54 + final String username =
1.55 + Config.getInstance().get(Config.MLPOLL_USER, "user");
1.56 + final String password =
1.57 + Config.getInstance().get(Config.MLPOLL_PASSWORD, "mysecret");
1.58 +
1.59 + return new PasswordAuthentication(username, password);
1.60 + }
1.61 +
1.62 + }
1.63 +
1.64 + @Override
1.65 + public void run()
1.66 + {
1.67 + Log.msg("Starting Mailinglist Poller...", false);
1.68 + int errors = 0;
1.69 + while(isRunning() && errors < 5)
1.70 + {
1.71 + try
1.72 + {
1.73 + // Wait some time between runs. At the beginning has advantages,
1.74 + // because the wait is not skipped if an exception occurs.
1.75 + Thread.sleep(60000 * (errors + 1)); // one minute * errors
1.76 +
1.77 + final String host =
1.78 + Config.getInstance().get(Config.MLPOLL_HOST, "samplehost");
1.79 + final String username =
1.80 + Config.getInstance().get(Config.MLPOLL_USER, "user");
1.81 + final String password =
1.82 + Config.getInstance().get(Config.MLPOLL_PASSWORD, "mysecret");
1.83 +
1.84 + Stats.getInstance().mlgwRunStart();
1.85 +
1.86 + // Create empty properties
1.87 + Properties props = System.getProperties();
1.88 + props.put("mail.pop3.host", host);
1.89 +
1.90 + // Get session
1.91 + Session session = Session.getInstance(props);
1.92 +
1.93 + // Get the store
1.94 + Store store = session.getStore("pop3");
1.95 + store.connect(host, 110, username, password);
1.96 +
1.97 + // Get folder
1.98 + Folder folder = store.getFolder("INBOX");
1.99 + folder.open(Folder.READ_WRITE);
1.100 +
1.101 + // Get directory
1.102 + Message[] messages = folder.getMessages();
1.103 +
1.104 + // Dispatch messages and delete it afterwards on the inbox
1.105 + for(Message message : messages)
1.106 + {
1.107 + String subject = message.getSubject();
1.108 + System.out.println("MLGateway: message with subject \"" + subject + "\" received.");
1.109 + if(Dispatcher.toGroup(message)
1.110 + || Config.getInstance().get(Config.MLPOLL_DELETEUNKNOWN, false))
1.111 + {
1.112 + // Delete the message
1.113 + message.setFlag(Flag.DELETED, true);
1.114 + }
1.115 + }
1.116 +
1.117 + // Close connection
1.118 + folder.close(true); // true to expunge deleted messages
1.119 + store.close();
1.120 + errors = 0;
1.121 +
1.122 + Stats.getInstance().mlgwRunEnd();
1.123 + }
1.124 + catch(NoSuchProviderException ex)
1.125 + {
1.126 + Log.msg(ex.toString(), false);
1.127 + shutdown();
1.128 + }
1.129 + catch(AuthenticationFailedException ex)
1.130 + {
1.131 + // AuthentificationFailedException may be thrown if credentials are
1.132 + // bad or if the Mailbox is in use (locked).
1.133 + ex.printStackTrace();
1.134 + errors++;
1.135 + }
1.136 + catch(InterruptedException ex)
1.137 + {
1.138 + System.out.println("sonews: " + this + " returns.");
1.139 + return;
1.140 + }
1.141 + catch(MessagingException ex)
1.142 + {
1.143 + ex.printStackTrace();
1.144 + errors++;
1.145 + }
1.146 + catch(Exception ex)
1.147 + {
1.148 + ex.printStackTrace();
1.149 + errors++;
1.150 + }
1.151 + }
1.152 + Log.msg("MailPoller exited.", false);
1.153 + }
1.154 +
1.155 +}