added url.encode and mail.error
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index 6a7395a..d493384 100644 (file)
@@ -11,7 +11,7 @@ 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();
 
@@ -62,6 +62,7 @@ public class Script extends Target {
             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");
+            while (ret instanceof JSReflection.Wrapper) ret = ((JSReflection.Wrapper)ret).unwrap();
             if (ret instanceof Target)      ((Target)ret).accept(m);
             //else if (ret instanceof Filter) ((Filter)ret).process(m);
             else throw new IOException("configuration script returned a " + ret.getClass().getName());
@@ -111,6 +112,8 @@ public class Script extends Target {
             case "log.warn": return METHOD;
             case "log.error": return METHOD;
             case "list": return getSub("list");
+            case "url": return getSub("url");
+            case "url.encode": return METHOD;
             case "mail": return getSub("mail");
             case "mail.forward": return METHOD;
             case "mail.forward2": return METHOD;
@@ -119,6 +122,7 @@ public class Script extends Target {
             case "mail.later": return Later.instance;
             case "mail.drop": return Drop.instance;
             case "mail.bounce": return METHOD;
+            case "mail.error": 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"));
@@ -126,8 +130,8 @@ public class Script extends Target {
             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
-            if (JSU.toString(name).startsWith("list.")) { return MailingList.getList(JSU.toString(name).substring(5)); }
             return super.get(name);
         }
 
@@ -138,6 +142,8 @@ public class Script extends Target {
             final int nargs = args.length;
             String name = JSU.toString(name0);
             try {
+                if (name.equals("url.encode")) return JSU.S(java.net.URLEncoder.encode(JSU.toString(args[0])));
+                if (name.equals("mail.list")) return JSReflection.wrap(MailingList.getMailingList(JSU.toString(args[0])));
                 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");
@@ -179,7 +185,7 @@ public class Script extends Target {
                     return JSU.B(ok);
                 }
                 if (name.equals("mail.bounce")) {
-                    return new Target() {
+                    return new JSTarget() {
                             public void accept(Message m) throws MailException {
                                 try {
                                     Message m2 = m.bounce(JSU.toString(a));
@@ -207,6 +213,8 @@ public class Script extends Target {
                     org.ibex.mail.protocol.SMTP.Outgoing.attempt(m2);
                     return Drop.instance;
                 }
+                if (name.equals("mail.error"))
+                    throw new MailException.Malformed(JSU.toString(a));
                 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; }
@@ -218,6 +226,7 @@ public class Script extends Target {
                 case 2:
                     if (name.equals("regexp")) {return new JSRegexp(a, b); }
                 }
+            } catch (MailException e) { throw e;
             } catch (Exception e) {
                 Log.warn(this, "ibex."+name+"() threw: " + e);
                 Log.warn(this, e);
@@ -258,4 +267,6 @@ public class Script extends Target {
                 }
             };
     }
+
+    private static abstract class JSTarget extends JS.Obj implements Target { }
 }