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.*;
7 import org.ibex.mail.store.*;
11 public class Script extends Target {
13 public static Script root = null;
16 root = new Script(System.getProperty("ibex.mail.conf",
17 File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf"));
18 } catch (Exception e) {
24 public Script(String filePath) throws JSExn, IOException {
25 js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
26 new ScriptScope(null)); }
28 private static class ScriptScope extends JSScope {
30 ScriptEnv env = new ScriptEnv();
31 public ScriptScope(Message m) { super(null); this.m = m; }
32 public Object get(Object o) {
33 if (o.equals("m")) return m;
34 if (o.equals("ibex")) return env;
39 public void accept(Message m) throws IOException {
41 // currently, we write all inbound messages to the transcript
43 Object ret = js.call(m, null, null, null, 1);
44 if (ret instanceof Target) {
45 ((Target)ret).accept(m);
46 } else if (ret instanceof Filter) {
47 // FIXME: return value?
48 ((Filter)ret).process(m);
50 if (ret == null) throw new IOException("configuration script returned null");
51 else throw new IOException("configuration script returned a " + ret.getClass().getName());
58 // FIXME: this should extend org.ibex.core.Ibex
59 public static class ScriptEnv extends JS {
61 // FIXME: duplicated code with org.ibex.core.Ibex; lift?
62 /** lets us put multi-level get/put/call keys all in the same method */
63 private class Sub extends JS {
65 Sub(String key) { this.key = key; }
66 public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); }
67 public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); }
68 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
69 return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs);
71 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
72 return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
75 private Sub getSub(String s) { return new Sub(s); }
77 public Object get(Object name) throws JSExn {
78 if (name.equals("math")) { return ibexMath; }
79 if (name.equals("string")) { return ibexString; }
80 if (name.equals("date")) { return METHOD; }
81 if (name.equals("regexp")) { return METHOD; }
82 if (name.equals("log")) { return getSub("log"); }
83 if (name.equals("log.debug")) { return METHOD; }
84 if (name.equals("log.info")) { return METHOD; }
85 if (name.equals("log.warn")) { return METHOD; }
86 if (name.equals("log.error")) { return METHOD; }
87 return super.get(name);
90 public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
92 if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
93 if (name.equals("log.debug")) { JS.debug(a== null ? "**null**" : a.toString()); return null; }
94 if (name.equals("log.info")) { JS.info(a== null ? "**null**" : a.toString()); return null; }
95 if (name.equals("log.warn")) { JS.warn(a== null ? "**null**" : a.toString()); return null; }
96 if (name.equals("log.error")) { JS.error(a== null ? "**null**" : a.toString()); return null; }
99 if (name.equals("regexp")) {return new JSRegexp(a, null); }
102 if (name.equals("regexp")) {return new JSRegexp(a, b); }
104 } catch (RuntimeException e) {
105 // FIXME: maybe JSExn should take a second argument, Exception
106 Log.warn(this, "ibex."+name+"() threw: " + e);
107 throw new JSExn("invalid argument for ibex object method "+name+"()");
109 throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
112 public static final JSMath ibexMath = new JSMath() {
113 private JS gs = new JSScope.Global();
114 public Object get(Object name) throws JSExn {
115 if (name.equals("isNaN")) { return gs.get("isNaN"); }
116 if (name.equals("isFinite")) { return gs.get("isFinite"); }
117 if (name.equals("NaN")) { return gs.get("NaN"); }
118 if (name.equals("Infinity")) { return gs.get("Infinity"); }
119 return super.get(name);
123 public static final JS ibexString = new JS() {
124 private JS gs = new JSScope.Global();
125 public void put(Object key, Object val) { }
126 public Object get(Object name) throws JSExn {
127 if (name.equals("parseInt")) { return gs.get("parseInt"); }
128 if (name.equals("parseFloat")) { return gs.get("parseFloat"); }
129 if (name.equals("decodeURI")) { return gs.get("decodeURI"); }
130 if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); }
131 if (name.equals("encodeURI")) { return gs.get("encodeURI"); }
132 if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); }
133 if (name.equals("escape")) { return gs.get("escape"); }
134 if (name.equals("unescape")) { return gs.get("unescape"); }
135 if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); }