pretty much working
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index ff16b57..4e06040 100644 (file)
@@ -2,6 +2,7 @@
 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 java.io.*;
@@ -11,15 +12,21 @@ public class Script extends Target {
 
     private static Script root = null;
     private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
-    public static Script root() throws JSExn, IOException {
-        if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
-        return root;
+    public static Script root() {
+        try {
+            if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
+            return root;
+        } catch (Exception e) {
+            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 class ScriptScope extends JSScope {
         ScriptEnv env = new ScriptEnv();
@@ -31,7 +38,7 @@ public class Script extends Target {
         }
     }
 
-    public synchronized void accept(Message m) throws IOException {
+    public synchronized void accept(Message m) throws IOException, MailException {
         this.m = m;
         try {
             Object ret = js.call(m, null, null, null, 1);
@@ -48,6 +55,15 @@ public class Script extends Target {
     // FIXME: this should extend org.ibex.core.Ibex
     public static class ScriptEnv extends JS {
 
+        private static PropertyFile prefs;
+        static {
+            try {
+                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;
@@ -73,6 +89,10 @@ 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 FileSystem.Mailbox.getForUser("megacz"); }
             return super.get(name);
         }