append sorta works; dates are broken
[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.*;
6 import org.ibex.mail.filter.*;
7 import org.ibex.mail.target.*;
8 import java.io.*;
9 import java.util.*;
10
11 public class Script extends Target {
12
13     private static Script root = null;
14     private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
15     public static Script root() {
16         try {
17             if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
18             return root;
19         } catch (Exception e) {
20             Log.error(Script.class, e);
21             return null;
22         }
23     }
24
25     final JS js;
26     private Message m = null;
27     public Script(String filePath) throws JSExn, IOException {
28         js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
29                                         new ScriptScope()); }
30
31     private class ScriptScope extends JSScope {
32         ScriptEnv env = new ScriptEnv();
33         public ScriptScope() { super(null); }
34         public Object get(Object o) {
35             if (o.equals("m")) return m;
36             if (o.equals("ibex")) return env;
37             return null;
38         }
39     }
40
41     public synchronized void accept(Message m) throws IOException, MailException {
42         this.m = m;
43         try {
44             Object ret = js.call(m, null, null, null, 1);
45             if (ret == null) throw new IOException("configuration script returned null");
46             if (ret instanceof Target)      ((Target)ret).accept(m);
47             //else if (ret instanceof Filter) ((Filter)ret).process(m);
48             else throw new IOException("configuration script returned a " + ret.getClass().getName());
49         } catch (JSExn e) {
50             Log.warn(this, e);
51             throw new IOException("configuration script threw an exception");
52         }
53     }
54
55     // FIXME: this should extend org.ibex.core.Ibex
56     public static class ScriptEnv extends JS {
57
58         private static PropertyFile prefs = null;
59             /*
60         static {
61             try {
62                 // FIXME
63                 prefs = new PropertyFile(new File("/etc/org.ibex.mail.properties"));
64             } catch (IOException e) {
65                 Log.error(ScriptEnv.class, e);
66             }
67         }
68             */
69
70         /** lets us put multi-level get/put/call keys all in the same method */
71         private class Sub extends JS {
72             String key;
73             Sub(String key) { this.key = key; }
74             public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); }
75             public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); }
76             public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
77                 return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs);
78             }
79             public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
80                 return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
81             }
82         }
83         private Sub getSub(String s) { return new Sub(s); }
84
85         public Object get(Object name) throws JSExn {
86             if (name.equals("math")) { return ibexMath; }
87             if (name.equals("string")) { return ibexString; }
88             if (name.equals("date")) { return METHOD; }
89             if (name.equals("regexp")) { return METHOD; }
90             if (name.equals("log")) { return getSub("log"); }
91             if (name.equals("log.debug")) { return METHOD; }
92             if (name.equals("log.info")) { return METHOD; }
93             if (name.equals("log.warn")) { return METHOD; }
94             if (name.equals("log.error")) { return METHOD; }
95             if (name.equals("mail")) { return getSub("mail"); }
96             if (name.equals("mail.my")) { return getSub("mail.my"); }
97             if (name.equals("mail.my.prefs")) { return prefs; }
98             //if (name.equals("mail.my.mailbox")) { return Mailbox.getForUser("megacz"); }
99             return super.get(name);
100         }
101
102         public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
103             try {
104                 if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
105                 if (name.equals("log.debug")) {    JS.debug(a== null ? "**null**" : a.toString()); return null; }
106                 if (name.equals("log.info")) {     JS.info(a== null ? "**null**" : a.toString()); return null; }
107                 if (name.equals("log.warn")) {     JS.warn(a== null ? "**null**" : a.toString()); return null; }
108                 if (name.equals("log.error")) {    JS.error(a== null ? "**null**" : a.toString()); return null; }
109                 switch (nargs) {
110                 case 1:
111                     if (name.equals("regexp")) {return new JSRegexp(a, null); }
112                     break;
113                 case 2:
114                     if (name.equals("regexp")) {return new JSRegexp(a, b); }
115                 }
116             } catch (RuntimeException e) {
117                 Log.warn(this, "ibex."+name+"() threw: " + e);
118                 throw new JSExn("invalid argument for ibex object method "+name+"()");
119             }
120             throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
121         }
122
123         public static final JSMath ibexMath = new JSMath() {
124                 private JS gs = new JSScope.Global();
125                 public Object get(Object name) throws JSExn {
126                     if (name.equals("isNaN")) { return gs.get("isNaN"); }
127                     if (name.equals("isFinite")) { return gs.get("isFinite"); }
128                     if (name.equals("NaN")) { return gs.get("NaN"); }
129                     if (name.equals("Infinity")) { return gs.get("Infinity"); }
130                     return super.get(name);
131                 }
132             };
133         
134         public static final JS ibexString = new JS() {
135                 private JS gs = new JSScope.Global();
136                 public void put(Object key, Object val) { }
137                 public Object get(Object name) throws JSExn {
138                     if (name.equals("parseInt")) { return gs.get("parseInt"); }
139                     if (name.equals("parseFloat")) { return gs.get("parseFloat"); }
140                     if (name.equals("decodeURI")) { return gs.get("decodeURI"); }
141                     if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); }
142                     if (name.equals("encodeURI")) { return gs.get("encodeURI"); }
143                     if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); }
144                     if (name.equals("escape")) { return gs.get("escape"); }
145                     if (name.equals("unescape")) { return gs.get("unescape"); }
146                     if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); }
147                     return null;
148                 }
149             };
150     }
151 }