pretty much working
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index 06c14ee..4e06040 100644 (file)
@@ -1,32 +1,69 @@
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.ibex.mail;
 import org.ibex.js.*;
+import org.ibex.util.*;
+import org.ibex.mail.*;
+import org.ibex.mail.filter.*;
+import org.ibex.mail.target.*;
+import java.io.*;
+import java.util.*;
 
-public class Script {
+public class Script extends Target {
 
-    public static final JS root =
-        new Script(System.getProperty("ibex.mail.conf", File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf"));
+    private static Script root = null;
+    private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
+    public static Script root() {
+        try {
+            if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
+            return root;
+        } catch (Exception e) {
+            Log.error(Script.class, e);
+            return null;
+        }
+    }
 
     final JS js;
-    public Script(String filePath) { js = JS.fromReader(CONF, 0, new InputStreamReader(new FileInputStream(CONF))); }
+    private Message m = null;
+    public Script(String filePath) throws JSExn, IOException {
+        js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
+                                        new ScriptScope()); }
+
+    private class ScriptScope extends JSScope {
+        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;
+            return null;
+        }
+    }
 
-    public void accept(Message m) throws IOException {
-        // currently, we write all inbound messages to the transcript
-        MessageStore.transcript.add(m);
-        Object ret = js.call(m);
-        if (ret instanceof Target) {
-            ((Target)ret).accept(m);
-        } else if (ret instanceof Filter) {
-            ((Filter)f).accept
-        } else {
-            throw new IOException("configuration script returned a " + ret.getClass().getName());
+    public synchronized void accept(Message m) throws IOException, MailException {
+        this.m = m;
+        try {
+            Object ret = js.call(m, null, null, null, 1);
+            if (ret == null) throw new IOException("configuration script returned null");
+            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());
+        } catch (JSExn e) {
+            Log.warn(this, e);
+            throw new IOException("configuration script threw an exception");
         }
     }
 
     // FIXME: this should extend org.ibex.core.Ibex
-    public static class ScriptEnvironment extends JS {
+    public static class ScriptEnv extends JS {
+
+        private static PropertyFile prefs;
+        static {
+            try {
+                prefs = new PropertyFile(new File("/etc/org.ibex.mail.properties"));
+            } catch (IOException e) {
+                Log.error(ScriptEnv.class, e);
+            }
+        }
 
-        // FIXME: duplicated code with org.ibex.core.Ibex; lift?
         /** lets us put multi-level get/put/call keys all in the same method */
         private class Sub extends JS {
             String key;
@@ -40,51 +77,40 @@ public class Script {
                 return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
             }
         }
-        private Cache subCache = new Cache(20);
-        private Sub getSub(String s) {
-            Sub ret = (Sub)subCache.get(s);
-            if (ret == null) subCache.put(s, ret = new Sub(s));
-            return ret;
-        }
+        private Sub getSub(String s) { return new Sub(s); }
 
         public Object get(Object name) throws JSExn {
-            if (name instanceof String && ((String)name).length() == 0) return rr;
-            //#switch(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;
-            //#end
+            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("mail")) { return getSub("mail"); }
+            if (name.equals("mail.my")) { return getSub("mail.my"); }
+            if (name.equals("mail.my.prefs")) { return prefs; }
+            if (name.equals("mail.my.mailbox")) { return FileSystem.Mailbox.getForUser("megacz"); }
             return super.get(name);
         }
 
         public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
             try {
-                //#switch(name)
-                case "date": return new JSDate(a, b, c, rest, nargs);
-                case "log.debug":    JS.debug(a== null ? "**null**" : a.toString()); return null;
-                case "log.info":     JS.info(a== null ? "**null**" : a.toString()); return null;
-                case "log.warn":     JS.warn(a== null ? "**null**" : a.toString()); return null;
-                case "log.error":    JS.error(a== null ? "**null**" : a.toString()); return null;
-                //#end
+                if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
+                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; }
                 switch (nargs) {
                 case 1:
-                    //#switch(name)
-                    case "regexp": return new JSRegexp(a, null);
-                    //#end
+                    if (name.equals("regexp")) {return new JSRegexp(a, null); }
                     break;
                 case 2:
-                    //#switch(name)
-                    case "regexp": return new JSRegexp(a, b);
-                    //#end
+                    if (name.equals("regexp")) {return new JSRegexp(a, b); }
                 }
             } catch (RuntimeException e) {
-                // FIXME: maybe JSExn should take a second argument, Exception
                 Log.warn(this, "ibex."+name+"() threw: " + e);
                 throw new JSExn("invalid argument for ibex object method "+name+"()");
             }
@@ -93,32 +119,28 @@ public class Script {
 
         public static final JSMath ibexMath = new JSMath() {
                 private JS gs = new JSScope.Global();
-                public Object get(Object key) throws JSExn {
-                    //#switch(key)
-                    case "isNaN": return gs.get("isNaN");
-                    case "isFinite": return gs.get("isFinite");
-                    case "NaN": return gs.get("NaN");
-                    case "Infinity": return gs.get("Infinity");
-                    //#end
-                   return super.get(key);
+                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 static final JS ibexString = new JS() {
                 private JS gs = new JSScope.Global();
                 public void put(Object key, Object val) { }
-                public Object get(Object key) throws JSExn {
-                    //#switch(key)
-                    case "parseInt": return gs.get("parseInt");
-                    case "parseFloat": return gs.get("parseFloat");
-                    case "decodeURI": return gs.get("decodeURI");
-                    case "decodeURIComponent": return gs.get("decodeURIComponent");
-                    case "encodeURI": return gs.get("encodeURI");
-                    case "encodeURIComponent": return gs.get("encodeURIComponent");
-                    case "escape": return gs.get("escape");
-                    case "unescape": return gs.get("unescape");
-                    case "fromCharCode": return gs.get("stringFromCharCode");
-                    //#end
+                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"); }
                     return null;
                 }
             };