chris@1
|
1 |
/*
|
chris@1
|
2 |
* SONEWS News Server
|
chris@1
|
3 |
* see AUTHORS for the list of contributors
|
chris@1
|
4 |
*
|
chris@1
|
5 |
* This program is free software: you can redistribute it and/or modify
|
chris@1
|
6 |
* it under the terms of the GNU General Public License as published by
|
chris@1
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
chris@1
|
8 |
* (at your option) any later version.
|
chris@1
|
9 |
*
|
chris@1
|
10 |
* This program is distributed in the hope that it will be useful,
|
chris@1
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
chris@1
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
chris@1
|
13 |
* GNU General Public License for more details.
|
chris@1
|
14 |
*
|
chris@1
|
15 |
* You should have received a copy of the GNU General Public License
|
chris@1
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
chris@1
|
17 |
*/
|
chris@1
|
18 |
|
chris@1
|
19 |
package org.sonews.mlgw;
|
chris@1
|
20 |
|
chris@1
|
21 |
import java.util.Properties;
|
chris@1
|
22 |
import javax.mail.AuthenticationFailedException;
|
chris@1
|
23 |
import javax.mail.Authenticator;
|
chris@1
|
24 |
import javax.mail.Flags.Flag;
|
chris@1
|
25 |
import javax.mail.Folder;
|
chris@1
|
26 |
import javax.mail.Message;
|
chris@1
|
27 |
import javax.mail.MessagingException;
|
chris@1
|
28 |
import javax.mail.NoSuchProviderException;
|
chris@1
|
29 |
import javax.mail.PasswordAuthentication;
|
chris@1
|
30 |
import javax.mail.Session;
|
chris@1
|
31 |
import javax.mail.Store;
|
chris@1
|
32 |
import org.sonews.daemon.Config;
|
chris@1
|
33 |
import org.sonews.daemon.AbstractDaemon;
|
chris@1
|
34 |
import org.sonews.util.Log;
|
chris@1
|
35 |
import org.sonews.util.Stats;
|
chris@1
|
36 |
|
chris@1
|
37 |
/**
|
chris@1
|
38 |
* Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
|
chris@1
|
39 |
* @author Christian Lins
|
chris@1
|
40 |
* @since sonews/0.5.0
|
chris@1
|
41 |
*/
|
chris@1
|
42 |
public class MailPoller extends AbstractDaemon
|
chris@1
|
43 |
{
|
chris@1
|
44 |
|
chris@1
|
45 |
static class PasswordAuthenticator extends Authenticator
|
chris@1
|
46 |
{
|
chris@1
|
47 |
|
chris@1
|
48 |
@Override
|
chris@1
|
49 |
public PasswordAuthentication getPasswordAuthentication()
|
chris@1
|
50 |
{
|
chris@1
|
51 |
final String username =
|
chris@1
|
52 |
Config.getInstance().get(Config.MLPOLL_USER, "user");
|
chris@1
|
53 |
final String password =
|
chris@1
|
54 |
Config.getInstance().get(Config.MLPOLL_PASSWORD, "mysecret");
|
chris@1
|
55 |
|
chris@1
|
56 |
return new PasswordAuthentication(username, password);
|
chris@1
|
57 |
}
|
chris@1
|
58 |
|
chris@1
|
59 |
}
|
chris@1
|
60 |
|
chris@1
|
61 |
@Override
|
chris@1
|
62 |
public void run()
|
chris@1
|
63 |
{
|
chris@1
|
64 |
Log.msg("Starting Mailinglist Poller...", false);
|
chris@1
|
65 |
int errors = 0;
|
chris@1
|
66 |
while(isRunning() && errors < 5)
|
chris@1
|
67 |
{
|
chris@1
|
68 |
try
|
chris@1
|
69 |
{
|
chris@1
|
70 |
// Wait some time between runs. At the beginning has advantages,
|
chris@1
|
71 |
// because the wait is not skipped if an exception occurs.
|
chris@1
|
72 |
Thread.sleep(60000 * (errors + 1)); // one minute * errors
|
chris@1
|
73 |
|
chris@1
|
74 |
final String host =
|
chris@1
|
75 |
Config.getInstance().get(Config.MLPOLL_HOST, "samplehost");
|
chris@1
|
76 |
final String username =
|
chris@1
|
77 |
Config.getInstance().get(Config.MLPOLL_USER, "user");
|
chris@1
|
78 |
final String password =
|
chris@1
|
79 |
Config.getInstance().get(Config.MLPOLL_PASSWORD, "mysecret");
|
chris@1
|
80 |
|
chris@1
|
81 |
Stats.getInstance().mlgwRunStart();
|
chris@1
|
82 |
|
chris@1
|
83 |
// Create empty properties
|
chris@1
|
84 |
Properties props = System.getProperties();
|
chris@1
|
85 |
props.put("mail.pop3.host", host);
|
chris@1
|
86 |
|
chris@1
|
87 |
// Get session
|
chris@1
|
88 |
Session session = Session.getInstance(props);
|
chris@1
|
89 |
|
chris@1
|
90 |
// Get the store
|
chris@1
|
91 |
Store store = session.getStore("pop3");
|
chris@1
|
92 |
store.connect(host, 110, username, password);
|
chris@1
|
93 |
|
chris@1
|
94 |
// Get folder
|
chris@1
|
95 |
Folder folder = store.getFolder("INBOX");
|
chris@1
|
96 |
folder.open(Folder.READ_WRITE);
|
chris@1
|
97 |
|
chris@1
|
98 |
// Get directory
|
chris@1
|
99 |
Message[] messages = folder.getMessages();
|
chris@1
|
100 |
|
chris@1
|
101 |
// Dispatch messages and delete it afterwards on the inbox
|
chris@1
|
102 |
for(Message message : messages)
|
chris@1
|
103 |
{
|
chris@1
|
104 |
String subject = message.getSubject();
|
chris@1
|
105 |
System.out.println("MLGateway: message with subject \"" + subject + "\" received.");
|
chris@1
|
106 |
if(Dispatcher.toGroup(message)
|
chris@1
|
107 |
|| Config.getInstance().get(Config.MLPOLL_DELETEUNKNOWN, false))
|
chris@1
|
108 |
{
|
chris@1
|
109 |
// Delete the message
|
chris@1
|
110 |
message.setFlag(Flag.DELETED, true);
|
chris@1
|
111 |
}
|
chris@1
|
112 |
}
|
chris@1
|
113 |
|
chris@1
|
114 |
// Close connection
|
chris@1
|
115 |
folder.close(true); // true to expunge deleted messages
|
chris@1
|
116 |
store.close();
|
chris@1
|
117 |
errors = 0;
|
chris@1
|
118 |
|
chris@1
|
119 |
Stats.getInstance().mlgwRunEnd();
|
chris@1
|
120 |
}
|
chris@1
|
121 |
catch(NoSuchProviderException ex)
|
chris@1
|
122 |
{
|
chris@1
|
123 |
Log.msg(ex.toString(), false);
|
chris@1
|
124 |
shutdown();
|
chris@1
|
125 |
}
|
chris@1
|
126 |
catch(AuthenticationFailedException ex)
|
chris@1
|
127 |
{
|
chris@1
|
128 |
// AuthentificationFailedException may be thrown if credentials are
|
chris@1
|
129 |
// bad or if the Mailbox is in use (locked).
|
chris@1
|
130 |
ex.printStackTrace();
|
chris@1
|
131 |
errors++;
|
chris@1
|
132 |
}
|
chris@1
|
133 |
catch(InterruptedException ex)
|
chris@1
|
134 |
{
|
chris@1
|
135 |
System.out.println("sonews: " + this + " returns.");
|
chris@1
|
136 |
return;
|
chris@1
|
137 |
}
|
chris@1
|
138 |
catch(MessagingException ex)
|
chris@1
|
139 |
{
|
chris@1
|
140 |
ex.printStackTrace();
|
chris@1
|
141 |
errors++;
|
chris@1
|
142 |
}
|
chris@1
|
143 |
catch(Exception ex)
|
chris@1
|
144 |
{
|
chris@1
|
145 |
ex.printStackTrace();
|
chris@1
|
146 |
errors++;
|
chris@1
|
147 |
}
|
chris@1
|
148 |
}
|
chris@1
|
149 |
Log.msg("MailPoller exited.", false);
|
chris@1
|
150 |
}
|
chris@1
|
151 |
|
chris@1
|
152 |
}
|