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 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
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 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
58
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
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
77
78 protected boolean executeBasic(MailAdapter mail, Arguments args,
79 SieveContext ctx) throws SieveException {
80
81
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
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 }