1 28 29 package com.caucho.web.webmail; 30 31 import com.caucho.vfs.Path; 32 import com.caucho.vfs.ReadStream; 33 import com.caucho.vfs.WriteStream; 34 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.Map ; 39 40 public class MboxArchive { 41 private Path path; 42 43 private ArrayList threads; 44 private ArrayList messages; 45 46 public MboxArchive(Path path) 47 throws IOException 48 { 49 this.path = path; 50 } 51 52 public ArrayList getThreads() 53 { 54 return threads; 55 } 56 57 public ArrayList getMessages() 58 { 59 return messages; 60 } 61 62 public void analyzeMessages() 63 throws IOException 64 { 65 threads = new ArrayList (); 66 messages = new ArrayList (); 67 68 int count = 0; 69 HashMap map = new HashMap (); 70 HashMap subjectMap = new HashMap (); 71 72 ReadStream rawIs = path.openRead(); 73 MboxStream mboxStream = new MboxStream(rawIs); 74 ReadStream message; 75 76 while ((message = mboxStream.openRead()) != null) { 77 MboxMessage msg = new MboxMessage(count++); 78 79 String id = (String ) message.getAttribute("Message-Id"); 80 81 id = normalizeMessageId(id); 82 83 msg.setMessageId(id); 84 85 msg.setFrom((String ) message.getAttribute("From")); 86 String subject = (String ) message.getAttribute("Subject"); 87 msg.setSubject(subject); 88 89 messages.add(msg); 90 91 String inReplyTo = (String ) message.getAttribute("In-Reply-To"); 92 93 inReplyTo = normalizeMessageId(inReplyTo); 94 95 MboxMessage parent = null; 96 if (inReplyTo != null) 97 parent = (MboxMessage) map.get(inReplyTo); 98 99 if (parent == null) 100 parent = getParentBySubject(subjectMap, subject); 101 102 if (parent != null) 103 msg.setParent(parent); 104 else 105 threads.add(msg); 106 107 map.put(id, msg); 108 setParentBySubject(subjectMap, subject, msg); 109 } 110 111 rawIs.close(); 112 } 113 114 private void generateThread(WriteStream os, MboxMessage threadHeader) 115 { 116 117 } 118 119 private String normalizeMessageId(String id) 120 { 121 if (id == null) 122 return id; 123 124 int start = id.indexOf(id, '<'); 125 if (start < 0) 126 return id; 127 128 int end = id.indexOf(id, '>'); 129 if (end < start) 130 return id; 131 132 return id.substring(start, end); 133 } 134 135 private MboxMessage getParentBySubject(Map subjectMap, String subject) 136 { 137 if (subject == null || subject.equals("")) 138 return null; 139 140 MboxMessage message = (MboxMessage) subjectMap.get(subject); 141 if (message != null) 142 return message; 143 144 while (subject.length() > 4) { 145 String prefix = subject.substring(0, 4).toLowerCase(); 146 147 if (! prefix.equals("re: ") && ! prefix.equals("aw: ")) 148 return null; 149 150 subject = subject.substring(4); 151 152 message = (MboxMessage) subjectMap.get(subject); 153 if (message != null) 154 return message; 155 } 156 157 return null; 158 } 159 160 private void setParentBySubject(Map subjectMap, String subject, 161 MboxMessage message) 162 { 163 if (subject == null || subject.equals("")) 164 return; 165 166 while (subject.length() > 4) { 167 String prefix = subject.substring(0, 4).toLowerCase(); 168 169 if (! prefix.equals("re: ") && ! prefix.equals("aw: ")) { 170 subjectMap.put(subject, message); 171 return; 172 } 173 174 subject = subject.substring(4); 175 } 176 177 if (subject == null || subject.equals("")) 178 return; 179 180 subjectMap.put(subject, message); 181 } 182 } 183 | Popular Tags |