Show
Ignore:
Timestamp:
08/02/08 05:15:29 PM (5 months ago)
Author:
octorian
Message:

UID support for messages

Files:
1 modified

Legend:

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

    r255 r262  
    454454    public static class FetchEnvelopeResponse { 
    455455        public int index; 
     456        public int uid; 
    456457        public MessageFlags flags; 
    457458        public MessageEnvelope envelope; 
     
    459460 
    460461    /** 
    461      * Execute the "FETCH (ENVELOPE)" command 
     462     * Execute the "FETCH (FLAGS UID ENVELOPE)" command 
    462463     * @param firstIndex Index of the first message 
    463464     * @param lastIndex Index of the last message 
     
    475476                                   Integer.toString(firstIndex) + ":" + 
    476477                                   Integer.toString(lastIndex) + 
    477                                    " (FLAGS ENVELOPE)"); 
     478                                   " (FLAGS UID ENVELOPE)"); 
    478479         
    479480        FetchEnvelopeResponse[] envResponses = new FetchEnvelopeResponse[(lastIndex - firstIndex)+1]; 
     
    526527                            envRespItem.flags = ImapParser.parseMessageFlags((Vector)parsedText.elementAt(j+1)); 
    527528                        } 
     529                        else if(((String)parsedText.elementAt(j)).equals("UID") && 
     530                                        parsedSize > j+1 && 
     531                                        parsedText.elementAt(j+1) instanceof String) { 
     532                                try { 
     533                                        envRespItem.uid = Integer.parseInt((String)parsedText.elementAt(j+1)); 
     534                                } catch (NumberFormatException e) { 
     535                                        envRespItem.uid = -1; 
     536                                } 
     537                        } 
    528538                        else if(((String)parsedText.elementAt(j)).equals("ENVELOPE") && 
    529539                                parsedSize > j+1 && 
     
    559569    /** 
    560570     * Execute the "FETCH (BODYSTRUCTURE)" command 
    561      * @param index Index of the message 
     571     * @param uid Unique ID of the message 
    562572     * @return Body structure tree 
    563573     */ 
    564     public ImapParser.MessageSection executeFetchBodystructure(int index) throws IOException, MailException { 
    565         if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
    566             EventLogger.logEvent( 
    567             AppInfo.GUID, 
    568             ("ImapProtocol.executeFetchBodyStructure("+index+")").getBytes(), 
    569             EventLogger.DEBUG_INFO); 
    570         } 
    571          
    572         String[] rawList = execute("FETCH", index + " (BODYSTRUCTURE)"); 
     574    public ImapParser.MessageSection executeFetchBodystructure(int uid) throws IOException, MailException { 
     575        if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
     576            EventLogger.logEvent( 
     577            AppInfo.GUID, 
     578            ("ImapProtocol.executeFetchBodyStructure("+uid+")").getBytes(), 
     579            EventLogger.DEBUG_INFO); 
     580        } 
     581         
     582        String[] rawList = execute("UID FETCH", uid + " (BODYSTRUCTURE)"); 
    573583 
    574584        // Pre-process the returned text to clean up mid-field line breaks 
     
    607617    /** 
    608618     * Execute the "FETCH (BODY)" command 
    609      * @param index Index of the message 
     619     * @param uid Unique ID of the message 
    610620     * @param address Address of the body section (i.e. "1", "1.2") 
    611621     * @return Body text as a string 
    612622     */ 
    613     public String executeFetchBody(int index, String address) throws IOException, MailException { 
    614         if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
    615             EventLogger.logEvent( 
    616             AppInfo.GUID, 
    617             ("ImapProtocol.executeFetchBody("+index+", \""+address+"\")").getBytes(), 
    618             EventLogger.DEBUG_INFO); 
    619         } 
    620  
    621         String[] rawList = execute("FETCH", index + " (BODY["+ address +"])"); 
     623    public String executeFetchBody(int uid, String address) throws IOException, MailException { 
     624        if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
     625            EventLogger.logEvent( 
     626            AppInfo.GUID, 
     627            ("ImapProtocol.executeFetchBody("+uid+", \""+address+"\")").getBytes(), 
     628            EventLogger.DEBUG_INFO); 
     629        } 
     630 
     631        String[] rawList = execute("UID FETCH", uid + " (BODY["+ address +"])"); 
    622632 
    623633        if(rawList.length <= 1) { 
     
    636646    /** 
    637647     * Execute the "STORE" command to update message flags. 
    638      * @param index The message index to modify. 
     648     * @param uid The message unique ID to modify. 
    639649     * @param addOrRemove True to add flags, false to remove them. 
    640650     * @param flags Array of flags to change.  (i.e. "\Seen", "\Answered") 
    641651     * @return Updated standard message flags, or null if there was a parse error. 
    642652     */ 
    643     public MessageFlags executeStore(int index, boolean addOrRemove, String[] flags) throws IOException, MailException { 
     653    public MessageFlags executeStore(int uid, boolean addOrRemove, String[] flags) throws IOException, MailException { 
    644654        if(EventLogger.getMinimumLevel() >= EventLogger.DEBUG_INFO) { 
    645655            StringBuffer buf = new StringBuffer(); 
     
    654664            EventLogger.logEvent( 
    655665            AppInfo.GUID, 
    656             ("ImapProtocol.executeStore("+index+", "+(addOrRemove?"add":"remove")+", {"+buf.toString()+"})").getBytes(), 
     666            ("ImapProtocol.executeStore("+uid+", "+(addOrRemove?"add":"remove")+", {"+buf.toString()+"})").getBytes(), 
    657667            EventLogger.DEBUG_INFO); 
    658668        } 
    659669 
    660670        StringBuffer buf = new StringBuffer(); 
    661         buf.append(index); 
     671        buf.append(uid); 
    662672        buf.append(' '); 
    663673        buf.append(addOrRemove?'+':'-'); 
     
    670680        } 
    671681        buf.append(')'); 
    672         String[] rawList = execute("STORE", buf.toString()); 
     682        String[] rawList = execute("UID STORE", buf.toString()); 
    673683        if(rawList.length < 1) { 
    674684            throw new MailException("Unable to set message flags"); 
     
    676686         
    677687        try { 
    678             int p = rawList[0].indexOf(' '); 
    679             int q = rawList[0].indexOf(' ', p+1); 
    680             int fetchIndex = Integer.parseInt(rawList[0].substring(p+1, q)); 
    681             if(fetchIndex != index) { 
    682                 return null; 
     688            int p = rawList[0].indexOf("UID"); 
     689            int q = rawList[0].lastIndexOf(')'); 
     690            if(p != -1 && q != -1 && p < q) { 
     691                int fetchIndex = Integer.parseInt(rawList[0].substring(p+4, q)); 
     692                if(fetchIndex != uid) { 
     693                    return null; 
     694                } 
    683695            } 
    684696            p = rawList[0].indexOf("FLAGS ("); 
    685             q = rawList[0].indexOf("))"); 
     697            q = rawList[0].indexOf(") UID"); 
    686698            if(p == -1 || q == -1) { 
    687699                return null; 
     
    702714     * Execute the "APPEND" command to add a message to an existing mailbox. 
    703715     * @param mboxName Mailbox name. 
    704      * @param rawMessage The raw message text, in RFC2822-compliant format. 
     716     * @param rawMessage The raw message text, in RFC2822-complaint format. 
    705717     * @param flags Flags to store the message with. 
    706718     */