add org.ibex.mail.VERP
[org.ibex.mail.git] / src / org / ibex / mail / Script.java
index 0e404e0..9459c1f 100644 (file)
@@ -13,6 +13,8 @@ import java.io.*;
 import java.util.*;
 import java.text.*;
 
+// FIXME: check for binaries (razor, clamassassin, etc) and complain if not present
+
 //
 //  - better matching syntax:
 //  - src-ip
@@ -133,8 +135,12 @@ public class Script extends JS.Obj implements Target {
            case "mail.drop": return METHOD;
            case "mail.razor": return getSub("mail.razor");
             case "mail.razor.check": return METHOD;
+           case "mail.clamav": return getSub("mail.clamav");
+            case "mail.clamav.check": return METHOD;
            case "mail.procmail": /* FEATURE */ return null;
            case "mail.vacation": /* FEATURE */ return null;
+           case "mail.verp": return getSub("mail.verp");
+           case "mail.verp.check": return METHOD;
            case "mail.dcc": return getSub("mail.dcc");
             case "mail.dcc.check": return METHOD;
             case "mail.bounce": return METHOD;
@@ -147,8 +153,8 @@ public class Script extends JS.Obj implements Target {
             } catch (IOException e) { throw new JSExn(e.toString()); }
             case "mail.whitelist": return JSReflection.wrap(org.ibex.mail.SMTP.whitelist);
             case "mail.my.mailbox":
-                Mailbox root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
-                return root.slash("user", true).slash("megacz", true);
+                MailTree root = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true);
+                return (JS)root.slash("user", true).slash("megacz", true);
             case "mail.list": return METHOD;
                 //#end
                 return super.get(name);
@@ -172,7 +178,7 @@ public class Script extends JS.Obj implements Target {
                     Log.warn("dbug", b.getClass().getName());
                     Message m = (Message)b;
                     final Process p = Runtime.getRuntime().exec(JSU.toString(a));
-                    new Thread() {
+                    Main.threadPool.start(new Runnable() {
                         public void run() {
                             try {
                                 BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
@@ -181,7 +187,7 @@ public class Script extends JS.Obj implements Target {
                                     Log.warn("shell", s);
                             } catch (Exception e) { e.printStackTrace(); }
                         }
-                    }.start();
+                    });
                     OutputStream os = p.getOutputStream();
                     Stream stream = new Stream(os);
 
@@ -237,13 +243,58 @@ public class Script extends JS.Obj implements Target {
                    ((Message)args[0]).getStream().transcribe(new Stream(p.getOutputStream()), true);
                    return JSU.N(p.waitFor());
                }
+                if (name.equals("mail.clamav.check")) {
+                    // FIXME: this is returning "is-virus-laden" when clamdscan is unhappy -- BAD!
+                    // should use error code: 0=clean, 1=virus, 2=malfunction
+                   Process p = Runtime.getRuntime().exec("clamdscan - --stdout --quiet");
+                   ((Message)args[0]).getStream().transcribe(new Stream(p.getOutputStream()), true);
+                   int result = p.waitFor();
+                    if (result==0) return JSU.N(0);
+                    StringBuffer ret = new StringBuffer();
+                    new Stream(p.getInputStream()).transcribe(ret);
+                    return JSU.S(ret.toString());
+               }
+                if (name.equals("mail.verp.check")) {
+                    String ret = VERP.verpVerify(Address.parse(JSU.toString(a)), "SECRET".getBytes(), 0);
+                    return ret==null ? null : JSU.S(ret);
+                }
                 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());
+                    String result = ret.toString();
+                    Log.warn("dcc", ((Message)args[0]).summary() + ":\n  " + result);
+                    int body = 0;
+                    int fuz1 = 0;
+                    int fuz2 = 0;
+                    int i_body = result.indexOf("Body=");
+                    int i_fuz1 = result.indexOf("Fuz1=");
+                    int i_fuz2 = result.indexOf("Fuz2=");
+                    if (i_body != -1) try {
+                        String s = result.substring(i_body+5);
+                        if (s.indexOf(' ') != -1) s = s.substring(0, s.indexOf(' '));
+                        if (s.indexOf('\n') != -1) s = s.substring(0, s.indexOf('\n'));
+                        body = s.trim().equals("many") ? 999 : Integer.parseInt(s.trim());
+                    } catch (Exception e) { Log.error("", e); }
+                    if (i_fuz1 != -1) try {
+                        String s = result.substring(i_fuz1+5);
+                        if (s.indexOf(' ') != -1) s = s.substring(0, s.indexOf(' '));
+                        if (s.indexOf('\n') != -1) s = s.substring(0, s.indexOf('\n'));
+                        fuz1 = s.trim().equals("many") ? 999 : Integer.parseInt(s.trim());
+                    } catch (Exception e) { Log.error("", e); }
+                    if (i_fuz2 != -1) try {
+                        String s = result.substring(i_fuz2+5);
+                        if (s.indexOf(' ') != -1) s = s.substring(0, s.indexOf(' '));
+                        if (s.indexOf('\n') != -1) s = s.substring(0, s.indexOf('\n'));
+                        fuz2 = s.trim().equals("many") ? 999 : Integer.parseInt(s.trim());
+                    } catch (Exception e) { Log.error("", e); }
+                    JSArray jsa = new JSArray();
+                    jsa.put(JSU.N(0), JSU.N(body));
+                    jsa.put(JSU.N(1), JSU.N(fuz1));
+                    jsa.put(JSU.N(2), JSU.N(fuz2));
+                   return jsa;
                }
                 if (name.equals("mail.drop")) {
                     return args.length==0 ? new Drop() : new Drop(JSU.toString(args[0]));