support RejectException
[org.ibex.mail.git] / src / org / ibex / mail / MailingList.java
index 027f2b0..ea0e248 100644 (file)
@@ -9,142 +9,171 @@ import org.ibex.mail.target.*;
 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 extends SkaringaFile {
+public class MailingList implements Target, Iterable<MailingList.Subscriber> {
 
-    public static MailingList getMailingList(String path) { return getMailingList(new File(path)); }
-    public static MailingList getMailingList(File path) {
-        if (!path.exists()) path.mkdirs();
-        File f = new File(path.getAbsolutePath() + File.separatorChar + ".mailinglist");
-        try {
-            if (!f.exists()) {
-                MailingList ret = new MailingList(path);
-                Subscriber s = new Subscriber();
-                s.address = Address.parse("adam@megacz.com");
-                ret.subscribers.put(s.address, s);
-                /*
-                Subscriber s = new Subscriber();
-                s.address = Address.parse("david@zentus.com");
-                ret.subscribers.put(s.address, s);
-                Subscriber s = new Subscriber();
-                s.address = Address.parse("brian@alliet.com");
-                ret.subscribers.put(s.address, s);
-                */
-                ret.write(f);
-                return ret;
+    // DO NOT move this below the stuff underneath it
+    private MailingList(File path) throws IOException {
+        this.path = path;
+        archive = FileBasedMailbox.getFileBasedMailbox(path.getCanonicalPath(), true);
+        properties = new PropertiesFile(new File(path.getCanonicalPath() + File.separatorChar + "properties"));
+        address = new Address(properties.get("address"));
+        homepage = properties.get("homepage");
+        one_line_description = properties.get("description");
+        message_footer = properties.get("footer");
+    }
+
+    public void banner(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        String basename = request.getServletPath();
+        PrintWriter pw = new PrintWriter(response.getWriter());
+        pw.println("<html>");
+        pw.println("  <body>");
+        String confirm = request.getParameter("confirm");
+        if (confirm != null) {
+            Subscribe sub = (Subscribe)Confirmation.decode(confirm, Long.parseLong(properties.get("secret")), new Date());
+            String email = sub.email;
+            synchronized(this) {
+                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);
+                    new FileOutputStream(path).close();
+                }
+            }
+            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);
             } else {
-                return (MailingList)SkaringaFile.read(f);
+                pw.println("    <form action='"+basename+"' method=post name=form1>");
+                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("       </select>");
+                pw.println("    </form>");
             }
-        } catch (Exception e) {
-            Log.error(MailingList.class, e);
-            return null;
         }
+        pw.println("  </body>");
+        pw.println("</html>");
+        pw.flush();
+        pw.close();
     }
 
-    private transient File path;
-    public  transient Mailbox   archive;
-
-    private MailingList() { }
-    private MailingList(File path) {
-        this.path = path;
-        archive = FileBasedMailbox.getFileBasedMailbox(path.getAbsolutePath(), true); }
-
-    public static enum UserType         { Administrator, Moderator, Member }
-    public static enum SubscriptionType { All, None, Digest, MimeDigest }
-    public static enum Visibility       { Members, Public, Nobody }
-    public static enum Action           { Accept, Hold, Reject }
-
-    public  Address   address;
-    private long      secret;
+    public class Subscribe extends Confirmation {
+        public String email;
+        public String basename;
+        public boolean un;
+        public String adj;
+        public Subscribe(String email, String basename, boolean un) {
+            super(new Address(email), new Date().getTime() + (1000 * 60 * 60 * 24));
+            this.email = email;
+            this.basename = basename;
+            this.un = un;
+            this.adj = un ? "unsubscribe" : "subscribe";
+        }
+        public String getDescription() { return adj + " " + email + " to " + properties.get("address"); }
+        public String getURL(String tail) { return basename + "?frame=banner&confirm="+URLEncoder.encode(tail); }
+    }
 
-    public Hashtable    subscribers = new Hashtable();
-    public Filter[]     filters  = new Filter[0];
+    private final File             path;
+    private final FileBasedMailbox archive;
+    private final PropertiesFile   properties;
 
-    public String       homepage             = "";
-    public String       one_line_description = "";
-    public String       long_description     = "";
-    public String       message_footer       = "";
+    private final long             secret               = new Random().nextLong();
+    public  final Address          address;
+    public        String           homepage;
 
-    public Visibility   listVisibility       = Visibility.Nobody;
-    public Visibility   membershipVisibility = Visibility.Nobody;
-    public Visibility   archiveVisibility    = Visibility.Members;
-    public Action       defaultPostingType   = Action.Hold;
+    public        String           one_line_description;
+    public        String           message_footer;
 
-    public int          bounceThreshhold     = 10;
+    public        int              bounceThreshhold     = 10;
 
     public static class Subscriber {
+        public Subscriber(Address a) { this.address = a; }
         public  Address          address;
-        public  Action           posting;
-        public  UserType         type;
-        public  SubscriptionType subscription;
-        public  boolean          send_copy_of_own_post;
-        public  boolean          filter_duplicates_when_ccd;
     }
 
-    public static abstract class JSTarget extends org.ibex.js.JS.Obj implements Target { }
-    public transient JSTarget acceptor = new JSTarget() {
-            public void accept(Message m) throws IOException, MailException {
-                Headers head = new Headers(m.headers.getStream());
-                head.put("list-id", one_line_description + "<"+address+">");
-                
-                m = Message.newMessage(new Fountain.Concatenate(head, m.getBody()));
-                Log.warn(MailingList.class, "archiving list message " + m.subject);
-                archive.accept(m);
-                
-                for(java.util.Enumeration e = subscribers.elements(); e.hasMoreElements();) try {
-                    Subscriber s = (Subscriber)e.nextElement();
-                    Log.warn(MailingList.class, "  trying " + s.address);
-                    SMTP.Outgoing.accept(Message.newMessage(m, m.envelopeFrom, s.address));
-                    Log.warn("[list]", "successfully sent to " + s);
-                } catch (Exception e2) { Log.error("[list]", e2); }
+
+    // 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;
+                    }
+                }
+                return null;
             }
         };
-
-
-    // Transactions ///////////////////////////////////////////////////////////////////////////
-    /*
-    public static Transaction create(final Address address, final Mailbox archive) {
-        final long random = new Random().nextLong();
-        return new Transaction() { public void executeOn(Object all, Date now) {
-            ((Hashtable)all).put(address.toString(false), new MailingList(address, archive, random)); } };
     }
 
-    public static Transaction delete(final Address address) {
-        return new Transaction() { public void executeOn(Object o,Date now) {
-            ((Hashtable)o).remove(address.toString(false)); } }; }
-
-    public static Query all() { return new Query() { public Object query(Object o, Date now) {
-        Hashtable all = (Hashtable)o;
-        MailingList[] ret = new MailingList[all.size()];
-        java.util.Enumeration e = all.elements();
-        for(int i=0; i<ret.length; i++) ret[i] = (MailingList)e.nextElement();
+    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 {
+        if (!path.exists()) path.mkdirs();
+        MailingList ret = cache.get(path.getCanonicalPath());
+        if (ret==null) cache.put(path.getCanonicalPath(), ret = new MailingList(path));
         return ret;
-    } }; }
+    }
 
-    public Query subscribers() { return new Query() { public Object query(Object o, Date now) {
-        Hashtable all = (Hashtable)o;
-        String[] ret = new String[subscribers.size()];
-        java.util.Enumeration e = subscribers.keys();
-        for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
-        return ret;
-    } }; }
-
-    public static Query forAddress(final Address a) { return new Query() {
-            public Object query(Object o, Date now) {
-                return ((Hashtable)o).get(a.toString(false)); } }; }
-
-    public static class AlterSubscription extends Confirmation {
-        public transient SubscriptionType newType = SubscriptionType.All;
-        public String list;
-        protected AlterSubscription(Address who, long expiration, String list, SubscriptionType newType) {
-            super(who, expiration); this.newType = newType; this.list = list; }
-        public String getDescription() { return "change your subscription"; }
-        public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
+    // 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 Filter[]     filters  = new Filter[0];
     //public static class Filter {
     //    public class EmergencyModerationFilter { }
     //    public class MaximumLengthFilter { }
@@ -154,3 +183,18 @@ public class MailingList extends SkaringaFile {
     //    public class AnonymizeSender { public boolean uncorrelated; }
     //}
 }    
+
+    //public static enum UserType         { Administrator, Moderator, Member }
+    //public static enum SubscriptionType { All, None, Digest, MimeDigest }
+    //public static enum Visibility       { Members, Public, Nobody }
+    //public static enum Action           { Accept, Hold, Reject }
+    //public        Visibility       listVisibility       = Visibility.Nobody;
+    //public        Visibility       membershipVisibility = Visibility.Nobody;
+    //public        Visibility       archiveVisibility    = Visibility.Members;
+    //public        Action           defaultPostingType   = Action.Hold;
+
+        //public  Action           posting = Action.Accept;
+        //public  UserType         type = UserType.Member;
+        //public  SubscriptionType subscription = SubscriptionType.All;
+        //public  boolean          send_copy_of_own_post = false;
+        //public  boolean          filter_duplicates_when_ccd = true;