1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.james.jspf.core;
22
23 import org.apache.james.jspf.core.exceptions.PermErrorException;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Stack;
28
29
30
31
32
33
34
35
36 public class SPFSession implements MacroData {
37
38 private String ipAddress = "";
39
40 private String mailFrom = "";
41
42 private String hostName = "";
43
44 private String currentSenderPart = "";
45
46 private String currentDomain = "";
47
48 private String inAddress = "in-addr";
49
50 private String clientDomain = null;
51
52 private String senderDomain = "";
53
54 private String readableIP = null;
55
56 private String receivingDomain = null;
57
58 private int currentDepth = 0;
59
60
61
62
63 public static final int MAX_DEPTH = 10;
64
65 private String explanation = null;
66
67 private String currentResult = null;
68
69 private boolean ignoreExplanation = false;
70
71 private Map attributes = new HashMap();
72
73 private Stack checkers = new Stack();
74
75 private String currentResultExpanded;
76
77
78
79
80
81
82
83
84
85
86
87
88 public SPFSession(String mailFrom, String heloDomain, String clientIP) {
89 super();
90 this.mailFrom = mailFrom.trim();
91 this.hostName = heloDomain.trim();
92
93 try {
94 this.ipAddress = IPAddr.getProperIpAddress(clientIP.trim());
95
96 this.inAddress = IPAddr.getInAddress(clientIP);
97 } catch (PermErrorException e) {
98
99 this.setCurrentResultExpanded(e.getResult());
100 }
101
102
103 if (mailFrom.equals("")) {
104 this.currentSenderPart = "postmaster";
105 this.senderDomain = hostName;
106 this.mailFrom = currentSenderPart + "@" + hostName;
107 } else {
108 String[] fromParts = mailFrom.split("@");
109
110 if (fromParts.length > 1) {
111 this.senderDomain = fromParts[fromParts.length -1];
112 this.currentSenderPart = mailFrom.substring(0, mailFrom.length() - senderDomain.length() - 1);
113 if (this.currentSenderPart.length() == 0) {
114 this.currentSenderPart = "postmaster";
115 }
116 } else {
117 this.currentSenderPart = "postmaster";
118 this.senderDomain = mailFrom;
119 }
120 }
121 this.currentDomain = this.senderDomain;
122 }
123
124
125
126
127 public String getCurrentSenderPart() {
128 return currentSenderPart;
129 }
130
131
132
133
134 public String getMailFrom() {
135 return mailFrom;
136 }
137
138
139
140
141 public String getHostName() {
142 return hostName;
143 }
144
145
146
147
148 public String getCurrentDomain() {
149 return currentDomain;
150 }
151
152
153
154
155 public String getInAddress() {
156 return inAddress;
157 }
158
159
160
161
162 public String getClientDomain() {
163 return clientDomain;
164 }
165
166
167
168
169
170 public void setClientDomain(String clientDomain) {
171 this.clientDomain = clientDomain;
172 }
173
174
175
176
177 public String getSenderDomain() {
178 return senderDomain;
179 }
180
181
182
183
184
185
186 public String getIpAddress() {
187 return ipAddress;
188 }
189
190
191
192
193 public String getMacroIpAddress() {
194
195 if (IPAddr.isIPV6(ipAddress)) {
196 try {
197 return IPAddr.getAddress(ipAddress).getNibbleFormat();
198 } catch (PermErrorException e) {
199 }
200 }
201
202 return ipAddress;
203
204 }
205
206
207
208
209 public long getTimeStamp() {
210 return System.currentTimeMillis();
211 }
212
213
214
215
216 public String getReadableIP() {
217 if (readableIP == null) {
218 readableIP = IPAddr.getReadableIP(ipAddress);
219 }
220 return readableIP;
221 }
222
223
224
225
226 public String getReceivingDomain() {
227 return receivingDomain;
228 }
229
230
231
232
233
234
235 public void setReceivingDomain(String receivingDomain) {
236 this.receivingDomain = receivingDomain;
237 }
238
239
240
241
242
243
244
245 public void increaseCurrentDepth() throws PermErrorException {
246 this.currentDepth++;
247 if (currentDepth > MAX_DEPTH)
248 throw new PermErrorException(
249 "Maximum mechanism/modifiers calls done: "
250 + currentDepth);
251 }
252
253
254
255
256
257
258 public void setCurrentDomain(String domain) {
259 this.currentDomain = domain;
260 }
261
262
263
264
265
266
267
268 public void setExplanation(String explanation) {
269 this.explanation = explanation;
270 }
271
272
273
274
275
276
277 public String getExplanation() {
278 return explanation;
279 }
280
281
282
283
284
285
286
287 public void setCurrentResult(String result) {
288 this.currentResult = result;
289 }
290
291
292
293
294
295
296 public String getCurrentResult() {
297 return currentResult;
298 }
299
300
301
302
303
304
305 public void setIgnoreExplanation(boolean ignoreExplanation) {
306 this.ignoreExplanation = ignoreExplanation;
307 }
308
309
310
311
312
313
314 public boolean ignoreExplanation() {
315 return ignoreExplanation;
316 }
317
318
319
320
321
322
323
324 public Object getAttribute(String key) {
325 return attributes.get(key);
326 }
327
328
329
330
331
332
333
334 public void setAttribute(String key, Object value) {
335 this.attributes.put(key, value);
336 }
337
338
339
340
341
342
343
344 public Object removeAttribute(String key) {
345 return this.attributes.remove(key);
346 }
347
348
349
350
351
352
353 public void pushChecker(SPFChecker checker) {
354 checkers.push(checker);
355 }
356
357
358
359
360
361
362
363 public SPFChecker popChecker() {
364 if (checkers.isEmpty()) {
365 return null;
366 } else {
367 SPFChecker checker = (SPFChecker) checkers.pop();
368 return checker;
369 }
370 }
371
372
373
374
375 public void setCurrentResultExpanded(String result) {
376 this.currentResultExpanded = result;
377 }
378
379
380
381
382 public String getCurrentResultExpanded() {
383 return currentResultExpanded;
384 }
385
386 }