added preliminary mailing list support
authoradam <adam@megacz.com>
Thu, 24 Mar 2005 11:14:24 +0000 (11:14 +0000)
committeradam <adam@megacz.com>
Thu, 24 Mar 2005 11:14:24 +0000 (11:14 +0000)
darcs-hash:20050324111424-5007d-b64d729fb4bb395017f1d8afaa31b1da2c26519a.gz

17 files changed:
src/org/ibex/mail/MailingList.java
src/org/ibex/mail/Message.java
src/org/ibex/mail/SkaringaFile.java [new file with mode: 0644]
src/org/ibex/mail/Target.java
src/org/ibex/mail/target/Bounce.java
src/org/ibex/mail/target/Darcs.java
src/org/ibex/mail/target/Drop.java
src/org/ibex/mail/target/LMTP.java
src/org/ibex/mail/target/Later.java
src/org/ibex/mail/target/List.java
src/org/ibex/mail/target/Mailbox.java
src/org/ibex/mail/target/Pipe.java
src/org/ibex/mail/target/Procmail.java
src/org/ibex/mail/target/Script.java
src/org/ibex/mail/target/Transcript.java
src/org/ibex/mail/target/Vacation.java
src/org/ibex/mail/target/XMLRPC.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; }
+    //}
 }    
index 7c5635f..0c74dbe 100644 (file)
@@ -52,14 +52,13 @@ public class Message extends MIME.Part {
         Stream stream = in.getStream();
         while(true) {
             String s = stream.readln();
-            if (s == null) break;
-            if (to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
-            if (s.toLowerCase().startsWith("return-path:")) continue;
-            if (s.length() == 0) {
+            if (s == null || s.length() == 0) {
                 if (to != null) sb.append("Envelope-To: " + to.toString(true) + "\r\n");
                 sb.append("\r\n");
                 break;
             }
+            if (to != null && s.toLowerCase().startsWith("envelope-to:")) continue;
+            if (s.toLowerCase().startsWith("return-path:")) continue;
             sb.append(s);
             sb.append("\r\n");
         }
@@ -142,6 +141,6 @@ public class Message extends MIME.Part {
     public String toString() { throw new RuntimeException("Message.toString() called"); }
     public String summary() { return "[" + envelopeFrom + " -> " + envelopeTo + "] " + subject; }
 
-    public static class Malformed extends Exception { public Malformed(String s) { super(s); } }
+    public static class Malformed extends MailException { public Malformed(String s) { super(s); } }
 }
 
diff --git a/src/org/ibex/mail/SkaringaFile.java b/src/org/ibex/mail/SkaringaFile.java
new file mode 100644 (file)
index 0000000..24c8630
--- /dev/null
@@ -0,0 +1,52 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+package org.ibex.mail;
+import org.ibex.util.*;
+import org.ibex.io.*;
+import org.ibex.mail.target.*;
+import org.ibex.mail.protocol.*;
+import java.util.*;
+import java.io.*;
+import com.skaringa.javaxml.*;
+import javax.xml.transform.stream.*;
+
+public class SkaringaFile {
+
+    //private transient File file;
+    //private transient String path;
+    //private transient boolean exists;
+    private static WeakHashMap cache = new WeakHashMap();
+
+    /*
+    public void delete() throws IOException {
+        exists = false;
+        cache.remove(path, this);
+        file.delete();
+    }
+    */
+
+    public static Object read(File file) throws Exception {
+        FileInputStream in = null;
+        try {
+            ObjectTransformer trans = ObjectTransformerFactory.getInstance().getImplementation();
+            in = new FileInputStream(file);
+            return trans.deserialize(new StreamSource(in));
+        } finally {
+            if (in!=null) in.close();
+        }
+    }
+
+    public void write(File file) throws Exception {
+        ObjectTransformer trans = ObjectTransformerFactory.getInstance().getImplementation();
+        trans.setProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
+        trans.setProperty("{http://xml.apache.org/xalan}indent-amount", "2");
+        FileOutputStream out = new FileOutputStream(file);
+        trans.serialize(this, new StreamResult(out));
+        out.close();
+        // FIXME: sync
+        // FIXME: write-aside
+    }
+
+}
index 7f89b70..952a46f 100644 (file)
@@ -8,7 +8,7 @@ import org.ibex.js.*;
 import org.ibex.mail.target.*;
 
 /** base class for mail message "destinations" */
-public class Target extends JS.Obj {
+public interface Target {
     public static final Target root = Script.root();
-    public void accept(Message m) throws IOException, MailException { throw new MailException("Target.accept() unimplemented"); }
+    public abstract void accept(Message m) throws IOException, MailException;
 }
index ad37d3f..c6af7f9 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** bounces a message */
-public class Bounce extends Target {
+public abstract class Bounce implements Target {
 }
index 7c96330..d827436 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** simple class to handle darcs patches */
-public class Darcs extends Pipe {
+public abstract class Darcs extends Pipe {
 }
index c17c64a..7663936 100644 (file)
@@ -9,7 +9,7 @@ import org.ibex.util.*;
 import org.ibex.mail.*;
 import org.ibex.mail.target.*;
 
-public class Drop extends Target {
+public class Drop extends JS.Obj implements Target {
     public static final Drop instance = new Drop();
     public void accept(Message m) throws IOException, MailException {
         Log.warn(this, "dropping message " + m.summary());
index 0312995..fa244a5 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** implementation of the Local Mail Transport Protocol */
-public class LMTP extends Target {
+public abstract class LMTP implements Target {
 }
index e41dc49..ee331ef 100644 (file)
@@ -9,7 +9,7 @@ import org.ibex.util.*;
 import org.ibex.mail.*;
 import org.ibex.mail.target.*;
 
-public class Later extends Target {
+public class Later extends JS.Obj implements Target {
     public static final Later instance = new Later();
     public static class LaterException extends RuntimeException { }
     public void accept(Message m) throws IOException, MailException {
index eafd9be..80abcee 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** A full-featured mailing list manager */
-public class List extends Target {
+public abstract class List implements Target {
 }
index ec122de..f5bf940 100644 (file)
@@ -13,7 +13,7 @@ import java.util.*;
 import java.text.*;
 
 /** abstract superclass for mailboxes, which store messages along with their flags */
-public abstract class Mailbox extends Target {
+public abstract class Mailbox extends JS.Obj implements Target {
 
     public JS get(JS key) throws JSExn {
         return slash(JSU.toString(key), true);
index 81ea8e0..f2f89f6 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** generic pipe-the-message-to-a-file target */
-public class Pipe extends Target {
+public abstract class Pipe implements Target {
 }
index e9e2d0a..891bcb4 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** callout to support legacy .procmailrc files */
-public class Procmail extends Pipe {
+public abstract class Procmail extends Pipe {
 }
index 6a7395a..b112b91 100644 (file)
@@ -11,7 +11,7 @@ import org.ibex.mail.target.*;
 import java.io.*;
 import java.util.*;
 
-public class Script extends Target {
+public class Script extends JS.Obj implements Target {
 
     private static final JS.Method METHOD = new JS.Method();
 
@@ -126,8 +126,8 @@ public class Script extends Target {
             case "mail.my.mailbox":
                 FileBasedMailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
                 return root.slash("user", true).slash("megacz", true).slash("newmail", true);
+            case "mail.list": return METHOD;
             //#end
-            if (JSU.toString(name).startsWith("list.")) { return MailingList.getList(JSU.toString(name).substring(5)); }
             return super.get(name);
         }
 
@@ -138,6 +138,7 @@ public class Script extends Target {
             final int nargs = args.length;
             String name = JSU.toString(name0);
             try {
+                if (name.equals("mail.list")) return MailingList.getMailingList(JS.toString(args[0])).acceptor;
                 if (name.equals("date")) { return new JSDate(args); }
                 if (name.equals("mail.send") || name.equals("send") || name.equals("mail.attempt") || name.equals("attempt")) {
                     boolean attempt = name.equals("mail.attempt") || name.equals("attempt");
@@ -179,7 +180,7 @@ public class Script extends Target {
                     return JSU.B(ok);
                 }
                 if (name.equals("mail.bounce")) {
-                    return new Target() {
+                    return new JSTarget() {
                             public void accept(Message m) throws MailException {
                                 try {
                                     Message m2 = m.bounce(JSU.toString(a));
@@ -258,4 +259,6 @@ public class Script extends Target {
                 }
             };
     }
+
+    private static abstract class JSTarget extends JS.Obj implements Target { }
 }
index 3000b0d..af70e95 100644 (file)
@@ -12,7 +12,7 @@ import java.util.*;
 import java.text.*;
 
 /** a fast-write, slow-read place to stash all messages we touch -- in case of a major f*ckup */
-public class Transcript extends Target {
+public class Transcript implements Target {
 
     public static final Transcript transcript = new Transcript(Mailbox.STORAGE_ROOT + File.separatorChar + "transcript");
 
index eaa5f12..cbb40ef 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** functionality similar to the UNIX vacation command */
-public class Vacation extends Target {
+public abstract class Vacation implements Target {
 }
index 234de76..3bbb33a 100644 (file)
@@ -5,5 +5,5 @@
 package org.ibex.mail.target;
 import org.ibex.mail.*;
 /** an XML-RPC <i>server</i> which accepts requests sent via SMTP */
-public class XMLRPC extends Target {
+public abstract class XMLRPC implements Target {
 }