updated for new org.ibex.js
authoradam <adam@megacz.com>
Fri, 7 Jan 2005 20:11:49 +0000 (20:11 +0000)
committeradam <adam@megacz.com>
Fri, 7 Jan 2005 20:11:49 +0000 (20:11 +0000)
darcs-hash:20050107201149-5007d-67ad1bd6589e0f3a5686a9df28be38ffd9f8763a.gz

src/org/ibex/mail/Confirmation.java
src/org/ibex/mail/MIME.java
src/org/ibex/mail/MailingList.java [moved from src/org/ibex/mail/List.java with 86% similarity]
src/org/ibex/mail/Target.java
src/org/ibex/mail/protocol/GMail.java
src/org/ibex/mail/target/Script.java

index c197337..9bf7804 100644 (file)
@@ -105,20 +105,20 @@ public abstract class Confirmation implements Externalizable {
         oos.flush();
         oos.close();
         byte[] b = os.toByteArray();
-        StringBuffer sb = new StringBuffer(new String(Base64.encode(b)));
+        StringBuffer sb = new StringBuffer(new String(Encode.toBase64(b)));
         sb.append('.');
         SHA1 sha1 = new SHA1();
         sha1.update(b, 0, b.length);
         b = new byte[sha1.getDigestSize()];
         sha1.doFinal(b, 0);
-        sb.append(new String(Base64.encode(b)));
+        sb.append(new String(Encode.toBase64(b)));
         return sb.toString();
     }
 
     public static Confirmation decode(String encoded, long secret, Date now) {
         try {
             String payload = encoded.substring(0, encoded.indexOf('.'));
-            ObjectInputStream ois = new ObjectInputStream(new InflaterInputStream(new Base64.InputStream(payload)));
+            ObjectInputStream ois = new ObjectInputStream(new InflaterInputStream(new Encode.Base64InputStream(payload)));
             Confirmation cve = (Confirmation)ois.readObject();
             if (!cve.sign(secret).equals(encoded)) throw new InvalidSignature();
             if (now.getTime() > cve.expiration) throw new Expired();
index abcb71f..67ee72d 100644 (file)
@@ -7,6 +7,7 @@ import org.ibex.crypto.*;
 import org.ibex.util.*;
 import org.ibex.mail.protocol.*;
 import org.ibex.io.*;
+import org.ibex.js.*;
 import java.util.*;
 import java.net.*;
 import java.io.*;
@@ -166,7 +167,7 @@ public class MIME {
                 lines++;
             }
             if ("quoted-printable".equals(encoding)) this.body = MIME.QuotedPrintable.decode(body.toString(),false);
-            else if ("base64".equals(encoding)) this.body = new String(Base64.decode(body.toString()));
+            else if ("base64".equals(encoding)) this.body = new String(Encode.fromBase64(body.toString()));
             else this.body = body.toString();
             this.last = last;
             this.lines = lines + headers.lines;
@@ -214,11 +215,11 @@ public class MIME {
             this.raw = all.toString();
             this.lines = lines;
 
-            Enumeration e = head.keys();
+            java.util.Enumeration e = head.keys();
             boolean mime = assumeMime | (gets("mime-version") != null && gets("mime-version").trim().equals("1.0"));
             /*
-            while(e.hasMoreElements()) {
-                String k = (String)e.nextElement();
+            while(e.hasNext()) {
+                String k = (String)e.next();
                 String v = (String)head.get(k);
                 if (mime) k = MIME.RFC2047.decode(k);
                 v = uncomment(v);
similarity index 86%
rename from src/org/ibex/mail/List.java
rename to src/org/ibex/mail/MailingList.java
index 1ba6482..a27683d 100644 (file)
@@ -13,7 +13,7 @@ import org.prevayler.*;
 import org.prevayler.Query;
 
 // FEATURE: umbrella structure to mailing lists
-public class List extends Target implements Serializable {
+public class MailingList extends Target implements Serializable {
 
     public static enum UserType         { Administrator, Moderator, Member }
     public static enum SubscriptionType { All, None, Digest, MimeDigest }
@@ -23,7 +23,7 @@ public class List extends Target implements Serializable {
     public Address      address;
     public Mailbox      archive;
     private final long  secret;
-    private List(Address a, Mailbox ar, long s) { this.address=a; this.archive=ar; this.secret=s; }
+    private MailingList(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];
@@ -40,12 +40,12 @@ public class List extends Target 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) {
+    public static MailingList getList(Object all, String listName) { return (MailingList)((Hashtable)all).get(listName); }
+    public static MailingList getList(final String listName) {
         try {
-            return (List)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
+            return (MailingList)p.execute(new Query() { public Object query(Object o, Date now) { return getList(o, listName); } });
         } catch (Exception e) {
-            Log.error(List.class, e);
+            Log.error(MailingList.class, e);
             return null;
         }
     }
@@ -86,7 +86,7 @@ public class List extends Target implements Serializable {
             Log.error("[list]", e2);
             throw new IOException(e2.toString());
         }
-        Log.warn(List.class, "got message " + m.subject);
+        Log.warn(MailingList.class, "got message " + m.subject);
         archive.accept(m);
         try {
             String[] subscribers = (String[])p.execute(subscribers());
@@ -94,7 +94,7 @@ public class List extends Target implements Serializable {
             for(int i=0; i<subscribers.length; i++) {
                 String s = subscribers[i];
                 try {
-                    Log.warn(List.class, "  trying " + s);
+                    Log.warn(MailingList.class, "  trying " + s);
                     SMTP.Outgoing.accept(Message.newMessage(new Stream(m.toString()), address, Address.parse(s)));
                     Log.warn("[list]", "successfully sent to " + s);
                 } catch (Exception e2) {
@@ -115,7 +115,7 @@ public class List extends Target implements Serializable {
     public static final String ROOT   = System.getProperty("ibex.mail.list.root", Mailbox.STORAGE_ROOT+File.separatorChar+"lists");
     public static Prevayler p;
     static { try { p = PrevaylerFactory.createPrevayler(new Hashtable(), ROOT); }
-    catch (Exception e) { Log.error(List.class, e); } }
+    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) {        
@@ -125,7 +125,7 @@ public class List extends Target implements Serializable {
                                       list,
                                       SubscriptionType.All).signAndSend(getList(o, list).secret, now);
             } catch (Exception e) {
-                Log.error(List.class, e);
+                Log.error(MailingList.class, e);
             }
         } }; }
 
@@ -148,7 +148,7 @@ public class List extends Target implements Serializable {
     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, archive, random)); } };
+            ((Hashtable)all).put(address.toString(false), new MailingList(address, archive, random)); } };
     }
 
     public static Transaction delete(final Address address) {
@@ -157,16 +157,16 @@ public class List extends Target implements Serializable {
 
     public static Query all() { return new Query() { public Object query(Object o, Date now) {
         Hashtable all = (Hashtable)o;
-        List[] ret = new List[all.size()];
-        Enumeration e = all.elements();
-        for(int i=0; i<ret.length; i++) ret[i] = (List)e.nextElement();
+        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()];
-        Enumeration e = subscribers.keys();
+        java.util.Enumeration e = subscribers.keys();
         for(int i=0; i<ret.length; i++) ret[i] = e.nextElement().toString();
         return ret;
     } }; }
index c3dcee4..7f89b70 100644 (file)
@@ -5,9 +5,10 @@
 package org.ibex.mail;
 import java.io.*;
 import org.ibex.js.*;
+import org.ibex.mail.target.*;
 
 /** base class for mail message "destinations" */
-public class Target extends JS {
+public class Target extends JS.Obj {
     public static final Target root = Script.root();
     public void accept(Message m) throws IOException, MailException { throw new MailException("Target.accept() unimplemented"); }
 }
index 3c2e849..823a4a6 100644 (file)
@@ -240,14 +240,14 @@ public class GMail extends Account {
             Log.info(GMail.class, "query: " + query);
             JSArray ret = http(gmail + query, jar);
             Hashtable h = new Hashtable();
-            for(int i=0; i<ret.length(); i++) {
+            for(int i=0; i<ret.size(); i++) {
                 JSArray j = (JSArray)ret.get(i);
-                if (j.elementAt(0).equals("t")) {
-                    for(int k=1; k<j.length(); k++) getSummary((String)((JSArray)j.get(k)).get(0), h);
-                } else if (j.elementAt(0).equals("ct") && labels.length == 0) {
+                if (j.get(0).equals("t")) {
+                    for(int k=1; k<j.size(); k++) getSummary((String)((JSArray)j.get(k)).get(0), h);
+                } else if (j.get(0).equals("ct") && labels.length == 0) {
                     Vec v = new Vec();
-                    j = (JSArray)j.elementAt(1);
-                    for(int k=0; k<j.length(); k++) v.addElement(((JSArray)j.elementAt(k)).elementAt(0));
+                    j = (JSArray)j.get(1);
+                    for(int k=0; k<j.size(); k++) v.addElement(((JSArray)j.get(k)).get(0));
                     v.copyInto(labels = new String[v.size()]);
                 }
             }
@@ -264,9 +264,9 @@ public class GMail extends Account {
     public void getSummary(String id, Hashtable ret) {
         try {
             JSArray js2 = http(gmail + "?search=query&start=0&view=cv&q=in:anywhere&th=" + URLEncoder.encode(id), jar);
-            for(int i2=0; i2<js2.length(); i2++) {
-                JSArray args = (JSArray)js2.elementAt(i2);
-                if (!args.elementAt(0).equals("mi")) continue;
+            for(int i2=0; i2<js2.size(); i2++) {
+                JSArray args = (JSArray)js2.get(i2);
+                if (!args.get(0).equals("mi")) continue;
                 Summary sum = new Summary(args);
                 Log.info(GMail.class, "summary: " + sum.subject);
                 ret.put(sum.id, sum);
@@ -295,11 +295,11 @@ public class GMail extends Account {
         }
         
         public Summary(JSArray m) {
-            try { this.date = new Date(m.elementAt(9).toString()); } catch (Exception e) { this.date = null; }
-            this.id = m.elementAt(3).toString();
-            this.to = Address.parse(m.elementAt(8).toString() + " <" + m.elementAt(10).toString() + ">");
-            this.from = Address.parse(m.elementAt(6).toString()+"<"+m.elementAt(7).toString()+">");
-            this.subject = m.elementAt(15).toString();
+            try { this.date = new Date(m.get(9).toString()); } catch (Exception e) { this.date = null; }
+            this.id = m.get(3).toString();
+            this.to = Address.parse(m.get(8).toString() + " <" + m.get(10).toString() + ">");
+            this.from = Address.parse(m.get(6).toString()+"<"+m.get(7).toString()+">");
+            this.subject = m.get(15).toString();
         }
     }
 
@@ -315,8 +315,8 @@ public class GMail extends Account {
         }
         buf.append("return ret;");
         synchronized(GMail.class) {
-            JS js = JS.fromReader("google", 0, new StringReader(buf.toString()));
-            return (JSArray)js.call(null, null, null, null, 0);
+            JS js = JSU.fromReader("google", 0, new StringReader(buf.toString()));
+            return (JSArray)js.call(JSU.emptyArgs);
         }
     }
 
index 7ef5d48..f183aa1 100644 (file)
@@ -2,7 +2,7 @@
 // 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;
+package org.ibex.mail.target;
 import org.ibex.js.*;
 import org.ibex.util.*;
 import org.ibex.mail.*;
@@ -13,6 +13,8 @@ import java.util.*;
 
 public class Script extends Target {
 
+    private static final JS.Method METHOD = new JS.Method();
+
     private static Script root = null;
     private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
     public static Script root() {
@@ -30,12 +32,11 @@ public class Script extends Target {
     private String filePath = null;
     public Script(String filePath) throws JSExn, IOException {
         this.filePath = filePath;
-        js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
-                                        new ScriptScope()); }
+        js = JSU.cloneWithNewGlobalScope(JSU.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
+                                         new ScriptScope()); }
 
-    private class ScriptScope extends JSScope {
+    private class ScriptScope extends JS.Immutable {
         ScriptEnv env = new ScriptEnv();
-        public ScriptScope() { super(null); }
         public Object get(Object o) {
             if (o.equals("m")) return m;
             if (o.equals("ibex")) return env;
@@ -55,7 +56,7 @@ public class Script extends Target {
     private synchronized void reallyAccept(Message m) throws IOException, MailException, JSExn {
         this.m = m;
         try {
-            Object ret = js.call(m, null, null, null, 1);
+            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");
             if (ret instanceof Target)      ((Target)ret).accept(m);
@@ -68,7 +69,7 @@ public class Script extends Target {
     }
 
     // FIXME: this should extend org.ibex.core.Ibex
-    public class ScriptEnv extends JS {
+    public class ScriptEnv extends JS.Obj {
 
         private PropertyFile prefs = null;
             /*
@@ -83,73 +84,72 @@ public class Script extends Target {
             */
 
         /** lets us put multi-level get/put/call keys all in the same method */
-        private class Sub extends JS {
+        private class Sub extends JS.Immutable {
             String key;
             Sub(String key) { this.key = key; }
-            public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); }
-            public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); }
-            public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-                return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs);
-            }
-            public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
-                return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
+            public void put(JS key, JS val) throws JSExn {
+                ScriptEnv.this.put(JSU.S(this.key + "." + JSU.toString(key)), val); }
+            public JS get(JS key) throws JSExn {
+                return ScriptEnv.this.get(JSU.S(this.key + "." + JSU.toString(key))); }
+            public JS call(JS method, JS[] args) throws JSExn {
+                return ScriptEnv.this.call(JSU.S(this.key + "." + JSU.toString(method)), args);
             }
         }
         private Sub getSub(String s) { return new Sub(s); }
 
-        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; }
-            if (name.equals("regexp")) { return METHOD; }
-            if (name.equals("log")) { return getSub("log"); }
-            if (name.equals("log.debug")) { return METHOD; }
-            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.forward2")) { return METHOD; }
-            if (name.equals("mail.send")) { return METHOD; }
-            if (name.equals("mail.my")) { return getSub("mail.my"); }
-            if (name.equals("mail.my.prefs")) {
-                try {
+        public JS get(JS name) throws JSExn {
+            //#jsswitch(name)
+            case "math": return ibexMath;
+            case "string": return ibexString;
+            case "date": return METHOD;
+            case "regexp": return METHOD;
+            case "log": return getSub("log");
+            case "log.debug": return METHOD;
+            case "log.info": return METHOD;
+            case "log.warn": return METHOD;
+            case "log.error": return METHOD;
+            case "list": return getSub("list");
+            case "mail": return getSub("mail");
+            case "mail.forward": return METHOD;
+            case "mail.forward2": return METHOD;
+            case "mail.send": return METHOD;
+            case "mail.my": return getSub("mail.my");
+            case "mail.my.prefs": try {
                     return new org.ibex.js.Directory(new File("/etc/org.ibex.mail.prefs"));
-                } catch (IOException e) {
-                    throw new JSExn(e.toString());
-                }
-            }
-            if (name.equals("mail.my.mailbox")) {
+            } catch (IOException e) { throw new JSExn(e.toString()); }
+            case "mail.my.mailbox":
                 FileBasedMailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
                 return root.slash("user", true).slash("megacz", true).slash("newmail", true);
-           }
+            //#end
+            if (JSU.toString(name).startsWith("list.")) { return MailingList.getList(JSU.toString(name).substring(5)); }
             return super.get(name);
         }
 
-        public Object callMethod(Object name, final Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
+        public JS call(JS name, JS[] args) throws JSExn {
+            final JS a = args.length >= 1 ? args[0] : null;
+            final JS b = args.length >= 2 ? args[1] : null;
+            final JS c = args.length >= 3 ? args[2] : null;
+            final int nargs = args.length;
             try {
-                if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
+                if (name.equals("date")) { return new JSDate(args); }
                 if (name.equals("mail.send")) {
                     JS m = (JS)a;
                     StringBuffer headers = new StringBuffer();
                     String body = "";
                     Address from = null, to = null;
-                    for(Enumeration e = m.keys(); e.hasMoreElements();) {
-                        String key = (String)e.nextElement();
-                        String val = m.get(key) == null ? null : m.get(key).toString();
-                        if ("body".equals(key)) body = val;
+                    for(JS.Enumeration e = m.keys(); e.hasNext();) {
+                        JS key = (JS)e.next();
+                        JS val = m.get(key) == null ? null : m.get(key);
+                        if ("body".equalsIgnoreCase(JSU.toString(key))) body = JSU.toString(val);
                         else headers.append(key + ": " + val + "\r\n");
-                        if ("from".equalsIgnoreCase(key)) from = Address.parse(val);
-                        if ("to".equalsIgnoreCase(key)) to = Address.parse(val);
+                        if ("from".equalsIgnoreCase(JSU.toString(key))) from = Address.parse(JSU.toString(val));
+                        if ("to".equalsIgnoreCase(JSU.toString(key))) to = Address.parse(JSU.toString(val));
                     }
                     Message message = Message.newMessage(new org.ibex.io.Stream(headers.toString() + "\r\n" + body), from, to);
                     //org.ibex.mail.protocol.SMTP.Outgoing.accept(message);
                     boolean ok = org.ibex.mail.protocol.SMTP.Outgoing.attempt(message);
                     if (!ok) throw new JSExn("SMTP server rejected message");
-                    return T;
+                    return JSU.T;
                 }
                 if (name.equals("mail.forward2")) {
                     try {
@@ -175,10 +175,10 @@ public class Script extends Target {
                             }
                         }
                     }; }
-                if (name.equals("log.debug")) {    JS.debug(a== null ? "**null**" : a.toString()); return null; }
-                if (name.equals("log.info")) {     JS.info(a== null ? "**null**" : a.toString()); return null; }
-                if (name.equals("log.warn")) {     JS.warn(a== null ? "**null**" : a.toString()); return null; }
-                if (name.equals("log.error")) {    JS.error(a== null ? "**null**" : a.toString()); return null; }
+                if (name.equals("log.debug")) {    JSU.debug(a== null ? "**null**" : a.toString()); return null; }
+                if (name.equals("log.info")) {     JSU.info(a== null ? "**null**" : a.toString()); return null; }
+                if (name.equals("log.warn")) {     JSU.warn(a== null ? "**null**" : a.toString()); return null; }
+                if (name.equals("log.error")) {    JSU.error(a== null ? "**null**" : a.toString()); return null; }
                 switch (nargs) {
                 case 1:
                     if (name.equals("regexp")) {return new JSRegexp(a, null); }
@@ -196,29 +196,32 @@ public class Script extends Target {
         }
 
         public final JSMath ibexMath = new JSMath() {
-                private JS gs = new JSScope.Global();
-                public Object get(Object name) throws JSExn {
-                    if (name.equals("isNaN")) { return gs.get("isNaN"); }
-                    if (name.equals("isFinite")) { return gs.get("isFinite"); }
-                    if (name.equals("NaN")) { return gs.get("NaN"); }
-                    if (name.equals("Infinity")) { return gs.get("Infinity"); }
-                    return super.get(name);
+                public JS get(JS name) throws JSExn {
+                    // FIXME!!!
+                    /*
+                    case "isNaN": return gs.get(name);
+                    case "isFinite": return gs.get(name);
+                    case "NaN": return gs.get(name);
+                    case "Infinity": return gs.get(name);
+                    */
+                    return null;
                 }
             };
         
-        public final JS ibexString = new JS() {
-                private JS gs = new JSScope.Global();
-                public void put(Object key, Object val) { }
-                public Object get(Object name) throws JSExn {
-                    if (name.equals("parseInt")) { return gs.get("parseInt"); }
-                    if (name.equals("parseFloat")) { return gs.get("parseFloat"); }
-                    if (name.equals("decodeURI")) { return gs.get("decodeURI"); }
-                    if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); }
-                    if (name.equals("encodeURI")) { return gs.get("encodeURI"); }
-                    if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); }
-                    if (name.equals("escape")) { return gs.get("escape"); }
-                    if (name.equals("unescape")) { return gs.get("unescape"); }
-                    if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); }
+        public final JS ibexString = new JS.Immutable() {
+                public JS get(JS name) throws JSExn {
+                    // FIXME!!!
+                    /*
+                    case "parseInt": return gs.get(JSU.S("parseInt"));
+                    case "parseFloat": return gs.get(JSU.S("parseFloat"));
+                    case "decodeURI": return gs.get(JSU.S("decodeURI"));
+                    case "decodeURIComponent": return gs.get(JSU.S("decodeURIComponent"));
+                    case "encodeURI": return gs.get(JSU.S("encodeURI"));
+                    case "encodeURIComponent": return gs.get(JSU.S("encodeURIComponent"));
+                    case "escape": return gs.get(JSU.S("escape"));
+                    case "unescape": return gs.get(JSU.S("unescape"));
+                    case "fromCharCode": return gs.get(JSU.S("stringFromCharCode"));
+                    */
                     return null;
                 }
             };