total overhaul; were using MIME and MIME.Part now
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.ibex.mail;
3 import org.ibex.js.*;
4 import org.ibex.util.*;
5 import org.ibex.mail.*;
6 import org.ibex.mail.filter.*;
7 import org.ibex.mail.target.*;
8 import java.io.*;
9 import java.util.*;
10
11 public class Script extends Target {
12
13     private static Script root = null;
14     private static final String DEFAULT_CONF = File.separatorChar + "etc" + File.separatorChar + "org.ibex.mail.conf";
15     public static Script root() {
16         try {
17             if (root == null) root = new Script(System.getProperty("ibex.mail.conf", DEFAULT_CONF));
18             return root;
19         } catch (Exception e) {
20             Log.error(Script.class, e);
21             return null;
22         }
23     }
24
25     final JS js;
26     private Message m = null;
27     public Script(String filePath) throws JSExn, IOException {
28         js = JS.cloneWithNewParentScope(JS.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
29                                         new ScriptScope()); }
30
31     private class ScriptScope extends JSScope {
32         ScriptEnv env = new ScriptEnv();
33         public ScriptScope() { super(null); }
34         public Object get(Object o) {
35             if (o.equals("m")) return m;
36             if (o.equals("ibex")) return env;
37             return null;
38         }
39     }
40
41     public synchronized void accept(Message m) throws IOException, MailException {
42         this.m = m;
43         try {
44             Log.info(this, "invoking config...");
45             Object ret = js.call(m, null, null, null, 1);
46             Log.info(this, "config returned " + ret);
47             if (ret == null) throw new IOException("configuration script returned null");
48             if (ret instanceof Target)      ((Target)ret).accept(m);
49             //else if (ret instanceof Filter) ((Filter)ret).process(m);
50             else throw new IOException("configuration script returned a " + ret.getClass().getName());
51         } catch (JSExn e) {
52             Log.warn(this, e);
53             throw new IOException("configuration script threw an exception");
54         }
55     }
56
57     // FIXME: this should extend org.ibex.core.Ibex
58     public static class ScriptEnv extends JS {
59
60         private static PropertyFile prefs = null;
61             /*
62         static {
63             try {
64                 // FIXME
65                 prefs = new PropertyFile(new File("/etc/org.ibex.mail.properties"));
66             } catch (IOException e) {
67                 Log.error(ScriptEnv.class, e);
68             }
69         }
70             */
71
72         /** lets us put multi-level get/put/call keys all in the same method */
73         private class Sub extends JS {
74             String key;
75             Sub(String key) { this.key = key; }
76             public void put(Object key, Object val) throws JSExn { ScriptEnv.this.put(this.key + "." + key, val); }
77             public Object get(Object key) throws JSExn { return ScriptEnv.this.get(this.key + "." + key); }
78             public Object call(Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
79                 return ScriptEnv.this.callMethod(this.key, a0, a1, a2, rest, nargs);
80             }
81             public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
82                 return ScriptEnv.this.callMethod(this.key + "." + method, a0, a1, a2, rest, nargs);
83             }
84         }
85         private Sub getSub(String s) { return new Sub(s); }
86
87         public Object get(Object name) throws JSExn {
88             if (name.equals("math")) { return ibexMath; }
89             if (name.equals("string")) { return ibexString; }
90             if (name.equals("date")) { return METHOD; }
91             if (name.equals("regexp")) { return METHOD; }
92             if (name.equals("log")) { return getSub("log"); }
93             if (name.equals("log.debug")) { return METHOD; }
94             if (name.equals("log.info")) { return METHOD; }
95             if (name.equals("log.warn")) { return METHOD; }
96             if (name.equals("log.error")) { return METHOD; }
97             if (name.equals("mail")) { return getSub("mail"); }
98             if (name.equals("mail.forward")) { return METHOD; }
99             if (name.equals("mail.send")) { return METHOD; }
100             if (name.equals("mail.my")) { return getSub("mail.my"); }
101             if (name.equals("mail.my.prefs")) {
102                 try {
103                     return new org.ibex.js.Directory(new File("/etc/org.ibex.mail.prefs"));
104                 } catch (IOException e) {
105                     throw new JSExn(e.toString());
106                 }
107             }
108             if (name.equals("mail.my.mailbox")) {
109                 return FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true).slash("user", true).slash("megacz", true).slash("newmail", true);
110             }
111             return super.get(name);
112         }
113
114         public Object callMethod(Object name, final Object a, Object b, Object c, Object[] rest, int nargs) throws JSExn {
115             try {
116                 if (name.equals("date")) { return new JSDate(a, b, c, rest, nargs); }
117                 if (name.equals("mail.send")) {
118                     JS m = (JS)a;
119                     StringBuffer headers = new StringBuffer();
120                     String body = "";
121                     for(Enumeration e = m.keys(); e.hasMoreElements();) {
122                         String key = (String)e.nextElement();
123                         String val = m.get(key).toString();
124                         if (key.equals("body")) body = val;
125                         else headers.append(key + ": " + val + "\r\n");
126                     }
127                     Message message = new Message(new org.ibex.io.Stream(headers.toString() + "\r\n" + body),
128                                                   new Message.Envelope(null, null, new Date()));
129                     //org.ibex.mail.protocol.SMTP.Outgoing.accept(message);
130                     boolean ok = org.ibex.mail.protocol.SMTP.Outgoing.attempt(message);
131                     if (!ok) throw new JSExn("SMTP server rejected message");
132                     return T;
133                 }
134                 if (name.equals("mail.forward")) { return new Target() {
135                         public void accept(Message m) throws MailException {
136                             try {
137                                 Message m2 = new Message(new org.ibex.io.Stream(m.toString()),
138                                                          new Message.Envelope(m.envelope.from,
139                                                                               new Address(a.toString()),
140                                                                               new Date()));
141                                 org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
142                             } catch (Exception e) {
143                                 throw new MailException(e.toString());
144                             }
145                         }
146                     }; }
147                 if (name.equals("log.debug")) {    JS.debug(a== null ? "**null**" : a.toString()); return null; }
148                 if (name.equals("log.info")) {     JS.info(a== null ? "**null**" : a.toString()); return null; }
149                 if (name.equals("log.warn")) {     JS.warn(a== null ? "**null**" : a.toString()); return null; }
150                 if (name.equals("log.error")) {    JS.error(a== null ? "**null**" : a.toString()); return null; }
151                 switch (nargs) {
152                 case 1:
153                     if (name.equals("regexp")) {return new JSRegexp(a, null); }
154                     break;
155                 case 2:
156                     if (name.equals("regexp")) {return new JSRegexp(a, b); }
157                 }
158             } catch (Exception e) {
159                 Log.warn(this, "ibex."+name+"() threw: " + e);
160                 if (e instanceof JSExn) throw ((JSExn)e);
161                 throw new JSExn("invalid argument for ibex object method "+name+"()");
162             }
163             throw new JSExn("invalid number of arguments ("+nargs+") for ibex object method "+name+"()");
164         }
165
166         public static final JSMath ibexMath = new JSMath() {
167                 private JS gs = new JSScope.Global();
168                 public Object get(Object name) throws JSExn {
169                     if (name.equals("isNaN")) { return gs.get("isNaN"); }
170                     if (name.equals("isFinite")) { return gs.get("isFinite"); }
171                     if (name.equals("NaN")) { return gs.get("NaN"); }
172                     if (name.equals("Infinity")) { return gs.get("Infinity"); }
173                     return super.get(name);
174                 }
175             };
176         
177         public static final JS ibexString = new JS() {
178                 private JS gs = new JSScope.Global();
179                 public void put(Object key, Object val) { }
180                 public Object get(Object name) throws JSExn {
181                     if (name.equals("parseInt")) { return gs.get("parseInt"); }
182                     if (name.equals("parseFloat")) { return gs.get("parseFloat"); }
183                     if (name.equals("decodeURI")) { return gs.get("decodeURI"); }
184                     if (name.equals("decodeURIComponent")) { return gs.get("decodeURIComponent"); }
185                     if (name.equals("encodeURI")) { return gs.get("encodeURI"); }
186                     if (name.equals("encodeURIComponent")) { return gs.get("encodeURIComponent"); }
187                     if (name.equals("escape")) { return gs.get("escape"); }
188                     if (name.equals("unescape")) { return gs.get("unescape"); }
189                     if (name.equals("fromCharCode")) { return gs.get("stringFromCharCode"); }
190                     return null;
191                 }
192             };
193     }
194 }