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@3
|
22 |
import javax.mail.Address;
|
chris@1
|
23 |
import javax.mail.AuthenticationFailedException;
|
chris@1
|
24 |
import javax.mail.Authenticator;
|
chris@1
|
25 |
import javax.mail.Flags.Flag;
|
chris@1
|
26 |
import javax.mail.Folder;
|
chris@1
|
27 |
import javax.mail.Message;
|
chris@1
|
28 |
import javax.mail.MessagingException;
|
chris@1
|
29 |
import javax.mail.NoSuchProviderException;
|
chris@1
|
30 |
import javax.mail.PasswordAuthentication;
|
chris@1
|
31 |
import javax.mail.Session;
|
chris@1
|
32 |
import javax.mail.Store;
|
chris@3
|
33 |
import org.sonews.config.Config;
|
chris@1
|
34 |
import org.sonews.daemon.AbstractDaemon;
|
chris@1
|
35 |
import org.sonews.util.Log;
|
chris@1
|
36 |
import org.sonews.util.Stats;
|
chris@1
|
37 |
|
chris@1
|
38 |
/**
|
chris@1
|
39 |
* Daemon polling for new mails in a POP3 account to be delivered to newsgroups.
|
chris@1
|
40 |
* @author Christian Lins
|
chris@1
|
41 |
* @since sonews/0.5.0
|
chris@1
|
42 |
*/
|
chris@1
|
43 |
public class MailPoller extends AbstractDaemon
|
chris@1
|
44 |
{
|
chris@1
|
45 |
|
chris@1
|
46 |
static class PasswordAuthenticator extends Authenticator
|
chris@1
|
47 |
{
|
chris@1
|
48 |
|
chris@1
|
49 |
@Override
|
chris@1
|
50 |
public PasswordAuthentication getPasswordAuthentication()
|
chris@1
|
51 |
{
|
chris@1
|
52 |
final String username =
|
chris@3
|
53 |
Config.inst().get(Config.MLPOLL_USER, "user");
|
chris@1
|
54 |
final String password =
|
chris@3
|
55 |
Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
|
chris@1
|
56 |
|
chris@1
|
57 |
return new PasswordAuthentication(username, password);
|
chris@1
|
58 |
}
|
chris@1
|
59 |
|
chris@1
|
60 |
}
|
chris@1
|
61 |
|
chris@1
|
62 |
@Override
|
chris@1
|
63 |
public void run()
|
chris@1
|
64 |
{
|
chris@1
|
65 |
Log.msg("Starting Mailinglist Poller...", false);
|
chris@1
|
66 |
int errors = 0;
|
chris@1
|
67 |
while(isRunning() && errors < 5)
|
chris@1
|
68 |
{
|
chris@1
|
69 |
try
|
chris@1
|
70 |
{
|
chris@1
|
71 |
// Wait some time between runs. At the beginning has advantages,
|
chris@1
|
72 |
// because the wait is not skipped if an exception occurs.
|
chris@1
|
73 |
Thread.sleep(60000 * (errors + 1)); // one minute * errors
|
chris@1
|
74 |
|
chris@1
|
75 |
final String host =
|
chris@3
|
76 |
Config.inst().get(Config.MLPOLL_HOST, "samplehost");
|
chris@1
|
77 |
final String username =
|
chris@3
|
78 |
Config.inst().get(Config.MLPOLL_USER, "user");
|
chris@1
|
79 |
final String password =
|
chris@3
|
80 |
Config.inst().get(Config.MLPOLL_PASSWORD, "mysecret");
|
chris@1
|
81 |
|
chris@1
|
82 |
Stats.getInstance().mlgwRunStart();
|
chris@1
|
83 |
|
chris@1
|
84 |
// Create empty properties
|
chris@1
|
85 |
Properties props = System.getProperties();
|
chris@1
|
86 |
props.put("mail.pop3.host", host);
|
chris@1
|
87 |
|
chris@1
|
88 |
// Get session
|
chris@1
|
89 |
Session session = Session.getInstance(props);
|
chris@1
|
90 |
|
chris@1
|
91 |
// Get the store
|
chris@1
|
92 |
Store store = session.getStore("pop3");
|
chris@1
|
93 |
store.connect(host, 110, username, password);
|
chris@1
|
94 |
|
chris@1
|
95 |
// Get folder
|
chris@1
|
96 |
Folder folder = store.getFolder("INBOX");
|
chris@1
|
97 |
folder.open(Folder.READ_WRITE);
|
chris@1
|
98 |
|
chris@1
|
99 |
// Get directory
|
chris@1
|
100 |
Message[] messages = folder.getMessages();
|
chris@1
|
101 |
|
chris@1
|
102 |
// Dispatch messages and delete it afterwards on the inbox
|
chris@1
|
103 |
for(Message message : messages)
|
chris@1
|
104 |
{
|
chris@1
|
105 |
if(Dispatcher.toGroup(message)
|
chris@3
|
106 |
|| Config.inst().get(Config.MLPOLL_DELETEUNKNOWN, false))
|
chris@1
|
107 |
{
|
chris@1
|
108 |
// Delete the message
|
chris@1
|
109 |
message.setFlag(Flag.DELETED, true);
|
chris@1
|
110 |
}
|
chris@1
|
111 |
}
|
chris@1
|
112 |
|
chris@1
|
113 |
// Close connection
|
chris@1
|
114 |
folder.close(true); // true to expunge deleted messages
|
chris@1
|
115 |
store.close();
|
chris@1
|
116 |
errors = 0;
|
chris@1
|
117 |
|
chris@1
|
118 |
Stats.getInstance().mlgwRunEnd();
|
chris@1
|
119 |
}
|
chris@1
|
120 |
catch(NoSuchProviderException ex)
|
chris@1
|
121 |
{
|
chris@1
|
122 |
Log.msg(ex.toString(), false);
|
chris@1
|
123 |
shutdown();
|
chris@1
|
124 |
}
|
chris@1
|
125 |
catch(AuthenticationFailedException ex)
|
chris@1
|
126 |
{
|
chris@1
|
127 |
// AuthentificationFailedException may be thrown if credentials are
|
chris@1
|
128 |
// bad or if the Mailbox is in use (locked).
|
chris@1
|
129 |
ex.printStackTrace();
|
chris@1
|
130 |
errors++;
|
chris@1
|
131 |
}
|
chris@1
|
132 |
catch(InterruptedException ex)
|
chris@1
|
133 |
{
|
chris@3
|
134 |
System.out.println("sonews: " + this + " returns: " + ex);
|
chris@1
|
135 |
return;
|
chris@1
|
136 |
}
|
chris@1
|
137 |
catch(MessagingException ex)
|
chris@1
|
138 |
{
|
chris@1
|
139 |
ex.printStackTrace();
|
chris@1
|
140 |
errors++;
|
chris@1
|
141 |
}
|
chris@1
|
142 |
catch(Exception ex)
|
chris@1
|
143 |
{
|
chris@1
|
144 |
ex.printStackTrace();
|
chris@1
|
145 |
errors++;
|
chris@1
|
146 |
}
|
chris@1
|
147 |
}
|
chris@1
|
148 |
Log.msg("MailPoller exited.", false);
|
chris@1
|
149 |
}
|
chris@1
|
150 |
|
chris@1
|
151 |
}
|