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.
5 package org.ibex.mail.target;
7 import org.ibex.util.*;
8 import org.ibex.mail.*;
9 import org.ibex.mail.filter.*;
10 import org.ibex.mail.target.*;
14 public class Script extends JS.Obj implements Target {
16 private static final JS.Method METHOD = new JS.Method();
18 private static Script root = null;
19 private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
20 public static Script root() {
22 if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
24 } catch (Exception e) {
25 Log.error(Script.class, e);
31 private Message m = null;
32 private String filePath = null;
33 public Script(String filePath) throws JSExn, IOException {
34 this.filePath = filePath;
35 js = JSU.cloneWithNewGlobalScope(JSU.fromReader(filePath, 1, new InputStreamReader(new FileInputStream(filePath))),
38 private class ScriptScope extends JS.Immutable {
39 ScriptEnv env = new ScriptEnv();
40 public JS get(JS name) throws JSExn {
43 case "ibex": return env;
50 public void accept(Message m) throws IOException, MailException {
52 new Script(filePath).reallyAccept(m);
55 throw new MailException(e.toString());
59 private synchronized void reallyAccept(Message m) throws IOException, MailException, JSExn {
62 Object ret = js.call(null, new JS[] { m });
63 Log.debug(this, "configuration script returned " + ret);
64 if (ret == null) throw new IOException("configuration script returned null");
65 while (ret instanceof JSReflection.Wrapper) ret = ((JSReflection.Wrapper)ret).unwrap();
66 if (ret instanceof Target) ((Target)ret).accept(m);
67 //else if (ret instanceof Filter) ((Filter)ret).process(m);
68 else throw new IOException("configuration script returned a " + ret.getClass().getName());
71 throw new IOException("configuration script threw an exception");
75 // FIXME: this should extend org.ibex.core.Ibex
76 public class ScriptEnv extends JS.Obj {
78 private PropertyFile prefs = null;
83 prefs = new PropertyFile(new File("/etc/org.ibex.mail.properties"));
84 } catch (IOException e) {
85 Log.error(ScriptEnv.class, e);
90 /** lets us put multi-level get/put/call keys all in the same method */
91 private class Sub extends JS.Immutable {
93 Sub(String key) { this.key = key; }
94 public void put(JS key, JS val) throws JSExn {
95 ScriptEnv.this.put(JSU.S(this.key + "." + JSU.toString(key)), val); }
96 public JS get(JS key) throws JSExn { return ScriptEnv.this.get(JSU.S(this.key + "." + JSU.toString(key))); }
97 public JS call(JS method, JS[] args) throws JSExn {
98 return ScriptEnv.this.call(JSU.S(this.key + "." + JSU.toString(method)), args);
101 private Sub getSub(String s) { return new Sub(s); }
103 public JS get(JS name) throws JSExn {
105 case "math": return ibexMath;
106 case "string": return ibexString;
107 case "date": return METHOD;
108 case "regexp": return METHOD;
109 case "log": return getSub("log");
110 case "log.debug": return METHOD;
111 case "log.info": return METHOD;
112 case "log.warn": return METHOD;
113 case "log.error": return METHOD;
114 case "list": return getSub("list");
115 case "url": return getSub("url");
116 case "url.encode": return METHOD;
117 case "mail": return getSub("mail");
118 case "mail.forward": return METHOD;
119 case "mail.forward2": return METHOD;
120 case "mail.send": return METHOD;
121 case "mail.attempt": return METHOD;
122 case "mail.later": return Later.instance;
123 case "mail.drop": return Drop.instance;
124 case "mail.bounce": return METHOD;
125 case "mail.error": return METHOD;
126 case "mail.my": return getSub("mail.my");
127 case "mail.my.prefs": try {
128 return new org.ibex.js.Directory(new File("/etc/org.ibex.mail.prefs"));
129 } catch (IOException e) { throw new JSExn(e.toString()); }
130 case "mail.my.mailbox":
131 FileBasedMailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
132 return root.slash("user", true).slash("megacz", true).slash("newmail", true);
133 case "mail.list": return METHOD;
135 return super.get(name);
138 public JS call(JS name0, JS[] args) throws JSExn {
139 final JS a = args.length >= 1 ? args[0] : null;
140 final JS b = args.length >= 2 ? args[1] : null;
141 final JS c = args.length >= 3 ? args[2] : null;
142 final int nargs = args.length;
143 String name = JSU.toString(name0);
145 if (name.equals("url.encode")) return JSU.S(java.net.URLEncoder.encode(JSU.toString(args[0])));
146 if (name.equals("mail.list")) return JSReflection.wrap(MailingList.getMailingList(JSU.toString(args[0])));
147 if (name.equals("date")) { return new JSDate(args); }
148 if (name.equals("mail.send") || name.equals("send") || name.equals("mail.attempt") || name.equals("attempt")) {
149 boolean attempt = name.equals("mail.attempt") || name.equals("attempt");
151 StringBuffer headers = new StringBuffer();
153 Address from = null, to = null, envelopeFrom = null, envelopeTo = null;
154 JS.Enumeration e = m.keys();
155 for(; e.hasNext();) {
156 JS key = (JS)e.next();
157 JS val = m.get(key) == null ? null : m.get(key);
158 if ("body".equalsIgnoreCase(JSU.toString(key))) body = JSU.toString(val);
159 else headers.append(JSU.toString(key) + ": " + JSU.toString(val) + "\r\n");
160 if ("from".equalsIgnoreCase(JSU.toString(key))) from = Address.parse(JSU.toString(val));
161 if ("to".equalsIgnoreCase(JSU.toString(key))) to = Address.parse(JSU.toString(val));
162 if ("envelopeFrom".equalsIgnoreCase(JSU.toString(key))) envelopeFrom = Address.parse(JSU.toString(val));
163 if ("envelopeTo".equalsIgnoreCase(JSU.toString(key))) envelopeTo = Address.parse(JSU.toString(val));
165 if (envelopeTo == null) envelopeTo = to;
166 if (envelopeFrom == null) envelopeFrom = from;
168 Message.newMessage(new org.ibex.io.Fountain.StringFountain(headers.toString() + "\r\n" + body),
176 org.ibex.mail.protocol.SMTP.Outgoing.attempt(message);
178 org.ibex.mail.protocol.SMTP.Outgoing.accept(message);
181 } catch (Exception ex) {
182 if (!attempt) Log.warn(this, ex);
184 if (!ok && !attempt) throw new JSExn("SMTP server rejected message");
187 if (name.equals("mail.bounce")) {
188 return new JSTarget() {
189 public void accept(Message m) throws MailException {
191 Message m2 = m.bounce(JSU.toString(a));
192 org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
193 Log.error(this, "BOUNCING! " + m2.summary());
194 } catch (Exception e) {
199 if (name.equals("mail.forward2") || name.equals("forward2")) {
201 Message m2 = Message.newMessage(new org.ibex.io.Fountain.StringFountain(m.toString()),
203 new Address(JSU.toString(a)));
204 org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
205 } catch (Exception e) {
207 throw new JSExn(e.toString());
211 if (name.equals("mail.forward") || name.equals("forward")) {
212 Message m2 = Message.newMessage(Script.this.m, Script.this.m.envelopeFrom, new Address(JSU.toString(a)));
213 org.ibex.mail.protocol.SMTP.Outgoing.attempt(m2);
214 return Drop.instance;
216 if (name.equals("mail.error"))
217 throw new MailException.Malformed(JSU.toString(a));
218 if (name.equals("log.debug") || name.equals("debug")) { JSU.debug(a== null ? "**null**" : JSU.toString(a)); return null; }
219 if (name.equals("log.info") || name.equals("info")) { JSU.info(a== null ? "**null**" : JSU.toString(a)); return null; }
220 if (name.equals("log.warn") || name.equals("warn")) { JSU.warn(a== null ? "**null**" : JSU.toString(a)); return null; }
221 if (name.equals("log.error") || name.equals("error")) { JSU.error(a== null ? "**null**" : JSU.toString(a)); return null; }
224 if (name.equals("regexp")) {return new JSRegexp(a, null); }
227 if (name.equals("regexp")) {return new JSRegexp(a, b); }
229 } catch (MailException e) { throw e;
230 } catch (Exception e) {
231 Log.warn(this, "ibex."+name+"() threw: " + e);
233 if (e instanceof JSExn) throw ((JSExn)e);
234 throw new JSExn("invalid argument for ibex object method "+JSU.toString(name0)+"()");
236 throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+JSU.toString(name0)+"()");
239 public final JSMath ibexMath = new JSMath() {
240 public JS get(JS name) throws JSExn {
243 case "isNaN": return gs.get(name);
244 case "isFinite": return gs.get(name);
245 case "NaN": return gs.get(name);
246 case "Infinity": return gs.get(name);
252 public final JS ibexString = new JS.Immutable() {
253 public JS get(JS name) throws JSExn {
256 case "parseInt": return gs.get(JSU.S("parseInt"));
257 case "parseFloat": return gs.get(JSU.S("parseFloat"));
258 case "decodeURI": return gs.get(JSU.S("decodeURI"));
259 case "decodeURIComponent": return gs.get(JSU.S("decodeURIComponent"));
260 case "encodeURI": return gs.get(JSU.S("encodeURI"));
261 case "encodeURIComponent": return gs.get(JSU.S("encodeURIComponent"));
262 case "escape": return gs.get(JSU.S("escape"));
263 case "unescape": return gs.get(JSU.S("unescape"));
264 case "fromCharCode": return gs.get(JSU.S("stringFromCharCode"));
271 private static abstract class JSTarget extends JS.Obj implements Target { }