minor fixups to Script
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import org.ibex.js.*;
7 import org.ibex.util.*;
8 import org.ibex.mail.*;
9 import org.ibex.mail.filter.*;
10 import org.ibex.mail.target.*;
11 import java.io.*;
12 import java.util.*;
13
14 public class Script extends Target {
15
16     private static Script root = null;
17     private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
18     public static Script root() {
19         try {
20             if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
21             return root;
22         } catch (Exception e) {
23             Log.error(Script.class, e);
24             return null;
25         }
26     }
27
28     final JS js;
29     private Message m = null;
30     private String filePath = null;
31     public Script(String filePath) throws JSExn, IOException {
32         this.filePath = filePath;
33         js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
34                                         new ScriptScope()); }
35
36     private class ScriptScope extends JSScope {
37         ScriptEnv env = new ScriptEnv();
38         public ScriptScope() { super(null); }
39         public Object get(Object o) {
40             if (o.equals("m")) return m;
41             if (o.equals("ibex")) return env;
42             return null;
43         }
44     }
45
46     public void accept(Message m) throws IOException, MailException {
47         try {
48             new Script(filePath).reallyAccept(m);
49         } catch (JSExn e) {
50             Log.error(this, e);
51             throw new MailException(e.toString());
52         }
53     }
54
55     private synchronized void reallyAccept(Message m) throws IOException, MailException, JSExn {
56         this.m = m;
57         try {
58             Object ret = js.call(m, null, null, null, 1);
59             Log.debug(this, "configuration script returned " + ret);
60             if (ret == null) throw new IOException("configuration script returned null");
61             if (ret instanceof Target)      ((Target)ret).accept(m);
62             //else if (ret instanceof Filter) ((Filter)ret).process(m);
63             else throw new IOException("configuration script returned a " + ret.getClass().getName());
64         } catch (JSExn e) {
65             Log.warn(this, e);
66             throw new IOException("configuration script threw an exception");
67         }
68     }
69
70     // FIXME: this should extend org.ibex.core.Ibex
71     public class ScriptEnv extends JS {
72
73         private PropertyFile prefs = null;
74             /*
75         static {
76             try {
77                 // FIXME
78                 prefs = new PropertyFile(new File("/etc/org.ibex.mail.properties"));
79             } catch (IOException e) {
80                 Log.error(ScriptEnv.class, e);
81             }
82         }
83             */
84
85         /** lets us put multi-level get/put/call keys all in the same method */
86         private class Sub extends JS {
87             String key;
88             Sub(String key) { this.key = key; }
89             public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); }
90             public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); }
91             public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
92                 return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs);
93             }
94             public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
95                 return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
96             }
97         }
98         private Sub getSub(String s) { return new Sub(s); }
99
100         public Object get(Object name_) throws JSExn {
101             String name = (String)name_;
102             if (name.equals("math")) { return ibexMath; }
103             if (name.equals("string")) { return ibexString; }
104             if (name.equals("date")) { return METHOD; }
105             if (name.equals("regexp")) { return METHOD; }
106             if (name.equals("log")) { return getSub("log"); }
107             if (name.equals("log.debug")) { return METHOD; }
108             if (name.equals("log.info")) { return METHOD; }
109             if (name.equals("log.warn")) { return METHOD; }
110             if (name.equals("log.error")) { return METHOD; }
111             if (name.equals("list")) { return getSub("list"); }
112             if (name.startsWith("list.")) { return org.ibex.mail.List.getList(name.substring(5)); }
113             if (name.equals("mail")) { return getSub("mail"); }
114             if (name.equals("mail.forward")) { return METHOD; }
115             if (name.equals("mail.forward2")) { return METHOD; }
116             if (name.equals("mail.send")) { return METHOD; }
117             if (name.equals("mail.my")) { return getSub("mail.my"); }
118             if (name.equals("mail.my.prefs")) {
119                 try {
120                     return new org.ibex.js.Directory(new File("/etc/org.ibex.mail.prefs"));
121                 } catch (IOException e) {
122                     throw new JSExn(e.toString());
123                 }
124             }
125             if (name.equals("mail.my.mailbox")) {
126                 FileBasedMailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
127                 return root.slash("user", true).slash("megacz", true).slash("newmail", true);
128             }
129             return super.get(name);
130         }
131
132         public Object callMethod(Object name, final Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
133             try {
134                 if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
135                 if (name.equals("mail.send")) {
136                     JS m = (JS)a;
137                     StringBuffer headers = new StringBuffer();
138                     String body = "";
139                     Address from = null, to = null;
140                     for(Enumeration e = m.keys(); e.hasMoreElements();) {
141                         String key = (String)e.nextElement();
142                         String val = m.get(key) == null ? null : m.get(key).toString();
143                         if ("body".equals(key)) body = val;
144                         else headers.append(key + ": " + val + "\r\n");
145                         if ("from".equalsIgnoreCase(key)) from = Address.parse(val);
146                         if ("to".equalsIgnoreCase(key)) to = Address.parse(val);
147                     }
148                     Message message = Message.newMessage(new org.ibex.io.Stream(headers.toString() + "\r\n" + body), from, to);
149                     //org.ibex.mail.protocol.SMTP.Outgoing.accept(message);
150                     boolean ok = org.ibex.mail.protocol.SMTP.Outgoing.attempt(message);
151                     if (!ok) throw new JSExn("SMTP server rejected message");
152                     return T;
153                 }
154                 if (name.equals("mail.forward2")) {
155                     try {
156                         Message m2 = Message.newMessage(new org.ibex.io.Stream(m.toString()),
157                                                         m.envelopeFrom,
158                                                         new Address(a.toString()));
159                         org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
160                     } catch (Exception e) {
161                         Log.warn(this, e);
162                         throw new JSExn(e.toString());
163                     }
164                     return null;
165                 }
166                 if (name.equals("mail.forward")) { return new Target() {
167                         public void accept(Message m) throws MailException {
168                             try {
169                                 Message m2 = Message.newMessage(new org.ibex.io.Stream(m.toString()),
170                                                          m.envelopeFrom,
171                                                          new Address(a.toString()));
172                                 org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
173                             } catch (Exception e) {
174                                 throw new MailException(e.toString());
175                             }
176                         }
177                     }; }
178                 if (name.equals("log.debug")) {    JS.debug(a== null ? "**null**" : a.toString()); return null; }
179                 if (name.equals("log.info")) {     JS.info(a== null ? "**null**" : a.toString()); return null; }
180                 if (name.equals("log.warn")) {     JS.warn(a== null ? "**null**" : a.toString()); return null; }
181                 if (name.equals("log.error")) {    JS.error(a== null ? "**null**" : a.toString()); return null; }
182                 switch (nargs) {
183                 case 1:
184                     if (name.equals("regexp")) {return new JSRegexp(a, null); }
185                     break;
186                 case 2:
187                     if (name.equals("regexp")) {return new JSRegexp(a, b); }
188                 }
189             } catch (Exception e) {
190                 Log.warn(this, "ibex."+name+"() threw: " + e);
191                 Log.warn(this, e);
192                 if (e instanceof JSExn) throw ((JSExn)e);
193                 throw new JSExn("invalid argument for ibex object method "+name+"()");
194             }
195             throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
196         }
197
198         public final JSMath ibexMath = new JSMath() {
199                 private JS gs = new JSScope.Global();
200                 public Object get(Object name) throws JSExn {
201                     if (name.equals("isNaN")) { return gs.get("isNaN"); }
202                     if (name.equals("isFinite")) { return gs.get("isFinite"); }
203                     if (name.equals("NaN")) { return gs.get("NaN"); }
204                     if (name.equals("Infinity")) { return gs.get("Infinity"); }
205                     return super.get(name);
206                 }
207             };
208         
209         public final JS ibexString = new JS() {
210                 private JS gs = new JSScope.Global();
211                 public void put(Object key, Object val) { }
212                 public Object get(Object name) throws JSExn {
213                     if (name.equals("parseInt")) { return gs.get("parseInt"); }
214                     if (name.equals("parseFloat")) { return gs.get("parseFloat"); }
215                     if (name.equals("decodeURI")) { return gs.get("decodeURI"); }
216                     if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); }
217                     if (name.equals("encodeURI")) { return gs.get("encodeURI"); }
218                     if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); }
219                     if (name.equals("escape")) { return gs.get("escape"); }
220                     if (name.equals("unescape")) { return gs.get("unescape"); }
221                     if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); }
222                     return null;
223                 }
224             };
225     }
226 }