dont try to create a FileBasedMailbox for STORAGE_ROOT/users
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index c4b7215..1f822a7 100644 (file)
@@ -2,50 +2,84 @@
 package org.ibex.mail;
 import org.ibex.js.*;
 import org.ibex.util.*;
+import org.ibex.mail.*;
 import org.ibex.mail.filter.*;
 import org.ibex.mail.target.*;
-import org.ibex.mail.store.*;
 import java.io.*;
 import java.util.*;
 
 public class Script extends Target {
 
-    public static Script root = null;
-    static {
+    private static Script root = null;
+    private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
+    public static Script root() {
         try {
-            root = new Script(System.getProperty("ibex.mail.conf", File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf"));
+            if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
+            return root;
         } catch (Exception e) {
-            e.printStackTrace();
+            Log.error(Script.class, e);
+            return null;
         }
     }
 
     final JS js;
+    private Message m = null;
+    private String filePath = null;
     public Script(String filePath) throws JSExn, IOException {
-        js = JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))); }
+        this.filePath = filePath;
+        js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
+                                        new ScriptScope()); }
 
-    public void accept(Message m) throws IOException {
+    private class ScriptScope extends JSScope {
+        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;
+            return null;
+        }
+    }
+
+    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 {
-            // currently, we write all inbound messages to the transcript
-            // FIXME
-            //MessageStore.transcript.add(m);
+            Log.info(this, "invoking config...");
             Object ret = js.call(m, null, null, null, 1);
-            if (ret instanceof Target) {
-                ((Target)ret).accept(m);
-            } else if (ret instanceof Filter) {
-                // FIXME: return value?
-                ((Filter)ret).process(m);
-            } else {
-                throw new IOException("configuration script returned a " + ret.getClass().getName());
-            }
+            Log.info(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);
+            else throw new IOException("configuration script returned a " + ret.getClass().getName());
         } catch (JSExn e) {
-            e.printStackTrace();
+            Log.warn(this, e);
+            throw new IOException("configuration script threw an exception");
         }
     }
 
     // FIXME: this should extend org.ibex.core.Ibex
     public static class ScriptEnv extends JS {
 
-        // FIXME: duplicated code with org.ibex.core.Ibex; lift?
+        private static 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 {
             String key;
@@ -61,7 +95,8 @@ public class Script extends Target {
         }
         private Sub getSub(String s) { return new Sub(s); }
 
-        public Object get(Object name) throws JSExn {
+        public Object get(Object name_) throws JSExn {
+            String name = (String)name_;
             if (name.equals("math")) { return ibexMath; }
             if (name.equals("string")) { return ibexString; }
             if (name.equals("date")) { return METHOD; }
@@ -71,26 +106,79 @@ public class Script extends Target {
             if (name.equals("log.info")) { return METHOD; }
             if (name.equals("log.warn")) { return METHOD; }
             if (name.equals("log.error")) { return METHOD; }
+            if (name.equals("list")) { return getSub("list"); }
+            if (name.startsWith("list.")) { return org.ibex.mail.List.getList(name.substring(5)); }
+            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")) {
+                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")) {
+                FileBasedMailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
+                return root.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 = "";
+                    Address from = null, to = null;
+                    for(Enumeration e = m.keys(); e.hasMoreElements();) {
+                        String key = (String)e.nextElement();
+                        String val = m.get(key) == null ? null : m.get(key).toString();
+                        if ("body".equals(key)) body = val;
+                        else headers.append(key + ": " + val + "\r\n");
+                        if ("from".equalsIgnoreCase(key)) from = Address.parse(val);
+                        if ("to".equalsIgnoreCase(key)) to = Address.parse(val);
+                    }
+                    Message message = new Message(new org.ibex.io.Stream(headers.toString() + "\r\n" + body),
+                                                  new Message.Envelope(from,
+                                                                       to,
+                                                                       new Date()));
+                    //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 T;
+                }
+                if (name.equals("mail.forward")) { return new Target() {
+                        public void accept(Message m) throws MailException {
+                            try {
+                                Message m2 = new Message(new org.ibex.io.Stream(m.toString()),
+                                                         new Message.Envelope(m.envelope.from,
+                                                                              new Address(a.toString()),
+                                                                              new Date()));
+                                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; }
                 if (name.equals("log.error")) {    JS.error(a== null ? "**null**" : a.toString()); return null; }
                 switch (nargs) {
                 case 1:
-                    if (name.equals("regexp")) { return new JSRegexp(a, null); }
+                    if (name.equals("regexp")) {return new JSRegexp(a, null); }
                     break;
                 case 2:
-                    if (name.equals("regexp")) { return new JSRegexp(a, b); }
+                    if (name.equals("regexp")) {return new JSRegexp(a, b); }
                 }
-            } catch (RuntimeException e) {
-                // FIXME: maybe JSExn should take a second argument, Exception
+            } catch (Exception e) {
                 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 number of arguments ("+nargs+") for ibex object method "+name+"()");