Show
Ignore:
Timestamp:
08/04/08 09:17:20 PM (5 months ago)
Author:
octorian
Message:

Initial IMAP IDLE implementation

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/imap/ImapProtocol.java

    r264 r266  
    4848public class ImapProtocol { 
    4949    private Connection connection; 
     50    private String idleCommandTag; 
    5051 
    5152    /** 
     
    889890 
    890891    /** 
     892     * Execute the "IDLE" command. 
     893     */ 
     894    public void executeIdle() throws IOException, MailException { 
     895        if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
     896            EventLogger.logEvent( 
     897            AppInfo.GUID, 
     898            ("ImapProtocol.executeIdle()").getBytes(), 
     899            EventLogger.DEBUG_INFO); 
     900        } 
     901        idleCommandTag = executeNoReply("IDLE", null); 
     902    } 
     903     
     904    /** 
     905     * Execute the "DONE" command. 
     906     */ 
     907    public void executeIdleDone() throws IOException, MailException { 
     908        if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
     909            EventLogger.logEvent( 
     910            AppInfo.GUID, 
     911            ("ImapProtocol.executeIdleDone()").getBytes(), 
     912            EventLogger.DEBUG_INFO); 
     913        } 
     914        executeUntagged("DONE", null, idleCommandTag); 
     915        idleCommandTag = null; 
     916    } 
     917     
     918    /** 
     919     * Polls the connection during the IDLE state. 
     920     * For now, this is a simple implementation.  It just checks for 
     921     * the presence of a "+ ?? recent" untagged response, and returns 
     922     * true if one is found. 
     923     */ 
     924    public boolean executeIdlePoll() throws IOException, MailException { 
     925                String result = receive(); 
     926                if(result != null && result.startsWith("*") && result.toLowerCase().endsWith("recent")) { 
     927                        return true; 
     928                } 
     929                else { 
     930                        return false; 
     931                } 
     932    } 
     933     
     934    /** 
    891935     * Executes an IMAP command several times, with different arguments, 
    892936     * and return the replies as an array of strings. 
     
    10061050        return result; 
    10071051    } 
     1052     
     1053    /** 
     1054     * Attempts to read a line of text from the server, without sending 
     1055     * anything first. 
     1056     * @return Returned string, or null if nothing is available 
     1057     */ 
     1058    protected String receive() 
     1059        throws IOException, MailException 
     1060        { 
     1061        String result; 
     1062         
     1063        if(connection.available() > 0) { 
     1064                System.err.println("-->connection.available() == " + connection.available()); 
     1065                result = connection.receive(); 
     1066                System.err.println("-->Read in "+result.length()); 
     1067                System.err.println("-->connection.available() == " + connection.available()); 
     1068        } 
     1069        else { 
     1070                result = null; 
     1071        } 
     1072        return result; 
     1073        } 
     1074     
     1075    /** 
     1076     * Executes an IMAP command directly and expects no reply. 
     1077     * @param command IMAP command 
     1078     * @param arguments Arguments for the command 
     1079     * @return Tag used to send the command 
     1080     */ 
     1081    protected String executeNoReply(String command, String arguments) 
     1082        throws IOException, MailException 
     1083        { 
     1084        String tag = "A" + commandCount++ + " "; 
     1085        connection.sendCommand(tag + command + (arguments == null ? "" : " " + arguments)); 
     1086 
     1087        return tag; 
     1088    } 
     1089     
     1090    /** 
     1091     * Executes an IMAP command without a tag prefix, 
     1092     * and returns the reply as an array of strings. 
     1093     * @param command IMAP command 
     1094     * @param arguments Arguments for the command 
     1095     * @param Known tag that indicates the end of the result 
     1096     * @return List of returned strings 
     1097     */ 
     1098    protected String[] executeUntagged(String command, String arguments, String endTag) 
     1099        throws IOException, MailException 
     1100    { 
     1101        String[] result = new String[0]; 
     1102         
     1103        connection.sendCommand(command + (arguments == null ? "" : " " + arguments)); 
     1104         
     1105        String temp = connection.receive(); 
     1106        while (!temp.startsWith(endTag)) { 
     1107            Arrays.add(result, temp); 
     1108            temp = connection.receive(); 
     1109        } 
     1110 
     1111        temp = temp.substring(endTag.length()); 
     1112        if (temp.startsWith("BAD ") || temp.startsWith("NO ")) { 
     1113            throw new MailException(temp); 
     1114        } 
     1115        return result; 
     1116    } 
    10081117}