1 16 package org.apache.commons.net.pop3; 17 18 import java.io.IOException ; 19 import java.io.Reader ; 20 import java.security.MessageDigest ; 21 import java.security.NoSuchAlgorithmException ; 22 import java.util.Enumeration ; 23 import java.util.StringTokenizer ; 24 import org.apache.commons.net.io.DotTerminatedMessageReader; 25 26 48 49 public class POP3Client extends POP3 50 { 51 52 private static POP3MessageInfo __parseStatus(String line) 53 { 54 int num, size; 55 StringTokenizer tokenizer; 56 57 tokenizer = new StringTokenizer (line); 58 59 if (!tokenizer.hasMoreElements()) 60 return null; 61 62 num = size = 0; 63 64 try 65 { 66 num = Integer.parseInt(tokenizer.nextToken()); 67 68 if (!tokenizer.hasMoreElements()) 69 return null; 70 71 size = Integer.parseInt(tokenizer.nextToken()); 72 } 73 catch (NumberFormatException e) 74 { 75 return null; 76 } 77 78 return new POP3MessageInfo(num, size); 79 } 80 81 private static POP3MessageInfo __parseUID(String line) 82 { 83 int num; 84 StringTokenizer tokenizer; 85 86 tokenizer = new StringTokenizer (line); 87 88 if (!tokenizer.hasMoreElements()) 89 return null; 90 91 num = 0; 92 93 try 94 { 95 num = Integer.parseInt(tokenizer.nextToken()); 96 97 if (!tokenizer.hasMoreElements()) 98 return null; 99 100 line = tokenizer.nextToken(); 101 } 102 catch (NumberFormatException e) 103 { 104 return null; 105 } 106 107 return new POP3MessageInfo(num, line); 108 } 109 110 127 public boolean login(String username, String password) throws IOException 128 { 129 if (getState() != AUTHORIZATION_STATE) 130 return false; 131 132 if (sendCommand(POP3Command.USER, username) != POP3Reply.OK) 133 return false; 134 135 if (sendCommand(POP3Command.PASS, password) != POP3Reply.OK) 136 return false; 137 138 setState(TRANSACTION_STATE); 139 140 return true; 141 } 142 143 144 177 public boolean login(String username, String timestamp, String secret) 178 throws IOException , NoSuchAlgorithmException 179 { 180 int i; 181 byte[] digest; 182 StringBuffer buffer, digestBuffer; 183 MessageDigest md5; 184 185 if (getState() != AUTHORIZATION_STATE) 186 return false; 187 188 md5 = MessageDigest.getInstance("MD5"); 189 timestamp += secret; 190 digest = md5.digest(timestamp.getBytes()); 191 digestBuffer = new StringBuffer (128); 192 193 for (i = 0; i < digest.length; i++) 194 digestBuffer.append(Integer.toHexString(digest[i] & 0xff)); 195 196 buffer = new StringBuffer (256); 197 buffer.append(username); 198 buffer.append(' '); 199 buffer.append(digestBuffer.toString()); 200 201 if (sendCommand(POP3Command.APOP, buffer.toString()) != POP3Reply.OK) 202 return false; 203 204 setState(TRANSACTION_STATE); 205 206 return true; 207 } 208 209 210 225 public boolean logout() throws IOException 226 { 227 if (getState() == TRANSACTION_STATE) 228 setState(UPDATE_STATE); 229 sendCommand(POP3Command.QUIT); 230 return (_replyCode == POP3Reply.OK); 231 } 232 233 234 246 public boolean noop() throws IOException 247 { 248 if (getState() == TRANSACTION_STATE) 249 return (sendCommand(POP3Command.NOOP) == POP3Reply.OK); 250 return false; 251 } 252 253 254 269 public boolean deleteMessage(int messageId) throws IOException 270 { 271 if (getState() == TRANSACTION_STATE) 272 return (sendCommand(POP3Command.DELE, Integer.toString(messageId)) 273 == POP3Reply.OK); 274 return false; 275 } 276 277 278 289 public boolean reset() throws IOException 290 { 291 if (getState() == TRANSACTION_STATE) 292 return (sendCommand(POP3Command.RSET) == POP3Reply.OK); 293 return false; 294 } 295 296 311 public POP3MessageInfo status() throws IOException 312 { 313 if (getState() != TRANSACTION_STATE) 314 return null; 315 if (sendCommand(POP3Command.STAT) != POP3Reply.OK) 316 return null; 317 return __parseStatus(_lastReplyLine.substring(3)); 318 } 319 320 321 338 public POP3MessageInfo listMessage(int messageId) throws IOException 339 { 340 if (getState() != TRANSACTION_STATE) 341 return null; 342 if (sendCommand(POP3Command.LIST, Integer.toString(messageId)) 343 != POP3Reply.OK) 344 return null; 345 return __parseStatus(_lastReplyLine.substring(3)); 346 } 347 348 349 366 public POP3MessageInfo[] listMessages() throws IOException 367 { 368 POP3MessageInfo[] messages; 369 Enumeration en; 370 int line; 371 372 if (getState() != TRANSACTION_STATE) 373 return null; 374 if (sendCommand(POP3Command.LIST) != POP3Reply.OK) 375 return null; 376 getAdditionalReply(); 377 378 messages = new POP3MessageInfo[_replyLines.size() - 2]; 380 en = _replyLines.elements(); 381 382 en.nextElement(); 384 385 for (line = 0; line < messages.length; line++) 387 messages[line] = __parseStatus((String )en.nextElement()); 388 389 return messages; 390 } 391 392 409 public POP3MessageInfo listUniqueIdentifier(int messageId) 410 throws IOException 411 { 412 if (getState() != TRANSACTION_STATE) 413 return null; 414 if (sendCommand(POP3Command.UIDL, Integer.toString(messageId)) 415 != POP3Reply.OK) 416 return null; 417 return __parseUID(_lastReplyLine.substring(3)); 418 } 419 420 421 438 public POP3MessageInfo[] listUniqueIdentifiers() throws IOException 439 { 440 POP3MessageInfo[] messages; 441 Enumeration en; 442 int line; 443 444 if (getState() != TRANSACTION_STATE) 445 return null; 446 if (sendCommand(POP3Command.UIDL) != POP3Reply.OK) 447 return null; 448 getAdditionalReply(); 449 450 messages = new POP3MessageInfo[_replyLines.size() - 2]; 452 en = _replyLines.elements(); 453 454 en.nextElement(); 456 457 for (line = 0; line < messages.length; line++) 459 messages[line] = __parseUID((String )en.nextElement()); 460 461 return messages; 462 } 463 464 465 492 public Reader retrieveMessage(int messageId) throws IOException 493 { 494 if (getState() != TRANSACTION_STATE) 495 return null; 496 if (sendCommand(POP3Command.RETR, Integer.toString(messageId)) 497 != POP3Reply.OK) 498 return null; 499 500 return new DotTerminatedMessageReader(_reader); 501 } 502 503 504 535 public Reader retrieveMessageTop(int messageId, int numLines) 536 throws IOException 537 { 538 if (numLines < 0 || getState() != TRANSACTION_STATE) 539 return null; 540 if (sendCommand(POP3Command.TOP, Integer.toString(messageId) + " " + 541 Integer.toString(numLines)) != POP3Reply.OK) 542 return null; 543 544 return new DotTerminatedMessageReader(_reader); 545 } 546 547 548 } 549 550 | Popular Tags |