public HookResult doRcpt(SMTPSession session, MailAddress sender, MailAddress rcpt) { if ((wNetworks == null) || (!wNetworks.matchInetNetwork( session.getRemoteAddress().getAddress().getHostAddress()))) { return super.doRcpt(session, sender, rcpt); } else { session .getLogger() .info( "IpAddress " + session.getRemoteAddress().getAddress().getHostAddress() + " is whitelisted. Skip greylisting."); } return new HookResult(HookReturnCode.DECLINED); }
/** * @see * org.apache.james.smtpserver.JamesMessageHook#onMessage(org.apache.james.protocols.smtp.SMTPSession, * org.apache.mailet.Mail) */ public HookResult onMessage(SMTPSession session, Mail mail) { try { MimeMessage message = mail.getMessage(); SpamAssassinInvoker sa = new SpamAssassinInvoker(spamdHost, spamdPort); sa.scanMail(message); // Add the headers for (String key : sa.getHeadersAsAttribute().keySet()) { mail.setAttribute(key, sa.getHeadersAsAttribute().get(key)); } // Check if rejectionHits was configured if (spamdRejectionHits > 0) { try { double hits = Double.parseDouble(sa.getHits()); // if the hits are bigger the rejectionHits reject the // message if (spamdRejectionHits <= hits) { String buffer = "Rejected message from " + session.getAttachment(SMTPSession.SENDER, State.Transaction).toString() + " from host " + session.getRemoteAddress().getHostName() + " (" + session.getRemoteAddress().getAddress().getHostAddress() + ") This message reach the spam hits treshold. Required rejection hits: " + spamdRejectionHits + " hits: " + hits; session.getLogger().info(buffer); // Message reject .. abort it! return new HookResult( HookReturnCode.DENY, DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_OTHER) + " This message reach the spam hits treshold. Please contact the Postmaster if the email is not SPAM. Message rejected"); } } catch (NumberFormatException e) { // hits unknown } } } catch (MessagingException e) { session.getLogger().error(e.getMessage()); } return new HookResult(HookReturnCode.DECLINED); }
/** * Return the service type which will be used in the Received headers. * * @param session * @param heloMode * @return type */ protected String getServiceType(SMTPSession session, String heloMode) { // Check if EHLO was used if (EHLO.equals(heloMode)) { // Not successful auth if (session.getUser() == null) { return ESMTP; } else { // See RFC3848 // The new keyword "ESMTPA" indicates the use of ESMTP when // the // SMTP // AUTH [3] extension is also used and authentication is // successfully // achieved. return ESMTPA; } } else { return SMTP; } }
/** Returns the Received header for the message. */ @SuppressWarnings("unchecked") @Override protected Collection<Header> headers(SMTPSession session) { StringBuilder headerLineBuffer = new StringBuilder(); String heloMode = (String) session.getAttachment(SMTPSession.CURRENT_HELO_MODE, State.Connection); String heloName = (String) session.getAttachment(SMTPSession.CURRENT_HELO_NAME, State.Connection); // Put our Received header first headerLineBuffer.append("from ").append(session.getRemoteAddress().getHostName()); if (heloName != null) { headerLineBuffer.append(" (").append(heloMode).append(" ").append(heloName).append(")"); } headerLineBuffer .append(" ([") .append(session.getRemoteAddress().getAddress().getHostAddress()) .append("])"); Header header = new Header("Received", headerLineBuffer.toString()); headerLineBuffer = new StringBuilder(); headerLineBuffer .append("by ") .append(session.getConfiguration().getHelloName()) .append(" (") .append(session.getConfiguration().getSoftwareName()) .append(") with ") .append(getServiceType(session, heloMode)); headerLineBuffer.append(" ID ").append(session.getSessionID()); if (((Collection<?>) session.getAttachment(SMTPSession.RCPT_LIST, State.Transaction)).size() == 1) { // Only indicate a recipient if they're the only recipient // (prevents email address harvesting and large headers in // bulk email) header.add(headerLineBuffer.toString()); headerLineBuffer = new StringBuilder(); headerLineBuffer .append("for <") .append( ((List<MailAddress>) session.getAttachment(SMTPSession.RCPT_LIST, State.Transaction)) .get(0) .toString()) .append(">;"); } else { // Put the ; on the end of the 'by' line headerLineBuffer.append(";"); } header.add(headerLineBuffer.toString()); headerLineBuffer = new StringBuilder(); headerLineBuffer.append(DATEFORMAT.get().format(new Date())); header.add(headerLineBuffer.toString()); return Arrays.asList(header); }