Moving source files into src/-subdir.
3 * see AUTHORS for the list of contributors
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.
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.
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/>.
19 package org.sonews.mlgw;
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;
38 * Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
39 * @author Christian Lins
42 public class MailPoller extends AbstractDaemon
45 static class PasswordAuthenticator extends Authenticator
49 public PasswordAuthentication getPasswordAuthentication()
51 final String username =
52 Config.inst().get(Config.MLPOLL_USER, "user");
53 final String password =
54 Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
56 return new PasswordAuthentication(username, password);
64 Log.get().info("Starting Mailinglist Poller...");
70 // Wait some time between runs. At the beginning has advantages,
71 // because the wait is not skipped if an exception occurs.
72 Thread.sleep(60000 * (errors + 1)); // one minute * errors
75 Config.inst().get(Config.MLPOLL_HOST, "samplehost");
76 final String username =
77 Config.inst().get(Config.MLPOLL_USER, "user");
78 final String password =
79 Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
81 Stats.getInstance().mlgwRunStart();
83 // Create empty properties
84 Properties props = System.getProperties();
85 props.put("mail.pop3.host", host);
86 props.put("mail.mime.address.strict", "false");
89 Session session = Session.getInstance(props);
92 Store store = session.getStore("pop3");
93 store.connect(host, 110, username, password);
96 Folder folder = store.getFolder("INBOX");
97 folder.open(Folder.READ_WRITE);
100 Message[] messages = folder.getMessages();
102 // Dispatch messages and delete it afterwards on the inbox
103 for(Message message : messages)
105 if(Dispatcher.toGroup(message)
106 || Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false))
108 // Delete the message
109 message.setFlag(Flag.DELETED, true);
114 folder.close(true); // true to expunge deleted messages
118 Stats.getInstance().mlgwRunEnd();
120 catch(NoSuchProviderException ex)
122 Log.get().severe(ex.toString());
125 catch(AuthenticationFailedException ex)
127 // AuthentificationFailedException may be thrown if credentials are
128 // bad or if the Mailbox is in use (locked).
129 ex.printStackTrace();
130 errors = errors < 5 ? errors + 1 : errors;
132 catch(InterruptedException ex)
134 System.out.println("sonews: " + this + " returns: " + ex);
137 catch(MessagingException ex)
139 ex.printStackTrace();
140 errors = errors < 5 ? errors + 1 : errors;
144 ex.printStackTrace();
145 errors = errors < 5 ? errors + 1 : errors;
148 Log.get().severe("MailPoller exited.");