1 21 22 27 28 package com.sun.mail.imap.protocol; 29 30 import java.util.Vector ; 31 import java.util.Date ; 32 import java.io.UnsupportedEncodingException ; 33 import java.text.ParseException ; 34 import javax.mail.internet.InternetAddress ; 35 import javax.mail.internet.AddressException ; 36 import javax.mail.internet.MailDateFormat ; 37 import javax.mail.internet.MimeUtility ; 38 import com.sun.mail.iap.*; 39 40 46 47 public class ENVELOPE implements Item { 48 49 public static char[] name = {'E','N','V','E','L','O','P','E'}; 51 public int msgno; 52 53 public Date date = null; 54 public String subject; 55 public InternetAddress [] from; 56 public InternetAddress [] sender; 57 public InternetAddress [] replyTo; 58 public InternetAddress [] to; 59 public InternetAddress [] cc; 60 public InternetAddress [] bcc; 61 public String inReplyTo; 62 public String messageId; 63 64 private static MailDateFormat mailDateFormat = new MailDateFormat (); 66 67 public ENVELOPE(FetchResponse r) throws ParsingException { 68 msgno = r.getNumber(); 69 70 r.skipSpaces(); 71 72 if (r.readByte() != '(') 73 throw new ParsingException("ENVELOPE parse error"); 74 75 String s = r.readString(); 76 if (s != null) { 77 try { 78 date = mailDateFormat.parse(s); 79 } catch (Exception pex) { 80 } 85 } 86 87 subject = r.readString(); 88 from = parseAddressList(r); 89 sender = parseAddressList(r); 90 replyTo = parseAddressList(r); 91 to = parseAddressList(r); 92 cc = parseAddressList(r); 93 bcc = parseAddressList(r); 94 inReplyTo = r.readString(); 95 messageId = r.readString(); 96 97 if (r.readByte() != ')') 98 throw new ParsingException("ENVELOPE parse error"); 99 } 100 101 private InternetAddress [] parseAddressList(Response r) 102 throws ParsingException { 103 r.skipSpaces(); 105 byte b = r.readByte(); 106 if (b == '(') { 107 Vector v = new Vector (); 108 109 do { 110 IMAPAddress a = new IMAPAddress(r); 111 if (!a.isEndOfGroup()) 113 v.addElement(a); 114 } while (r.peekByte() != ')'); 115 116 r.skip(1); 118 119 InternetAddress [] a = new InternetAddress [v.size()]; 120 v.copyInto(a); 121 return a; 122 } else if (b == 'N' || b == 'n') { r.skip(2); return null; 125 } else 126 throw new ParsingException("ADDRESS parse error"); 127 } 128 } 129 130 class IMAPAddress extends InternetAddress { 131 private boolean group = false; 132 private InternetAddress [] grouplist; 133 private String groupname; 134 135 private static final long serialVersionUID = -3835822029483122232L; 136 137 IMAPAddress(Response r) throws ParsingException { 138 r.skipSpaces(); 140 if (r.readByte() != '(') 141 throw new ParsingException("ADDRESS parse error"); 142 143 encodedPersonal = r.readString(); 144 145 r.readString(); String mb = r.readString(); 147 String host = r.readString(); 148 if (r.readByte() != ')') throw new ParsingException("ADDRESS parse error"); 150 151 if (host == null) { 152 group = true; 154 groupname = mb; 155 if (groupname == null) return; 157 StringBuffer sb = new StringBuffer (); 161 sb.append(groupname).append(':'); 162 Vector v = new Vector (); 163 while (r.peekByte() != ')') { 164 IMAPAddress a = new IMAPAddress(r); 165 if (a.isEndOfGroup()) break; 167 if (v.size() != 0) sb.append(','); 169 sb.append(a.toString()); 170 v.addElement(a); 171 } 172 sb.append(';'); 173 address = sb.toString(); 174 grouplist = new IMAPAddress[v.size()]; 175 v.copyInto(grouplist); 176 } else { 177 if (mb == null || mb.length() == 0) 178 address = host; 179 else if (host == null || host.length() == 0) 180 address = mb; 181 else 182 address = mb + "@" + host; 183 } 184 185 } 186 187 boolean isEndOfGroup() { 188 return group && groupname == null; 189 } 190 191 public boolean isGroup() { 192 return group; 193 } 194 195 public InternetAddress [] getGroup(boolean strict) throws AddressException { 196 if (grouplist == null) 197 return null; 198 return (InternetAddress [])grouplist.clone(); 199 } 200 } 201 | Popular Tags |