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  
24  import org.apache.jsieve.Argument;
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   * Implementation of body extension defined in
37   * <a href='http://tools.ietf.org/html/rfc5173'>RFC5173</a>.
38   */
39  public class Body extends AbstractTest {
40      private StringListArgument strings;
41  
42      public Body() {
43          super();
44          strings = null;
45      }
46  
47      // TODO: Check how complete this is of the body specification
48      // Validate (sorta); we're only implementing part of the spec
49      protected void validateArguments(Arguments args, SieveContext ctx)
50              throws SieveException {
51  
52          final List<Argument> arglist = args.getArgumentList();
53          if (arglist.size() != 2) {
54              throw new SyntaxException(
55                      "Currently body-test can only two arguments");
56          }
57  
58          // TODO: FIXME: As this is a limited implementation force the use of
59          // ':contains'.
60          Argument arg = arglist.get(0);
61          if (!(arg instanceof TagArgument)) {
62              throw new SyntaxException("Body expects a :contains tag");
63          }
64  
65          if (!((TagArgument) arg).getTag().equals(":contains")) {
66              throw new SyntaxException("Body expects a :contains tag");
67          }
68  
69          // Get list of strings to search for
70          arg = arglist.get(1);
71          if (!(arg instanceof StringListArgument)) {
72              throw new SyntaxException("Body expects a list of strings");
73          }
74          strings = (StringListArgument) args.getArgumentList().get(1);
75      }
76  
77      // This implement body tests of the form
78      // "body :contains ['string' 'string' ....]"
79      protected boolean executeBasic(MailAdapter mail, Arguments args,
80              SieveContext ctx) throws SieveException {
81          // Attempt to fetch content as a string. If we can't do this it's
82          // not a message we can handle.
83          if (mail.getContentType().indexOf("text/") != 0) {
84              throw new SieveMailException("Message is not of type 'text'");
85          }
86  
87          // Compare each test string with body, ignoring case
88          for (final String phrase:strings.getList()) {
89              if (mail.isInBodyText(phrase)) {
90                  return true;
91              }
92          }
93          return false;
94      }
95  
96  }