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  package org.apache.jsieve.tests.optional;
21  
22  import java.util.List;
23  import java.util.ListIterator;
24  
25  import org.apache.jsieve.Arguments;
26  import org.apache.jsieve.SieveContext;
27  import org.apache.jsieve.StringListArgument;
28  import org.apache.jsieve.TagArgument;
29  import org.apache.jsieve.exception.SieveException;
30  import org.apache.jsieve.exception.SyntaxException;
31  import org.apache.jsieve.mail.MailAdapter;
32  import org.apache.jsieve.mail.SieveMailException;
33  import org.apache.jsieve.tests.AbstractTest;
34  
35  /**
36   * This implements a single subset of the 'body' Sieve test as define here:
37   * http://tools.ietf.org/html/draft-ietf-sieve-body-00
38   */
39  public class Body extends AbstractTest {
40      private StringListArgument strings;
41  
42      public Body() {
43          super();
44          strings = null;
45      }
46  
47      // Validate (sorta); we're only implementing part of the spec
48      protected void validateArguments(Arguments args, SieveContext ctx)
49              throws SieveException {
50  
51          List arglist = args.getArgumentList();
52          if (arglist.size() != 2) {
53              throw new SyntaxException(
54                      "Currently body-test can only two arguments");
55          }
56  
57          // FIXME: As this is a limited implementation force the use of
58          // ':contains'.
59          Object arg = arglist.get(0);
60          if (!(arg instanceof TagArgument)) {
61              throw new SyntaxException("Body expects a :contains tag");
62          }
63  
64          if (!((TagArgument) arg).getTag().equals(":contains")) {
65              throw new SyntaxException("Body expects a :contains tag");
66          }
67  
68          // Get list of strings to search for
69          arg = arglist.get(1);
70          if (!(arg instanceof StringListArgument)) {
71              throw new SyntaxException("Body expects a list of strings");
72          }
73          strings = (StringListArgument) args.getArgumentList().get(1);
74      }
75  
76      // This implement body tests of the form
77      // "body :contains ['string' 'string' ....]"
78      protected boolean executeBasic(MailAdapter mail, Arguments args,
79              SieveContext ctx) throws SieveException {
80          // Attempt to fetch content as a string. If we can't do this it's
81          // not a message we can handle.
82          if (mail.getContentType().indexOf("text/") != 0) {
83              throw new SieveMailException("Message is not of type 'text'");
84          }
85          String body = (String) mail.getContent();
86          body = body.toLowerCase();
87  
88          // Compare each test string with body, ignoring case
89          ListIterator iter = strings.getList().listIterator();
90          while (iter.hasNext()) {
91              String str = (String) iter.next();
92              if (body.indexOf(str.toLowerCase()) != -1) {
93                  return true;
94              }
95          }
96          return false;
97      }
98  
99  }