added synchronous send via SMTP
[org.ibex.mail.git] / src / org / ibex / mail / target / Script.java
index f3e8adf..6a7395a 100644 (file)
@@ -32,7 +32,7 @@ public class Script extends Target {
     private String filePath = null;
     public Script(String filePath) throws JSExn, IOException {
         this.filePath = filePath;
-        js = JSU.cloneWithNewGlobalScope(JSU.fromReader(filePath, 0, new InputStreamReader(new FileInputStream(filePath))),
+        js = JSU.cloneWithNewGlobalScope(JSU.fromReader(filePath, 1, new InputStreamReader(new FileInputStream(filePath))),
                                          new ScriptScope()); }
 
     private class ScriptScope extends JS.Immutable {
@@ -115,6 +115,10 @@ public class Script extends Target {
             case "mail.forward": return METHOD;
             case "mail.forward2": return METHOD;
             case "mail.send": return METHOD;
+            case "mail.attempt": return METHOD;
+            case "mail.later": return Later.instance;
+            case "mail.drop": return Drop.instance;
+            case "mail.bounce": return METHOD;
             case "mail.my": return getSub("mail.my");
             case "mail.my.prefs": try {
                     return new org.ibex.js.Directory(new File("/etc/org.ibex.mail.prefs"));
@@ -135,7 +139,8 @@ public class Script extends Target {
             String name = JSU.toString(name0);
             try {
                 if (name.equals("date")) { return new JSDate(args); }
-                if (name.equals("mail.send") || name.equals("send")) {
+                if (name.equals("mail.send") || name.equals("send") || name.equals("mail.attempt") || name.equals("attempt")) {
+                    boolean attempt = name.equals("mail.attempt") || name.equals("attempt");
                     JS m = (JS)a;
                     StringBuffer headers = new StringBuffer();
                     String body = "";
@@ -153,17 +158,41 @@ public class Script extends Target {
                     }
                     if (envelopeTo == null) envelopeTo = to;
                     if (envelopeFrom == null) envelopeFrom = from;
-                    Message message = Message.newMessage(new org.ibex.io.Stream(headers.toString() + "\r\n" + body),
-                                                         envelopeFrom,
-                                                         envelopeTo);
-                    //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 JSU.T;
+                    Message message =
+                        Message.newMessage(new org.ibex.io.Fountain.StringFountain(headers.toString() + "\r\n" + body),
+                                           envelopeFrom,
+                                           envelopeTo
+                                           );
+                    
+                    boolean ok = false;
+                    try {
+                        if (attempt) {
+                            org.ibex.mail.protocol.SMTP.Outgoing.attempt(message);
+                        } else {
+                            org.ibex.mail.protocol.SMTP.Outgoing.accept(message);
+                        }
+                        ok = true;
+                    } catch (Exception ex) {
+                        if (!attempt) Log.warn(this, ex);
+                    }
+                    if (!ok && !attempt) throw new JSExn("SMTP server rejected message");
+                    return JSU.B(ok);
+                }
+                if (name.equals("mail.bounce")) {
+                    return new Target() {
+                            public void accept(Message m) throws MailException {
+                                try {
+                                    Message m2 = m.bounce(JSU.toString(a));
+                                    org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
+                                    Log.error(this, "BOUNCING! " + m2.summary());
+                                } catch (Exception e) {
+                                    Log.warn(this, e);
+                                }
+                            } };
                 }
                 if (name.equals("mail.forward2") || name.equals("forward2")) {
                     try {
-                        Message m2 = Message.newMessage(new org.ibex.io.Stream(m.toString()),
+                        Message m2 = Message.newMessage(new org.ibex.io.Fountain.StringFountain(m.toString()),
                                                         m.envelopeFrom,
                                                         new Address(JSU.toString(a)));
                         org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
@@ -173,18 +202,11 @@ public class Script extends Target {
                     }
                     return null;
                 }
-                if (name.equals("mail.forward") || name.equals("forward")) { return new Target() {
-                        public void accept(Message m) throws MailException {
-                            try {
-                                Message m2 = Message.newMessage(new org.ibex.io.Stream(m.toString()),
-                                                         m.envelopeFrom,
-                                                         new Address(a.toString()));
-                                org.ibex.mail.protocol.SMTP.Outgoing.accept(m2);
-                            } catch (Exception e) {
-                                throw new MailException(e.toString());
-                            }
-                        }
-                    }; }
+                if (name.equals("mail.forward") || name.equals("forward")) {
+                    Message m2 = Message.newMessage(Script.this.m, Script.this.m.envelopeFrom, new Address(JSU.toString(a)));
+                    org.ibex.mail.protocol.SMTP.Outgoing.attempt(m2);
+                    return Drop.instance;
+                }
                 if (name.equals("log.debug") || name.equals("debug")) {    JSU.debug(a== null ? "**null**" : JSU.toString(a)); return null; }
                 if (name.equals("log.info") || name.equals("info")) {     JSU.info(a== null ? "**null**" : JSU.toString(a)); return null; }
                 if (name.equals("log.warn") || name.equals("warn")) {     JSU.warn(a== null ? "**null**" : JSU.toString(a)); return null; }