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