View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.transport.matchers;
23  
24  import org.apache.mailet.base.GenericMatcher;
25  import org.apache.mailet.Mail;
26  
27  import javax.mail.MessagingException;
28  import javax.mail.internet.MimeMessage;
29  import java.util.Collection;
30  
31  /**
32   * <p>This matcher tests for the Hebeas Warrant Mark.
33   * For details see: http://www.hebeas.com</p>
34   *
35   * <p>Usage: Place this matcher</p>
36   * <pre><code>
37   * &lt;mailet match="HasHabeasWarrantMark" class="ToProcessor"&gt;
38   *     &lt;processor&gt; transport &lt;/processor&gt;
39   * &lt;/mailet&gt;
40   * </code></pre>
41   * <p>in the root processs before the DNSRBL block lists (the InSpammerBlacklist matcher).</p>
42   *
43   * <p>Because the Habeas Warrant Mark is copyright material, I have asked for and
44   * received the following explicit statement from Habeas:</p>
45   * <pre>
46   * -----------------------------------
47   * From: Lindsey Pettit [mailto:support@habeas.com]
48   * Sent: Sunday, September 29, 2002 5:51
49   * To: Noel J. Bergman
50   * Subject: RE: Habeas and Apache James
51   *
52   * Dear Noel,
53   * 
54   * > I guess that since your Warrant Mark is copyright, I should ask for
55   * > something from you to explicitly authorize that Hebeas will permit
56   * > this code to be included and distributed as part of Apache James
57   * > under the Apache Software License.  As we have established, the use
58   * > of the Habeas Warrant Mark for filtering is not restricted, but I
59   * > would like something to confirm that, so that Apache will be happy.
60   *
61   * I can hereby confirm to you that there is no license necessary in 
62   * order to use the Habeas mark for filtering.  That said, however, we 
63   * do insist that it not ever be used as a basis for rejecting email which 
64   * bears the Habeas mark.
65   * -----------------------------------
66   * </pre>
67   */
68  
69  public class HasHabeasWarrantMark extends GenericMatcher
70  {
71      public static final String[][] warrantMark =
72      {
73          { "X-Habeas-SWE-1", "winter into spring" }, 
74          { "X-Habeas-SWE-2", "brightly anticipated" }, 
75          { "X-Habeas-SWE-3", "like Habeas SWE (tm)" }, 
76          { "X-Habeas-SWE-4", "Copyright 2002 Habeas (tm)" }, 
77          { "X-Habeas-SWE-5", "Sender Warranted Email (SWE) (tm). The sender of this" }, 
78          { "X-Habeas-SWE-6", "email in exchange for a license for this Habeas" }, 
79          { "X-Habeas-SWE-7", "warrant mark warrants that this is a Habeas Compliant" }, 
80          { "X-Habeas-SWE-8", "Message (HCM) and not spam. Please report use of this" }, 
81          { "X-Habeas-SWE-9", "mark in spam to <http://www.habeas.com/report/>." }, 
82      };
83  
84      public Collection match(Mail mail) throws MessagingException
85      {
86          MimeMessage message = mail.getMessage();
87  
88          //Loop through all the patterns
89          for (int i = 0; i < warrantMark.length; i++) try
90          {
91              String headerName = warrantMark[i][0];                      //Get the header name
92              String requiredValue = warrantMark[i][1];                   //Get the required value
93              String headerValue = message.getHeader(headerName, null);   //Get the header value(s)
94  
95              // We want an exact match, so only test the first value.
96              // If there are multiple values, the header may be
97              // (illegally) forged.  I'll leave it as an exercise to
98              // others if they want to detect and report potentially
99              // forged headers.
100 
101             if (!(requiredValue.equals(headerValue))) return null;
102         }
103         catch (Exception e)
104         {
105             log(e.toString());
106             return null;            //if we get an exception, don't validate the mark
107         }
108 
109         // If we get here, all headers are present and match.
110         return mail.getRecipients();
111     }
112 
113     /*
114      * Returns information about the matcher, such as author, version, and copyright.
115      * <p>
116      * The string that this method returns should be plain text and not markup
117      * of any kind (such as HTML, XML, etc.).
118      *
119      * @return a String containing matcher information
120      */
121 
122     public String getMatcherInfo()
123     {
124         return "Habeas Warrant Mark Matcher (see http://www.habeas.com for details).";
125     }
126 }
127