1 package com.quadcap.pop3.client; 2 3 40 41 import java.util.ArrayList ; 42 import java.util.Date ; 43 import java.util.Enumeration ; 44 import java.util.HashMap ; 45 import java.util.Iterator ; 46 import java.util.Properties ; 47 import java.util.List ; 48 49 import java.io.DataInputStream ; 50 import java.io.IOException ; 51 import java.io.InputStream ; 52 import java.io.OutputStream ; 53 54 import java.net.InetAddress ; 55 56 import com.quadcap.io.HeaderEnumeration; 57 import com.quadcap.io.IO; 58 59 import com.quadcap.util.Debug; 60 61 70 public class Pop3Agent { 71 HashMap uidlMap = new HashMap (); 72 String hostName = null; 73 int portNumber = 110; 74 String login = null; 75 String passwd = null; 76 int daysKeep = 999999; 77 boolean enabled = true; 78 79 Session pop3 = null; 80 81 public void write(OutputStream w) throws IOException { 82 Iterator iter = uidlMap.keySet().iterator(); 83 while (iter.hasNext()) { 84 UidlEntry u = (UidlEntry)uidlMap.get(iter.next()); 85 IO.write(w, "UIDL: " + u.toString() + "\n"); 86 } 87 } 88 89 public void read(InputStream r) throws IOException { 90 DataInputStream d = new DataInputStream (r); 91 String line; 92 while ((line = d.readLine()) != null) { 93 if (line.startsWith("UIDL: ")) { 94 line = line.substring(6).trim(); 95 UidlEntry uidl = new UidlEntry(line); 96 uidlMap.put(uidl.getUidl(), uidl); 97 } 98 } 99 } 100 101 104 public Pop3Agent() { 105 } 106 107 110 public void run(Properties props, MessageHook hook) { 111 try { 112 hostName = props.getProperty("host"); 113 portNumber = Integer.parseInt(props.getProperty("port", "110")); 114 login = props.getProperty("user"); 115 passwd = props.getProperty("passwd"); 116 daysKeep = Integer.parseInt(props.getProperty("daysKeep", "3")); 117 enabled = "true".equalsIgnoreCase(props.getProperty("enabled", 118 "true")); 119 if (!enabled) return; 120 121 if (pop3 == null) { 122 pop3 = new Session(hostName, portNumber); 123 } 124 pop3.connect(); 125 126 int ret = pop3.user(login); 127 if (ret != Session.OK) { 128 throw new IOException ("login failed"); 129 } 130 131 ret = pop3.pass(passwd); 132 if (ret != Session.OK) { 133 throw new IOException ("login (passwd) failed"); 134 } 135 136 getMail(hook); 137 } catch (Throwable t) { 138 Debug.print(t); 139 } finally { 140 try { 141 if (pop3 != null) pop3.quit(); 142 } catch (IOException e) { 143 Debug.print(e); 144 } 145 pop3 = null; 148 } 149 } 150 151 152 156 void getMail(MessageHook hook) throws IOException { 157 InputStream uidls = pop3.uidl(); 158 ArrayList newMessages = new ArrayList (); ArrayList delMessages = new ArrayList (); int msgNum = 0; 161 String uidl = null; 162 StringBuffer sb = new StringBuffer (); 163 Date now = new Date (); 164 long ms = daysKeep * 24L * 60 * 60 * 1000; 165 Date windowStart = new Date (now.getTime() - ms); 166 int c = uidls.read(); 167 while (c >= 0) { 168 while (Character.isDigit((char)c)) { 169 sb.append((char)c); 170 c = uidls.read(); 171 } 172 if (sb.length() == 0) { 173 if (c < 0) break; 174 else { 175 c = uidls.read(); 176 continue; 177 } 178 } 179 msgNum = Integer.parseInt(sb.toString()); 180 181 sb.setLength(0); 182 while (Character.isWhitespace((char)c)) { 183 c = uidls.read(); 184 } 185 if (c < 0) continue; 186 while (c >= 0x21 && c <= 0x7e) { 187 sb.append((char)c); 188 c = uidls.read(); 189 } 190 uidl = sb.toString(); 191 sb.setLength(0); 192 UidlEntry e = getUidlEntry(uidl); 193 if (e == null) { 194 e = putUidlEntry(uidl, msgNum, now); 195 newMessages.add(e); 196 } else { 197 e.setMessageNumber(msgNum); 198 if (e.getFirstSeen().before(windowStart)) { 199 if (!e.markedForDeletion) { 200 delMessages.add(e); 201 e.markedForDeletion = true; 202 } 203 } 204 } 205 while (c >= 0 && c != 0x0a) { 206 c = uidls.read(); 207 } 208 if (c == 0x0a) c = uidls.read(); 209 } 210 getMessages(hook, newMessages); 211 deleteMessages(delMessages); 212 } 213 214 219 void getMessages(MessageHook hook, ArrayList v) throws IOException { 220 for (int i = 0; i < v.size(); i++) { 221 UidlEntry e = (UidlEntry)v.get(i); 222 int msg = e.getMessageNumber(); 223 boolean pass = hook.passAllHeaders(); 224 if (!pass) { 225 InputStream is = pop3.top("" + msg, 0); 226 try { 227 HashMap map = new HashMap (); 228 new HeaderEnumeration(is).getHeaderMap(map); 229 pass = hook.passHeaders(map); 230 } finally { 231 is.close(); 232 } 233 } 234 if (pass) { 235 InputStream body = pop3.retr("" + msg); 236 try { 237 if (hook.passMessage(body)) { 238 if (!e.markedForDeletion) { 239 pop3.dele(msg); 240 e.markedForDeletion = true; 241 } 242 } 243 } catch (IOException e1) { 244 Debug.print(e1); 245 throw e1; 246 } catch (Exception e2) { 247 Debug.print(e2); 249 throw new IOException (e2.toString()); 251 } finally { 252 body.close(); 253 } 254 } 255 } 256 } 257 258 264 void deleteMessages(ArrayList v) throws IOException { 265 for (int i = 0; i < v.size(); i++) { 266 UidlEntry e = (UidlEntry)v.get(i); 267 int msg = e.getMessageNumber(); 268 pop3.dele(msg); 269 uidlMap.remove(e.getUidl()); 270 } 271 } 272 273 280 UidlEntry getUidlEntry(String uidl) { 281 return (UidlEntry)uidlMap.get(uidl); 282 } 283 284 293 UidlEntry putUidlEntry(String uidl, int msgNum, Date d) { 294 UidlEntry e = new UidlEntry(uidl, msgNum, d); 295 uidlMap.put(uidl, e); 296 return e; 297 } 298 } 299 | Popular Tags |