added a servlet for webmail
authoradam <adam@megacz.com>
Fri, 18 Mar 2005 09:09:58 +0000 (09:09 +0000)
committeradam <adam@megacz.com>
Fri, 18 Mar 2005 09:09:58 +0000 (09:09 +0000)
darcs-hash:20050318090958-5007d-15db562ddd7eaa45e4a5597104e98079cadcb30d.gz

src/org/ibex/mail/target/FileBasedMailbox.java

index 428319f..e3b9afd 100644 (file)
@@ -13,6 +13,8 @@ import java.nio.channels.*;
 import java.net.*;
 import java.util.*;
 import java.text.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
 
 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
 public class FileBasedMailbox extends Mailbox.Default {
@@ -142,4 +144,131 @@ public class FileBasedMailbox extends Mailbox.Default {
             } finally { if (fis != null) try { fis.close(); } catch (Exception e) { /* DELIBERATE */ } }
         }
     }
+
+    public static class Servlet extends HttpServlet {
+        public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {doGet(request, response);}
+        private void frames(HttpServletRequest request, HttpServletResponse response, boolean top) throws IOException {
+            String basename = request.getServletPath();
+            PrintWriter pw = new PrintWriter(response.getWriter());
+            pw.println("<html>");
+            if (top) {
+                pw.println("  <frameset rows='30%,*'>");
+                pw.println("    <frame src='"+basename+"?frame=top' marginwidth=0 marginheight=0 name=top/>");
+                pw.println("    <frame src='"+basename+"?frame=bottom' marginwidth=0 marginheight=0 name='bottom'/>");
+            } else {
+                pw.println("  <frameset cols='150,*'>");
+                pw.println("    <frame src='"+basename+"?frame=topleft' marginwidth=0 marginheight=0 name=topleft/>");
+                pw.println("    <frame src='"+basename+"?frame=topright' marginwidth=0 marginheight=0 name=topright/>");
+            }
+            pw.println("  </frameset>");
+            pw.println("</html>");
+            pw.flush();
+        }
+
+        public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+            String frame = request.getParameter("frame");
+            String basename = request.getServletPath();
+
+            if (frame == null) { frames(request, response, true); return; }
+            if (frame.equals("top")) { frames(request, response, false); return; }
+            if (frame.equals("topleft")) { return; }
+
+            if (request.getServletPath().indexOf("..") != -1) throw new IOException(".. not allowed in image paths");
+            ServletContext cx = getServletContext();
+            String path = cx.getRealPath(request.getServletPath());
+            Mailbox mbox = FileBasedMailbox.getFileBasedMailbox(path, false);
+            if (mbox == null) throw new IOException("no such mailbox: " + path);
+
+            Vec msgs = new Vec();
+            for(Mailbox.Iterator it = mbox.iterator(); it.next();) {
+                String[] s = new String[4];
+                Message m = it.cur();
+                s[0] = (m.from==null?"":m.from.toString(true));
+                s[1] = m.subject;
+                s[2] = (m.date + "").trim().replaceAll(" ","&nbsp;");
+                s[3] = it.num() + "";
+                msgs.addElement(s);
+            }
+            String[][] messages;
+            msgs.copyInto(messages = new String[msgs.size()][]);
+
+            if ("bottom".equals(frame)) { bottom(request, response, messages, mbox); return; }
+            if ("topright".equals(frame)) { topright(request, response, messages); return; }
+        }
+
+        private void bottom(HttpServletRequest request, HttpServletResponse response, String[][] messages, Mailbox mbox)
+            throws IOException {
+            PrintWriter pw = new PrintWriter(response.getWriter());
+            pw.println("<html>");
+            pw.println("  <body>");
+            pw.println("    <pre>");
+            if (request.getParameter("msgnum") != null) {
+                int target = Integer.parseInt(request.getParameter("msgnum"));
+                for(Mailbox.Iterator it = mbox.iterator(); it.next();) {
+                    if (it.num() == target) {
+                        StringBuffer tgt = new StringBuffer();
+                        it.cur().getBody().getStream().transcribe(tgt);
+                        pw.println(tgt.toString());
+                        break;
+                    }
+                }
+            }
+            pw.println("    </pre>");
+            pw.println("  </body>");
+            pw.println("</html>");
+        }
+        
+        private void topright(HttpServletRequest request, HttpServletResponse response, String[][] messages) throws IOException {
+            PrintWriter pw = new PrintWriter(response.getWriter());
+            String basename = request.getServletPath();
+            pw.println("<html>");
+            pw.println("  <head>");
+            pw.println("    <style>");
+            pw.println("      a:link    { color: #000; text-decoration: none; }");
+            pw.println("      a:active  { color: #f00; text-decoration: none; }");
+            pw.println("      a:visited { color: #777; text-decoration: none; }");
+            pw.println("      a:hover   { color: #000; text-decoration: none; }");
+            pw.println("      /* a:hover   { color: #00f; text-decoration: none; border-bottom: 1px dotted; } */");
+            pw.println("    </style>");
+            pw.println("  </head>");
+            pw.println("  <body onKeyPress='doKey(event.keyCode)'>");
+            pw.println("    <script>");
+            pw.println("      var chosen = null;");
+            pw.println("      var all = [];");
+            pw.println("      function doKey(x) {");
+            pw.println("          if (chosen == null) { choose(all[0]); return; }");
+            pw.println("          switch(x) {");
+            pw.println("              case 112: if (chosen.id > 0) choose(all[chosen.id-1]); break;");
+            pw.println("              case 110: if (chosen.id < (all.length-1)) choose(all[1+(1*chosen.id)]); break;");
+            pw.println("          }");
+            pw.println("      }");
+            pw.println("      function choose(who) {");
+            pw.println("          who.style.background = '#ffc';");
+            pw.println("          if (chosen != null) chosen.style.background = '#aaa';");
+            pw.println("          top.bottom.location='"+basename+"?frame=bottom&msgnum='+who.id;");
+            pw.println("          chosen = who;");
+            pw.println("      }");
+            pw.println("    </script>");
+            pw.println("    <table width=100% border=0 cellpadding=0 cellspacing=0>");
+            boolean odd=false;
+            for(int i=0; i<messages.length; i++) {
+                odd = !odd;
+                pw.println("      <tr style='background: "+(odd?"#e8eef7":"white")+"' id='"+i+"' "+
+                           "onmouseover='this.style.color=\"blue\"' "+
+                           "onmouseout='this.style.color=\"black\"' "+
+                           "onclick='choose(this);'>");
+                String[] m = messages[i];
+                pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[0]+"</td>");
+                pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[1]+"</td>");
+                pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[2]+"</td>");
+                pw.println("</tr>");
+                pw.println("<script> all["+i+"] = document.getElementById('"+i+"'); </script>");
+                pw.println("<tr height=1 bgcolor=black><td colspan=3>");
+                pw.println("    <img src=http://www.xwt.org/images/clearpixel.gif></td></tr>");
+            }
+            pw.println("    </table>");
+            pw.println("  </body>");
+            pw.println("</html>");
+        }
+    }
 }