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", File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf"));
17 } catch (Exception e) {
23 public Script(String filePath) throws JSExn, IOException {
24 js = JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))); }
26 public void accept(Message m) throws IOException {
28 // currently, we write all inbound messages to the transcript
30 //MessageStore.transcript.add(m);
31 Object ret = js.call(m, null, null, null, 1);
32 if (ret instanceof Target) {
33 ((Target)ret).accept(m);
34 } else if (ret instanceof Filter) {
35 // FIXME: return value?
36 ((Filter)ret).process(m);
38 throw new IOException("configuration script returned a " + ret.getClass().getName());
45 // FIXME: this should extend org.ibex.core.Ibex
46 public static class ScriptEnv extends JS {
48 // FIXME: duplicated code with org.ibex.core.Ibex; lift?
49 /** lets us put multi-level get/put/call keys all in the same method */
50 private class Sub extends JS {
52 Sub(String key) { this.key = key; }
53 public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); }
54 public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); }
55 public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
56 return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs);
58 public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
59 return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
62 private Sub getSub(String s) { return new Sub(s); }
64 public Object get(Object name) throws JSExn {
65 if (name.equals("math")) { return ibexMath; }
66 if (name.equals("string")) { return ibexString; }
67 if (name.equals("date")) { return METHOD; }
68 if (name.equals("regexp")) { return METHOD; }
69 if (name.equals("log")) { return getSub("log"); }
70 if (name.equals("log.debug")) { return METHOD; }
71 if (name.equals("log.info")) { return METHOD; }
72 if (name.equals("log.warn")) { return METHOD; }
73 if (name.equals("log.error")) { return METHOD; }
74 return super.get(name);
77 public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
79 if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
80 if (name.equals("log.debug")) { JS.debug(a== null ? "**null**" : a.toString()); return null; }
81 if (name.equals("log.info")) { JS.info(a== null ? "**null**" : a.toString()); return null; }
82 if (name.equals("log.warn")) { JS.warn(a== null ? "**null**" : a.toString()); return null; }
83 if (name.equals("log.error")) { JS.error(a== null ? "**null**" : a.toString()); return null; }
86 if (name.equals("regexp")) { return new JSRegexp(a, null); }
89 if (name.equals("regexp")) { return new JSRegexp(a, b); }
91 } catch (RuntimeException e) {
92 // FIXME: maybe JSExn should take a second argument, Exception
93 Log.warn(this, "ibex."+name+"() threw: " + e);
94 throw new JSExn("invalid argument for ibex object method "+name+"()");
96 throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
99 public static final JSMath ibexMath = new JSMath() {
100 private JS gs = new JSScope.Global();
101 public Object get(Object name) throws JSExn {
102 if (name.equals("isNaN")) { return gs.get("isNaN"); }
103 if (name.equals("isFinite")) { return gs.get("isFinite"); }
104 if (name.equals("NaN")) { return gs.get("NaN"); }
105 if (name.equals("Infinity")) { return gs.get("Infinity"); }
106 return super.get(name);
110 public static final JS ibexString = new JS() {
111 private JS gs = new JSScope.Global();
112 public void put(Object key, Object val) { }
113 public Object get(Object name) throws JSExn {
114 if (name.equals("parseInt")) { return gs.get("parseInt"); }
115 if (name.equals("parseFloat")) { return gs.get("parseFloat"); }
116 if (name.equals("decodeURI")) { return gs.get("decodeURI"); }
117 if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); }
118 if (name.equals("encodeURI")) { return gs.get("encodeURI"); }
119 if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); }
120 if (name.equals("escape")) { return gs.get("escape"); }
121 if (name.equals("unescape")) { return gs.get("unescape"); }
122 if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); }