home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Mail For Me / Mail4ME.jar / de / trantor / mail / ImapClient.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-10-21  |  5.7 KB  |  164 lines

  1. package de.trantor.mail;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6.  
  7. public class ImapClient implements InboxClient {
  8.    private Connection socket;
  9.    private InputStream input;
  10.    private OutputStream output;
  11.    private boolean debug = false;
  12.    private int commandCount = 0;
  13.  
  14.    public void open(String host, String user, String pass) throws Exception, IOException, Pop3Exception {
  15.       if (this.connected()) {
  16.          this.close();
  17.       }
  18.  
  19.       this.socket = Connection.createConnection();
  20.       this.socket.open(host, 143);
  21.       this.input = this.socket.getInputStream();
  22.       this.output = this.socket.getOutputStream();
  23.  
  24.       try {
  25.          this.execute("LOGIN", user + " " + pass, (Message)null);
  26.          this.execute("SELECT", "INBOX", (Message)null);
  27.       } catch (Pop3Exception var5) {
  28.          this.socket.close();
  29.          this.input = null;
  30.          this.output = null;
  31.          this.socket = null;
  32.          throw var5;
  33.       }
  34.    }
  35.  
  36.    public void close() throws IOException, Pop3Exception {
  37.       if (this.connected()) {
  38.          this.execute("CLOSE", "", (Message)null);
  39.          this.execute("LOGOUT", "", (Message)null);
  40.       }
  41.  
  42.       this.socket.close();
  43.       this.input = null;
  44.       this.output = null;
  45.       this.socket = null;
  46.    }
  47.  
  48.    public boolean connected() {
  49.       return this.socket != null;
  50.    }
  51.  
  52.    private void send(String s) throws IOException, Pop3Exception {
  53.       if (this.debug) {
  54.          System.out.println("[IMAP/SEND] " + s);
  55.       }
  56.  
  57.       this.output.write(s.getBytes());
  58.       this.output.write(13);
  59.       this.output.write(10);
  60.    }
  61.  
  62.    private String receive() throws IOException, Pop3Exception {
  63.       StringBuffer buffer = new StringBuffer("");
  64.  
  65.       char c;
  66.       do {
  67.          c = (char)this.input.read();
  68.          if (c != -1 && c != '\r' && c != '\n') {
  69.             buffer.append(c);
  70.          }
  71.       } while(c != '\r');
  72.  
  73.       if (this.debug) {
  74.          System.out.println("[IMAP/RECV] " + buffer);
  75.       }
  76.  
  77.       return new String(buffer);
  78.    }
  79.  
  80.    private String execute(String command, String arguments, Message message) throws IOException, Pop3Exception {
  81.       String result = null;
  82.       String tag = "A" + this.commandCount++ + " ";
  83.       this.send(tag + command + " " + arguments);
  84.  
  85.       String temp;
  86.       for(temp = this.receive(); !temp.startsWith(tag); temp = this.receive()) {
  87.          if (temp.indexOf(" " + command + " ") != -1) {
  88.             int p = temp.indexOf(40);
  89.             int q = temp.indexOf(41, p + 1);
  90.             if (p != -1) {
  91.                if (q > p) {
  92.                   result = temp.substring(p + 1, q);
  93.                } else if (message != null) {
  94.                   int left = temp.indexOf(123);
  95.                   int right = temp.indexOf(125, left);
  96.                   this.receiveMessage(message, Integer.parseInt(temp.substring(left + 1, right)));
  97.                }
  98.             }
  99.          }
  100.       }
  101.  
  102.       temp = temp.substring(tag.length());
  103.       if (!temp.startsWith("BAD ") && !temp.startsWith("NO ")) {
  104.          return result;
  105.       } else {
  106.          throw new Pop3Exception(temp);
  107.       }
  108.    }
  109.  
  110.    public int getMessageCount() throws IOException, Pop3Exception {
  111.       String buffer = this.execute("STATUS", "INBOX (MESSAGES)", (Message)null);
  112.       int space = buffer.indexOf(32);
  113.       return Integer.parseInt(buffer.substring(space + 1));
  114.    }
  115.  
  116.    private void receiveMessage(Message message, int size) throws IOException, Pop3Exception {
  117.       int count = 0;
  118.       String buffer = this.receive();
  119.  
  120.       int octets;
  121.       for(octets = buffer.length() + 2; !buffer.equals(""); octets = octets + buffer.length() + 2) {
  122.          if (!buffer.startsWith(" ") && !buffer.startsWith("\t")) {
  123.             message.addHeaderLine(buffer);
  124.             ++count;
  125.          } else {
  126.             message.setHeaderLine(count - 1, message.getHeaderLine(count - 1) + "\r\n" + buffer);
  127.          }
  128.  
  129.          buffer = this.receive();
  130.       }
  131.  
  132.       while(octets < size) {
  133.          buffer = this.receive();
  134.          octets = octets + buffer.length() + 2;
  135.          message.addBodyLine(buffer);
  136.       }
  137.  
  138.    }
  139.  
  140.    public Message getMessage(int index) throws IOException, Pop3Exception {
  141.       Message message = new Message();
  142.       this.execute("FETCH", index + 1 + " (RFC822)", message);
  143.       return message;
  144.    }
  145.  
  146.    public Message getHeaders(int index) throws IOException, Pop3Exception {
  147.       Message message = new Message();
  148.       this.execute("FETCH", index + 1 + " (RFC822.HEADER)", message);
  149.       return message;
  150.    }
  151.  
  152.    public void removeMessage(int index) throws IOException, Pop3Exception {
  153.       this.execute("STORE", index + 1 + " +FLAGS.SILENT (\\DELETED)", (Message)null);
  154.    }
  155.  
  156.    public void setDebug(boolean debug) {
  157.       this.debug = debug;
  158.    }
  159.  
  160.    public boolean getDebug() {
  161.       return this.debug;
  162.    }
  163. }
  164.