added preliminary mailing list support
[org.ibex.mail.git] / src / org / ibex / mail / MailingList.java
index f5fade4..027f2b0 100644 (file)
@@ -12,18 +12,52 @@ import java.io.*;
 import org.prevayler.*;
 import org.prevayler.Query;
 
-// FEATURE: umbrella structure to mailing lists
-public class MailingList extends Target implements Serializable {
+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 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;
-    public Mailbox      archive;
-    private final long  secret;
-    private MailingList(Address a, Mailbox ar, long s) { this.address=a; this.archive=ar; this.secret=s; }
+    public  Address   address;
+    private long      secret;
 
     public Hashtable    subscribers = new Hashtable();
     public Filter[]     filters  = new Filter[0];
@@ -40,23 +74,7 @@ public class MailingList extends Target implements Serializable {
 
     public int          bounceThreshhold     = 10;
 
-    public static MailingList getList(Object all, String listName) { return (MailingList)((Hashtable)all).get(listName); }
-    public static MailingList getList(final String listName) {
-        try {
-            return (MailingList)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
-        } catch (Exception e) {
-            Log.error(MailingList.class, e);
-            return null;
-        }
-    }
-
-    public synchronized Subscriber getSubscriber(Address subscriber) {
-        Subscriber s = (Subscriber)subscribers.get(subscriber.toString(false));
-        if (s == null) subscribers.put(subscriber, s = new Subscriber());
-        return s;
-    }
-
-    public static class Subscriber implements Serializable {
+    public static class Subscriber {
         public  Address          address;
         public  Action           posting;
         public  UserType         type;
@@ -64,94 +82,29 @@ public class MailingList extends Target implements Serializable {
         public  boolean          send_copy_of_own_post;
         public  boolean          filter_duplicates_when_ccd;
     }
-    
-    //public static class Filter {
-    //    public class EmergencyModerationFilter { }
-    //    public class MaximumLengthFilter { }
-    //    public class SpamFilter { }
-    //    public class MIMETypes { }
-    //    public class MungeReplyTo { }
-    //    public class AnonymizeSender { public boolean uncorrelated; }
-    //}
 
-
-    public void accept(Message m) throws IOException, MailException {
-        try {
-            m = Message.newMessage(new Fountain.StringFountain("List-Id: " + one_line_description + "<"+address+">\r\n" +
-                                       m.toString() +
-                                       "--\r\n" +
-                                       message_footer + "\r\n" +
-                                       "to unsubscribe, go to " + homepage + "\r\n"));
-        } catch (Exception e2) {
-            Log.error("[list]", e2);
-            throw new IOException(e2.toString());
-        }
-        Log.warn(MailingList.class, "got message " + m.subject);
-        archive.accept(m);
-        try {
-            String[] subscribers = (String[])p.execute(subscribers());
-            Log.warn("**", "length is " + subscribers.length);
-            for(int i=0; i<subscribers.length; i++) {
-                String s = subscribers[i];
-                try {
-                    Log.warn(MailingList.class, "  trying " + s);
-                    /* FIXME
-                    SMTP.Outgoing.accept(Message.newMessage(new Fountain.StringFountain(m.toString()),
-                                                            address, Address.parse(s)));
-                    */
+    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);
-                }
+                } catch (Exception e2) { Log.error("[list]", e2); }
             }
-        } catch (Exception e2) {
-            Log.error("[list]", e2);
-        }
-    }
+        };
 
 
     // Transactions ///////////////////////////////////////////////////////////////////////////
-
-
-    //////////////////////////////////////////////////////////////////////////////
-
-    public static final String ROOT   = System.getProperty("ibex.mail.list.root", Mailbox.STORAGE_ROOT+File.separatorChar+"lists");
-    public static Prevayler p;
-    static { try {
-        PrevaylerFactory pf = new PrevaylerFactory();
-        //pf.configureSnapshotManager(new org.prevayler.implementation.snapshot.XmlSnapshotManager(new Hashtable(), ROOT));
-        pf.configurePrevalenceBase(ROOT);
-        p = pf.create();
-    } catch (Exception e) { Log.error(MailingList.class, e); } }
-
-    public static Transaction subscribeNewUser(final Address user, final String list) {
-        return new Transaction() { public void executeOn(final Object o, final Date now) {        
-            try {
-                new AlterSubscription(user,
-                                      now.getTime() + 1000*60*60*24,
-                                      list,
-                                      SubscriptionType.All).signAndSend(getList(o, list).secret, now);
-            } catch (Exception e) {
-                Log.error(MailingList.class, e);
-            }
-        } }; }
-
     /*
-    static {
-        try {
-            if (getList("test") == null) {
-                Mailbox archive = FileBasedMailbox.getFileBasedMailbox("/var/org.ibex.mail/lists/test@testing.megacz.com", true);
-                p.execute(create(Address.parse("test@testing.megacz.com"), archive));
-                p.execute(new Transaction() { public void executeOn(Object all, Date now) {
-                    getList(all, "test@testing.megacz.com").getSubscriber(Address.parse("megacz@gmail.com")).subscription = SubscriptionType.All;
-                }});
-            }
-        } catch (Exception e) {
-            Log.error(List.class, e);
-        }
-    }
-    */
-
     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) {
@@ -190,5 +143,14 @@ public class MailingList extends Target implements Serializable {
         public String getDescription() { return "change your subscription"; }
         public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
     }
+    */
 
+    //public static class Filter {
+    //    public class EmergencyModerationFilter { }
+    //    public class MaximumLengthFilter { }
+    //    public class SpamFilter { }
+    //    public class MIMETypes { }
+    //    public class MungeReplyTo { }
+    //    public class AnonymizeSender { public boolean uncorrelated; }
+    //}
 }