001: /*
002: * Copyright (c) 1998-2000 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.util.CharBuffer;
032: import com.caucho.util.IntMap;
033: import com.caucho.vfs.Path;
034: import com.caucho.vfs.ReadStream;
035: import com.caucho.vfs.Vfs;
036: import com.caucho.vfs.WriteStream;
037:
038: import javax.servlet.GenericServlet;
039: import javax.servlet.ServletException;
040: import javax.servlet.ServletRequest;
041: import javax.servlet.ServletResponse;
042: import java.io.IOException;
043: import java.io.PrintWriter;
044:
045: public class WebmailServlet extends GenericServlet {
046: private boolean parseMail(ReadStream is, Path dst)
047: throws IOException {
048: CharBuffer line = CharBuffer.allocate();
049: String topId = null;
050: int count = 1;
051: WriteStream ws = null;
052: IntMap messages = new IntMap();
053:
054: try {
055: while (true) {
056: do {
057: line.clear();
058: if (!is.readLine(line)) {
059: if (ws != null)
060: ws.println("</message>");
061: return false;
062: }
063: if (ws != null && !line.startsWith("From ")) {
064: for (int i = 0; i < line.length(); i++) {
065: char ch = line.charAt(i);
066: if (ch == '<')
067: ws.print("<");
068: else
069: ws.print(ch);
070: }
071: ws.println();
072: }
073: } while (!line.startsWith("From "));
074:
075: if (ws != null) {
076: ws.println("</message>");
077: ws.close();
078: ws = null;
079: }
080:
081: String date = null;
082: String subject = null;
083: String from = null;
084: String id = null;
085: String references = null;
086:
087: do {
088: line.clear();
089: if (!is.readLine(line))
090: return false;
091: if (line.length() == 0)
092: break;
093:
094: String lower = line.toString().toLowerCase();
095:
096: if (lower.startsWith("subject: ")) {
097: subject = line.substring("subject: ".length())
098: .trim();
099:
100: if (subject.toLowerCase().startsWith("re:"))
101: subject = subject.substring(3).trim();
102: } else if (lower.startsWith("from: ")) {
103: from = line.substring("from: ".length());
104: } else if (lower.startsWith("date: ")) {
105: date = line.substring("from: ".length());
106: }
107: } while (line.length() > 0);
108:
109: int index = messages.get(subject);
110:
111: if (index >= 0) {
112: ws = dst.lookup("" + index + ".xtp").openAppend();
113: } else {
114: if (subject != null && !subject.equals(""))
115: messages.put(subject, count);
116:
117: ws = dst.lookup("" + count++ + ".xtp").openWrite();
118: ws.println("<title>" + subject + "</title>");
119: }
120: ws.println("<em>" + from + "</em>");
121: ws.println("<date>" + date + "</date>");
122: ws.println("<message>");
123: }
124: } finally {
125: if (ws != null)
126: ws.close();
127: }
128: }
129:
130: public void service(ServletRequest request, ServletResponse response)
131: throws ServletException, IOException {
132: PrintWriter pw = response.getWriter();
133:
134: Path path = Vfs
135: .lookup("/home/ferg/majordomo/archive/resin-interest.0006");
136: Path dst = Vfs.lookup("/tmp/dst");
137: dst.mkdirs();
138:
139: ReadStream is = path.openRead();
140: try {
141: parseMail(is, dst);
142: } finally {
143: is.close();
144: }
145: pw.println("done");
146: }
147: }
|