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
23 import static org.apache.jsieve.tests.AddressPartTags.DOMAIN_TAG;
24 import static org.apache.jsieve.tests.AddressPartTags.LOCALPART_TAG;
25
26 import java.util.List;
27
28 import org.apache.jsieve.SieveContext;
29 import org.apache.jsieve.comparators.ComparatorUtils;
30 import org.apache.jsieve.exception.SieveException;
31 import org.apache.jsieve.mail.MailAdapter;
32 import org.apache.jsieve.mail.SieveMailException;
33 import org.apache.jsieve.mail.optional.EnvelopeAccessors;
34 import org.apache.jsieve.tests.AbstractCompatatorTest;
35
36
37
38
39
40 public class Envelope extends AbstractCompatatorTest {
41
42
43
44
45 public Envelope() {
46 super();
47 }
48
49 protected List<String> getMatchingValues(MailAdapter mail, String valueName)
50 throws SieveMailException {
51 return ((EnvelopeAccessors) mail).getMatchingEnvelope(valueName);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66 protected boolean match(String addressPart, String comparator,
67 String matchType, String headerValue, String key,
68 SieveContext context) throws SieveException {
69
70
71 String matchAddress = null;
72 if (addressPart.equals(":all"))
73 matchAddress = headerValue;
74 else {
75 int localStart = 0;
76 int localEnd = 0;
77 int domainStart = 0;
78 int domainEnd = headerValue.length();
79 int splitIndex = headerValue.indexOf('@');
80
81 if (splitIndex == -1) {
82 localEnd = domainEnd;
83 domainStart = domainEnd;
84 } else {
85 localEnd = splitIndex;
86 domainStart = splitIndex + 1;
87 }
88 matchAddress = (addressPart.equals(LOCALPART_TAG) ? headerValue
89 .substring(localStart, localEnd) : headerValue.substring(
90 domainStart, domainEnd));
91 }
92
93
94 String matchKey = null;
95 if (addressPart.equals(DOMAIN_TAG)) {
96 matchKey = key.toLowerCase();
97 matchAddress = matchAddress.toLowerCase();
98 } else
99 matchKey = key;
100
101
102 return ComparatorUtils.match(comparator, matchType, matchAddress,
103 matchKey, context);
104 }
105
106 protected boolean match(MailAdapter mail, String addressPart,
107 String comparator, String matchType, String headerName, String key,
108 SieveContext context) throws SieveException {
109 final List<String> headerValues = getMatchingValues(mail, headerName);
110 boolean isMatched = false;
111 for (final String value:headerValues) {
112 isMatched = match(addressPart, comparator, matchType, value, key, context);
113 if (isMatched) {
114 break;
115 }
116 }
117 return isMatched;
118 }
119
120 }