X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2Ftarget%2FScript.java;h=b112b91b83a0c66de9a210c8ccae70f7b7453491;hb=8c4c8f2fe9140050635f237122bab4528a399b9c;hp=65746b3e0fc8b5f6104aade46ff53cad1468994f;hpb=a18d27681a90b7630234b278d2e09a1a1040eb8a;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/target/Script.java b/src/org/ibex/mail/target/Script.java index 65746b3..b112b91 100644 --- a/src/org/ibex/mail/target/Script.java +++ b/src/org/ibex/mail/target/Script.java @@ -1,5 +1,8 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] -package org.ibex.mail; +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + +package org.ibex.mail.target; import org.ibex.js.*; import org.ibex.util.*; import org.ibex.mail.*; @@ -8,7 +11,9 @@ import org.ibex.mail.target.*; import java.io.*; import java.util.*; -public class Script extends Target { +public class Script extends JS.Obj implements Target { + + private static final JS.Method METHOD = new JS.Method(); private static Script root = null; private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf"; @@ -24,24 +29,38 @@ public class Script extends Target { final JS js; private Message m = null; + private String filePath = null; public Script(String filePath) throws JSExn, IOException { - js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))), - new ScriptScope()); } + this.filePath = filePath; + js = JSU.cloneWithNewGlobalScope(JSU.fromReader(filePath, 1, new InputStreamReader(new FileInputStream(filePath))), + new ScriptScope()); } - private class ScriptScope extends JSScope { + private class ScriptScope extends JS.Immutable { ScriptEnv env = new ScriptEnv(); - public ScriptScope() { super(null); } - public Object get(Object o) { - if (o.equals("m")) return m; - if (o.equals("ibex")) return env; + public JS get(JS name) throws JSExn { + //#jsswitch(name) + case "m": return m; + case "ibex": return env; + default: return null; + //#end return null; } } - public synchronized void accept(Message m) throws IOException, MailException { + public void accept(Message m) throws IOException, MailException { + try { + new Script(filePath).reallyAccept(m); + } catch (JSExn e) { + Log.error(this, e); + throw new MailException(e.toString()); + } + } + + private synchronized void reallyAccept(Message m) throws IOException, MailException, JSExn { this.m = m; try { - Object ret = js.call(m, null, null, null, 1); + Object ret = js.call(null, new JS[] { m }); + Log.debug(this, "configuration script returned " + ret); if (ret == null) throw new IOException("configuration script returned null"); if (ret instanceof Target) ((Target)ret).accept(m); //else if (ret instanceof Filter) ((Filter)ret).process(m); @@ -53,43 +72,146 @@ public class Script extends Target { } // FIXME: this should extend org.ibex.core.Ibex - public static class ScriptEnv extends JS { + public class ScriptEnv extends JS.Obj { + + private PropertyFile prefs = null; + /* + static { + try { + // FIXME + prefs = new PropertyFile(new File("/etc/org.ibex.mail.properties")); + } catch (IOException e) { + Log.error(ScriptEnv.class, e); + } + } + */ /** lets us put multi-level get/put/call keys all in the same method */ - private class Sub extends JS { + private class Sub extends JS.Immutable { String key; Sub(String key) { this.key = key; } - public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); } - public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); } - public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { - return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs); - } - public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { - return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs); + public void put(JS key, JS val) throws JSExn { + ScriptEnv.this.put(JSU.S(this.key + "." + JSU.toString(key)), val); } + public JS get(JS key) throws JSExn { return ScriptEnv.this.get(JSU.S(this.key + "." + JSU.toString(key))); } + public JS call(JS method, JS[] args) throws JSExn { + return ScriptEnv.this.call(JSU.S(this.key + "." + JSU.toString(method)), args); } } private Sub getSub(String s) { return new Sub(s); } - public Object get(Object name) throws JSExn { - if (name.equals("math")) { return ibexMath; } - if (name.equals("string")) { return ibexString; } - if (name.equals("date")) { return METHOD; } - if (name.equals("regexp")) { return METHOD; } - if (name.equals("log")) { return getSub("log"); } - if (name.equals("log.debug")) { return METHOD; } - if (name.equals("log.info")) { return METHOD; } - if (name.equals("log.warn")) { return METHOD; } - if (name.equals("log.error")) { return METHOD; } + public JS get(JS name) throws JSExn { + //#jsswitch(name) + case "math": return ibexMath; + case "string": return ibexString; + case "date": return METHOD; + case "regexp": return METHOD; + case "log": return getSub("log"); + case "log.debug": return METHOD; + case "log.info": return METHOD; + case "log.warn": return METHOD; + case "log.error": return METHOD; + case "list": return getSub("list"); + case "mail": return getSub("mail"); + case "mail.forward": return METHOD; + case "mail.forward2": return METHOD; + case "mail.send": return METHOD; + case "mail.attempt": return METHOD; + case "mail.later": return Later.instance; + case "mail.drop": return Drop.instance; + case "mail.bounce": return METHOD; + case "mail.my": return getSub("mail.my"); + case "mail.my.prefs": try { + return new org.ibex.js.Directory(new File("/etc/org.ibex.mail.prefs")); + } catch (IOException e) { throw new JSExn(e.toString()); } + case "mail.my.mailbox": + FileBasedMailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true); + return root.slash("user", true).slash("megacz", true).slash("newmail", true); + case "mail.list": return METHOD; + //#end return super.get(name); } - public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn { + public JS call(JS name0, JS[] args) throws JSExn { + final JS a = args.length >= 1 ? args[0] : null; + final JS b = args.length >= 2 ? args[1] : null; + final JS c = args.length >= 3 ? args[2] : null; + final int nargs = args.length; + String name = JSU.toString(name0); try { - if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); } - if (name.equals("log.debug")) { JS.debug(a== null ? "**null**" : a.toString()); return null; } - if (name.equals("log.info")) { JS.info(a== null ? "**null**" : a.toString()); return null; } - if (name.equals("log.warn")) { JS.warn(a== null ? "**null**" : a.toString()); return null; } - if (name.equals("log.error")) { JS.error(a== null ? "**null**" : a.toString()); return null; } + if (name.equals("mail.list")) return MailingList.getMailingList(JS.toString(args[0])).acceptor; + if (name.equals("date")) { return new JSDate(args); } + if (name.equals("mail.send") || name.equals("send") || name.equals("mail.attempt") || name.equals("attempt")) { + boolean attempt = name.equals("mail.attempt") || name.equals("attempt"); + JS m = (JS)a; + StringBuffer headers = new StringBuffer(); + String body = ""; + Address from = null, to = null, envelopeFrom = null, envelopeTo = null; + JS.Enumeration e = m.keys(); + for(; e.hasNext();) { + JS key = (JS)e.next(); + JS val = m.get(key) == null ? null : m.get(key); + if ("body".equalsIgnoreCase(JSU.toString(key))) body = JSU.toString(val); + else headers.append(JSU.toString(key) + ": " + JSU.toString(val) + "\r\n"); + if ("from".equalsIgnoreCase(JSU.toString(key))) from = Address.parse(JSU.toString(val)); + if ("to".equalsIgnoreCase(JSU.toString(key))) to = Address.parse(JSU.toString(val)); + if ("envelopeFrom".equalsIgnoreCase(JSU.toString(key))) envelopeFrom = Address.parse(JSU.toString(val)); + if ("envelopeTo".equalsIgnoreCase(JSU.toString(key))) envelopeTo = Address.parse(JSU.toString(val)); + } + if (envelopeTo == null) envelopeTo = to; + if (envelopeFrom == null) envelopeFrom = from; + Message message = + Message.newMessage(new org.ibex.io.Fountain.StringFountain(headers.toString() + "\r\n" + body), + envelopeFrom, + envelopeTo + ); + + boolean ok = false; + try { + if (attempt) { + org.ibex.mail.protocol.SMTP.Outgoing.attempt(message); + } else { + org.ibex.mail.protocol.SMTP.Outgoing.accept(message); + } + ok = true; + } catch (Exception ex) { + if (!attempt) Log.warn(this, ex); + } + if (!ok && !attempt) throw new JSExn("SMTP server rejected message"); + return JSU.B(ok); + } + if (name.equals("mail.bounce")) { + return new JSTarget() { + public void accept(Message m) throws MailException { + try { + Message m2 = m.bounce(JSU.toString(a)); + org.ibex.mail.protocol.SMTP.Outgoing.accept(m2); + Log.error(this, "BOUNCING! " + m2.summary()); + } catch (Exception e) { + Log.warn(this, e); + } + } }; + } + if (name.equals("mail.forward2") || name.equals("forward2")) { + try { + Message m2 = Message.newMessage(new org.ibex.io.Fountain.StringFountain(m.toString()), + m.envelopeFrom, + new Address(JSU.toString(a))); + org.ibex.mail.protocol.SMTP.Outgoing.accept(m2); + } catch (Exception e) { + Log.warn(this, e); + throw new JSExn(e.toString()); + } + return null; + } + if (name.equals("mail.forward") || name.equals("forward")) { + Message m2 = Message.newMessage(Script.this.m, Script.this.m.envelopeFrom, new Address(JSU.toString(a))); + org.ibex.mail.protocol.SMTP.Outgoing.attempt(m2); + return Drop.instance; + } + if (name.equals("log.debug") || name.equals("debug")) { JSU.debug(a== null ? "**null**" : JSU.toString(a)); return null; } + if (name.equals("log.info") || name.equals("info")) { JSU.info(a== null ? "**null**" : JSU.toString(a)); return null; } + if (name.equals("log.warn") || name.equals("warn")) { JSU.warn(a== null ? "**null**" : JSU.toString(a)); return null; } + if (name.equals("log.error") || name.equals("error")) { JSU.error(a== null ? "**null**" : JSU.toString(a)); return null; } switch (nargs) { case 1: if (name.equals("regexp")) {return new JSRegexp(a, null); } @@ -97,39 +219,46 @@ public class Script extends Target { case 2: if (name.equals("regexp")) {return new JSRegexp(a, b); } } - } catch (RuntimeException e) { + } catch (Exception e) { Log.warn(this, "ibex."+name+"() threw: " + e); - throw new JSExn("invalid argument for ibex object method "+name+"()"); + Log.warn(this, e); + if (e instanceof JSExn) throw ((JSExn)e); + throw new JSExn("invalid argument for ibex object method "+JSU.toString(name0)+"()"); } - throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()"); + throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+JSU.toString(name0)+"()"); } - public static final JSMath ibexMath = new JSMath() { - private JS gs = new JSScope.Global(); - public Object get(Object name) throws JSExn { - if (name.equals("isNaN")) { return gs.get("isNaN"); } - if (name.equals("isFinite")) { return gs.get("isFinite"); } - if (name.equals("NaN")) { return gs.get("NaN"); } - if (name.equals("Infinity")) { return gs.get("Infinity"); } - return super.get(name); + public final JSMath ibexMath = new JSMath() { + public JS get(JS name) throws JSExn { + // FIXME!!! + /* + case "isNaN": return gs.get(name); + case "isFinite": return gs.get(name); + case "NaN": return gs.get(name); + case "Infinity": return gs.get(name); + */ + return null; } }; - public static final JS ibexString = new JS() { - private JS gs = new JSScope.Global(); - public void put(Object key, Object val) { } - public Object get(Object name) throws JSExn { - if (name.equals("parseInt")) { return gs.get("parseInt"); } - if (name.equals("parseFloat")) { return gs.get("parseFloat"); } - if (name.equals("decodeURI")) { return gs.get("decodeURI"); } - if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); } - if (name.equals("encodeURI")) { return gs.get("encodeURI"); } - if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); } - if (name.equals("escape")) { return gs.get("escape"); } - if (name.equals("unescape")) { return gs.get("unescape"); } - if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); } + public final JS ibexString = new JS.Immutable() { + public JS get(JS name) throws JSExn { + // FIXME!!! + /* + case "parseInt": return gs.get(JSU.S("parseInt")); + case "parseFloat": return gs.get(JSU.S("parseFloat")); + case "decodeURI": return gs.get(JSU.S("decodeURI")); + case "decodeURIComponent": return gs.get(JSU.S("decodeURIComponent")); + case "encodeURI": return gs.get(JSU.S("encodeURI")); + case "encodeURIComponent": return gs.get(JSU.S("encodeURIComponent")); + case "escape": return gs.get(JSU.S("escape")); + case "unescape": return gs.get(JSU.S("unescape")); + case "fromCharCode": return gs.get(JSU.S("stringFromCharCode")); + */ return null; } }; } + + private static abstract class JSTarget extends JS.Obj implements Target { } }