Changeset 275

Show
Ignore:
Timestamp:
08/19/08 08:34:23 PM (3 months ago)
Author:
octorian
Message:
 
Location:
trunk/LogicMail/src/org/logicprobe/LogicMail
Files:
10 modified

Legend:

Unmodified
Added
Removed
  • trunk/LogicMail/src/org/logicprobe/LogicMail/conf/GlobalConfig.java

    r237 r275  
    149149    
    150150    public boolean getHideDeletedMsg() { 
    151         return hideDeletedMsg; 
     151        return hideDeletedMsg; 
    152152    } 
    153153 
    154154    public void setHideDeletedMsg(boolean hideDeletedMsg) { 
    155         this.hideDeletedMsg = hideDeletedMsg; 
     155        this.hideDeletedMsg = hideDeletedMsg; 
    156156    } 
    157157      
  • trunk/LogicMail/src/org/logicprobe/LogicMail/mail/imap/ImapParser.java

    r262 r275  
    88 * 
    99 * 1. Redistributions of source code must retain the above copyright 
    10  *    notice, this list of conditions and the following disclaimer.  
     10 *    notice, this list of conditions and the following disclaimer. 
    1111 * 2. Redistributions in binary form must reproduce the above copyright 
    1212 *    notice, this list of conditions and the following disclaimer in the 
    13  *    documentation and/or other materials provided with the distribution.  
     13 *    documentation and/or other materials provided with the distribution. 
    1414 * 3. Neither the name of the project nor the names of its 
    1515 *    contributors may be used to endorse or promote products derived 
     
    2929 * OF THE POSSIBILITY OF SUCH DAMAGE. 
    3030 */ 
    31  
    3231package org.logicprobe.LogicMail.mail.imap; 
    3332 
    34 import java.io.UnsupportedEncodingException; 
    35 import java.util.Calendar; 
    36 import java.util.Vector; 
    3733import net.rim.device.api.system.EventLogger; 
     34 
    3835import org.logicprobe.LogicMail.AppInfo; 
    3936import org.logicprobe.LogicMail.message.MessageEnvelope; 
    4037import org.logicprobe.LogicMail.util.StringParser; 
     38 
     39import java.io.UnsupportedEncodingException; 
     40 
     41import java.util.Calendar; 
     42import java.util.Vector; 
     43 
    4144 
    4245/** 
     
    4548 */ 
    4649class ImapParser { 
    47     /** 
    48      * Simple container for a parsed message structure tree 
    49      */ 
    50     public static class MessageSection { 
    51         public String address; 
    52         public String type; 
    53         public String subtype; 
    54         public String encoding; 
    55         public String charset; 
    56         public int size; 
    57         public MessageSection[] subsections; 
    58     } 
    59      
    60     private ImapParser() { } 
    61      
    6250    private static String strNIL = "NIL"; 
    63      
     51    private static final String MODIFIED_BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; 
     52 
     53    private ImapParser() { 
     54    } 
     55 
    6456    static ImapProtocol.MessageFlags parseMessageFlags(Vector flagsVec) { 
    6557        ImapProtocol.MessageFlags flags = new ImapProtocol.MessageFlags(); 
    66          
     58 
    6759        String text; 
    6860        int size = flagsVec.size(); 
    69         for(int i = 0; i < size; i++) { 
    70             if(flagsVec.elementAt(i) instanceof String) { 
    71                 text = (String)flagsVec.elementAt(i); 
    72                 if(text.equalsIgnoreCase("\\Seen")) { 
     61 
     62        for (int i = 0; i < size; i++) { 
     63            if (flagsVec.elementAt(i) instanceof String) { 
     64                text = (String) flagsVec.elementAt(i); 
     65 
     66                if (text.equalsIgnoreCase("\\Seen")) { 
    7367                    flags.seen = true; 
    74                 } 
    75                 else if(text.equalsIgnoreCase("\\Answered")) { 
     68                } else if (text.equalsIgnoreCase("\\Answered")) { 
    7669                    flags.answered = true; 
    77                 } 
    78                 else if(text.equalsIgnoreCase("\\Flagged")) { 
     70                } else if (text.equalsIgnoreCase("\\Flagged")) { 
    7971                    flags.flagged = true; 
    80                 } 
    81                 else if(text.equalsIgnoreCase("\\Deleted")) { 
     72                } else if (text.equalsIgnoreCase("\\Deleted")) { 
    8273                    flags.deleted = true; 
    83                 } 
    84                 else if(text.equalsIgnoreCase("\\Draft")) { 
     74                } else if (text.equalsIgnoreCase("\\Draft")) { 
    8575                    flags.draft = true; 
    86                 } 
    87                 else if(text.equalsIgnoreCase("\\Recent")) { 
     76                } else if (text.equalsIgnoreCase("\\Recent")) { 
    8877                    flags.recent = true; 
    89                 } 
    90                 else if(text.equalsIgnoreCase("Junk") || text.equalsIgnoreCase("$Junk")) { 
     78                } else if (text.equalsIgnoreCase("Junk") || 
     79                        text.equalsIgnoreCase("$Junk")) { 
    9180                    flags.junk = true; 
    9281                } 
    9382            } 
    9483        } 
     84 
    9585        return flags; 
    9686    } 
     
    9888    static String createMessageFlagsString(ImapProtocol.MessageFlags flags) { 
    9989        StringBuffer buf = new StringBuffer(); 
    100         if(flags.seen) { 
     90 
     91        if (flags.seen) { 
    10192            buf.append("\\Seen"); 
    10293        } 
    103         if(flags.answered) { 
    104             if(buf.length() > 0) { buf.append(' '); } 
     94 
     95        if (flags.answered) { 
     96            if (buf.length() > 0) { 
     97                buf.append(' '); 
     98            } 
     99 
    105100            buf.append("\\Answered"); 
    106101        } 
    107         if(flags.flagged) { 
    108             if(buf.length() > 0) { buf.append(' '); } 
     102 
     103        if (flags.flagged) { 
     104            if (buf.length() > 0) { 
     105                buf.append(' '); 
     106            } 
     107 
    109108            buf.append("\\Flagged"); 
    110109        } 
    111         if(flags.deleted) { 
    112             if(buf.length() > 0) { buf.append(' '); } 
     110 
     111        if (flags.deleted) { 
     112            if (buf.length() > 0) { 
     113                buf.append(' '); 
     114            } 
     115 
    113116            buf.append("\\Deleted"); 
    114117        } 
    115         if(flags.draft) { 
    116             if(buf.length() > 0) { buf.append(' '); } 
     118 
     119        if (flags.draft) { 
     120            if (buf.length() > 0) { 
     121                buf.append(' '); 
     122            } 
     123 
    117124            buf.append("\\Draft"); 
    118125        } 
    119         if(flags.recent) { 
    120             if(buf.length() > 0) { buf.append(' '); } 
     126 
     127        if (flags.recent) { 
     128            if (buf.length() > 0) { 
     129                buf.append(' '); 
     130            } 
     131 
    121132            buf.append("\\Recent"); 
    122133        } 
     134 
    123135        return buf.toString(); 
    124136    } 
    125      
     137 
    126138    static MessageEnvelope parseMessageEnvelope(Vector parsedEnv) { 
    127139        // Sanity checking 
    128         if(parsedEnv.size() < 10) { 
    129            EventLogger.logEvent( 
    130                AppInfo.GUID, 
    131                "ImapParser.parseMessageEnvelope: Sanity check failed".getBytes(), 
    132                EventLogger.WARNING); 
    133            return generateDummyEnvelope(); 
    134         } 
    135              
     140        if (parsedEnv.size() < 10) { 
     141            EventLogger.logEvent(AppInfo.GUID, 
     142                "ImapParser.parseMessageEnvelope: Sanity check failed".getBytes(), 
     143                EventLogger.WARNING); 
     144 
     145            return generateDummyEnvelope(); 
     146        } 
     147 
    136148        MessageEnvelope env = new MessageEnvelope(); 
    137149 
    138         if(parsedEnv.elementAt(0) instanceof String) { 
     150        if (parsedEnv.elementAt(0) instanceof String) { 
    139151            try { 
    140                 env.date = StringParser.parseDateString((String)parsedEnv.elementAt(0)); 
     152                env.date = StringParser.parseDateString((String) parsedEnv.elementAt( 
     153                            0)); 
    141154            } catch (Exception e) { 
    142155                env.date = Calendar.getInstance().getTime(); 
    143156            } 
    144157        } 
    145          
    146         if(parsedEnv.elementAt(1) instanceof String) { 
    147             env.subject = StringParser.parseEncodedHeader((String)parsedEnv.elementAt(1)); 
    148         } 
    149  
    150         if(parsedEnv.elementAt(2) instanceof Vector) { 
    151             env.from = parseAddressList((Vector)parsedEnv.elementAt(2)); 
    152         } 
    153          
    154         if(parsedEnv.elementAt(3) instanceof Vector) { 
    155             env.sender = parseAddressList((Vector)parsedEnv.elementAt(3)); 
    156         } 
    157  
    158         if(parsedEnv.elementAt(4) instanceof Vector) { 
    159             env.replyTo = parseAddressList((Vector)parsedEnv.elementAt(4)); 
    160         } 
    161  
    162         if(parsedEnv.elementAt(5) instanceof Vector) { 
    163             env.to = parseAddressList((Vector)parsedEnv.elementAt(5)); 
    164         } 
    165  
    166         if(parsedEnv.elementAt(6) instanceof Vector) { 
    167             env.cc = parseAddressList((Vector)parsedEnv.elementAt(6)); 
    168         } 
    169  
    170         if(parsedEnv.elementAt(7) instanceof Vector) { 
    171             env.bcc = parseAddressList((Vector)parsedEnv.elementAt(7)); 
    172         } 
    173  
    174         if(parsedEnv.elementAt(8) instanceof String) { 
    175             env.inReplyTo = (String)parsedEnv.elementAt(8); 
    176             if(env.inReplyTo.equals(strNIL)) { 
     158 
     159        if (parsedEnv.elementAt(1) instanceof String) { 
     160            env.subject = StringParser.parseEncodedHeader((String) parsedEnv.elementAt( 
     161                        1)); 
     162        } 
     163 
     164        if (parsedEnv.elementAt(2) instanceof Vector) { 
     165            env.from = parseAddressList((Vector) parsedEnv.elementAt(2)); 
     166        } 
     167 
     168        if (parsedEnv.elementAt(3) instanceof Vector) { 
     169            env.sender = parseAddressList((Vector) parsedEnv.elementAt(3)); 
     170        } 
     171 
     172        if (parsedEnv.elementAt(4) instanceof Vector) { 
     173            env.replyTo = parseAddressList((Vector) parsedEnv.elementAt(4)); 
     174        } 
     175 
     176        if (parsedEnv.elementAt(5) instanceof Vector) { 
     177            env.to = parseAddressList((Vector) parsedEnv.elementAt(5)); 
     178        } 
     179 
     180        if (parsedEnv.elementAt(6) instanceof Vector) { 
     181            env.cc = parseAddressList((Vector) parsedEnv.elementAt(6)); 
     182        } 
     183 
     184        if (parsedEnv.elementAt(7) instanceof Vector) { 
     185            env.bcc = parseAddressList((Vector) parsedEnv.elementAt(7)); 
     186        } 
     187 
     188        if (parsedEnv.elementAt(8) instanceof String) { 
     189            env.inReplyTo = (String) parsedEnv.elementAt(8); 
     190 
     191            if (env.inReplyTo.equals(strNIL)) { 
    177192                env.inReplyTo = ""; 
    178193            } 
    179194        } 
    180195 
    181         if(parsedEnv.elementAt(9) instanceof String) { 
    182             env.messageId = (String)parsedEnv.elementAt(9); 
    183             if(env.messageId.equals(strNIL)) { 
     196        if (parsedEnv.elementAt(9) instanceof String) { 
     197            env.messageId = (String) parsedEnv.elementAt(9); 
     198 
     199            if (env.messageId.equals(strNIL)) { 
    184200                env.messageId = ""; 
    185201            } 
    186202        } 
     203 
    187204        return env; 
    188205    } 
     
    192209        String[] addrList = new String[addrVec.size()]; 
    193210        int index = 0; 
    194          
    195         for(int i=0;i<addrVec.size();i++) { 
    196             if((addrVec.elementAt(i) instanceof Vector) && 
    197                ((Vector)addrVec.elementAt(i)).size() >= 4) { 
    198                  
    199                 Vector entry = (Vector)addrVec.elementAt(i); 
     211 
     212        for (int i = 0; i < addrVec.size(); i++) { 
     213            if ((addrVec.elementAt(i) instanceof Vector) && 
     214                    (((Vector) addrVec.elementAt(i)).size() >= 4)) { 
     215                Vector entry = (Vector) addrVec.elementAt(i); 
    200216 
    201217                String realName = strNIL; 
    202                 if(entry.elementAt(0) instanceof String) { 
    203                     realName = StringParser.parseEncodedHeader((String)entry.elementAt(0)); 
     218 
     219                if (entry.elementAt(0) instanceof String) { 
     220                    realName = StringParser.parseEncodedHeader((String) entry.elementAt( 
     221                                0)); 
    204222                } 
    205223 
    206224                String mbName = strNIL; 
    207                 if(entry.elementAt(2) instanceof String) { 
    208                     mbName = (String)entry.elementAt(2); 
     225 
     226                if (entry.elementAt(2) instanceof String) { 
     227                    mbName = (String) entry.elementAt(2); 
    209228                } 
    210229 
    211230                String hostName = strNIL; 
    212                 if(entry.elementAt(3) instanceof String) { 
    213                     hostName = (String)entry.elementAt(3); 
    214                 } 
     231 
     232                if (entry.elementAt(3) instanceof String) { 
     233                    hostName = (String) entry.elementAt(3); 
     234                } 
     235 
     236                String addrStr = (mbName.equals(strNIL) ? "" : mbName) + 
     237                    (hostName.equals(strNIL) ? "" : ('@' + hostName)); 
     238 
    215239                // Now assemble these into a single address entry 
    216240                // (possibly eventually storing them separately) 
    217                 if(realName.length() > 0 && !realName.equals(strNIL)) { 
    218                     addrList[index] = realName + " <" + mbName + "@" + hostName + ">"; 
    219                 } 
    220                 else { 
    221                     addrList[index] = mbName + "@" + hostName; 
    222                 } 
     241                if ((realName.length() > 0) && !realName.equals(strNIL)) { 
     242                    addrList[index] = realName + " <" + addrStr + ">"; 
     243                } else { 
     244                    addrList[index] = addrStr; 
     245                } 
     246 
    223247                index++; 
    224248            } 
    225249        } 
     250 
    226251        return addrList; 
    227252    } 
    228      
     253 
    229254    static MessageEnvelope generateDummyEnvelope() { 
    230255        MessageEnvelope env = new MessageEnvelope(); 
     
    233258        env.from[0] = "<sender>"; 
    234259        env.subject = "<subject>"; 
     260 
    235261        return env; 
    236262    } 
     
    244270    static MessageSection parseMessageStructure(String rawText) { 
    245271        Vector parsedText = null; 
     272 
    246273        try { 
    247             parsedText = StringParser.nestedParenStringLexer(rawText.substring(rawText.indexOf('('))); 
     274            parsedText = StringParser.nestedParenStringLexer(rawText.substring( 
     275                        rawText.indexOf('('))); 
    248276        } catch (Exception exp) { 
    249             EventLogger.logEvent( 
    250                 AppInfo.GUID, 
     277            EventLogger.logEvent(AppInfo.GUID, 
    251278                ("ImapParser.parseMessageStructure: " + 
    252                 "Caught exception when parsing input:\r\n"+ 
    253                 exp.toString()).getBytes(), 
     279                "Caught exception when parsing input:\r\n" + exp.toString()).getBytes(), 
    254280                EventLogger.WARNING); 
     281 
    255282            return null; 
    256283        } 
    257284 
    258285        // Sanity checking 
    259         if(parsedText.size() < 4 || 
    260            !(parsedText.elementAt(3) instanceof Vector)) { 
    261            EventLogger.logEvent( 
    262                AppInfo.GUID, 
    263                "ImapParser.parseMessageStructure: Sanity check failed".getBytes(), 
    264                EventLogger.WARNING); 
    265            return null; 
    266         } 
    267          
    268         Vector parsedStruct = (Vector)parsedText.elementAt(3); 
    269         MessageSection msgStructure = parseMessageStructureHelper(null, 1, parsedStruct); 
     286        if ((parsedText.size() < 2) || 
     287                !(parsedText.elementAt(1) instanceof Vector)) { 
     288            EventLogger.logEvent(AppInfo.GUID, 
     289                "ImapParser.parseMessageStructure: Sanity check failed".getBytes(), 
     290                EventLogger.WARNING); 
     291 
     292            return null; 
     293        } 
     294 
     295        Vector parsedStruct = (Vector) parsedText.elementAt(1); 
     296        MessageSection msgStructure = parseMessageStructureHelper(null, 1, 
     297                parsedStruct); 
    270298        fixMessageStructure(msgStructure); 
     299 
    271300        return msgStructure; 
    272301    } 
     
    276305     */ 
    277306    private static void fixMessageStructure(MessageSection msgStructure) { 
    278         if(msgStructure == null) { 
     307        if (msgStructure == null) { 
    279308            return; 
    280309        } 
     310 
    281311        int p = msgStructure.address.indexOf('.'); 
    282         if(p != -1 && p+1 < msgStructure.address.length()) { 
    283             msgStructure.address = msgStructure.address.substring(p+1); 
    284         } 
    285          
    286         if(msgStructure.subsections != null && msgStructure.subsections.length > 0) { 
    287             for(int i=0;i<msgStructure.subsections.length;i++) { 
     312 
     313        if ((p != -1) && ((p + 1) < msgStructure.address.length())) { 
     314            msgStructure.address = msgStructure.address.substring(p + 1); 
     315        } 
     316 
     317        if ((msgStructure.subsections != null) && 
     318                (msgStructure.subsections.length > 0)) { 
     319            for (int i = 0; i < msgStructure.subsections.length; i++) { 
    288320                fixMessageStructure(msgStructure.subsections[i]); 
    289321            } 
    290322        } 
    291323    } 
    292      
    293     private static MessageSection parseMessageStructureHelper(String parentAddress, 
    294                                                               int index, 
    295                                                               Vector parsedStruct) { 
     324 
     325    private static MessageSection parseMessageStructureHelper( 
     326        String parentAddress, int index, Vector parsedStruct) { 
    296327        // Determine the address of this body part 
    297328        String address; 
    298         if(parentAddress == null) { 
     329 
     330        if (parentAddress == null) { 
    299331            address = Integer.toString(index); 
    300         } 
    301         else { 
     332        } else { 
    302333            address = parentAddress + "." + Integer.toString(index); 
    303334        } 
     335 
    304336        // Determine the number of body parts and parse 
    305         if(parsedStruct.elementAt(0) instanceof String) { 
     337        if (parsedStruct.elementAt(0) instanceof String) { 
    306338            // The first element is a string, so we hit a simple message part 
    307339            MessageSection section = parseMessageStructureSection(parsedStruct); 
    308340            section.address = address; 
     341 
    309342            return section; 
    310         } 
    311         else if(parsedStruct.elementAt(0) instanceof Vector) { 
     343        } else if (parsedStruct.elementAt(0) instanceof Vector) { 
    312344            // The first element is a vector, so we hit a multipart message part 
    313345            int size = parsedStruct.size(); 
    314             MessageSection[] subSections = new MessageSection[size-4]; 
    315             for(int i=0;i<size;++i) { 
     346            MessageSection[] subSections = new MessageSection[size - 4]; 
     347 
     348            for (int i = 0; i < size; ++i) { 
    316349                // Iterate through the message parts 
    317                 if(parsedStruct.elementAt(i) instanceof Vector) 
    318                     subSections[i] = parseMessageStructureHelper(address, i+1, (Vector)parsedStruct.elementAt(i)); 
    319                 else if(parsedStruct.elementAt(i) instanceof String) { 
     350                if (parsedStruct.elementAt(i) instanceof Vector) { 
     351                    subSections[i] = parseMessageStructureHelper(address, 
     352                            i + 1, (Vector) parsedStruct.elementAt(i)); 
     353                } else if (parsedStruct.elementAt(i) instanceof String) { 
    320354                    MessageSection section = new MessageSection(); 
    321355                    section.type = "multipart"; 
    322                     section.subtype = ((String)parsedStruct.elementAt(i)).toLowerCase(); 
     356                    section.subtype = ((String) parsedStruct.elementAt(i)).toLowerCase(); 
    323357                    section.subsections = subSections; 
    324358                    section.address = address; 
     359 
    325360                    return section; 
    326361                } 
    327362            } 
    328363        } 
     364 
    329365        return null; 
    330366    } 
    331      
    332     private static MessageSection parseMessageStructureSection(Vector sectionList) { 
     367 
     368    private static MessageSection parseMessageStructureSection( 
     369        Vector sectionList) { 
    333370        MessageSection sec = new MessageSection(); 
    334371        Vector tmpVec; 
    335          
    336         if(sectionList.elementAt(0) instanceof String) { 
    337             sec.type = ((String)sectionList.elementAt(0)).toLowerCase(); 
    338         } 
    339  
    340         if(sectionList.elementAt(1) instanceof String) { 
    341             sec.subtype = ((String)sectionList.elementAt(1)).toLowerCase(); 
     372 
     373        if (sectionList.elementAt(0) instanceof String) { 
     374            sec.type = ((String) sectionList.elementAt(0)).toLowerCase(); 
     375        } 
     376 
     377        if (sectionList.elementAt(1) instanceof String) { 
     378            sec.subtype = ((String) sectionList.elementAt(1)).toLowerCase(); 
    342379        } 
    343380 
    344381        sec.charset = null; 
    345         if(sectionList.elementAt(2) instanceof Vector) { 
    346             tmpVec = (Vector)sectionList.elementAt(2); 
    347             if(tmpVec.size() >= 2) { 
    348                 if((tmpVec.elementAt(0) instanceof String) && 
    349                    ((String)tmpVec.elementAt(0)).equalsIgnoreCase("charset") && 
    350                    tmpVec.elementAt(1) instanceof String) { 
    351                     sec.charset = (String)tmpVec.elementAt(1);                     
    352                 } 
    353             } 
    354         } 
    355          
    356         if(sectionList.elementAt(5) instanceof String) { 
    357             sec.encoding = ((String)sectionList.elementAt(5)).toLowerCase(); 
    358         } 
    359  
    360         if(sectionList.elementAt(6) instanceof String) { 
     382 
     383        if (sectionList.elementAt(2) instanceof Vector) { 
     384            tmpVec = (Vector) sectionList.elementAt(2); 
     385 
     386            if (tmpVec.size() >= 2) { 
     387                if ((tmpVec.elementAt(0) instanceof String) && 
     388                        ((String) tmpVec.elementAt(0)).equalsIgnoreCase( 
     389                            "charset") && 
     390                        tmpVec.elementAt(1) instanceof String) { 
     391                    sec.charset = (String) tmpVec.elementAt(1); 
     392                } 
     393            } 
     394        } 
     395 
     396        if (sectionList.elementAt(5) instanceof String) { 
     397            sec.encoding = ((String) sectionList.elementAt(5)).toLowerCase(); 
     398        } 
     399 
     400        if (sectionList.elementAt(6) instanceof String) { 
    361401            try { 
    362                 sec.size = Integer.parseInt((String)sectionList.elementAt(6)); 
     402                sec.size = Integer.parseInt((String) sectionList.elementAt(6)); 
    363403            } catch (Exception exp) { 
    364404                sec.size = -1; 
     
    368408        return sec; 
    369409    } 
    370      
     410 
    371411    /** 
    372412     * Takes in the raw IMAP folder name, and outputs a string that 
     
    383423        int len = rawText.length(); 
    384424        boolean usMode = true; 
    385         while(index < len) { 
     425 
     426        while (index < len) { 
    386427            char ch = rawText.charAt(index); 
    387             if(usMode) { 
    388                 if(ch != '&') { 
     428 
     429            if (usMode) { 
     430                if (ch != '&') { 
    389431                    buf.append(ch); 
    390432                    index++; 
    391                 } 
    392                 else if(ch == '&' && index < len - 1 && rawText.charAt(index+1) == '-') { 
     433                } else if ((ch == '&') && (index < (len - 1)) &&