home *** CD-ROM | disk | FTP | other *** search
- package de.trantor.mail;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
-
- public class Pop3Client implements InboxClient {
- private Connection socket;
- private InputStream input;
- private OutputStream output;
- private boolean debug = false;
-
- public void open(String host, String user, String pass) throws Exception, IOException, Pop3Exception {
- if (this.connected()) {
- this.close();
- }
-
- this.socket = Connection.createConnection();
- this.socket.open(host, 110);
- this.input = this.socket.getInputStream();
- this.output = this.socket.getOutputStream();
-
- try {
- this.execute((String)null);
- this.execute("USER " + user);
- this.execute("PASS " + pass);
- } catch (Pop3Exception var5) {
- this.socket.close();
- this.input = null;
- this.output = null;
- this.socket = null;
- throw var5;
- }
- }
-
- public void close() throws IOException, Pop3Exception {
- if (this.connected()) {
- this.execute("QUIT");
- }
-
- this.socket.close();
- this.input = null;
- this.output = null;
- this.socket = null;
- }
-
- public boolean connected() {
- return this.socket != null;
- }
-
- private void send(String s) throws IOException, Pop3Exception {
- if (this.debug) {
- System.out.println("[POP3/SEND] " + s);
- }
-
- this.output.write(s.getBytes());
- this.output.write(13);
- this.output.write(10);
- }
-
- private String receive() throws IOException, Pop3Exception {
- StringBuffer buffer = new StringBuffer("");
-
- char c;
- do {
- c = (char)this.input.read();
- if (c != -1 && c != '\r' && c != '\n') {
- buffer.append(c);
- }
- } while(c != '\r');
-
- if (this.debug) {
- System.out.println("[POP3/RECV] " + buffer);
- }
-
- return new String(buffer);
- }
-
- private String execute(String command) throws IOException, Pop3Exception {
- if (command != null) {
- this.send(command);
- }
-
- String result = this.receive();
- if (result.length() > 1 && result.charAt(0) == '-') {
- throw new Pop3Exception(result);
- } else {
- return result;
- }
- }
-
- public int getMessageCount() throws IOException, Pop3Exception {
- String buffer = this.execute("STAT");
- int space1 = buffer.indexOf(32);
- int space2 = buffer.indexOf(32, space1 + 1);
- return Integer.parseInt(buffer.substring(space1 + 1, space2));
- }
-
- private Message receiveMessage() throws IOException, Pop3Exception {
- Message message = new Message();
- int count = 0;
-
- for(String buffer = this.receive(); !buffer.equals(""); buffer = this.receive()) {
- if (buffer.length() > 1 && buffer.charAt(0) == '.') {
- buffer = buffer.substring(1);
- }
-
- if (!buffer.startsWith(" ") && !buffer.startsWith("\t")) {
- message.addHeaderLine(buffer);
- ++count;
- } else {
- message.setHeaderLine(count - 1, message.getHeaderLine(count - 1) + "\r\n" + buffer);
- }
- }
-
- for(String var4 = this.receive(); !var4.equals("."); var4 = this.receive()) {
- if (var4.length() > 1 && var4.charAt(0) == '.') {
- var4 = var4.substring(1);
- }
-
- message.addBodyLine(var4);
- }
-
- return message;
- }
-
- public Message getMessage(int index) throws IOException, Pop3Exception {
- this.execute("RETR " + (index + 1));
- return this.receiveMessage();
- }
-
- public Message getHeaders(int index) throws IOException, Pop3Exception {
- this.execute("TOP " + (index + 1) + " 0");
- return this.receiveMessage();
- }
-
- public void removeMessage(int index) throws IOException, Pop3Exception {
- this.execute("DELE " + (index + 1));
- }
-
- public void setDebug(boolean debug) {
- this.debug = debug;
- }
-
- public boolean getDebug() {
- return this.debug;
- }
- }
-