1 28 29 package com.caucho.web.webmail; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.util.IntMap; 33 import com.caucho.vfs.Path; 34 import com.caucho.vfs.ReadStream; 35 import com.caucho.vfs.Vfs; 36 import com.caucho.vfs.WriteStream; 37 38 import javax.servlet.GenericServlet ; 39 import javax.servlet.ServletException ; 40 import javax.servlet.ServletRequest ; 41 import javax.servlet.ServletResponse ; 42 import java.io.IOException ; 43 import java.io.PrintWriter ; 44 45 public class WebmailServlet extends GenericServlet { 46 private boolean parseMail(ReadStream is, Path dst) 47 throws IOException 48 { 49 CharBuffer line = CharBuffer.allocate(); 50 String topId = null; 51 int count = 1; 52 WriteStream ws = null; 53 IntMap messages = new IntMap(); 54 55 try { 56 while (true) { 57 do { 58 line.clear(); 59 if (! is.readLine(line)) { 60 if (ws != null) 61 ws.println("</message>"); 62 return false; 63 } 64 if (ws != null && ! line.startsWith("From ")) { 65 for (int i = 0; i < line.length(); i++) { 66 char ch = line.charAt(i); 67 if (ch == '<') 68 ws.print("<"); 69 else 70 ws.print(ch); 71 } 72 ws.println(); 73 } 74 } while (! line.startsWith("From ")); 75 76 if (ws != null) { 77 ws.println("</message>"); 78 ws.close(); 79 ws = null; 80 } 81 82 83 String date = null; 84 String subject = null; 85 String from = null; 86 String id = null; 87 String references = null; 88 89 do { 90 line.clear(); 91 if (! is.readLine(line)) 92 return false; 93 if (line.length() == 0) 94 break; 95 96 String lower = line.toString().toLowerCase(); 97 98 if (lower.startsWith("subject: ")) { 99 subject = line.substring("subject: ".length()).trim(); 100 101 if (subject.toLowerCase().startsWith("re:")) 102 subject = subject.substring(3).trim(); 103 } 104 else if (lower.startsWith("from: ")) { 105 from = line.substring("from: ".length()); 106 } 107 else if (lower.startsWith("date: ")) { 108 date = line.substring("from: ".length()); 109 } 110 } while (line.length() > 0); 111 112 int index = messages.get(subject); 113 114 if (index >= 0) { 115 ws = dst.lookup("" + index + ".xtp").openAppend(); 116 } 117 else { 118 if (subject != null && ! subject.equals("")) 119 messages.put(subject, count); 120 121 ws = dst.lookup("" + count++ + ".xtp").openWrite(); 122 ws.println("<title>" + subject + "</title>"); 123 } 124 ws.println("<em>" + from + "</em>"); 125 ws.println("<date>" + date + "</date>"); 126 ws.println("<message>"); 127 } 128 } finally { 129 if (ws != null) 130 ws.close(); 131 } 132 } 133 134 public void service(ServletRequest request, ServletResponse response) 135 throws ServletException , IOException 136 { 137 PrintWriter pw = response.getWriter(); 138 139 Path path = Vfs.lookup("/home/ferg/majordomo/archive/resin-interest.0006"); 140 Path dst = Vfs.lookup("/tmp/dst"); 141 dst.mkdirs(); 142 143 ReadStream is = path.openRead(); 144 try { 145 parseMail(is, dst); 146 } finally { 147 is.close(); 148 } 149 pw.println("done"); 150 } 151 } 152 | Popular Tags |