fixed JS bug in ibex.mail.forward()
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index f183aa1..3a87567 100644 (file)
@@ -32,14 +32,17 @@ public class Script extends Target {
     private String filePath = null;
     public Script(String filePath) throws JSExn, IOException {
         this.filePath = filePath;
-        js = JSU.cloneWithNewGlobalScope(JSU.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
+        js = JSU.cloneWithNewGlobalScope(JSU.fromReader(filePath, 1, new InputStreamReader(new FileInputStream(filePath))),
                                          new ScriptScope()); }
 
     private class ScriptScope extends JS.Immutable {
         ScriptEnv env = new ScriptEnv();
-        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;
         }
     }
@@ -89,8 +92,7 @@ public class Script extends Target {
             Sub(String key) { this.key = key; }
             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 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);
             }
@@ -125,37 +127,45 @@ public class Script extends Target {
             return super.get(name);
         }
 
-        public JS call(JS name, JS[] args) 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(args); }
-                if (name.equals("mail.send")) {
+                if (name.equals("mail.send") || name.equals("send")) {
                     JS m = (JS)a;
                     StringBuffer headers = new StringBuffer();
                     String body = "";
-                    Address from = null, to = null;
-                    for(JS.Enumeration e = m.keys(); e.hasNext();) {
+                    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(key + ": " + val + "\r\n");
+                        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));
                     }
-                    Message message = Message.newMessage(new org.ibex.io.Stream(headers.toString() + "\r\n" + body), from, to);
+                    if (envelopeTo == null) envelopeTo = to;
+                    if (envelopeFrom == null) envelopeFrom = from;
+                    Message message = Message.newMessage(new org.ibex.io.Stream(headers.toString() + "\r\n" + body),
+                                                         envelopeFrom,
+                                                         envelopeTo);
                     //org.ibex.mail.protocol.SMTP.Outgoing.accept(message);
                     boolean ok = org.ibex.mail.protocol.SMTP.Outgoing.attempt(message);
                     if (!ok) throw new JSExn("SMTP server rejected message");
                     return JSU.T;
                 }
-                if (name.equals("mail.forward2")) {
+                if (name.equals("mail.forward2") || name.equals("forward2")) {
                     try {
                         Message m2 = Message.newMessage(new org.ibex.io.Stream(m.toString()),
                                                         m.envelopeFrom,
-                                                        new Address(a.toString()));
+                                                        new Address(JSU.toString(a)));
                         org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
                     } catch (Exception e) {
                         Log.warn(this, e);
@@ -163,22 +173,22 @@ public class Script extends Target {
                     }
                     return null;
                 }
-                if (name.equals("mail.forward")) { return new Target() {
+                if (name.equals("mail.forward") || name.equals("forward")) { return new Target() {
                         public void accept(Message m) throws MailException {
                             try {
                                 Message m2 = Message.newMessage(new org.ibex.io.Stream(m.toString()),
-                                                         m.envelopeFrom,
-                                                         new Address(a.toString()));
+                                                                m.envelopeFrom,
+                                                                new Address(JSU.toString(a)));
                                 org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
                             } catch (Exception e) {
                                 throw new MailException(e.toString());
                             }
                         }
                     }; }
-                if (name.equals("log.debug")) {    JSU.debug(a== null ? "**null**" : a.toString()); return null; }
-                if (name.equals("log.info")) {     JSU.info(a== null ? "**null**" : a.toString()); return null; }
-                if (name.equals("log.warn")) {     JSU.warn(a== null ? "**null**" : a.toString()); return null; }
-                if (name.equals("log.error")) {    JSU.error(a== null ? "**null**" : a.toString()); return null; }
+                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); }
@@ -190,9 +200,9 @@ public class Script extends Target {
                 Log.warn(this, "ibex."+name+"() threw: " + e);
                 Log.warn(this, e);
                 if (e instanceof JSExn) throw ((JSExn)e);
-                throw new JSExn("invalid argument for ibex object method "+name+"()");
+                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 final JSMath ibexMath = new JSMath() {