00001
package com.quadcap.pop3.client;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.util.ArrayList;
00042
import java.util.Date;
00043
import java.util.Enumeration;
00044
import java.util.HashMap;
00045
import java.util.Iterator;
00046
import java.util.Properties;
00047
import java.util.List;
00048
00049
import java.io.DataInputStream;
00050
import java.io.IOException;
00051
import java.io.InputStream;
00052
import java.io.OutputStream;
00053
00054
import java.net.InetAddress;
00055
00056
import com.quadcap.io.HeaderEnumeration;
00057
import com.quadcap.io.IO;
00058
00059
import com.quadcap.util.Debug;
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 public class Pop3Agent {
00071 HashMap
uidlMap =
new HashMap();
00072 String
hostName = null;
00073 int portNumber = 110;
00074 String
login = null;
00075 String
passwd = null;
00076 int daysKeep = 999999;
00077 boolean enabled =
true;
00078
00079 Session pop3 = null;
00080
00081 public void write(OutputStream w)
throws IOException {
00082 Iterator iter =
uidlMap.keySet().iterator();
00083
while (iter.hasNext()) {
00084
UidlEntry u = (
UidlEntry)
uidlMap.get(iter.next());
00085
IO.write(w,
"UIDL: " + u.
toString() +
"\n");
00086 }
00087 }
00088
00089 public void read(InputStream r)
throws IOException {
00090 DataInputStream d =
new DataInputStream(r);
00091 String line;
00092
while ((line = d.readLine()) != null) {
00093
if (line.startsWith(
"UIDL: ")) {
00094 line = line.substring(6).trim();
00095
UidlEntry uidl =
new UidlEntry(line);
00096
uidlMap.put(uidl.
getUidl(), uidl);
00097 }
00098 }
00099 }
00100
00101
00102
00103
00104 public Pop3Agent() {
00105 }
00106
00107
00108
00109
00110 public void run(Properties props,
MessageHook hook) {
00111
try {
00112
hostName = props.getProperty(
"host");
00113
portNumber = Integer.parseInt(props.getProperty(
"port",
"110"));
00114
login = props.getProperty(
"user");
00115
passwd = props.getProperty(
"passwd");
00116
daysKeep = Integer.parseInt(props.getProperty(
"daysKeep",
"3"));
00117
enabled =
"true".equalsIgnoreCase(props.getProperty(
"enabled",
00118
"true"));
00119
if (!
enabled)
return;
00120
00121
if (
pop3 == null) {
00122
pop3 =
new Session(
hostName,
portNumber);
00123 }
00124
pop3.
connect();
00125
00126
int ret =
pop3.
user(
login);
00127
if (ret !=
Session.OK) {
00128
throw new IOException(
"login failed");
00129 }
00130
00131 ret =
pop3.
pass(
passwd);
00132
if (ret !=
Session.OK) {
00133
throw new IOException(
"login (passwd) failed");
00134 }
00135
00136
getMail(hook);
00137 }
catch (Throwable t) {
00138
Debug.print(t);
00139 } finally {
00140
try {
00141
if (
pop3 != null)
pop3.
quit();
00142 }
catch (IOException e) {
00143
Debug.print(e);
00144 }
00145
00146
00147
pop3 = null;
00148 }
00149 }
00150
00151
00152
00153
00154
00155
00156 void getMail(
MessageHook hook)
throws IOException {
00157 InputStream uidls =
pop3.
uidl();
00158 ArrayList newMessages =
new ArrayList();
00159 ArrayList delMessages =
new ArrayList();
00160
int msgNum = 0;
00161 String uidl = null;
00162 StringBuffer sb =
new StringBuffer();
00163 Date now =
new Date();
00164
long ms =
daysKeep * 24L * 60 * 60 * 1000;
00165 Date windowStart =
new Date(now.getTime() - ms);
00166
int c = uidls.read();
00167
while (c >= 0) {
00168
while (Character.isDigit((
char)c)) {
00169 sb.append((
char)c);
00170 c = uidls.read();
00171 }
00172
if (sb.length() == 0) {
00173
if (c < 0)
break;
00174
else {
00175 c = uidls.read();
00176
continue;
00177 }
00178 }
00179 msgNum = Integer.parseInt(sb.toString());
00180
00181 sb.setLength(0);
00182
while (Character.isWhitespace((
char)c)) {
00183 c = uidls.read();
00184 }
00185
if (c < 0)
continue;
00186
while (c >= 0x21 && c <= 0x7e) {
00187 sb.append((
char)c);
00188 c = uidls.read();
00189 }
00190 uidl = sb.toString();
00191 sb.setLength(0);
00192
UidlEntry e =
getUidlEntry(uidl);
00193
if (e == null) {
00194 e =
putUidlEntry(uidl, msgNum, now);
00195 newMessages.add(e);
00196 }
else {
00197 e.
setMessageNumber(msgNum);
00198
if (e.
getFirstSeen().before(windowStart)) {
00199
if (!e.
markedForDeletion) {
00200 delMessages.add(e);
00201 e.
markedForDeletion =
true;
00202 }
00203 }
00204 }
00205
while (c >= 0 && c != 0x0a) {
00206 c = uidls.read();
00207 }
00208
if (c == 0x0a) c = uidls.read();
00209 }
00210
getMessages(hook, newMessages);
00211
deleteMessages(delMessages);
00212 }
00213
00214
00215
00216
00217
00218
00219 void getMessages(
MessageHook hook, ArrayList v)
throws IOException {
00220
for (
int i = 0; i < v.size(); i++) {
00221
UidlEntry e = (
UidlEntry)v.get(i);
00222
int msg = e.
getMessageNumber();
00223
boolean pass = hook.passAllHeaders();
00224
if (!pass) {
00225 InputStream is =
pop3.
top(
"" + msg, 0);
00226
try {
00227 HashMap map =
new HashMap();
00228
new HeaderEnumeration(is).getHeaderMap(map);
00229 pass = hook.passHeaders(map);
00230 } finally {
00231 is.close();
00232 }
00233 }
00234
if (pass) {
00235 InputStream body =
pop3.
retr(
"" + msg);
00236
try {
00237
if (hook.passMessage(body)) {
00238
if (!e.
markedForDeletion) {
00239
pop3.
dele(msg);
00240 e.
markedForDeletion =
true;
00241 }
00242 }
00243 }
catch (IOException e1) {
00244
Debug.print(e1);
00245
throw e1;
00246 }
catch (Exception e2) {
00247
00248
Debug.print(e2);
00249
00250
throw new IOException(e2.toString());
00251 } finally {
00252 body.close();
00253 }
00254 }
00255 }
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 void deleteMessages(ArrayList v)
throws IOException {
00265
for (
int i = 0; i < v.size(); i++) {
00266
UidlEntry e = (
UidlEntry)v.get(i);
00267
int msg = e.
getMessageNumber();
00268
pop3.
dele(msg);
00269
uidlMap.remove(e.
getUidl());
00270 }
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280 UidlEntry getUidlEntry(String uidl) {
00281
return (
UidlEntry)
uidlMap.get(uidl);
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 UidlEntry putUidlEntry(String uidl,
int msgNum, Date d) {
00294
UidlEntry e =
new UidlEntry(uidl, msgNum, d);
00295
uidlMap.put(uidl, e);
00296
return e;
00297 }
00298 }