more fixups to MailingList
authoradam <adam@megacz.com>
Fri, 25 Mar 2005 06:29:45 +0000 (06:29 +0000)
committeradam <adam@megacz.com>
Fri, 25 Mar 2005 06:29:45 +0000 (06:29 +0000)
darcs-hash:20050325062945-5007d-f716a11e992ed0bf11459b56a307c76d4624c68b.gz

src/org/ibex/mail/Headers.java
src/org/ibex/mail/MailingList.java
src/org/ibex/mail/SkaringaFile.java [deleted file]
src/org/ibex/mail/protocol/SMTP.java
src/org/ibex/mail/target/FileBasedMailbox.java
src/org/ibex/mail/target/Script.java

index 70ad93c..5c2c479 100644 (file)
@@ -51,6 +51,7 @@ public class Headers extends JS.Immutable implements Fountain {
             lines++;
             all.append(k + ": " + v + "\r\n");
         }
+        all.append("\r\n");
         this.raw = all.toString();
         this.lines = lines;
         this.fountain = new Fountain.StringFountain(this.raw);
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 { }
diff --git a/src/org/ibex/mail/SkaringaFile.java b/src/org/ibex/mail/SkaringaFile.java
deleted file mode 100644 (file)
index 24c8630..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-// 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 cbba7ea..af39827 100644 (file)
@@ -30,6 +30,11 @@ public class SMTP {
 
     static { new Thread() { public void run() { Outgoing.runq(); } }.start(); }
 
+    public static void accept(Message m) throws IOException {
+        if (!m.envelopeTo.isLocal()) Outgoing.accept(m);
+        else                         Target.root.accept(m);
+    }
+
     public static class SMTPException extends MailException {
         int code;
         String message;
@@ -108,8 +113,7 @@ public class SMTP {
                         Message m = null;
                         for(int i=0; i<to.size(); i++) {
                            m = Message.newMessage(new Fountain.StringFountain(body), from, (Address)to.elementAt(i));
-                            if (!m.envelopeTo.isLocal()) Outgoing.accept(m);
-                            else                         Target.root.accept(m);
+                            accept(m);
                        }
                         if (m != null) Log.info(SMTP.class, "accepted message: " + m.summary());
                         conn.println("250 message accepted");
index e3b9afd..2fe5996 100644 (file)
@@ -253,11 +253,11 @@ public class FileBasedMailbox extends Mailbox.Default {
             boolean odd=false;
             for(int i=0; i<messages.length; i++) {
                 odd = !odd;
-                pw.println("      <tr style='background: "+(odd?"#e8eef7":"white")+"' id='"+i+"' "+
+                String[] m = messages[i];
+                pw.println("      <tr style='background: "+(odd?"#e8eef7":"white")+"' id='"+m[3]+"' "+
                            "onmouseover='this.style.color=\"blue\"' "+
                            "onmouseout='this.style.color=\"black\"' "+
                            "onclick='choose(this);'>");
-                String[] m = messages[i];
                 pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[0]+"</td>");
                 pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[1]+"</td>");
                 pw.println("<td style='padding:5px; padding-bottom:2px'>"+m[2]+"</td>");
index b112b91..0d65268 100644 (file)
@@ -62,6 +62,7 @@ public class Script extends JS.Obj implements Target {
             Object ret = js.call(null, new JS[] { m });
             Log.debug(this, "configuration script returned " + ret);
             if (ret == null) throw new IOException("configuration script returned null");
+            while (ret instanceof JSReflection.Wrapper) ret = ((JSReflection.Wrapper)ret).unwrap();
             if (ret instanceof Target)      ((Target)ret).accept(m);
             //else if (ret instanceof Filter) ((Filter)ret).process(m);
             else throw new IOException("configuration script returned a " + ret.getClass().getName());
@@ -138,7 +139,7 @@ public class Script extends JS.Obj implements 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("mail.list")) return JSReflection.wrap(MailingList.getMailingList(JSU.toString(args[0])));
                 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");