1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
39 public class Body extends AbstractTest {
40 private StringListArgument strings;
41
42 public Body() {
43 super();
44 strings = null;
45 }
46
47
48
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
59
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
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
78
79 protected boolean executeBasic(MailAdapter mail, Arguments args,
80 SieveContext ctx) throws SieveException {
81
82
83 if (mail.getContentType().indexOf("text/") != 0) {
84 throw new SieveMailException("Message is not of type 'text'");
85 }
86
87
88 for (final String phrase:strings.getList()) {
89 if (mail.isInBodyText(phrase)) {
90 return true;
91 }
92 }
93 return false;
94 }
95
96 }