List can now send messages
authoradam <adam@megacz.com>
Thu, 23 Sep 2004 07:03:30 +0000 (07:03 +0000)
committeradam <adam@megacz.com>
Thu, 23 Sep 2004 07:03:30 +0000 (07:03 +0000)
darcs-hash:20040923070330-5007d-edbc7c4f5679bde4acdde4a201e01c1c8140f946.gz

src/org/ibex/mail/List.java
src/org/ibex/mail/target/Script.java

index 8b54d9a..5686f0f 100644 (file)
@@ -1,26 +1,25 @@
 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 org.prevayler.*;
-//import org.prevayler.Query;
+import org.prevayler.*;
+import org.prevayler.Query;
 
 // FEATURE: umbrella structure to mailing lists
-public class List implements Serializable {
+public class List extends Target implements Serializable {
 
-    // disabled until we figure out how to cope with the Prevayler dependency
-    /*
     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      pending;
     public Mailbox      archive;
     private final long  secret;
-    private List(Address a, Mailbox p, Mailbox ar, long s) {this.address=a;this.pending=p;this.archive=ar;this.secret=s;}
+    private List(Address a, Mailbox ar, long s) { this.address=a; this.archive=ar; this.secret=s; }
 
     public Hashtable subscribers = new Hashtable();
     public Filter[]     filters  = new Filter[0];
@@ -38,6 +37,15 @@ public class List implements Serializable {
     public int          bounceThreshhold     = 10;
 
     public static List getList(Object all, String listName) { return (List)((Hashtable)all).get(listName); }
+    public static List getList(final String listName) {
+        try {
+            return (List)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
+        } catch (Exception e) {
+            Log.error(List.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());
@@ -62,6 +70,40 @@ public class List implements Serializable {
     //    public class AnonymizeSender { public boolean uncorrelated; }
     //}
 
+
+    public void accept(Message m) throws IOException, MailException {
+        try {
+            m = new Message(new Stream("List-Id: " + one_line_description + "<"+address+">\r\n" +
+                                       m.toString() +
+                                       "--\r\n" +
+                                       message_footer + "\r\n" +
+                                       "to unsubscribe, go to " + homepage + "\r\n"),
+                            null);
+        } catch (Exception e2) {
+            Log.error("[list]", e2);
+            throw new IOException(e2.toString());
+        }
+        Log.warn(List.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(List.class, "  trying " + s);
+                    SMTP.Outgoing.accept(new Message(new Stream(m.toString()), new Message.Envelope(address, Address.parse(s), new Date())));
+                    Log.warn("[list]", "successfully sent to " + s);
+                } catch (Exception e2) {
+                    Log.error("[list]", e2);
+                }
+            }
+        } catch (Exception e2) {
+            Log.error("[list]", e2);
+        }
+    }
+
+
     // Transactions ///////////////////////////////////////////////////////////////////////////
 
 
@@ -78,16 +120,32 @@ public class List implements Serializable {
                 new AlterSubscription(user,
                                       now.getTime() + 1000*60*60*24,
                                       list,
-                                      SubscriptionType.All).signAndSend(getList(o, list).secret);
+                                      SubscriptionType.All).signAndSend(getList(o, list).secret, now);
             } catch (Exception e) {
                 Log.error(List.class, e);
             }
         } }; }
 
-    public static Transaction create(final Address address, final Mailbox pending, final Mailbox archive) {
+    /*
+    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) {
-            ((Hashtable)all).put(address.toString(false), new List(address, pending, archive, random)); } };
+            ((Hashtable)all).put(address.toString(false), new List(address, archive, random)); } };
     }
 
     public static Transaction delete(final Address address) {
@@ -102,6 +160,14 @@ public class List implements Serializable {
         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()];
+        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)); } }; }
@@ -114,5 +180,5 @@ public class List implements Serializable {
         public String getDescription() { return "change your subscription"; }
         public void executeOn(Object all, Date now) { getList(all, list).getSubscriber(who).subscription = newType; }
     }
-    */
+
 }    
index 27541d7..bb25080 100644 (file)
@@ -95,7 +95,8 @@ public class Script extends Target {
         }
         private Sub getSub(String s) { return new Sub(s); }
 
-        public Object get(Object name) throws JSExn {
+        public Object get(Object name_) throws JSExn {
+            String name = (String)name_;
             if (name.equals("math")) { return ibexMath; }
             if (name.equals("string")) { return ibexString; }
             if (name.equals("date")) { return METHOD; }
@@ -105,6 +106,8 @@ public class Script extends Target {
             if (name.equals("log.info")) { return METHOD; }
             if (name.equals("log.warn")) { return METHOD; }
             if (name.equals("log.error")) { return METHOD; }
+            if (name.equals("list")) { return getSub("list"); }
+            if (name.startsWith("list.")) { return org.ibex.mail.List.getList(name.substring(5)); }
             if (name.equals("mail")) { return getSub("mail"); }
             if (name.equals("mail.forward")) { return METHOD; }
             if (name.equals("mail.send")) { return METHOD; }