comment out BODYSTRUCTURE
[org.ibex.mail.git] / src / org / ibex / mail / MailingList.java
index ea0e248..54fed95 100644 (file)
@@ -10,17 +10,20 @@ import org.ibex.mail.protocol.*;
 import java.util.*;
 import java.io.*;
 import java.net.*;
-import org.prevayler.*;
-import org.prevayler.Query;
 import javax.servlet.*;
 import javax.servlet.http.*;
 
-public class MailingList implements Target, Iterable<MailingList.Subscriber> {
+// TODO: RFC 2369
+// TODO: RFC 2919
 
-    // DO NOT move this below the stuff underneath it
-    private MailingList(File path) throws IOException {
+// FEATURE: store interesting/important stuff in sqlite
+public class MailingList extends Mailbox.MailboxWrapper {
+
+    // FIXME
+    public MailingList(File path, FileBasedMailbox fbm) throws IOException {
+        super(fbm);
         this.path = path;
-        archive = FileBasedMailbox.getFileBasedMailbox(path.getCanonicalPath(), true);
+        this.mailbox = fbm;
         properties = new PropertiesFile(new File(path.getCanonicalPath() + File.separatorChar + "properties"));
         address = new Address(properties.get("address"));
         homepage = properties.get("homepage");
@@ -29,16 +32,27 @@ public class MailingList implements Target, Iterable<MailingList.Subscriber> {
     }
 
     public void banner(HttpServletRequest request, HttpServletResponse response) throws IOException {
-        String basename = request.getServletPath();
+        String basename = request.getRequestURL()+"";
         PrintWriter pw = new PrintWriter(response.getWriter());
+        /*
         pw.println("<html>");
+        pw.println("  <head>");
+        pw.println("   <style>");
+        pw.println("     TH, TD, P, LI, BODY {");
+        pw.println("         font-family: helvetica, verdana, arial, sans-serif;");
+        pw.println("         font-size: 12px;  ");
+        pw.println("         text-decoration:none; ");
+        pw.println("     }");
+        pw.println("   </style>");
+        pw.println("  </head>");
         pw.println("  <body>");
+        */
         String confirm = request.getParameter("confirm");
         if (confirm != null) {
-            Subscribe sub = (Subscribe)Confirmation.decode(confirm, Long.parseLong(properties.get("secret")), new Date());
+            Subscribe sub = (Subscribe)Confirmation.decode(confirm, secret, new Date());
             String email = sub.email;
             synchronized(this) {
-                String path = this.path.getAbsolutePath() + File.separatorChar + "subscribers" + File.separatorChar + sub.email;
+                String path = this.path.getAbsolutePath() + File.separatorChar + "subscribers" + File.separatorChar+sub.email;
                 if (sub.un) new File(path).delete();
                 else {
                     Log.warn(null, "creating " + path);
@@ -47,30 +61,42 @@ public class MailingList implements Target, Iterable<MailingList.Subscriber> {
             }
             pw.println("    <b>successfully "+sub.adj+"d " + email + " to " + properties.get("address"));
         } else {
-            pw.println("    <b>"+properties.get("address")+"</b><br>");
-            pw.println("    <tt><a href="+properties.get("nntp")+">"+properties.get("nntp")+"</a></tt><br>");
             String action = request.getParameter("action");
             String email = request.getParameter("email");
             if (action != null) {
                 Subscribe sub = new Subscribe(email, request.getRequestURL().toString(), action.equals("unsubscribe"));
-                sub.signAndSend(new Address(properties.get("owner")), Long.parseLong(properties.get("secret")), new Date());
-                pw.println("a confirmation email has been sent to " + email + "; click the enclosed link to confirm your request to " + action);
+                sub.signAndSend(new Address(properties.get("owner")),
+                                secret, new Date());
+                pw.println("a confirmation email has been sent to " + email +
+                          "; click the enclosed link to confirm your request to " + action);
             } else {
-                pw.println("    <form action='"+basename+"' method=post name=form1>");
+                pw.println("<table width=100% border=0 cellpadding=0 cellspacing=0><tr><td>");
+                pw.println("    <b>"+properties.get("address")+"</b><br> ");
+                pw.println("    <i>"+properties.get("description")+"</i> ");
+                pw.println("</td><td align=right>");
+                pw.println("<i>access via:</i> ");
+                if (path.getAbsolutePath().startsWith("/afs/"))
+                    pw.println("<a target=_top href='file:" + path.getAbsolutePath() +"'>[AFS]</a> ");
+                pw.println("<a target=_top href="+properties.get("nntp")+">[NNTP]</a></tt>");
+                pw.println("<a target=_top href='mailto:"+properties.get("address")+"'>[SMTP]</a></tt>");
+                pw.println("<a target=_top href='"+request.getRequestURL()+"'>[HTTP]</a></tt>");
+                pw.println("</td></tr><tr><td colspan=2 align=right>");
+                pw.println("    <form action='"+basename+"' method=post name=form1 target=_top>");
                 pw.println("       <input type=text width=100 value='your@email.com' name=email>");
                 pw.println("       <input type=hidden name=frame value=banner>");
                 pw.println("       <select name=action onchange='form1.submit()'>");
                 pw.println("          <option>--choose action--</option>");
                 pw.println("          <option>subscribe</option>");
                 pw.println("          <option>unsubscribe</option>");
+                pw.println("          <option>preferences</option>");
                 pw.println("       </select>");
                 pw.println("    </form>");
+                pw.println("</td></tr></table>");
             }
         }
-        pw.println("  </body>");
-        pw.println("</html>");
+        //pw.println("  </body>");
+        //pw.println("</html>");
         pw.flush();
-        pw.close();
     }
 
     public class Subscribe extends Confirmation {
@@ -90,8 +116,8 @@ public class MailingList implements Target, Iterable<MailingList.Subscriber> {
     }
 
     private final File             path;
-    private final FileBasedMailbox archive;
     private final PropertiesFile   properties;
+    private final FileBasedMailbox mailbox;
 
     private final long             secret               = new Random().nextLong();
     public  final Address          address;
@@ -110,38 +136,41 @@ public class MailingList implements Target, Iterable<MailingList.Subscriber> {
 
     // Pooling //////////////////////////////////////////////////////////////////////////////
 
-    
-    public Iterator<Subscriber> iterator() {
-        final File subdir = new File(path.getAbsolutePath() + File.separatorChar + "subscribers");
-        if (!subdir.exists()) subdir.mkdirs();
-        final String[] subs = !subdir.isDirectory() ? new String[0] : subdir.list();
-        return new Iterator<Subscriber>() {
-            int i=0;
-            Subscriber prep = null;
-            public void remove() {
-                try {
-                    new File(subdir.getAbsolutePath() + File.separatorChar + subs[i++]).delete();
-                } catch (Exception e) {
-                    Log.error(MailingList.class, e);
-                }
-            }
-            public boolean hasNext() { if (prep==null) prep = next(); return prep!=null; }
-            public Subscriber next() {
-                if (prep!=null) { Subscriber ret = prep; prep = null; return ret; }
-                while(i<subs.length) {
-                    if (subs[i].indexOf('@')==-1) i++;
-                    else try {
-                        return new Subscriber(new Address(subs[i++]));
-                    } catch (Exception e) {
-                        Log.warn(MailingList.class, e);
-                        continue;
+    public Iterable<Subscriber> subscribers() {
+        return new Iterable<Subscriber>() {
+            public java.util.Iterator<Subscriber> iterator() {
+                final File subdir = new File(path.getAbsolutePath() + File.separatorChar + "subscribers");
+                if (!subdir.exists()) subdir.mkdirs();
+                final String[] subs = !subdir.isDirectory() ? new String[0] : subdir.list();
+                return new java.util.Iterator<Subscriber>() {
+                    int i=0;
+                    Subscriber prep = null;
+                    public void remove() {
+                        try {
+                            new File(subdir.getAbsolutePath() + File.separatorChar + subs[i++]).delete();
+                        } catch (Exception e) {
+                            Log.error(MailingList.class, e);
+                        }
                     }
-                }
-                return null;
+                    public boolean hasNext() { if (prep==null) prep = next(); return prep!=null; }
+                    public Subscriber next() {
+                        if (prep!=null) { Subscriber ret = prep; prep = null; return ret; }
+                        while(i<subs.length) {
+                            if (subs[i].indexOf('@')==-1) i++;
+                            else try {
+                                    return new Subscriber(new Address(subs[i++]));
+                                } catch (Exception e) {
+                                    Log.warn(MailingList.class, e);
+                                    continue;
+                                }
+                        }
+                        return null;
+                    }
+                };
             }
         };
     }
-
+    /*
     private static HashMap<String,MailingList> cache = new HashMap<String,MailingList>();
     public static MailingList getMailingList(String path) throws IOException { return getMailingList(new File(path)); }
     public static MailingList getMailingList(File path) throws IOException {
@@ -150,27 +179,37 @@ public class MailingList implements Target, Iterable<MailingList.Subscriber> {
         if (ret==null) cache.put(path.getCanonicalPath(), ret = new MailingList(path));
         return ret;
     }
-
+    */
     // Methods //////////////////////////////////////////////////////////////////////////////
 
-    public Mailbox getArchive() throws IOException { return archive; }
-
-    public void accept(Message m) throws IOException, MailException {
-        StringBuffer buf = new StringBuffer();
-        m.getBody().getStream().transcribe(buf);
-        Headers head = new Headers(m.headers.getStream());
-        head.put("List-Id", one_line_description + "<"+address+">");
-        head.put("Subject", properties.get("prefix") + " " + head.get("Subject"));
-        
-        m = Message.newMessage(new Fountain.StringFountain(head.getString()+"\r\n"+buf.toString()));
-        Log.warn(MailingList.class, "archiving list message " + m.subject);
-        getArchive().accept(m);
-
-        for(Subscriber s : this) try {
-            Log.warn(MailingList.class, "  trying " + s.address);
-            SMTP.accept(Message.newMessage(m, m.envelopeFrom, s.address));
-            Log.warn("[list]", "successfully sent to " + s);
-        } catch (Exception e2) { Log.error("[list]", e2); }
+    public void add(Message message) {
+        try {
+            accept(message);
+        } catch (Exception e) { throw new RuntimeException(e); }
+    }
+    public void add(Message message, int flags) { add(message); /* FIXME: flags? */ }
+    public void accept(Message m) throws MailException {
+        try {
+            StringBuffer buf = new StringBuffer();
+            m.getBody().getStream().transcribe(buf);
+            Headers head = new Headers(m.headers,
+                                       new String[] {
+                                           "List-Id", one_line_description + "<"+address+">",
+                                           "Subject", properties.get("prefix") + " " + m.headers.get("Subject")
+                                       });
+            
+            m = Message.newMessage(Fountain.Util.concat(new Fountain[] { head, 
+                                                                         Fountain.Util.create("\r\n"),
+                                                                         Fountain.Util.create(buf.toString()) }));
+            Log.warn(MailingList.class, "archiving list message " + m.subject);
+            mailbox.accept(m);
+            
+            for(Subscriber s : subscribers()) try {
+                    Log.warn(MailingList.class, "  trying " + s.address);
+                    SMTP.enqueue(m.withEnvelope(m.envelopeFrom, s.address));
+                    Log.warn("[list]", "successfully sent to " + s);
+                } catch (Exception e2) { Log.error("[list]", e2); }
+        } catch (Exception e) { throw new RuntimeException(e); }
     }
 
     //public Filter[]     filters  = new Filter[0];