Differences between revisions 5 and 6
Revision 5 as of 2006-08-28 11:04:29
Size: 8957
Comment: Example of init parameters only first time
Revision 6 as of 2009-09-20 22:58:30
Size: 8955
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 39: Line 39:
You need [http://james.apache.org/jspf/ jSPF], actually beta release, you can download from: [http://people.apache.org/dist/james/jspf/binaries/ jSPF Binaries] You need [[http://james.apache.org/jspf/|jSPF]], actually beta release, you can download from: [[http://people.apache.org/dist/james/jspf/binaries/|jSPF Binaries]]
Line 172: Line 172:
Can you see that this scripted code use log4j, you also can setup, see also [wiki:Self:log4j Log4J in James] Can you see that this scripted code use log4j, you also can setup, see also [[log4j|Log4J in James]]
Line 267: Line 267:
See also [http://svn.apache.org/viewvc/james/server/branches/v2.3/src/java/org/apache/james/transport/mailets/SetMimeHeader.java?view=markup SetMimeHeader] See also [[http://svn.apache.org/viewvc/james/server/branches/v2.3/src/java/org/apache/james/transport/mailets/SetMimeHeader.java?view=markup|SetMimeHeader]]

jSPF

This is an example of use the BeanShell for James and SPF for anti-spam.

Config of Scripted jSPF: SAR-INF/config.xml

  <!-- jSPF: Sender Policy Framework :: QUERY -->
  <!-- This code make the query SPF and save  -->
  <!-- data in mail attribs                   -->
  <mailet match="All" class="BeanShell">
    <script>/opt/james/bsh/jSPF.bsh</script>
    <method>query</method>
    <log>jSPF.query</log>
    <attrName>org.apache.james.jspf</attrName>
  </mailet>

  <!-- jSPF: Sender Policy Framework :: STAMP -->
  <!-- This code read mail attribs and create -->
  <!-- the MimeHeader Received-SPF            -->
  <mailet match="All" class="BeanShell">
    <script>/opt/james/bsh/jSPF.bsh</script>
    <method>stamp</method>
    <log>jSPF.stamp</log>
    <header>Received-SPF</header>
    <!-- jSPF -->
    <haveResult>true</haveResult>
    <attrName>org.apache.james.jspf</attrName>
  </mailet>

  <!-- jSPF: Sender Policy Framework :: TRAP  -->
  <!-- This code check the mail attrib and if -->
  <!-- the result of SPF is FAIL,... oops! ;) -->
  <mailet match="HasMailAttributeWithValue=org.apache.james.jspf.result, fail"
          class="ToProcessor">
    <processor> fake-mail </processor>
  </mailet>

You need jSPF, actually beta release, you can download from: jSPF Binaries

jSPF BeanShell Mailet: /opt/james/bsh/jSPF.bsh

   1 /*
   2  Scripted jSPF for BeanShell and James
   3  Author: Guillermo Grandes
   4 
   5  The ASF licenses this file to you under the Apache License v2.0
   6  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
   7 
   8  http://www.apache.org/licenses/LICENSE-2.0
   9  */
  10 import org.apache.log4j.Logger;
  11 import org.apache.log4j.Level;
  12 import org.apache.james.jspf.SPF;
  13 import org.apache.james.jspf.SPFResult;
  14 import org.apache.james.jspf.SPF1Utils;
  15 import org.apache.james.jspf.Log4JLogger;
  16 import javax.mail.internet.MimeMessage;
  17 
  18 setStrictJava(true);
  19 
  20 // QUERY SPF
  21 void query(Mail mail, Mailet ma) {
  22   //
  23   // Init parameters
  24   String attrName = ma.getInitParameter("attrName", "jSPF.ret");
  25   String logName = ma.getInitParameter("log", "jSPF");
  26   Logger logger = Logger.getLogger(logName);
  27   //
  28   String ip = mail.getRemoteAddr();
  29   String sender = mail.getSender().toString();
  30   String helo = mail.getRemoteHost();
  31   if (helo == null) helo = ip;
  32   //
  33   SPF spf = new SPF();
  34   SPFResult res = spf.checkSPF(ip, sender, helo);
  35   //
  36   if (res == null) {
  37     mail.removeAttribute(attrName);
  38   } else {
  39     logger.debug((new StringBuffer(160).append("result=").
  40            append(res.getResult()).append("|").
  41            append("header_comment=").append(res.getHeaderText()).
  42            append("|").append("smtp_comment=").
  43            append(res.getExplanation())).toString());
  44     mail.setAttribute(attrName + "." + "result", res.getResult());
  45     mail.setAttribute(attrName + "." + "header_comment", res.getHeaderText());
  46     mail.setAttribute(attrName + "." + "smtp_comment", res.getExplanation());
  47     mail.setAttribute(attrName, "true");
  48   }
  49   return;
  50 }
  51 
  52 static String s_attrName = null;
  53 static String s_headerName = null;
  54 static String s_logName = null;
  55 static Logger s_logger = null;
  56 static HashMap levels = null;
  57 static boolean s_haveResult = false;
  58 
  59 // STAMP SPF HEADERS
  60 void stamp(Mail mail, Mailet ma) {
  61   //
  62   // Init parameters
  63   if (levels == null) {
  64     levels = new HashMap();
  65     levels.put("pass", Level.INFO);     // Autorizado
  66     levels.put("neutral", Level.INFO);  // Neutral
  67     levels.put("softfail", Level.WARN); // Desautorizado, pero ok mientras se migra
  68     levels.put("none", Level.WARN);     // Sin configuracion SPF
  69     levels.put("fail", Level.ERROR);    // Desautorizado
  70     levels.put("error", Level.ERROR);   // Error temporal
  71     levels.put("unknown", Level.ERROR); // Error de configuracion
  72   }
  73   //
  74   s_attrName = ma.getInitParameter("attrName", "org.apache.james.jspf");
  75   s_headerName = ma.getInitParameter("header", "Received-SPF");
  76   s_logName = ma.getInitParameter("log", "jSPF");
  77   s_logger = Logger.getLogger(s_logName);
  78   s_haveResult = Boolean.parseBoolean(ma.getInitParameter("haveResult", "true"));
  79   //
  80   // Stamp Received-SPF Header
  81   try {
  82     // Set the header name and value (supplied at init time).
  83     // Received-SPF: $result ($header_comment)
  84     String result = mail.getAttribute(s_attrName + ".result");
  85     String headerValue = mail.getAttribute(s_attrName + ".header_comment");
  86     //
  87     if (!s_haveResult) {
  88       headerValue = (new StringBuffer(160).append(result).append(" (").
  89                      append(headerValue).append(")")).toString();
  90     }
  91     //
  92     MimeMessage message = mail.getMessage();
  93     if ((headerValue != null) && (message != null)) {
  94       message.setHeader(s_headerName, headerValue);
  95       message.saveChanges();
  96       Level level = (Level)levels.get(result);
  97       if (level == null) {
  98         level = Level.ERROR;
  99       }
 100       // Si hay error, indicar en el Notice el motivo usando el SPF_smtp_comment
 101       if (level.toInt() >= Level.ERROR_INT) {
 102         String smtp_comment = mail.getAttribute(s_attrName + ".smtp_comment");
 103         if (smtp_comment != null) {
 104           StringBuffer sb = new StringBuffer(256);
 105           if (mail.getErrorMessage() != null) {
 106             sb.append(mail.getErrorMessage()).append("\r\n");
 107           } else {
 108             sb.append("550 Requested action not taken: rejected - ");
 109           }
 110           sb.append(smtp_comment);
 111           mail.setErrorMessage(sb.toString());
 112           s_logger.log(level, sb.toString());
 113         }
 114       }
 115       s_logger.log(level, (new StringBuffer(160).append(mail.getName()).
 116                append(" ").append(s_headerName).append("=").
 117                append(headerValue)).toString());
 118     }
 119   } catch (javax.mail.MessagingException me) {
 120     s_logger.error(me.getMessage());
 121   }
 122   return;
 123 }
 124 
 125 return this;

Can you see that this scripted code use log4j, you also can setup, see also Log4J in James


RemoveMimeHeader & RemoveMimeHeaderList

This is an example exacted from request of MailetIdeas.

Config of Scripted RemoveMimeHeader: SAR-INF/config.xml

  <!-- Remove One Header -->
  <mailet match="All" class="BeanShell">
    <script>/opt/james/bsh/mimeheads.bsh</script>
    <method>RemoveMimeHeader</method>
    <name>X-BadHeader</name>
  </mailet>
  <!-- Clean Multiples Headers in the top of root processor -->
  <mailet match="All" class="BeanShell">
    <script>/opt/james/bsh/mimeheads.bsh</script>
    <method>RemoveMimeHeaderList</method>
    <name1>X-UserIsAuth</name1>
    <name2>X-WasSigned</name2>
    <name3>X-MessageIsSpam</name3>
    <name4>X-MessageIsSpamProbability</name4>
  </mailet>

RemoveMimeHeader BeanShell Mailet: /opt/james/bsh/mimeheads.bsh

   1 /*
   2  Scripted RemoveMimeHeader / RemoveMimeHeaderList for BeanShell and James
   3  Author: Guillermo Grandes
   4 
   5  The ASF licenses this file to you under the Apache License v2.0
   6  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND
   7 
   8  http://www.apache.org/licenses/LICENSE-2.0
   9  */
  10 import javax.mail.internet.MimeMessage;
  11 
  12 setStrictJava(true);
  13 
  14 void RemoveMimeHeader(Mail mail, Mailet ma) {
  15   //
  16   // Init parameters
  17   String headerName = ma.getInitParameter("name");
  18   
  19   // Check if needed config values are used
  20   if (headerName == null || headerName.equals("")) {
  21     ma.log("Please configure a name");
  22   }
  23   
  24   try {
  25     MimeMessage message = mail.getMessage();
  26   
  27     //Remove the header name (supplied at init time).
  28     message.removeHeader(headerName);
  29     message.saveChanges();
  30   } catch (javax.mail.MessagingException me) {
  31     ma.log(me.getMessage());
  32   }
  33 }
  34 //
  35 static String[] heads = null;
  36 void RemoveMimeHeaderList(Mail mail, Mailet ma) {
  37   // Init parameters
  38   if (heads == null) {
  39     Vector v = new Vector();
  40     for (Iterator i = ma.getInitParameterNames(); i.hasNext();) {
  41       String key = i.next();
  42       if (key.startsWith("name")) {
  43         v.add(ma.getInitParameter(key));
  44       }
  45     }
  46     heads = new String[v.size()];
  47     v.toArray(heads);
  48     ma.log("Init Headers=" + v.toString());
  49   }
  50   try {
  51     MimeMessage message = mail.getMessage();
  52     //Remove the header name (supplied at init time).
  53     for (int i = 0; i < heads.length; i++) {
  54       message.removeHeader(heads[i]);
  55     }
  56     message.saveChanges();
  57   } catch (javax.mail.MessagingException me) {
  58     ma.log(me.getMessage());
  59   }
  60 }
  61 return this;

In only 15 mins, is fantastic, right? :-)

See also SetMimeHeader

BeanShellExamples (last edited 2009-09-20 22:58:30 by localhost)