1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
4 import org.ibex.util.*;
5 import org.ibex.mail.filter.*;
6 import org.ibex.mail.target.*;
10 public class Script extends Target {
12 private static Script root = null;
13 private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
14 public static Script root() throws JSExn, IOException {
15 if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
20 public Script(String filePath) throws JSExn, IOException {
21 js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
22 new ScriptScope(null)); }
24 private class ScriptScope extends JSScope {
25 ScriptEnv env = new ScriptEnv();
26 public ScriptScope() { super(null); }
27 public Object get(Object o) {
28 if (o.equals("m")) return m;
29 if (o.equals("ibex")) return env;
34 public synchronized void accept(Message m) throws IOException {
37 Object ret = js.call(m, null, null, null, 1);
38 if (ret == null) throw new IOException("configuration script returned null");
39 if (ret instanceof Target) ((Target)ret).accept(m);
40 //else if (ret instanceof Filter) ((Filter)ret).process(m);
41 else throw new IOException("configuration script returned a " + ret.getClass().getName());
44 throw new IOException("configuration script threw an exception");
48 // FIXME: this should extend org.ibex.core.Ibex
49 public static class ScriptEnv extends JS {
51 /** lets us put multi-level get/put/call keys all in the same method */
52 private class Sub extends JS {
54 Sub(String key) { this.key = key; }
55 public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); }
56 public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); }
57 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
58 return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs);
60 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
61 return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
64 private Sub getSub(String s) { return new Sub(s); }
66 public Object get(Object name) throws JSExn {
67 if (name.equals("math")) { return ibexMath; }
68 if (name.equals("string")) { return ibexString; }
69 if (name.equals("date")) { return METHOD; }
70 if (name.equals("regexp")) { return METHOD; }
71 if (name.equals("log")) { return getSub("log"); }
72 if (name.equals("log.debug")) { return METHOD; }
73 if (name.equals("log.info")) { return METHOD; }
74 if (name.equals("log.warn")) { return METHOD; }
75 if (name.equals("log.error")) { return METHOD; }
76 return super.get(name);
79 public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
81 if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
82 if (name.equals("log.debug")) { JS.debug(a== null ? "**null**" : a.toString()); return null; }
83 if (name.equals("log.info")) { JS.info(a== null ? "**null**" : a.toString()); return null; }
84 if (name.equals("log.warn")) { JS.warn(a== null ? "**null**" : a.toString()); return null; }
85 if (name.equals("log.error")) { JS.error(a== null ? "**null**" : a.toString()); return null; }
88 if (name.equals("regexp")) {return new JSRegexp(a, null); }
91 if (name.equals("regexp")) {return new JSRegexp(a, b); }
93 } catch (RuntimeException e) {
94 Log.warn(this, "ibex."+name+"() threw: " + e);
95 throw new JSExn("invalid argument for ibex object method "+name+"()");
97 throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
100 public static final JSMath ibexMath = new JSMath() {
101 private JS gs = new JSScope.Global();
102 public Object get(Object name) throws JSExn {
103 if (name.equals("isNaN")) { return gs.get("isNaN"); }
104 if (name.equals("isFinite")) { return gs.get("isFinite"); }
105 if (name.equals("NaN")) { return gs.get("NaN"); }
106 if (name.equals("Infinity")) { return gs.get("Infinity"); }
107 return super.get(name);
111 public static final JS ibexString = new JS() {
112 private JS gs = new JSScope.Global();
113 public void put(Object key, Object val) { }
114 public Object get(Object name) throws JSExn {
115 if (name.equals("parseInt")) { return gs.get("parseInt"); }
116 if (name.equals("parseFloat")) { return gs.get("parseFloat"); }
117 if (name.equals("decodeURI")) { return gs.get("decodeURI"); }
118 if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); }
119 if (name.equals("encodeURI")) { return gs.get("encodeURI"); }
120 if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); }
121 if (name.equals("escape")) { return gs.get("escape"); }
122 if (name.equals("unescape")) { return gs.get("unescape"); }
123 if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); }