X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2Ftarget%2FScript.java;h=b509b8616d875fdd595d0e0d1b5c2208a2858d22;hb=06127bb081212cee67a63c429825b1a7de4fc41b;hp=ac94ca95853b43eb4aa24308115d2048b3339fe1;hpb=79d757b55fd4507718b35f584a02b449bea76603;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/target/Script.java b/src/org/ibex/mail/target/Script.java index ac94ca9..b509b86 100644 --- a/src/org/ibex/mail/target/Script.java +++ b/src/org/ibex/mail/target/Script.java @@ -4,6 +4,7 @@ package org.ibex.mail.target; import org.ibex.js.*; +import org.ibex.io.*; import org.ibex.util.*; import org.ibex.mail.*; import org.ibex.mail.filter.*; @@ -11,7 +12,7 @@ import org.ibex.mail.target.*; import java.io.*; import java.util.*; -public class Script extends Target { +public class Script extends JS.Obj implements Target { private static final JS.Method METHOD = new JS.Method(); @@ -32,7 +33,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 { @@ -62,6 +63,7 @@ public class Script extends Target { Object ret = js.call(null, new JS[] { m }); Log.debug(this, "configuration script returned " + ret); if (ret == null) throw new IOException("configuration script returned null"); + while (ret instanceof JSReflection.Wrapper) ret = ((JSReflection.Wrapper)ret).unwrap(); 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()); @@ -111,10 +113,21 @@ public class Script extends Target { case "log.warn": return METHOD; case "log.error": return METHOD; case "list": return getSub("list"); + case "url": return getSub("url"); + case "url.encode": return METHOD; case "mail": return getSub("mail"); 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 METHOD; + case "mail.razor": return getSub("mail.razor"); + case "mail.razor.check": return METHOD; + case "mail.dcc": return getSub("mail.dcc"); + case "mail.dcc.check": return METHOD; + case "mail.bounce": return METHOD; + case "mail.reject": 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")); @@ -122,8 +135,8 @@ public class Script extends Target { case "mail.my.mailbox": FileBasedMailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true); return root.slash("user", true).slash("megacz", true).slash("newmail", true); + case "mail.list": return METHOD; //#end - if (JSU.toString(name).startsWith("list.")) { return MailingList.getList(JSU.toString(name).substring(5)); } return super.get(name); } @@ -134,29 +147,79 @@ public class Script extends Target { final int nargs = args.length; String name = JSU.toString(name0); try { + if (name.equals("url.encode")) return JSU.S(java.net.URLEncoder.encode(JSU.toString(args[0]))); + if (name.equals("mail.list")) return JSReflection.wrap(MailingList.getMailingList(JSU.toString(args[0]))); 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 = ""; - Address from = null, to = null; - for(JS.Enumeration e = m.keys(); e.hasNext();) { + Address from = null, to = null, envelopeFrom = null, envelopeTo = null; + JS.Enumeration e = m.keys(); + for(; e.hasNext();) { JS key = (JS)e.next(); JS val = m.get(key) == null ? null : m.get(key); if ("body".equalsIgnoreCase(JSU.toString(key))) body = JSU.toString(val); - else headers.append(key + ": " + val + "\r\n"); + else headers.append(JSU.toString(key) + ": " + JSU.toString(val) + "\r\n"); if ("from".equalsIgnoreCase(JSU.toString(key))) from = Address.parse(JSU.toString(val)); if ("to".equalsIgnoreCase(JSU.toString(key))) to = Address.parse(JSU.toString(val)); + if ("envelopeFrom".equalsIgnoreCase(JSU.toString(key))) envelopeFrom = Address.parse(JSU.toString(val)); + if ("envelopeTo".equalsIgnoreCase(JSU.toString(key))) envelopeTo = Address.parse(JSU.toString(val)); } - Message message = Message.newMessage(new org.ibex.io.Stream(headers.toString() + "\r\n" + body), from, to); - //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; + if (envelopeTo == null) envelopeTo = to; + if (envelopeFrom == null) envelopeFrom = from; + 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.razor.check")) { + Process p = Runtime.getRuntime().exec("razor-check"); + ((Message)args[0]).getStream().transcribe(new Stream(p.getOutputStream()), true); + return JSU.N(p.waitFor()); + } + if (name.equals("mail.dcc.check")) { + Process p = Runtime.getRuntime().exec(new String[] { "dccproc", "-H" }); + ((Message)args[0]).getStream().transcribe(new Stream(p.getOutputStream()), true); + StringBuffer ret = new StringBuffer(); + new Stream(p.getInputStream()).transcribe(ret); + p.waitFor(); + return JSU.S(ret.toString()); + } + if (name.equals("mail.drop")) { + return args.length==0 ? new Drop() : new Drop(JSU.toString(args[0])); + } + if (name.equals("mail.bounce")) { + return new JSTarget() { + 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); @@ -166,18 +229,13 @@ 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, false); + return Drop.instance; + } + if (name.equals("mail.reject")) + return new Reject(JSU.toString(a)); 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; } @@ -189,6 +247,7 @@ public class Script extends Target { case 2: if (name.equals("regexp")) {return new JSRegexp(a, b); } } + } catch (MailException e) { throw e; } catch (Exception e) { Log.warn(this, "ibex."+name+"() threw: " + e); Log.warn(this, e); @@ -229,4 +288,6 @@ public class Script extends Target { } }; } + + private static abstract class JSTarget extends JS.Obj implements Target { } }