bogus patch
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index c742cc4..d7328b4 100644 (file)
@@ -41,7 +41,9 @@ public class Script extends Target {
     public synchronized void accept(Message m) throws IOException, MailException {
         this.m = m;
         try {
+            Log.error(this, "invoking config...");
             Object ret = js.call(m, null, null, null, 1);
+            Log.error(this, "config 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);
@@ -93,17 +95,51 @@ public class Script extends Target {
             if (name.equals("log.warn")) { return METHOD; }
             if (name.equals("log.error")) { return METHOD; }
             if (name.equals("mail")) { return getSub("mail"); }
+            if (name.equals("mail.forward")) { return METHOD; }
+            if (name.equals("mail.send")) { return METHOD; }
             if (name.equals("mail.my")) { return getSub("mail.my"); }
-            if (name.equals("mail.my.prefs")) { return prefs; }
+            if (name.equals("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());
+                }
+            }
             if (name.equals("mail.my.mailbox")) {
                return FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true).slash("user", true).slash("megacz", true).slash("newmail", true);
            }
             return super.get(name);
         }
 
-        public Object callMethod(Object name, Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
+        public Object callMethod(Object name, final Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
             try {
                 if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
+                if (name.equals("mail.send")) {
+                    JS m = (JS)a;
+                    StringBuffer headers = new StringBuffer();
+                    String body = "";
+                    for(Enumeration e = m.keys(); e.hasMoreElements();) {
+                        String key = (String)e.nextElement();
+                        String val = m.get(key).toString();
+                        if (key.equals("body")) body = val;
+                        else headers.append(key + ": " + val + "\r\n");
+                    }
+                    Message message = new Message(null, null, new org.ibex.io.Stream(headers.toString() + "\r\n" + body));
+                    org.ibex.mail.protocol.SMTP.Outgoing.accept(message);
+                    return T;
+                }
+                if (name.equals("mail.forward")) { return new Target() {
+                        public void accept(Message m) throws MailException {
+                            try {
+                                Message m2 = new Message(m.envelopeFrom,
+                                                         new Address(a.toString()),
+                                                         new org.ibex.io.Stream(m.toString()));
+                                org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
+                            } catch (Exception e) {
+                                throw new MailException(e.toString());
+                            }
+                        }
+                    }; }
                 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; }
@@ -115,8 +151,9 @@ 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);
+                if (e instanceof JSExn) throw ((JSExn)e);
                 throw new JSExn("invalid argument for ibex object method "+name+"()");
             }
             throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");