Dear Wiki user, You have subscribed to a wiki page or wiki category on "James Wiki" for change notification.
The following page has been changed by GuillermoGrandes: http://wiki.apache.org/james/BeanShellExamples The comment on the change is: jSPF James BeanShell example New page: = BeanShellExamples = ---- === 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 [http://james.apache.org/jspf/ jSPF], actually beta release, you can download from: [http://people.apache.org/dist/james/jspf/binaries/ jSPF Binaries] ==== jSPF BeanShell Mailet: /opt/james/bsh/jSPF.bsh ==== {{{ #!java /* Scripted jSPF for BeanShell and James Author: Guillermo Grandes The ASF licenses this file to you under the Apache License v2.0 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND http://www.apache.org/licenses/LICENSE-2.0 */ import org.apache.log4j.Logger; import org.apache.log4j.Level; import org.apache.james.jspf.SPF; import org.apache.james.jspf.SPFResult; import org.apache.james.jspf.SPF1Utils; import org.apache.james.jspf.Log4JLogger; import javax.mail.internet.MimeMessage; setStrictJava(true); // QUERY SPF void query(Mail mail, Mailet ma) { // // Init parameters String attrName = ma.getInitParameter("attrName", "jSPF.ret"); String logName = ma.getInitParameter("log", "jSPF"); Logger logger = Logger.getLogger(logName); // String ip = mail.getRemoteAddr(); String sender = mail.getSender().toString(); String helo = mail.getRemoteHost(); if (helo == null) helo = ip; // SPF spf = new SPF(); SPFResult res = spf.checkSPF(ip, sender, helo); // if (res == null) { mail.removeAttribute(attrName); } else { logger.debug((new StringBuffer(160).append("result="). append(res.getResult()).append("|"). append("header_comment=").append(res.getHeaderText()). append("|").append("smtp_comment="). append(res.getExplanation())).toString()); mail.setAttribute(attrName + "." + "result", res.getResult()); mail.setAttribute(attrName + "." + "header_comment", res.getHeaderText()); mail.setAttribute(attrName + "." + "smtp_comment", res.getExplanation()); mail.setAttribute(attrName, "true"); } return; } static String s_attrName = null; static String s_headerName = null; static String s_logName = null; static Logger s_logger = null; static HashMap levels = null; static boolean s_haveResult = false; // STAMP SPF HEADERS void stamp(Mail mail, Mailet ma) { // // Init parameters if (levels == null) { levels = new HashMap(); levels.put("pass", Level.INFO); // Autorizado levels.put("neutral", Level.INFO); // Neutral levels.put("softfail", Level.WARN); // Desautorizado, pero ok mientras se migra levels.put("none", Level.WARN); // Sin configuracion SPF levels.put("fail", Level.ERROR); // Desautorizado levels.put("error", Level.ERROR); // Error temporal levels.put("unknown", Level.ERROR); // Error de configuracion } // s_attrName = ma.getInitParameter("attrName", "org.apache.james.jspf"); s_headerName = ma.getInitParameter("header", "Received-SPF"); s_logName = ma.getInitParameter("log", "jSPF"); s_logger = Logger.getLogger(s_logName); s_haveResult = Boolean.parseBoolean(ma.getInitParameter("haveResult", "true")); // // Stamp Received-SPF Header try { // Set the header name and value (supplied at init time). // Received-SPF: $result ($header_comment) String result = mail.getAttribute(s_attrName + ".result"); String headerValue = mail.getAttribute(s_attrName + ".header_comment"); // if (!s_haveResult) { headerValue = (new StringBuffer(160).append(result).append(" ("). append(headerValue).append(")")).toString(); } // MimeMessage message = mail.getMessage(); if ((headerValue != null) && (message != null)) { message.setHeader(s_headerName, headerValue); message.saveChanges(); Level level = (Level)levels.get(result); if (level == null) { level = Level.ERROR; } // Si hay error, indicar en el Notice el motivo usando el SPF_smtp_comment if (level.toInt() >= Level.ERROR_INT) { String smtp_comment = mail.getAttribute(s_attrName + ".smtp_comment"); if (smtp_comment != null) { StringBuffer sb = new StringBuffer(256); if (mail.getErrorMessage() != null) { sb.append(mail.getErrorMessage()).append("\r\n"); } else { sb.append("550 Requested action not taken: rejected - "); } sb.append(smtp_comment); mail.setErrorMessage(sb.toString()); s_logger.log(level, sb.toString()); } } s_logger.log(level, (new StringBuffer(160).append(mail.getName()). append(" ").append(s_headerName).append("="). append(headerValue)).toString()); } } catch (javax.mail.MessagingException me) { s_logger.error(me.getMessage()); } return; } return this; }}} Can you see that this scripted code use log4j, you also can setup, see also [wiki:Self:log4j Log4J in James]