1 package org.columba.mail.parser; 17 18 import java.util.ArrayList ; 19 import java.util.Hashtable ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.logging.Logger ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 import org.columba.ristretto.io.CharSequenceSource; 27 import org.columba.ristretto.message.Address; 28 import org.columba.ristretto.message.BasicHeader; 29 import org.columba.ristretto.parser.HeaderParser; 30 import org.columba.ristretto.parser.ParserException; 31 32 33 34 public class MailUrlParser { 35 36 private static final Logger LOG = Logger 37 .getLogger("org.columba.mail.parser"); 38 39 private static final Pattern mailtoPattern = Pattern.compile("mailto:([^?]*)\\??(.*)", Pattern.CASE_INSENSITIVE); 40 private static final Pattern headerPattern = Pattern.compile("&?([^=]+)=([^&]+)"); 41 42 public static Map parse(String in) throws ParserException { 43 Hashtable result = new Hashtable (); 44 StringBuffer temp = new StringBuffer (); 45 46 Matcher matcher = mailtoPattern.matcher(in); 47 48 if( matcher.matches() ) { 49 if( matcher.group(1) != null ) { 50 temp.append("To: "); 51 temp.append(decodeHTML(matcher.group(1))); 52 temp.append("\r\n"); 53 } 54 55 if( matcher.group(2) != null) { 56 matcher = headerPattern.matcher(matcher.group(2)); 57 while( matcher.find()) { 58 String key = matcher.group(1).toLowerCase(); 59 60 if( key.equals("to") || key.equals("cc") || key.equals("bcc") || key.equals("subject")) { 61 temp.append(matcher.group(1)); 62 temp.append(": "); 63 temp.append(decodeHTML(matcher.group(2))); 64 temp.append("\r\n"); 65 } else if( key.equals("body")) { 66 result.put("body", decodeHTML(matcher.group(2))); 67 } else { 68 LOG.warning("Unsafe header in mailto-URL: " + matcher.group()); 69 } 70 } 71 } 72 73 BasicHeader header = new BasicHeader( HeaderParser.parse(new CharSequenceSource(temp))); 74 75 Address[] addresses = header.getTo(); 77 78 List addressList = new ArrayList (); 79 80 for(int i=0; i<addresses.length; i++ ) { 81 addressList.add(addresses[i].toString()); 82 } 83 result.put("to", addressList.toArray(new String [0])); 84 85 86 addresses = header.getCc(); 87 if( addresses.length > 0) { 88 addressList.clear(); 89 for(int i=0; i<addresses.length; i++ ) { 90 addressList.add(addresses[i].toString()); 91 } 92 result.put("cc", addressList.toArray(new String [0])); 93 } 94 95 addresses = header.getBcc(); 96 if( addresses.length > 0) { 97 addressList.clear(); 98 for(int i=0; i<addresses.length; i++ ) { 99 addressList.add(addresses[i].toString()); 100 } 101 result.put("bcc", addressList.toArray(new String [0])); 102 } 103 104 if( header.getSubject() != null) { 105 result.put("subject", header.getSubject()); 106 } 107 108 } 109 110 return result; 111 } 112 113 private static String decodeHTML(String in) { 114 StringBuffer result = new StringBuffer (in.length()); 115 int pos = 0; 116 int nextpos = in.indexOf('%', pos); 117 118 while( nextpos != -1 ) { 119 result.append(in.substring(pos, nextpos)); 120 result.append((char) Integer.parseInt(in.substring(nextpos+1,nextpos+3),16)); 121 122 pos = nextpos + 3; 123 nextpos = in.indexOf('%', pos); 124 } 125 126 result.append( in.substring(pos)); 127 128 return result.toString(); 129 } 130 131 } 132 133 | Popular Tags |