bugfixes
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index b7deee8..c742cc4 100644 (file)
@@ -2,33 +2,35 @@
 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;
     public Script(String filePath) throws JSExn, IOException {
         js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
-                                        new ScriptScope(null)); }
+                                        new ScriptScope()); }
 
-    private static class ScriptScope extends JSScope {
-        Message m;
+    private class ScriptScope extends JSScope {
         ScriptEnv env = new ScriptEnv();
-        public ScriptScope(Message m) { super(null); this.m = m; }
+        public ScriptScope() { super(null); }
         public Object get(Object o) {
             if (o.equals("m")) return m;
             if (o.equals("ibex")) return env;
@@ -36,29 +38,35 @@ public class Script extends Target {
         }
     }
 
-    public void accept(Message m) throws IOException {
+    public synchronized void accept(Message m) throws IOException, MailException {
+        this.m = m;
         try {
-            // currently, we write all inbound messages to the transcript
-            // FIXME
             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 {
-                if (ret == null) throw new IOException("configuration script returned null");
-                else throw new IOException("configuration script returned a " + ret.getClass().getName());
-            }
+            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;
@@ -84,6 +92,12 @@ 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("mail")) { return getSub("mail"); }
+            if (name.equals("mail.my")) { return getSub("mail.my"); }
+            if (name.equals("mail.my.prefs")) { return prefs; }
+            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);
         }
 
@@ -102,7 +116,6 @@ public class Script extends Target {
                     if (name.equals("regexp")) {return new JSRegexp(a, b); }
                 }
             } catch (RuntimeException e) {
-                // FIXME: maybe JSExn should take a second argument, Exception
                 Log.warn(this, "ibex."+name+"() threw: " + e);
                 throw new JSExn("invalid argument for ibex object method "+name+"()");
             }