more fixups to MailingList
[org.ibex.mail.git] / src / org / ibex / mail / MailingList.java
index 027f2b0..636fa4e 100644 (file)
@@ -12,67 +12,29 @@ import java.io.*;
 import org.prevayler.*;
 import org.prevayler.Query;
 
-public class MailingList extends SkaringaFile {
-
-    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;
-            } else {
-                return (MailingList)SkaringaFile.read(f);
-            }
-        } catch (Exception e) {
-            Log.error(MailingList.class, e);
-            return null;
-        }
-    }
-
-    private transient File path;
-    public  transient Mailbox   archive;
-
-    private MailingList() { }
-    private MailingList(File path) {
-        this.path = path;
-        archive = FileBasedMailbox.getFileBasedMailbox(path.getAbsolutePath(), true); }
+public class MailingList extends Persistent implements Target {
 
     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        Address     address;
+    private final long        secret               = new Random().nextLong();
 
-    public Hashtable    subscribers = new Hashtable();
-    public Filter[]     filters  = new Filter[0];
+    public        Hashtable   subscribers          = new Hashtable();
 
-    public String       homepage             = "";
-    public String       one_line_description = "";
-    public String       long_description     = "";
-    public String       message_footer       = "";
+    public        String      homepage             = "";
+    public        String      one_line_description = "";
+    public        String      long_description     = "";
+    public        String      message_footer       = "";
 
-    public Visibility   listVisibility       = Visibility.Nobody;
-    public Visibility   membershipVisibility = Visibility.Nobody;
-    public Visibility   archiveVisibility    = Visibility.Members;
-    public Action       defaultPostingType   = Action.Hold;
+    public        Visibility  listVisibility       = Visibility.Nobody;
+    public        Visibility  membershipVisibility = Visibility.Nobody;
+    public        Visibility  archiveVisibility    = Visibility.Members;
+    public        Action      defaultPostingType   = Action.Hold;
 
-    public int          bounceThreshhold     = 10;
+    public        int         bounceThreshhold     = 10;
 
     public static class Subscriber {
         public  Address          address;
@@ -83,68 +45,48 @@ public class MailingList extends SkaringaFile {
         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); }
-            }
-        };
-
-
-    // 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)); } };
+
+    // Pooling //////////////////////////////////////////////////////////////////////////////
+
+    private MailingList(File path) { super(path); }
+    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()) return (MailingList)Persistent.read(f);
+            MailingList ret = new MailingList(path);
+            ret.write();
+            return ret;
+        } catch (Exception e) {
+            Log.error(MailingList.class, e);
+            return null;
+        }
     }
 
-    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();
-        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() { return FileBasedMailbox.getFileBasedMailbox(path, true); }
+
+    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+">");
+        
+        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(java.util.Enumeration e = subscribers.elements(); e.hasMoreElements();) try {
+            Subscriber s = (Subscriber)e.nextElement();
+            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 { }