/* ============================================================================ * Douglas Thrift's IMAP Handler License * * Copyright (C) 2002, Douglas Thrift. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: * * "This product includes software developed by Douglas Thrift * (http://computers.douglasthrift.net/webcontact.html)." * * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * * 4. The names "Douglas Thrift" and "Douglas Thrift's IMAP Handler" must not * be used to endorse or promote products derived from this software without * specific prior written permission. For written permission, please visit * http://www.douglasthrift.net/contact.html for contact information. * * 5. Products derived from this software may not be called "Douglas Thrift's * IMAP Handler", nor may "Douglas Thrift's IMAP Handler" appear in their * name, without prior written permission. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================ */ // IMAP Handler // // Douglas Thrift // // IMAPHandler.java import java.io.*; import java.net.*; import java.text.*; public class IMAPHandler { IMAPHandler(String host, int port, boolean debug) throws IOException { this.debug = debug; letter = 'a'; number = 0; format = NumberFormat.getInstance(); format.setMaximumIntegerDigits(3); format.setMinimumIntegerDigits(3); socket = new Socket(host, port); out = new PrintWriter(socket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String input = in.readLine(); if (debug) System.err.println(input); } public String capability() throws IOException { return imap("CAPABILITY"); } public String noop() throws IOException { return imap("NOOP"); } public String logout() throws IOException { return imap("LOGOUT"); } public String login(String args) throws IOException { return imap("LOGIN", args); } public String select(String args) throws IOException { return imap("SELECT", args); } public String examine(String args) throws IOException { return imap("EXAMINE", args); } public String create(String args) throws IOException { return imap("CREATE", args); } public String delete(String args) throws IOException { return imap("DELETE", args); } public String rename(String args) throws IOException { return imap("RENAME", args); } public String subscribe(String args) throws IOException { return imap("SUBSCRIBE", args); } public String unsubscribe(String args) throws IOException { return imap("UNSUBSCRIBE", args); } public String list(String args) throws IOException { return imap("LIST", args); } public String lsub(String args) throws IOException { return imap("LSUB", args); } public String status(String args) throws IOException { return imap("STATUS", args); } public String append(String args, String message) throws IOException { return imap("APPEND", args, message); } public String check() throws IOException { return imap("CHECK"); } public String close() throws IOException { return imap("CLOSE"); } public String expunge() throws IOException { return imap("EXPUNGE"); } public String search(String args) throws IOException { return imap("SEARCH", args); } public String fetch(String args) throws IOException { return imap("FETCH", args); } public String store(String args) throws IOException { return imap("STORE", args); } public String copy(String args) throws IOException { return imap("COPY", args); } public String uid(String args) throws IOException { return imap("UID", args); } public boolean successful() { return success; } private Socket socket; private PrintWriter out; private BufferedReader in; private boolean debug; private NumberFormat format; private char letter; private int number; private boolean success; private String command() { String command = letter + format.format(number++); if (number > 999) { letter++; number = 0; } if (letter > 'z') { letter = 'a'; } return command; } private String imap(String imap) throws IOException { StringBuffer result = new StringBuffer(); String command = command(); String output = command + " " + imap; out.println(output); if (debug) System.err.println(output); while (true) { String input = in.readLine(); if (debug) System.err.println(input); if (input.indexOf(command + " OK") == 0) { success = true; break; } else if (input.indexOf(command + " NO") == 0) { success = false; break; } else if (input.indexOf(command + " BAD") == 0) { success = false; break; } else { result.append(input + "\r\n"); } } return result.toString(); } private String imap(String imap, String args) throws IOException { StringBuffer result = new StringBuffer(); String command = command(); String output = command + " " + imap + " " + args; out.println(output); if (debug) System.err.println(output); while (true) { String input = in.readLine(); if (debug) System.err.println(input); if (input.indexOf(command + " OK") == 0) { success = true; break; } else if (input.indexOf(command + " NO") == 0) { success = false; break; } else if (input.indexOf(command + " BAD") == 0) { success = false; break; } else { result.append(input + "\r\n"); } } return result.toString(); } private String imap(String imap, String args, String message) throws IOException { StringBuffer result = new StringBuffer(); String command = command(); String output = command + " " + imap + " " + args; out.println(output); if (debug) System.err.println(output); output = message; out.println(output); if (debug) System.err.println(output); while (true) { String input = in.readLine(); if (debug) System.err.println(input); if (input.indexOf(command + " OK") == 0) { success = true; break; } else if (input.indexOf(command + " NO") == 0) { success = false; break; } else if (input.indexOf(command + " BAD") == 0) { success = false; break; } else { result.append(input + "\r\n"); } } return result.toString(); } }