Repository: james-project Updated Branches: refs/heads/master 21bd4ffad -> 500510f11
JAMES-2340 Introduce IsMarkedAsSpam matcher Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/500510f1 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/500510f1 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/500510f1 Branch: refs/heads/master Commit: 500510f11432196114023c7bcac37d985ad5b7f2 Parents: 21bd4ff Author: Antoine Duprat <adup...@linagora.com> Authored: Tue Feb 20 15:17:38 2018 +0100 Committer: Antoine Duprat <adup...@linagora.com> Committed: Tue Feb 27 08:36:09 2018 +0100 ---------------------------------------------------------------------- .../transport/matchers/IsMarkedAsSpam.java | 83 +++++++++++++ .../transport/matchers/IsMarkedAsSpamTest.java | 121 +++++++++++++++++++ 2 files changed, 204 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/500510f1/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/IsMarkedAsSpam.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/IsMarkedAsSpam.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/IsMarkedAsSpam.java new file mode 100644 index 0000000..4faea53 --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/matchers/IsMarkedAsSpam.java @@ -0,0 +1,83 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.transport.matchers; + +import java.io.Serializable; +import java.util.Collection; +import java.util.Locale; + +import javax.mail.MessagingException; + +import org.apache.james.core.MailAddress; +import org.apache.james.util.scanner.SpamAssassinInvoker; +import org.apache.mailet.Mail; +import org.apache.mailet.base.GenericMatcher; + +import com.google.common.collect.ImmutableList; + +/** + * <p> + * Matches mails having a <pre>org.apache.james.spamassassin.status</pre> attribute with a <pre>Yes</pre> value. + * </p> + * + * As an example, here is a part of a mailet pipeline which can be used in your LocalDelivery processor: + * <pre>{@code + * <!-- SpamAssassing mailets pipeline --> + * <mailet match="RecipientIsLocal" class="SpamAssassin"> + * <spamdHost>spamassassin</spamdHost> + * <spamdPort>783</spamdPort> + * </mailet> + * <mailet match="IsMarkedAsSpam" class="ToRecipientFolder"> + * <folder>Spam</folder> + * <consume>true</consume> + * </mailet> + * <!-- End of SpamAssassing mailets pipeline --> + * }</pre> + */ +public class IsMarkedAsSpam extends GenericMatcher { + + private static final String YES = "yes"; + + @Override + public String getMatcherInfo() { + return "Has org.apache.james.spamassassin.status attribute with a Yes value Matcher"; + } + + @Override + public void init() throws MessagingException { + } + + @Override + public Collection<MailAddress> match(Mail mail) throws MessagingException { + Serializable attribute = mail.getAttribute(SpamAssassinInvoker.STATUS_MAIL_ATTRIBUTE_NAME); + if (isMarkedAsSpam(attribute)) { + return mail.getRecipients(); + } + return ImmutableList.of(); + } + + private boolean isMarkedAsSpam(Serializable attribute) { + return attribute != null && + attribute.toString() + .toLowerCase(Locale.US) + .startsWith(YES); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/500510f1/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/IsMarkedAsSpamTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/IsMarkedAsSpamTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/IsMarkedAsSpamTest.java new file mode 100644 index 0000000..a33f763 --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/matchers/IsMarkedAsSpamTest.java @@ -0,0 +1,121 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.transport.matchers; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Collection; + +import org.apache.james.core.MailAddress; +import org.apache.mailet.base.test.FakeMail; +import org.junit.Before; +import org.junit.Test; + +public class IsMarkedAsSpamTest { + + private IsMarkedAsSpam matcher; + + @Before + public void setup() { + matcher = new IsMarkedAsSpam(); + } + + @Test + public void isMarkedAsSpamShouldNotMatchWhenNoHeader() throws Exception { + FakeMail mail = FakeMail.builder() + .sender("sen...@james.org") + .recipient("t...@james.org") + .build(); + + Collection<MailAddress> matches = matcher.match(mail); + assertThat(matches).isEmpty(); + } + + @Test + public void isMarkedAsSpamShouldNotMatchWhenHeaderButNullValue() throws Exception { + FakeMail mail = FakeMail.builder() + .sender("sen...@james.org") + .recipient("t...@james.org") + .attribute("org.apache.james.spamassassin.status", null) + .build(); + + Collection<MailAddress> matches = matcher.match(mail); + assertThat(matches).isEmpty(); + } + + @Test + public void isMarkedAsSpamShouldNotMatchWhenHeaderButEmptyValue() throws Exception { + FakeMail mail = FakeMail.builder() + .sender("sen...@james.org") + .recipient("t...@james.org") + .attribute("org.apache.james.spamassassin.status", "") + .build(); + + Collection<MailAddress> matches = matcher.match(mail); + assertThat(matches).isEmpty(); + } + + @Test + public void isMarkedAsSpamShouldNotMatchWhenHeaderButOtherValue() throws Exception { + FakeMail mail = FakeMail.builder() + .sender("sen...@james.org") + .recipient("t...@james.org") + .attribute("org.apache.james.spamassassin.status", "other") + .build(); + + Collection<MailAddress> matches = matcher.match(mail); + assertThat(matches).isEmpty(); + } + + @Test + public void isMarkedAsSpamShouldNotMatchWhenHeaderButNoValue() throws Exception { + FakeMail mail = FakeMail.builder() + .sender("sen...@james.org") + .recipient("t...@james.org") + .attribute("org.apache.james.spamassassin.status", "No, hits=1.8 required=5.0") + .build(); + + Collection<MailAddress> matches = matcher.match(mail); + assertThat(matches).isEmpty(); + } + + @Test + public void isMarkedAsSpamShouldMatchWhenHeaderAndYesValue() throws Exception { + FakeMail mail = FakeMail.builder() + .sender("sen...@james.org") + .recipient("t...@james.org") + .attribute("org.apache.james.spamassassin.status", "Yes, hits=6.8 required=5.0") + .build(); + + Collection<MailAddress> matches = matcher.match(mail); + assertThat(matches).contains(new MailAddress("t...@james.org")); + } + + @Test + public void isMarkedAsSpamShouldMatchWhenHeaderAndYesValueInOtherCase() throws Exception { + FakeMail mail = FakeMail.builder() + .sender("sen...@james.org") + .recipient("t...@james.org") + .attribute("org.apache.james.spamassassin.status", "YES, hits=6.8 required=5.0") + .build(); + + Collection<MailAddress> matches = matcher.match(mail); + assertThat(matches).contains(new MailAddress("t...@james.org")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org