0
|
1 <%
|
|
2 package nabble.view.web.tools;
|
|
3
|
|
4 import nabble.model.DailyNumber;
|
|
5 import nabble.model.ModelException;
|
|
6 import nabble.model.Person;
|
|
7 import nabble.model.SystemProperties;
|
|
8 import nabble.model.User;
|
|
9 import nabble.view.lib.Jtp;
|
|
10 import nabble.view.lib.Shared;
|
|
11 import org.slf4j.Logger;
|
|
12 import org.slf4j.LoggerFactory;
|
|
13
|
|
14 import javax.servlet.ServletException;
|
|
15 import javax.servlet.http.HttpServlet;
|
|
16 import javax.servlet.http.HttpServletRequest;
|
|
17 import javax.servlet.http.HttpServletResponse;
|
|
18 import java.io.IOException;
|
|
19 import java.io.PrintWriter;
|
|
20 import java.util.ArrayList;
|
|
21 import java.util.List;
|
|
22 import java.util.regex.Pattern;
|
|
23
|
|
24
|
|
25 public final class SpamRules extends HttpServlet {
|
|
26
|
|
27 private static final Logger logger = LoggerFactory.getLogger(SpamRules.class);
|
|
28
|
|
29 private static final List<Rule> RULES = new ArrayList<Rule>();
|
|
30
|
|
31 static {
|
|
32 String spamRules = SystemProperties.get("spam.rules");
|
|
33 if (spamRules != null)
|
|
34 RULES.addAll(parseRules(spamRules));
|
|
35 }
|
|
36
|
|
37 private interface Rule {
|
|
38 public String checkSubject(String subject) throws ModelException.SpamException;
|
|
39 public String checkMessage(String message, Person visitor) throws ModelException.SpamException;
|
|
40 }
|
|
41 /*
|
|
42 public static void checkSubject(HttpServletRequest request, String subject)
|
|
43 throws ModelException.SpamException
|
|
44 {
|
|
45 if (subject == null)
|
|
46 return;
|
|
47
|
|
48 // Removes duplicated space chars
|
|
49 subject = subject.replaceAll("[ ]+"," ");
|
|
50 for (Rule rule : RULES) {
|
|
51 try {
|
|
52 rule.checkSubject(subject);
|
|
53 } catch (ModelException.SpamException e) {
|
|
54 DailyNumber.blockedSpams.inc();
|
|
55 if (request != null)
|
|
56 logger.info("host="+request.getServerName()+" | IP=" + Jtp.getClientIpAddr(request) + " | " + e.getMessage());
|
|
57 else
|
|
58 logger.info("Blocked Mail | " + e.getMessage());
|
|
59 throw e;
|
|
60 }
|
|
61 }
|
|
62 }
|
|
63
|
|
64 public static void checkMessage(HttpServletRequest request, String message, Person visitor)
|
|
65 throws ModelException.SpamException
|
|
66 {
|
|
67 if (message == null)
|
|
68 return;
|
|
69
|
|
70 // Removes duplicated space chars
|
|
71 message = message.replaceAll("[ ]+"," ");
|
|
72 for (Rule rule : RULES) {
|
|
73 try {
|
|
74 rule.checkMessage(message, visitor);
|
|
75 } catch (ModelException.SpamException e) {
|
|
76 DailyNumber.blockedSpams.inc();
|
|
77 String email = visitor instanceof User? ((User) visitor).getEmail() : "NO EMAIL";
|
|
78 if (request != null)
|
|
79 logger.info("host="+request.getServerName()+" | IP=" + Jtp.getClientIpAddr(request) + " | " + email + " | " + e.getMessage());
|
|
80 else
|
|
81 logger.info("Blocked Mail | " + email + " | " + e.getMessage());
|
|
82 throw e;
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86 */
|
|
87 protected void service(HttpServletRequest request,HttpServletResponse response)
|
|
88 throws ServletException, IOException
|
|
89 {
|
|
90 String spamRules = SystemProperties.get("spam.rules");
|
|
91
|
|
92 if ("save".equals(request.getParameter("action"))) {
|
|
93 String rules = request.getParameter("rules");
|
|
94 RULES.clear();
|
|
95 if (rules != null && rules.length() > 0) {
|
|
96 List<Rule> r = parseRules(rules);
|
|
97 if (r.size() > 0)
|
|
98 RULES.addAll(r);
|
|
99 }
|
|
100 SystemProperties.set("spam.rules", rules);
|
|
101 response.sendRedirect("/tools/");
|
|
102 return;
|
|
103 }
|
|
104
|
|
105 PrintWriter out = response.getWriter();
|
|
106 %>
|
|
107 <html>
|
|
108 <head>
|
|
109 <script src="<%=Shared.getJQueryPath()%>"></script>
|
|
110 <link rel="stylesheet" href="<%=Shared.getCssPath()%>" type="text/css" />
|
|
111 </head>
|
|
112 <body>
|
|
113 <div>
|
|
114 <a href="/tools/">Tools</a>
|
|
115 </div>
|
|
116 <h1>Spam Rules</h1>
|
|
117 <div id="nabble" class="nabble">
|
|
118 <form action="/tools/SpamRules.jtp" method="POST">
|
|
119 <input type="hidden" name="action" value="save">
|
|
120 <textarea name="rules" rows=15 style="width:80%"><%=Jtp.hideNull(spamRules)%></textarea>
|
|
121 <br>
|
|
122 <input type="submit" value="Save Rules">
|
|
123 </form>
|
|
124 </div>
|
|
125 </body>
|
|
126 </html>
|
|
127 <%
|
|
128 }
|
|
129
|
|
130 private static synchronized List<Rule> parseRules(String rules) {
|
|
131 List<Rule> r = new ArrayList<Rule>();
|
|
132 String[] lines = rules.split("\n");
|
|
133 for (String line : lines) {
|
|
134 if (line.length() == 0 || line.startsWith("#")) {
|
|
135 // do nothing
|
|
136 } else if (line.startsWith("subject-contains:"))
|
|
137 r.add(new SubjectContainsRule(line.substring("subject-contains:".length()).trim()));
|
|
138 else if (line.startsWith("message-contains:"))
|
|
139 r.add(new MessageContainsRule(line.substring("message-contains:".length()).trim()));
|
|
140 else if (line.startsWith("message-contains-pattern:"))
|
|
141 r.add(new MessageContainsPattern(line.substring("message-contains-pattern:".length()).trim()));
|
|
142 else if (line.startsWith("message-word-count:"))
|
|
143 r.add(new WordCountRule(line.substring("message-word-count:".length()).trim()));
|
|
144 else if (line.startsWith("subject-max-words:"))
|
|
145 r.add(new SubjectMaxWordsRule(line.substring("subject-max-words:".length()).trim()));
|
|
146 else if (line.startsWith("message-max-words:"))
|
|
147 r.add(new MessageMaxWordsRule(line.substring("message-max-words:".length()).trim()));
|
|
148 else if (line.startsWith("user-email-pattern:"))
|
|
149 r.add(new UserEmailPattern(line.substring("user-email-pattern:".length()).trim()));
|
|
150 }
|
|
151 return r;
|
|
152 }
|
|
153
|
|
154 private static class SubjectContainsRule implements Rule {
|
|
155 private String text;
|
|
156 private String[] parts;
|
|
157 public SubjectContainsRule(String text) {
|
|
158 this.text = text;
|
|
159 this.parts = text.toLowerCase().split("\\+");
|
|
160 }
|
|
161 public String checkSubject(String subject)
|
|
162 throws ModelException.SpamException
|
|
163 {
|
|
164 if (subject == null) return null;
|
|
165 subject = subject.toLowerCase();
|
|
166 for (String part : parts) {
|
|
167 if (subject.indexOf(part) == -1)
|
|
168 return null;
|
|
169 }
|
|
170 throw new ModelException.SubjectContainsInvalidWord(text);
|
|
171 }
|
|
172 public String checkMessage(String message, Person visitor) { return null; }
|
|
173 }
|
|
174
|
|
175 private static class SubjectMaxWordsRule implements Rule {
|
|
176 private int max;
|
|
177 private String[] parts;
|
|
178 public SubjectMaxWordsRule(String text) {
|
|
179 String[] params = text.split("\\|");
|
|
180 this.max = Integer.valueOf(params[0]);
|
|
181 this.parts = params[1].toLowerCase().split("\\+");
|
|
182 }
|
|
183 public String checkSubject(String subject)
|
|
184 throws ModelException.SpamException
|
|
185 {
|
|
186 if (subject == null) return null;
|
|
187 subject = subject.toLowerCase();
|
|
188 int count = 0;
|
|
189 for (String part : parts) {
|
|
190 if (subject.indexOf(part) >= 0)
|
|
191 count++;
|
|
192 if (count >= max)
|
|
193 throw new ModelException.SubjectContainsCommonSpamWords(subject);
|
|
194 }
|
|
195 return null;
|
|
196 }
|
|
197 public String checkMessage(String message, Person visitor) { return null; }
|
|
198 }
|
|
199
|
|
200 private static class MessageMaxWordsRule implements Rule {
|
|
201 private int max;
|
|
202 private String[] parts;
|
|
203 public MessageMaxWordsRule(String text) {
|
|
204 String[] params = text.split("\\|");
|
|
205 this.max = Integer.valueOf(params[0]);
|
|
206 this.parts = params[1].toLowerCase().split("\\+");
|
|
207 }
|
|
208 public String checkMessage(String message, Person visitor)
|
|
209 throws ModelException.SpamException
|
|
210 {
|
|
211 if (message == null) return null;
|
|
212 message = message.toLowerCase();
|
|
213 int count = 0;
|
|
214 for (String part : parts) {
|
|
215 if (message.indexOf(part) >= 0)
|
|
216 count++;
|
|
217 if (count >= max)
|
|
218 throw new ModelException.MessageContainsCommonSpamWords(message);
|
|
219 }
|
|
220 return null;
|
|
221 }
|
|
222 public String checkSubject(String subject) { return null; }
|
|
223 }
|
|
224
|
|
225 private static class MessageContainsRule implements Rule {
|
|
226 private String text;
|
|
227 private String[] parts;
|
|
228 public MessageContainsRule(String text) {
|
|
229 this.text = text;
|
|
230 this.parts = text.toLowerCase().split("\\+");
|
|
231 }
|
|
232 public String checkMessage(String message, Person visitor)
|
|
233 throws ModelException.SpamException
|
|
234 {
|
|
235 if (message == null) return null;
|
|
236 String msg = message.toLowerCase();
|
|
237 for (String part : parts) {
|
|
238 if (msg.indexOf(part) == -1)
|
|
239 return null;
|
|
240 }
|
|
241 throw new ModelException.MessageContainsInvalidWord(text);
|
|
242 }
|
|
243 public String checkSubject(String subject) { return null; }
|
|
244 }
|
|
245
|
|
246 private static class MessageContainsPattern implements Rule {
|
|
247 private Pattern[] pattern;
|
|
248 public MessageContainsPattern(String patternList) {
|
|
249 String[] patterns = patternList.split("____");
|
|
250 this.pattern = new Pattern[patterns.length];
|
|
251 for (int i = 0; i < patterns.length; i++)
|
|
252 this.pattern[i] = Pattern.compile(patterns[i]);
|
|
253 }
|
|
254 public String checkMessage(String message, Person visitor)
|
|
255 throws ModelException.SpamException
|
|
256 {
|
|
257 if (message == null) return null;
|
|
258 for (Pattern p : pattern) {
|
|
259 if (!p.matcher(message).find())
|
|
260 return null;
|
|
261 }
|
|
262 throw new ModelException.MessageContainsCommonSpamWords(message);
|
|
263 }
|
|
264 public String checkSubject(String subject) { return null; }
|
|
265 }
|
|
266
|
|
267 private static class UserEmailPattern implements Rule {
|
|
268 private Pattern pattern;
|
|
269 public UserEmailPattern(String pattern) {
|
|
270 this.pattern = Pattern.compile(pattern);
|
|
271 }
|
|
272 public String checkMessage(String message, Person visitor)
|
|
273 throws ModelException.SpamException
|
|
274 {
|
|
275 if (visitor instanceof User) {
|
|
276 String email = ((User) visitor).getEmail();
|
|
277 if (!pattern.matcher(email).matches())
|
|
278 return null;
|
|
279 throw new ModelException.MessageContainsInvalidWord("EMAIL: " + email);
|
|
280 }
|
|
281 return null;
|
|
282 }
|
|
283 public String checkSubject(String subject) { return null; }
|
|
284 }
|
|
285
|
|
286 private static class WordCountRule implements Rule {
|
|
287 private String text;
|
|
288 private int count;
|
|
289 public WordCountRule(String value) {
|
|
290 String[] values = value.split(">");
|
|
291 this.text = values[0].toLowerCase();
|
|
292 this.count = Integer.valueOf(values[1]);
|
|
293 }
|
|
294 public String checkMessage(String message, Person visitor)
|
|
295 throws ModelException.SpamException
|
|
296 {
|
|
297 if (message == null) return null;
|
|
298 String msg = message.toLowerCase();
|
|
299 int oldPos = 0;
|
|
300 int newPos = msg.indexOf(text);
|
|
301 int c = 0;
|
|
302 while (newPos > oldPos) {
|
|
303 c++;
|
|
304 if (c >= count)
|
|
305 throw new ModelException.MessageContainsManyInvalidWords(text);
|
|
306 oldPos = newPos;
|
|
307 newPos = msg.indexOf(text, oldPos+text.length());
|
|
308 }
|
|
309 return null;
|
|
310 }
|
|
311 public String checkSubject(String subject) { return null; }
|
|
312 }
|
|
313 }
|
|
314 %>
|