001: /*
002: * Copyright (c) 1998-2001 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.web.webmail;
030:
031: import com.caucho.vfs.Path;
032: import com.caucho.vfs.ReadStream;
033: import com.caucho.vfs.WriteStream;
034:
035: import java.io.IOException;
036: import java.util.ArrayList;
037: import java.util.HashMap;
038: import java.util.Map;
039:
040: public class MboxArchive {
041: private Path path;
042:
043: private ArrayList threads;
044: private ArrayList messages;
045:
046: public MboxArchive(Path path) throws IOException {
047: this .path = path;
048: }
049:
050: public ArrayList getThreads() {
051: return threads;
052: }
053:
054: public ArrayList getMessages() {
055: return messages;
056: }
057:
058: public void analyzeMessages() throws IOException {
059: threads = new ArrayList();
060: messages = new ArrayList();
061:
062: int count = 0;
063: HashMap map = new HashMap();
064: HashMap subjectMap = new HashMap();
065:
066: ReadStream rawIs = path.openRead();
067: MboxStream mboxStream = new MboxStream(rawIs);
068: ReadStream message;
069:
070: while ((message = mboxStream.openRead()) != null) {
071: MboxMessage msg = new MboxMessage(count++);
072:
073: String id = (String) message.getAttribute("Message-Id");
074:
075: id = normalizeMessageId(id);
076:
077: msg.setMessageId(id);
078:
079: msg.setFrom((String) message.getAttribute("From"));
080: String subject = (String) message.getAttribute("Subject");
081: msg.setSubject(subject);
082:
083: messages.add(msg);
084:
085: String inReplyTo = (String) message
086: .getAttribute("In-Reply-To");
087:
088: inReplyTo = normalizeMessageId(inReplyTo);
089:
090: MboxMessage parent = null;
091: if (inReplyTo != null)
092: parent = (MboxMessage) map.get(inReplyTo);
093:
094: if (parent == null)
095: parent = getParentBySubject(subjectMap, subject);
096:
097: if (parent != null)
098: msg.setParent(parent);
099: else
100: threads.add(msg);
101:
102: map.put(id, msg);
103: setParentBySubject(subjectMap, subject, msg);
104: }
105:
106: rawIs.close();
107: }
108:
109: private void generateThread(WriteStream os, MboxMessage threadHeader) {
110:
111: }
112:
113: private String normalizeMessageId(String id) {
114: if (id == null)
115: return id;
116:
117: int start = id.indexOf(id, '<');
118: if (start < 0)
119: return id;
120:
121: int end = id.indexOf(id, '>');
122: if (end < start)
123: return id;
124:
125: return id.substring(start, end);
126: }
127:
128: private MboxMessage getParentBySubject(Map subjectMap,
129: String subject) {
130: if (subject == null || subject.equals(""))
131: return null;
132:
133: MboxMessage message = (MboxMessage) subjectMap.get(subject);
134: if (message != null)
135: return message;
136:
137: while (subject.length() > 4) {
138: String prefix = subject.substring(0, 4).toLowerCase();
139:
140: if (!prefix.equals("re: ") && !prefix.equals("aw: "))
141: return null;
142:
143: subject = subject.substring(4);
144:
145: message = (MboxMessage) subjectMap.get(subject);
146: if (message != null)
147: return message;
148: }
149:
150: return null;
151: }
152:
153: private void setParentBySubject(Map subjectMap, String subject,
154: MboxMessage message) {
155: if (subject == null || subject.equals(""))
156: return;
157:
158: while (subject.length() > 4) {
159: String prefix = subject.substring(0, 4).toLowerCase();
160:
161: if (!prefix.equals("re: ") && !prefix.equals("aw: ")) {
162: subjectMap.put(subject, message);
163: return;
164: }
165:
166: subject = subject.substring(4);
167: }
168:
169: if (subject == null || subject.equals(""))
170: return;
171:
172: subjectMap.put(subject, message);
173: }
174: }
|