1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.mercury.smtp.logging;
14
15 import org.abstracthorizon.danube.connection.Connection;
16 import org.abstracthorizon.danube.support.logging.patternsupport.PatternProcessor;
17 import org.abstracthorizon.danube.support.logging.util.StringUtil;
18 import org.abstracthorizon.mercury.smtp.SMTPSession;
19 import org.abstracthorizon.mercury.smtp.filter.MailSessionData;
20 import org.abstracthorizon.mercury.smtp.util.Path;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public class SMTPSPAMPatternProcessor implements PatternProcessor {
37
38
39 protected int bytesSentIndex = -1;
40
41
42 protected int bytesSent0Index = -1;
43
44
45 protected int ehloIndex = -1;
46
47
48 protected int returnCodeIndex = -1;
49
50
51 protected int sourceIndex = -1;
52
53
54 protected int recipientsIndex = -1;
55
56
57
58
59
60 public SMTPSPAMPatternProcessor() {
61 }
62
63
64
65
66
67
68 public int init(int index, StringBuffer message) {
69 bytesSentIndex = -1;
70 bytesSent0Index = -1;
71 ehloIndex = -1;
72
73 if (message.indexOf("%b") >= 0) {
74 StringUtil.replaceAll(message, "%b", "{" + index + "}");
75 bytesSentIndex = index;
76 index = index + 1;
77 }
78
79 if (message.indexOf("%B") >= 0) {
80 StringUtil.replaceAll(message, "%B", "{" + index + "}");
81 bytesSent0Index = index;
82 index = index + 1;
83 }
84
85 if (message.indexOf("%E") >= 0) {
86 StringUtil.replaceAll(message, "%E", "{" + index + "}");
87 ehloIndex = index;
88 index = index + 1;
89 }
90
91 if (message.indexOf("%R") >= 0) {
92 StringUtil.replaceAll(message, "%R", "{" + index + "}");
93 recipientsIndex = index;
94 index = index + 1;
95 }
96
97 if (message.indexOf("%r") >= 0) {
98 StringUtil.replaceAll(message, "%r", "{" + index + "}");
99 returnCodeIndex = index;
100 index = index + 1;
101 }
102
103 if (message.indexOf("%S") >= 0) {
104 StringUtil.replaceAll(message, "%S", "{" + index + "}");
105 sourceIndex = index;
106 index = index + 1;
107 }
108
109 return index;
110 }
111
112
113
114
115
116
117 public void process(Connection connection, Object[] array) {
118 SMTPSession smtpSession = (SMTPSession)connection.adapt(SMTPSession.class);
119 MailSessionData data = smtpSession.getMailSessionData();
120
121 if (bytesSentIndex >= 0) {
122 long b = data.getTotalBytes();
123 if (b <= 0) {
124 array[bytesSentIndex] = "-";
125 } else {
126 array[bytesSentIndex] = Long.toString(b);
127 }
128 }
129
130 if (bytesSent0Index >= 0) {
131 array[bytesSent0Index] = Long.toString(data.getTotalBytes());
132 }
133
134 if (ehloIndex >= 0) {
135 array[ehloIndex] = data.getSourceDomain();
136 }
137
138 if (returnCodeIndex >= 0) {
139 int code = data.getReturnCode();
140 if (code == 250) {
141 code = 1;
142 }
143 array[returnCodeIndex] = Integer.toString(code);
144 }
145
146 if (sourceIndex >= 0) {
147 Path source = data.getSourceMailbox();
148 if (source == null) {
149 array[sourceIndex] = "-";
150 } else {
151 array[sourceIndex] = source.toMailboxString();
152 }
153 }
154
155 if (recipientsIndex >= 0) {
156 StringBuffer recipients = new StringBuffer();
157 boolean first = true;
158 if (data.getDestinationMailboxes() != null) {
159 for (Path path : data.getDestinationMailboxes()) {
160 if (first) {
161 first = false;
162 } else {
163 recipients.append(',');
164 }
165 recipients.append(path.toMailboxString());
166 }
167 if (recipients.length() == 0) {
168 array[recipientsIndex] = "-";
169 } else {
170 array[recipientsIndex] = recipients;
171 }
172 } else {
173 array[recipientsIndex] = "-";
174 }
175 }
176 }
177 }