almost working
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.mail;
3 import org.ibex.js.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.filter.*;
6 import org.ibex.mail.target.*;
7 import org.ibex.mail.store.*;
8 import java.io.*;
9 import java.util.*;
10
11 public class Script extends Target {
12
13     public static Script root = null;
14     static {
15         try {
16             root = new Script(System.getProperty("ibex.mail.conf",
17                                                  File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf"));
18         } catch (Exception e) {
19             e.printStackTrace();
20         }
21     }
22
23     final JS js;
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)); }
27
28     private static class ScriptScope extends JSScope {
29         Message m;
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;
35             return null;
36         }
37     }
38
39     public void accept(Message m) throws IOException {
40         try {
41             // currently, we write all inbound messages to the transcript
42             // FIXME
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);
49             } else {
50                 if (ret == null) throw new IOException("configuration script returned null");
51                 else throw new IOException("configuration script returned a " + ret.getClass().getName());
52             }
53         } catch (JSExn e) {
54             e.printStackTrace();
55         }
56     }
57
58     // FIXME: this should extend org.ibex.core.Ibex
59     public static class ScriptEnv extends JS {
60
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 {
64             String key;
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);
70             }
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);
73             }
74         }
75         private Sub getSub(String s) { return new Sub(s); }
76
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);
88         }
89
90         public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
91             try {
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; }
97                 switch (nargs) {
98                 case 1:
99                     if (name.equals("regexp")) {return new JSRegexp(a, null); }
100                     break;
101                 case 2:
102                     if (name.equals("regexp")) {return new JSRegexp(a, b); }
103                 }
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+"()");
108             }
109             throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
110         }
111
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);
120                 }
121             };
122         
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"); }
136                     return null;
137                 }
138             };
139     }
140 }