1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve.util.check;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.List;
28
29 import javax.mail.Header;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32
33 import org.apache.jsieve.SieveContext;
34 import org.apache.jsieve.exception.SieveException;
35 import org.apache.jsieve.mail.Action;
36 import org.apache.jsieve.mail.MailAdapter;
37 import org.apache.jsieve.mail.MailUtils;
38 import org.apache.jsieve.mail.SieveMailException;
39 import org.apache.jsieve.parser.address.SieveAddressBuilder;
40 import org.apache.jsieve.parser.generated.address.ParseException;
41
42
43
44
45
46
47 public class ScriptCheckMailAdapter implements MailAdapter {
48
49 private final List<Action> actions;
50
51 private final List<Action> executedActions;
52
53 private Message mail = null;
54
55 public ScriptCheckMailAdapter() {
56 actions = new ArrayList<Action>();
57 executedActions = new ArrayList<Action>();
58 }
59
60
61
62
63
64
65 public Message getMail() {
66 return mail;
67 }
68
69
70
71
72
73
74
75
76 public void setMail(Message mail) {
77 this.mail = mail;
78 reset();
79 }
80
81
82
83
84
85
86
87 public void addAction(final Action action) {
88 actions.add(action);
89 }
90
91
92
93
94 public void executeActions() throws SieveException {
95 executedActions.clear();
96 executedActions.addAll(actions);
97 }
98
99
100
101
102
103
104
105
106 public List<Action> getExecutedActions() {
107 final ArrayList<Action> result = new ArrayList<Action>(executedActions);
108 return result;
109 }
110
111
112
113
114
115
116
117
118 public List<Action> getActions() {
119 final List<Action> result = Collections.unmodifiableList(actions);
120 return result;
121 }
122
123
124
125
126
127 public void reset() {
128 executedActions.clear();
129 actions.clear();
130 }
131
132
133
134
135
136
137
138
139
140
141 @SuppressWarnings("unchecked")
142 public List<String> getHeader(String name) throws SieveMailException {
143 List<String> result = Collections.EMPTY_LIST;
144 if (mail != null) {
145 try {
146 String[] values = mail.getHeader(name);
147 if (values != null) {
148 result = Arrays.asList(values);
149 }
150 } catch (MessagingException e) {
151 throw new SieveMailException(e);
152 }
153 }
154 return result;
155 }
156
157
158
159
160
161
162
163
164
165 @SuppressWarnings("unchecked")
166 public List<String> getHeaderNames() throws SieveMailException {
167 List<String> results = Collections.EMPTY_LIST;
168 if (mail != null) {
169 try {
170 results = new ArrayList<String>();
171 for (final Enumeration en = mail.getAllHeaders(); en
172 .hasMoreElements();) {
173 final Header header = (Header) en.nextElement();
174 final String name = header.getName();
175 if (!results.contains(name)) {
176 results.add(name);
177 }
178 }
179 } catch (MessagingException e) {
180 throw new SieveMailException(e);
181 }
182 }
183 return results;
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 @SuppressWarnings("unchecked")
205 public List<String> getMatchingHeader(String name) throws SieveMailException {
206 List<String> result = Collections.EMPTY_LIST;
207 if (mail != null) {
208 result = MailUtils.getMatchingHeader(this, name);
209 }
210 return result;
211 }
212
213
214
215
216
217
218
219 public int getSize() throws SieveMailException {
220 int result = 0;
221 if (mail != null) {
222 try {
223 result = mail.getSize();
224 } catch (MessagingException e) {
225 throw new SieveMailException(e);
226 }
227 }
228 return result;
229 }
230
231
232
233
234
235
236
237
238 public String getContentType() throws SieveMailException {
239 String result = null;
240 if (mail != null) {
241 try {
242 result = mail.getContentType();
243 } catch (MessagingException e) {
244 throw new SieveMailException(e);
245 }
246 }
247 return result;
248 }
249
250 public Address[] parseAddresses(String headerName)
251 throws SieveMailException {
252 return parseAddresses(headerName, mail);
253 }
254
255
256
257
258
259
260
261
262
263
264
265 public Address[] parseAddresses(final String headerName,
266 final Message message) throws SieveMailException {
267 try {
268 final SieveAddressBuilder builder = new SieveAddressBuilder();
269
270 for (Enumeration en = message.getAllHeaders(); en.hasMoreElements();) {
271 final Header header = (Header) en.nextElement();
272 final String name = header.getName();
273 if (name.trim().equalsIgnoreCase(headerName)) {
274 builder.addAddresses(header.getValue());
275 }
276 }
277
278 final Address[] results = builder.getAddresses();
279 return results;
280
281 } catch (MessagingException ex) {
282 throw new SieveMailException(ex);
283 } catch (ParseException ex) {
284 throw new SieveMailException(ex);
285 }
286 }
287
288 public boolean isInBodyText(String phraseCaseInsensitive) throws SieveMailException {
289 boolean result = false;
290 if (mail != null) {
291 try {
292 result = mail.getContent().toString().toLowerCase().contains(phraseCaseInsensitive);
293 } catch (MessagingException e) {
294 throw new SieveMailException(e);
295 } catch (IOException e) {
296 throw new SieveMailException(e);
297 }
298 }
299 return result;
300 }
301
302 public void setContext(SieveContext context) {}
303
304 }