// 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.filter.*;
+import org.ibex.mail.target.*;
+import org.ibex.mail.store.*;
+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"));
+ public static Script root = null;
+ static {
+ try {
+ root = new Script(System.getProperty("ibex.mail.conf", File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf"));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
final JS js;
- public Script(String filePath) { js = JS.fromReader(CONF, 0, new InputStreamReader(new FileInputStream(CONF))); }
+ public Script(String filePath) throws JSExn, IOException {
+ js = JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))); }
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());
+ try {
+ // currently, we write all inbound messages to the transcript
+ // FIXME
+ //MessageStore.transcript.add(m);
+ Object ret = js.call(m, null, null, null, 1);
+ if (ret instanceof Target) {
+ ((Target)ret).accept(m);
+ } else if (ret instanceof Filter) {
+ // FIXME: return value?
+ ((Filter)ret).process(m);
+ } else {
+ throw new IOException("configuration script returned a " + ret.getClass().getName());
+ }
+ } catch (JSExn e) {
+ e.printStackTrace();
}
}
// FIXME: this should extend org.ibex.core.Ibex
- public static class ScriptEnvironment extends JS {
+ public static class ScriptEnv extends JS {
// FIXME: duplicated code with org.ibex.core.Ibex; lift?
/** lets us put multi-level get/put/call keys all in the same method */
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; }
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
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;
}
};