Commit | Line | Data |
---|---|---|
23f87bed MB |
1 | ;;; spam.el --- Identifying spam |
2 | ;; Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. | |
3 | ||
4 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
5 | ;; Keywords: network | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
9 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation; either version 2, or (at your option) | |
12 | ;; any later version. | |
13 | ||
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | ;; Boston, MA 02111-1307, USA. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;;; This module addresses a few aspects of spam control under Gnus. Page | |
27 | ;;; breaks are used for grouping declarations and documentation relating to | |
28 | ;;; each particular aspect. | |
29 | ||
30 | ;;; The integration with Gnus is not yet complete. See various `FIXME' | |
31 | ;;; comments, below, for supplementary explanations or discussions. | |
32 | ||
33 | ;;; Several TODO items are marked as such | |
34 | ||
35 | ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting, | |
36 | ;; remote processing, training through files | |
37 | ||
38 | ;;; Code: | |
39 | ||
40 | (eval-when-compile (require 'cl)) | |
41 | ||
42 | (require 'gnus-sum) | |
43 | ||
44 | (require 'gnus-uu) ; because of key prefix issues | |
45 | ;;; for the definitions of group content classification and spam processors | |
46 | (require 'gnus) | |
47 | (require 'message) ;for the message-fetch-field functions | |
48 | ||
49 | ;; for nnimap-split-download-body-default | |
50 | (eval-when-compile (require 'nnimap)) | |
51 | ||
52 | ;; autoload executable-find | |
53 | (eval-and-compile | |
54 | ;; executable-find is not autoloaded in Emacs 20 | |
55 | (autoload 'executable-find "executable")) | |
56 | ||
57 | ;; autoload query-dig | |
58 | (eval-and-compile | |
59 | (autoload 'query-dig "dig")) | |
60 | ||
61 | ;; autoload spam-report | |
62 | (eval-and-compile | |
63 | (autoload 'spam-report-gmane "spam-report")) | |
64 | ||
65 | ;; autoload gnus-registry | |
66 | (eval-and-compile | |
67 | (autoload 'gnus-registry-group-count "gnus-registry") | |
68 | (autoload 'gnus-registry-add-group "gnus-registry") | |
69 | (autoload 'gnus-registry-store-extra-entry "gnus-registry") | |
70 | (autoload 'gnus-registry-fetch-extra "gnus-registry")) | |
71 | ||
72 | ;; autoload query-dns | |
73 | (eval-and-compile | |
74 | (autoload 'query-dns "dns")) | |
75 | ||
76 | ;;; Main parameters. | |
77 | ||
78 | (defgroup spam nil | |
ba5037ec RS |
79 | "Spam configuration." |
80 | :version "21.4") | |
23f87bed | 81 | |
531e5812 | 82 | (defcustom spam-directory (nnheader-concat gnus-directory "spam/") |
23f87bed MB |
83 | "Directory for spam whitelists and blacklists." |
84 | :type 'directory | |
85 | :group 'spam) | |
86 | ||
87 | (defcustom spam-move-spam-nonspam-groups-only t | |
88 | "Whether spam should be moved in non-spam groups only. | |
89 | When t, only ham and unclassified groups will have their spam moved | |
90 | to the spam-process-destination. When nil, spam will also be moved from | |
91 | spam groups." | |
92 | :type 'boolean | |
93 | :group 'spam) | |
94 | ||
95 | (defcustom spam-process-ham-in-nonham-groups nil | |
96 | "Whether ham should be processed in non-ham groups." | |
97 | :type 'boolean | |
98 | :group 'spam) | |
99 | ||
100 | (defcustom spam-log-to-registry nil | |
101 | "Whether spam/ham processing should be logged in the registry." | |
102 | :type 'boolean | |
103 | :group 'spam) | |
104 | ||
105 | (defcustom spam-split-symbolic-return nil | |
106 | "Whether `spam-split' should work with symbols or group names." | |
107 | :type 'boolean | |
108 | :group 'spam) | |
109 | ||
110 | (defcustom spam-split-symbolic-return-positive nil | |
111 | "Whether `spam-split' should ALWAYS work with symbols or group names. | |
112 | Do not set this if you use `spam-split' in a fancy split | |
113 | method." | |
114 | :type 'boolean | |
115 | :group 'spam) | |
116 | ||
117 | (defcustom spam-process-ham-in-spam-groups nil | |
118 | "Whether ham should be processed in spam groups." | |
119 | :type 'boolean | |
120 | :group 'spam) | |
121 | ||
122 | (defcustom spam-mark-only-unseen-as-spam t | |
123 | "Whether only unseen articles should be marked as spam in spam groups. | |
124 | When nil, all unread articles in a spam group are marked as | |
125 | spam. Set this if you want to leave an article unread in a spam group | |
126 | without losing it to the automatic spam-marking process." | |
127 | :type 'boolean | |
128 | :group 'spam) | |
129 | ||
130 | (defcustom spam-mark-ham-unread-before-move-from-spam-group nil | |
131 | "Whether ham should be marked unread before it's moved. | |
132 | The article is moved out of a spam group according to ham-process-destination. | |
133 | This variable is an official entry in the international Longest Variable Name | |
134 | Competition." | |
135 | :type 'boolean | |
136 | :group 'spam) | |
137 | ||
138 | (defcustom spam-disable-spam-split-during-ham-respool nil | |
139 | "Whether `spam-split' should be ignored while resplitting ham in a process | |
140 | destination. This is useful to prevent ham from ending up in the same spam | |
141 | group after the resplit. Don't set this to t if you have spam-split as the | |
142 | last rule in your split configuration." | |
143 | :type 'boolean | |
144 | :group 'spam) | |
145 | ||
146 | (defcustom spam-autodetect-recheck-messages nil | |
147 | "Should spam.el recheck all meessages when autodetecting? | |
148 | Normally this is nil, so only unseen messages will be checked." | |
149 | :type 'boolean | |
150 | :group 'spam) | |
151 | ||
152 | (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory) | |
153 | "The location of the whitelist. | |
154 | The file format is one regular expression per line. | |
155 | The regular expression is matched against the address." | |
156 | :type 'file | |
157 | :group 'spam) | |
158 | ||
159 | (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory) | |
160 | "The location of the blacklist. | |
161 | The file format is one regular expression per line. | |
162 | The regular expression is matched against the address." | |
163 | :type 'file | |
164 | :group 'spam) | |
165 | ||
166 | (defcustom spam-use-dig t | |
167 | "Whether `query-dig' should be used instead of `query-dns'." | |
168 | :type 'boolean | |
169 | :group 'spam) | |
170 | ||
171 | (defcustom spam-use-blacklist nil | |
172 | "Whether the blacklist should be used by `spam-split'." | |
173 | :type 'boolean | |
174 | :group 'spam) | |
175 | ||
176 | (defcustom spam-blacklist-ignored-regexes nil | |
177 | "Regular expressions that the blacklist should ignore." | |
178 | :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting")) | |
179 | :group 'spam) | |
180 | ||
181 | (defcustom spam-use-whitelist nil | |
182 | "Whether the whitelist should be used by `spam-split'." | |
183 | :type 'boolean | |
184 | :group 'spam) | |
185 | ||
186 | (defcustom spam-use-whitelist-exclusive nil | |
187 | "Whether whitelist-exclusive should be used by `spam-split'. | |
188 | Exclusive whitelisting means that all messages from senders not in the whitelist | |
189 | are considered spam." | |
190 | :type 'boolean | |
191 | :group 'spam) | |
192 | ||
193 | (defcustom spam-use-blackholes nil | |
194 | "Whether blackholes should be used by `spam-split'." | |
195 | :type 'boolean | |
196 | :group 'spam) | |
197 | ||
198 | (defcustom spam-use-hashcash nil | |
199 | "Whether hashcash payments should be detected by `spam-split'." | |
200 | :type 'boolean | |
201 | :group 'spam) | |
202 | ||
203 | (defcustom spam-use-regex-headers nil | |
204 | "Whether a header regular expression match should be used by `spam-split'. | |
205 | Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'." | |
206 | :type 'boolean | |
207 | :group 'spam) | |
208 | ||
209 | (defcustom spam-use-regex-body nil | |
210 | "Whether a body regular expression match should be used by `spam-split'. | |
211 | Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'." | |
212 | :type 'boolean | |
213 | :group 'spam) | |
214 | ||
215 | (defcustom spam-use-bogofilter-headers nil | |
216 | "Whether bogofilter headers should be used by `spam-split'. | |
217 | Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them." | |
218 | :type 'boolean | |
219 | :group 'spam) | |
220 | ||
221 | (defcustom spam-use-bogofilter nil | |
222 | "Whether bogofilter should be invoked by `spam-split'. | |
223 | Enable this if you want Gnus to invoke Bogofilter on new messages." | |
224 | :type 'boolean | |
225 | :group 'spam) | |
226 | ||
227 | (defcustom spam-use-BBDB nil | |
228 | "Whether BBDB should be used by `spam-split'." | |
229 | :type 'boolean | |
230 | :group 'spam) | |
231 | ||
232 | (defcustom spam-use-BBDB-exclusive nil | |
233 | "Whether BBDB-exclusive should be used by `spam-split'. | |
234 | Exclusive BBDB means that all messages from senders not in the BBDB are | |
235 | considered spam." | |
236 | :type 'boolean | |
237 | :group 'spam) | |
238 | ||
239 | (defcustom spam-use-ifile nil | |
240 | "Whether ifile should be used by `spam-split'." | |
241 | :type 'boolean | |
242 | :group 'spam) | |
243 | ||
244 | (defcustom spam-use-stat nil | |
245 | "Whether `spam-stat' should be used by `spam-split'." | |
246 | :type 'boolean | |
247 | :group 'spam) | |
248 | ||
249 | (defcustom spam-use-spamoracle nil | |
250 | "Whether spamoracle should be used by `spam-split'." | |
251 | :type 'boolean | |
252 | :group 'spam) | |
253 | ||
254 | (defcustom spam-install-hooks (or | |
255 | spam-use-dig | |
256 | spam-use-blacklist | |
257 | spam-use-whitelist | |
258 | spam-use-whitelist-exclusive | |
259 | spam-use-blackholes | |
260 | spam-use-hashcash | |
261 | spam-use-regex-headers | |
262 | spam-use-regex-body | |
263 | spam-use-bogofilter-headers | |
264 | spam-use-bogofilter | |
265 | spam-use-BBDB | |
266 | spam-use-BBDB-exclusive | |
267 | spam-use-ifile | |
268 | spam-use-stat | |
269 | spam-use-spamoracle) | |
270 | "Whether the spam hooks should be installed. | |
271 | Default to t if one of the spam-use-* variables is set." | |
272 | :group 'spam | |
273 | :type 'boolean) | |
274 | ||
275 | (defcustom spam-split-group "spam" | |
276 | "Group name where incoming spam should be put by `spam-split'." | |
277 | :type 'string | |
278 | :group 'spam) | |
279 | ||
280 | ;;; TODO: deprecate this variable, it's confusing since it's a list of strings, | |
281 | ;;; not regular expressions | |
282 | (defcustom spam-junk-mailgroups (cons | |
283 | spam-split-group | |
284 | '("mail.junk" "poste.pourriel")) | |
285 | "Mailgroups with spam contents. | |
286 | All unmarked article in such group receive the spam mark on group entry." | |
287 | :type '(repeat (string :tag "Group")) | |
288 | :group 'spam) | |
289 | ||
290 | (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org" | |
291 | "dev.null.dk" "relays.visi.com") | |
292 | "List of blackhole servers." | |
293 | :type '(repeat (string :tag "Server")) | |
294 | :group 'spam) | |
295 | ||
296 | (defcustom spam-blackhole-good-server-regex nil | |
297 | "String matching IP addresses that should not be checked in the blackholes." | |
ad136a7c | 298 | :type '(radio (const nil) regexp) |
23f87bed MB |
299 | :group 'spam) |
300 | ||
301 | (defcustom spam-face 'gnus-splash-face | |
302 | "Face for spam-marked articles." | |
303 | :type 'face | |
304 | :group 'spam) | |
305 | ||
306 | (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES") | |
307 | "Regular expression for positive header spam matches." | |
308 | :type '(repeat (regexp :tag "Regular expression to match spam header")) | |
309 | :group 'spam) | |
310 | ||
311 | (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO") | |
312 | "Regular expression for positive header ham matches." | |
313 | :type '(repeat (regexp :tag "Regular expression to match ham header")) | |
314 | :group 'spam) | |
315 | ||
316 | (defcustom spam-regex-body-spam '() | |
317 | "Regular expression for positive body spam matches." | |
318 | :type '(repeat (regexp :tag "Regular expression to match spam body")) | |
319 | :group 'spam) | |
320 | ||
321 | (defcustom spam-regex-body-ham '() | |
322 | "Regular expression for positive body ham matches." | |
323 | :type '(repeat (regexp :tag "Regular expression to match ham body")) | |
324 | :group 'spam) | |
325 | ||
326 | (defgroup spam-ifile nil | |
327 | "Spam ifile configuration." | |
328 | :group 'spam) | |
329 | ||
330 | (defcustom spam-ifile-path (executable-find "ifile") | |
331 | "File path of the ifile executable program." | |
332 | :type '(choice (file :tag "Location of ifile") | |
333 | (const :tag "ifile is not installed")) | |
334 | :group 'spam-ifile) | |
335 | ||
336 | (defcustom spam-ifile-database-path nil | |
337 | "File path of the ifile database." | |
338 | :type '(choice (file :tag "Location of the ifile database") | |
339 | (const :tag "Use the default")) | |
340 | :group 'spam-ifile) | |
341 | ||
342 | (defcustom spam-ifile-spam-category "spam" | |
343 | "Name of the spam ifile category." | |
344 | :type 'string | |
345 | :group 'spam-ifile) | |
346 | ||
347 | (defcustom spam-ifile-ham-category nil | |
348 | "Name of the ham ifile category. | |
349 | If nil, the current group name will be used." | |
350 | :type '(choice (string :tag "Use a fixed category") | |
351 | (const :tag "Use the current group name")) | |
352 | :group 'spam-ifile) | |
353 | ||
354 | (defcustom spam-ifile-all-categories nil | |
355 | "Whether the ifile check will return all categories, or just spam. | |
356 | Set this to t if you want to use the `spam-split' invocation of ifile as | |
357 | your main source of newsgroup names." | |
358 | :type 'boolean | |
359 | :group 'spam-ifile) | |
360 | ||
361 | (defgroup spam-bogofilter nil | |
362 | "Spam bogofilter configuration." | |
363 | :group 'spam) | |
364 | ||
365 | (defcustom spam-bogofilter-path (executable-find "bogofilter") | |
366 | "File path of the Bogofilter executable program." | |
367 | :type '(choice (file :tag "Location of bogofilter") | |
368 | (const :tag "Bogofilter is not installed")) | |
369 | :group 'spam-bogofilter) | |
370 | ||
371 | (defcustom spam-bogofilter-header "X-Bogosity" | |
372 | "The header that Bogofilter inserts in messages." | |
373 | :type 'string | |
374 | :group 'spam-bogofilter) | |
375 | ||
376 | (defcustom spam-bogofilter-spam-switch "-s" | |
377 | "The switch that Bogofilter uses to register spam messages." | |
378 | :type 'string | |
379 | :group 'spam-bogofilter) | |
380 | ||
381 | (defcustom spam-bogofilter-ham-switch "-n" | |
382 | "The switch that Bogofilter uses to register ham messages." | |
383 | :type 'string | |
384 | :group 'spam-bogofilter) | |
385 | ||
386 | (defcustom spam-bogofilter-spam-strong-switch "-S" | |
387 | "The switch that Bogofilter uses to unregister ham messages." | |
388 | :type 'string | |
389 | :group 'spam-bogofilter) | |
390 | ||
391 | (defcustom spam-bogofilter-ham-strong-switch "-N" | |
392 | "The switch that Bogofilter uses to unregister spam messages." | |
393 | :type 'string | |
394 | :group 'spam-bogofilter) | |
395 | ||
396 | (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)" | |
397 | "The regex on `spam-bogofilter-header' for positive spam identification." | |
398 | :type 'regexp | |
399 | :group 'spam-bogofilter) | |
400 | ||
401 | (defcustom spam-bogofilter-database-directory nil | |
402 | "Directory path of the Bogofilter databases." | |
403 | :type '(choice (directory | |
404 | :tag "Location of the Bogofilter database directory") | |
405 | (const :tag "Use the default")) | |
406 | :group 'spam-bogofilter) | |
407 | ||
408 | (defgroup spam-spamoracle nil | |
409 | "Spam spamoracle configuration." | |
410 | :group 'spam) | |
411 | ||
412 | (defcustom spam-spamoracle-database nil | |
413 | "Location of spamoracle database file. When nil, use the default | |
414 | spamoracle database." | |
415 | :type '(choice (directory :tag "Location of spamoracle database file.") | |
416 | (const :tag "Use the default")) | |
417 | :group 'spam-spamoracle) | |
418 | ||
419 | (defcustom spam-spamoracle-binary (executable-find "spamoracle") | |
420 | "Location of the spamoracle binary." | |
421 | :type '(choice (directory :tag "Location of the spamoracle binary") | |
422 | (const :tag "Use the default")) | |
423 | :group 'spam-spamoracle) | |
424 | ||
425 | ;;; Key bindings for spam control. | |
426 | ||
427 | (gnus-define-keys gnus-summary-mode-map | |
428 | "St" spam-bogofilter-score | |
429 | "Sx" gnus-summary-mark-as-spam | |
430 | "Mst" spam-bogofilter-score | |
431 | "Msx" gnus-summary-mark-as-spam | |
432 | "\M-d" gnus-summary-mark-as-spam) | |
433 | ||
434 | (defvar spam-old-ham-articles nil | |
435 | "List of old ham articles, generated when a group is entered.") | |
436 | ||
437 | (defvar spam-old-spam-articles nil | |
438 | "List of old spam articles, generated when a group is entered.") | |
439 | ||
440 | (defvar spam-split-disabled nil | |
441 | "If non-nil, `spam-split' is disabled, and always returns nil.") | |
442 | ||
443 | (defvar spam-split-last-successful-check nil | |
444 | "`spam-split' will set this to nil or a spam-use-XYZ check if it | |
445 | finds ham or spam.") | |
446 | ||
447 | ;; convenience functions | |
448 | (defun spam-xor (a b) | |
449 | "Logical exclusive `or'." | |
450 | (and (or a b) (not (and a b)))) | |
451 | ||
452 | (defun spam-group-ham-mark-p (group mark &optional spam) | |
453 | (when (stringp group) | |
454 | (let* ((marks (spam-group-ham-marks group spam)) | |
455 | (marks (if (symbolp mark) | |
456 | marks | |
457 | (mapcar 'symbol-value marks)))) | |
458 | (memq mark marks)))) | |
459 | ||
460 | (defun spam-group-spam-mark-p (group mark) | |
461 | (spam-group-ham-mark-p group mark t)) | |
462 | ||
463 | (defun spam-group-ham-marks (group &optional spam) | |
464 | (when (stringp group) | |
465 | (let* ((marks (if spam | |
466 | (gnus-parameter-spam-marks group) | |
467 | (gnus-parameter-ham-marks group))) | |
468 | (marks (car marks)) | |
469 | (marks (if (listp (car marks)) (car marks) marks))) | |
470 | marks))) | |
471 | ||
472 | (defun spam-group-spam-marks (group) | |
473 | (spam-group-ham-marks group t)) | |
474 | ||
475 | (defun spam-group-spam-contents-p (group) | |
476 | (if (stringp group) | |
477 | (or (member group spam-junk-mailgroups) | |
478 | (memq 'gnus-group-spam-classification-spam | |
479 | (gnus-parameter-spam-contents group))) | |
480 | nil)) | |
481 | ||
482 | (defun spam-group-ham-contents-p (group) | |
483 | (if (stringp group) | |
484 | (memq 'gnus-group-spam-classification-ham | |
485 | (gnus-parameter-spam-contents group)) | |
486 | nil)) | |
487 | ||
488 | (defvar spam-list-of-processors | |
489 | '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane) | |
490 | (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter) | |
491 | (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist) | |
492 | (gnus-group-spam-exit-processor-ifile spam spam-use-ifile) | |
493 | (gnus-group-spam-exit-processor-stat spam spam-use-stat) | |
494 | (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle) | |
495 | (gnus-group-ham-exit-processor-ifile ham spam-use-ifile) | |
496 | (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter) | |
497 | (gnus-group-ham-exit-processor-stat ham spam-use-stat) | |
498 | (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist) | |
499 | (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB) | |
500 | (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy) | |
501 | (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle)) | |
502 | "The spam-list-of-processors list contains pairs associating a | |
503 | ham/spam exit processor variable with a classification and a | |
504 | spam-use-* variable.") | |
505 | ||
506 | (defun spam-group-processor-p (group processor) | |
507 | (if (and (stringp group) | |
508 | (symbolp processor)) | |
509 | (or (member processor (nth 0 (gnus-parameter-spam-process group))) | |
510 | (spam-group-processor-multiple-p | |
511 | group | |
512 | (cdr-safe (assoc processor spam-list-of-processors)))) | |
513 | nil)) | |
514 | ||
515 | (defun spam-group-processor-multiple-p (group processor-info) | |
516 | (let* ((classification (nth 0 processor-info)) | |
517 | (check (nth 1 processor-info)) | |
518 | (parameters (nth 0 (gnus-parameter-spam-process group))) | |
519 | found) | |
520 | (dolist (parameter parameters) | |
521 | (when (and (null found) | |
522 | (listp parameter) | |
523 | (eq classification (nth 0 parameter)) | |
524 | (eq check (nth 1 parameter))) | |
525 | (setq found t))) | |
526 | found)) | |
527 | ||
528 | (defun spam-group-spam-processor-report-gmane-p (group) | |
529 | (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane)) | |
530 | ||
531 | (defun spam-group-spam-processor-bogofilter-p (group) | |
532 | (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter)) | |
533 | ||
534 | (defun spam-group-spam-processor-blacklist-p (group) | |
535 | (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist)) | |
536 | ||
537 | (defun spam-group-spam-processor-ifile-p (group) | |
538 | (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile)) | |
539 | ||
540 | (defun spam-group-ham-processor-ifile-p (group) | |
541 | (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile)) | |
542 | ||
543 | (defun spam-group-spam-processor-spamoracle-p (group) | |
544 | (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle)) | |
545 | ||
546 | (defun spam-group-ham-processor-bogofilter-p (group) | |
547 | (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter)) | |
548 | ||
549 | (defun spam-group-spam-processor-stat-p (group) | |
550 | (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat)) | |
551 | ||
552 | (defun spam-group-ham-processor-stat-p (group) | |
553 | (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat)) | |
554 | ||
555 | (defun spam-group-ham-processor-whitelist-p (group) | |
556 | (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist)) | |
557 | ||
558 | (defun spam-group-ham-processor-BBDB-p (group) | |
559 | (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB)) | |
560 | ||
561 | (defun spam-group-ham-processor-copy-p (group) | |
562 | (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy)) | |
563 | ||
564 | (defun spam-group-ham-processor-spamoracle-p (group) | |
565 | (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle)) | |
566 | ||
567 | ;;; Summary entry and exit processing. | |
568 | ||
569 | (defun spam-summary-prepare () | |
570 | (setq spam-old-ham-articles | |
571 | (spam-list-articles gnus-newsgroup-articles 'ham)) | |
572 | (setq spam-old-spam-articles | |
573 | (spam-list-articles gnus-newsgroup-articles 'spam)) | |
574 | (spam-mark-junk-as-spam-routine)) | |
575 | ||
576 | ;; The spam processors are invoked for any group, spam or ham or neither | |
577 | (defun spam-summary-prepare-exit () | |
578 | (unless gnus-group-is-exiting-without-update-p | |
579 | (gnus-message 6 "Exiting summary buffer and applying spam rules") | |
580 | ||
581 | ;; first of all, unregister any articles that are no longer ham or spam | |
582 | ;; we have to iterate over the processors, or else we'll be too slow | |
583 | (dolist (classification '(spam ham)) | |
584 | (let* ((old-articles (if (eq classification 'spam) | |
585 | spam-old-spam-articles | |
586 | spam-old-ham-articles)) | |
587 | (new-articles (spam-list-articles | |
588 | gnus-newsgroup-articles | |
589 | classification)) | |
590 | (changed-articles (gnus-set-difference old-articles new-articles))) | |
591 | ;; now that we have the changed articles, we go through the processors | |
592 | (dolist (processor-param spam-list-of-processors) | |
593 | (let ((processor (nth 0 processor-param)) | |
594 | (processor-classification (nth 1 processor-param)) | |
595 | (check (nth 2 processor-param)) | |
596 | unregister-list) | |
597 | (dolist (article changed-articles) | |
598 | (let ((id (spam-fetch-field-message-id-fast article))) | |
599 | (when (spam-log-unregistration-needed-p | |
600 | id 'process classification check) | |
601 | (push article unregister-list)))) | |
602 | ;; call spam-register-routine with specific articles to unregister, | |
603 | ;; when there are articles to unregister and the check is enabled | |
604 | (when (and unregister-list (symbol-value check)) | |
605 | (spam-register-routine classification check t unregister-list)))))) | |
606 | ||
607 | ;; find all the spam processors applicable to this group | |
608 | (dolist (processor-param spam-list-of-processors) | |
609 | (let ((processor (nth 0 processor-param)) | |
610 | (classification (nth 1 processor-param)) | |
611 | (check (nth 2 processor-param))) | |
612 | (when (and (eq 'spam classification) | |
613 | (spam-group-processor-p gnus-newsgroup-name processor)) | |
614 | (spam-register-routine classification check)))) | |
615 | ||
616 | (if spam-move-spam-nonspam-groups-only | |
617 | (when (not (spam-group-spam-contents-p gnus-newsgroup-name)) | |
618 | (spam-mark-spam-as-expired-and-move-routine | |
619 | (gnus-parameter-spam-process-destination gnus-newsgroup-name))) | |
620 | (gnus-message 5 "Marking spam as expired and moving it to %s" | |
621 | gnus-newsgroup-name) | |
622 | (spam-mark-spam-as-expired-and-move-routine | |
623 | (gnus-parameter-spam-process-destination gnus-newsgroup-name))) | |
624 | ||
625 | ;; now we redo spam-mark-spam-as-expired-and-move-routine to only | |
626 | ;; expire spam, in case the above did not expire them | |
627 | (gnus-message 5 "Marking spam as expired without moving it") | |
628 | (spam-mark-spam-as-expired-and-move-routine nil) | |
629 | ||
630 | (when (or (spam-group-ham-contents-p gnus-newsgroup-name) | |
631 | (and (spam-group-spam-contents-p gnus-newsgroup-name) | |
632 | spam-process-ham-in-spam-groups) | |
633 | spam-process-ham-in-nonham-groups) | |
634 | ;; find all the ham processors applicable to this group | |
635 | (dolist (processor-param spam-list-of-processors) | |
636 | (let ((processor (nth 0 processor-param)) | |
637 | (classification (nth 1 processor-param)) | |
638 | (check (nth 2 processor-param))) | |
639 | (when (and (eq 'ham classification) | |
640 | (spam-group-processor-p gnus-newsgroup-name processor)) | |
641 | (spam-register-routine classification check))))) | |
642 | ||
643 | (when (spam-group-ham-processor-copy-p gnus-newsgroup-name) | |
644 | (gnus-message 5 "Copying ham") | |
645 | (spam-ham-copy-routine | |
646 | (gnus-parameter-ham-process-destination gnus-newsgroup-name))) | |
647 | ||
648 | ;; now move all ham articles out of spam groups | |
649 | (when (spam-group-spam-contents-p gnus-newsgroup-name) | |
650 | (gnus-message 5 "Moving ham messages from spam group") | |
651 | (spam-ham-move-routine | |
652 | (gnus-parameter-ham-process-destination gnus-newsgroup-name)))) | |
653 | ||
654 | (setq spam-old-ham-articles nil) | |
655 | (setq spam-old-spam-articles nil)) | |
656 | ||
657 | (defun spam-mark-junk-as-spam-routine () | |
658 | ;; check the global list of group names spam-junk-mailgroups and the | |
659 | ;; group parameters | |
660 | (when (spam-group-spam-contents-p gnus-newsgroup-name) | |
661 | (gnus-message 5 "Marking %s articles as spam" | |
662 | (if spam-mark-only-unseen-as-spam | |
663 | "unseen" | |
664 | "unread")) | |
665 | (let ((articles (if spam-mark-only-unseen-as-spam | |
666 | gnus-newsgroup-unseen | |
667 | gnus-newsgroup-unreads))) | |
668 | (dolist (article articles) | |
669 | (gnus-summary-mark-article article gnus-spam-mark))))) | |
670 | ||
671 | (defun spam-mark-spam-as-expired-and-move-routine (&rest groups) | |
672 | (if (and (car-safe groups) (listp (car-safe groups))) | |
673 | (apply 'spam-mark-spam-as-expired-and-move-routine (car groups)) | |
674 | (gnus-summary-kill-process-mark) | |
675 | (let ((articles gnus-newsgroup-articles) | |
676 | (backend-supports-deletions | |
677 | (gnus-check-backend-function | |
678 | 'request-move-article gnus-newsgroup-name)) | |
679 | article tomove deletep) | |
680 | (dolist (article articles) | |
681 | (when (eq (gnus-summary-article-mark article) gnus-spam-mark) | |
682 | (gnus-summary-mark-article article gnus-expirable-mark) | |
683 | (push article tomove))) | |
684 | ||
685 | ;; now do the actual copies | |
686 | (dolist (group groups) | |
687 | (when (and tomove | |
688 | (stringp group)) | |
689 | (dolist (article tomove) | |
690 | (gnus-summary-set-process-mark article)) | |
691 | (when tomove | |
692 | (if (or (not backend-supports-deletions) | |
693 | (> (length groups) 1)) | |
694 | (progn | |
695 | (gnus-summary-copy-article nil group) | |
696 | (setq deletep t)) | |
697 | (gnus-summary-move-article nil group))))) | |
698 | ||
699 | ;; now delete the articles, if there was a copy done, and the | |
700 | ;; backend allows it | |
701 | (when (and deletep backend-supports-deletions) | |
702 | (dolist (article tomove) | |
703 | (gnus-summary-set-process-mark article)) | |
704 | (when tomove | |
705 | (let ((gnus-novice-user nil)) ; don't ask me if I'm sure | |
706 | (gnus-summary-delete-article nil)))) | |
707 | ||
708 | (gnus-summary-yank-process-mark)))) | |
709 | ||
710 | (defun spam-ham-copy-or-move-routine (copy groups) | |
711 | (gnus-summary-kill-process-mark) | |
712 | (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham)) | |
713 | (backend-supports-deletions | |
714 | (gnus-check-backend-function | |
715 | 'request-move-article gnus-newsgroup-name)) | |
716 | (respool-method (gnus-find-method-for-group gnus-newsgroup-name)) | |
717 | article mark todo deletep respool) | |
718 | ||
719 | (when (member 'respool groups) | |
720 | (setq respool t) ; boolean for later | |
721 | (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it | |
722 | ||
723 | ;; now do the actual move | |
724 | (dolist (group groups) | |
725 | (when (and todo (stringp group)) | |
726 | (dolist (article todo) | |
727 | (when spam-mark-ham-unread-before-move-from-spam-group | |
728 | (gnus-summary-mark-article article gnus-unread-mark)) | |
729 | (gnus-summary-set-process-mark article)) | |
730 | ||
731 | (if respool ; respooling is with a "fake" group | |
732 | (let ((spam-split-disabled | |
733 | (or spam-split-disabled | |
734 | spam-disable-spam-split-during-ham-respool))) | |
735 | (gnus-summary-respool-article nil respool-method)) | |
736 | (if (or (not backend-supports-deletions) ; else, we are not respooling | |
737 | (> (length groups) 1)) | |
738 | (progn ; if copying, copy and set deletep | |
739 | (gnus-summary-copy-article nil group) | |
740 | (setq deletep t)) | |
741 | (gnus-summary-move-article nil group))))) ; else move articles | |
742 | ||
743 | ;; now delete the articles, unless a) copy is t, and there was a copy done | |
744 | ;; b) a move was done to a single group | |
745 | ;; c) backend-supports-deletions is nil | |
746 | (unless copy | |
747 | (when (and deletep backend-supports-deletions) | |
748 | (dolist (article todo) | |
749 | (gnus-summary-set-process-mark article)) | |
750 | (when todo | |
751 | (let ((gnus-novice-user nil)) ; don't ask me if I'm sure | |
752 | (gnus-summary-delete-article nil)))))) | |
753 | ||
754 | (gnus-summary-yank-process-mark)) | |
755 | ||
756 | (defun spam-ham-copy-routine (&rest groups) | |
757 | (if (and (car-safe groups) (listp (car-safe groups))) | |
758 | (apply 'spam-ham-copy-routine (car groups)) | |
759 | (spam-ham-copy-or-move-routine t groups))) | |
760 | ||
761 | (defun spam-ham-move-routine (&rest groups) | |
762 | (if (and (car-safe groups) (listp (car-safe groups))) | |
763 | (apply 'spam-ham-move-routine (car groups)) | |
764 | (spam-ham-copy-or-move-routine nil groups))) | |
765 | ||
766 | (eval-and-compile | |
767 | (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol) | |
768 | 'point-at-eol | |
769 | 'line-end-position))) | |
770 | ||
771 | (defun spam-get-article-as-string (article) | |
772 | (let ((article-buffer (spam-get-article-as-buffer article)) | |
773 | article-string) | |
774 | (when article-buffer | |
775 | (save-window-excursion | |
776 | (set-buffer article-buffer) | |
777 | (setq article-string (buffer-string)))) | |
778 | article-string)) | |
779 | ||
780 | (defun spam-get-article-as-buffer (article) | |
781 | (let ((article-buffer)) | |
782 | (when (numberp article) | |
783 | (save-window-excursion | |
784 | (gnus-summary-goto-subject article) | |
785 | (gnus-summary-show-article t) | |
786 | (setq article-buffer (get-buffer gnus-article-buffer)))) | |
787 | article-buffer)) | |
788 | ||
789 | ;; disabled for now | |
790 | ;; (defun spam-get-article-as-filename (article) | |
791 | ;; (let ((article-filename)) | |
792 | ;; (when (numberp article) | |
793 | ;; (nnml-possibly-change-directory | |
794 | ;; (gnus-group-real-name gnus-newsgroup-name)) | |
795 | ;; (setq article-filename (expand-file-name | |
796 | ;; (int-to-string article) nnml-current-directory))) | |
797 | ;; (if (file-exists-p article-filename) | |
798 | ;; article-filename | |
799 | ;; nil))) | |
800 | ||
801 | (defun spam-fetch-field-from-fast (article) | |
802 | "Fetch the `from' field quickly, using the internal gnus-data-list function" | |
803 | (if (and (numberp article) | |
804 | (assoc article (gnus-data-list nil))) | |
805 | (mail-header-from | |
806 | (gnus-data-header (assoc article (gnus-data-list nil)))) | |
807 | nil)) | |
808 | ||
809 | (defun spam-fetch-field-subject-fast (article) | |
810 | "Fetch the `subject' field quickly, using the internal | |
811 | gnus-data-list function" | |
812 | (if (and (numberp article) | |
813 | (assoc article (gnus-data-list nil))) | |
814 | (mail-header-subject | |
815 | (gnus-data-header (assoc article (gnus-data-list nil)))) | |
816 | nil)) | |
817 | ||
818 | (defun spam-fetch-field-message-id-fast (article) | |
819 | "Fetch the `Message-ID' field quickly, using the internal | |
820 | gnus-data-list function" | |
821 | (if (and (numberp article) | |
822 | (assoc article (gnus-data-list nil))) | |
823 | (mail-header-message-id | |
824 | (gnus-data-header (assoc article (gnus-data-list nil)))) | |
825 | nil)) | |
826 | ||
827 | \f | |
828 | ;;;; Spam determination. | |
829 | ||
830 | (defvar spam-list-of-checks | |
831 | '((spam-use-blacklist . spam-check-blacklist) | |
832 | (spam-use-regex-headers . spam-check-regex-headers) | |
833 | (spam-use-regex-body . spam-check-regex-body) | |
834 | (spam-use-whitelist . spam-check-whitelist) | |
835 | (spam-use-BBDB . spam-check-BBDB) | |
836 | (spam-use-ifile . spam-check-ifile) | |
837 | (spam-use-spamoracle . spam-check-spamoracle) | |
838 | (spam-use-stat . spam-check-stat) | |
839 | (spam-use-blackholes . spam-check-blackholes) | |
840 | (spam-use-hashcash . spam-check-hashcash) | |
841 | (spam-use-bogofilter-headers . spam-check-bogofilter-headers) | |
842 | (spam-use-bogofilter . spam-check-bogofilter)) | |
843 | "The spam-list-of-checks list contains pairs associating a | |
844 | parameter variable with a spam checking function. If the | |
845 | parameter variable is true, then the checking function is called, | |
846 | and its value decides what happens. Each individual check may | |
847 | return nil, t, or a mailgroup name. The value nil means that the | |
848 | check does not yield a decision, and so, that further checks are | |
849 | needed. The value t means that the message is definitely not | |
850 | spam, and that further spam checks should be inhibited. | |
851 | Otherwise, a mailgroup name or the symbol 'spam (depending on | |
852 | spam-split-symbolic-return) is returned where the mail should go, | |
853 | and further checks are also inhibited. The usual mailgroup name | |
854 | is the value of `spam-split-group', meaning that the message is | |
855 | definitely a spam.") | |
856 | ||
857 | (defvar spam-list-of-statistical-checks | |
858 | '(spam-use-ifile | |
859 | spam-use-regex-body | |
860 | spam-use-stat | |
861 | spam-use-bogofilter | |
862 | spam-use-spamoracle) | |
863 | "The spam-list-of-statistical-checks list contains all the mail | |
864 | splitters that need to have the full message body available.") | |
865 | ||
866 | ;;;TODO: modify to invoke self with each check if invoked without specifics | |
867 | (defun spam-split (&rest specific-checks) | |
868 | "Split this message into the `spam' group if it is spam. | |
869 | This function can be used as an entry in the variable `nnmail-split-fancy', | |
870 | for example like this: (: spam-split). It can take checks as | |
871 | parameters. A string as a parameter will set the | |
872 | spam-split-group to that string. | |
873 | ||
874 | See the Info node `(gnus)Fancy Mail Splitting' for more details." | |
875 | (interactive) | |
876 | (setq spam-split-last-successful-check nil) | |
877 | (unless spam-split-disabled | |
878 | (let ((spam-split-group-choice spam-split-group)) | |
879 | (dolist (check specific-checks) | |
880 | (when (stringp check) | |
881 | (setq spam-split-group-choice check) | |
882 | (setq specific-checks (delq check specific-checks)))) | |
883 | ||
884 | (let ((spam-split-group spam-split-group-choice)) | |
885 | (save-excursion | |
886 | (save-restriction | |
887 | (dolist (check spam-list-of-statistical-checks) | |
888 | (when (and (symbolp check) (symbol-value check)) | |
889 | (widen) | |
890 | (gnus-message 8 "spam-split: widening the buffer (%s requires it)" | |
891 | (symbol-name check)) | |
892 | (return))) | |
893 | ;; (progn (widen) (debug (buffer-string))) | |
894 | (let ((list-of-checks spam-list-of-checks) | |
895 | decision) | |
896 | (while (and list-of-checks (not decision)) | |
897 | (let ((pair (pop list-of-checks))) | |
898 | (when (and (symbol-value (car pair)) | |
899 | (or (null specific-checks) | |
900 | (memq (car pair) specific-checks))) | |
901 | (gnus-message 5 "spam-split: calling the %s function" | |
902 | (symbol-name (cdr pair))) | |
903 | (setq decision (funcall (cdr pair))) | |
904 | ;; if we got a decision at all, save the current check | |
905 | (when decision | |
906 | (setq spam-split-last-successful-check (car pair))) | |
907 | ||
908 | (when (eq decision 'spam) | |
909 | (if spam-split-symbolic-return | |
910 | (setq decision spam-split-group) | |
911 | (gnus-error | |
912 | 5 | |
913 | (format "spam-split got %s but %s is nil" | |
914 | (symbol-name decision) | |
915 | (symbol-name spam-split-symbolic-return)))))))) | |
916 | (if (eq decision t) | |
917 | (if spam-split-symbolic-return-positive 'ham nil) | |
918 | decision)))))))) | |
919 | ||
920 | (defun spam-find-spam () | |
921 | "This function will detect spam in the current newsgroup using spam-split." | |
922 | (interactive) | |
923 | ||
924 | (let* ((group gnus-newsgroup-name) | |
925 | (autodetect (gnus-parameter-spam-autodetect group)) | |
926 | (methods (gnus-parameter-spam-autodetect-methods group)) | |
927 | (first-method (nth 0 methods))) | |
928 | (when (and autodetect | |
929 | (not (equal first-method 'none))) | |
930 | (mapcar | |
931 | (lambda (article) | |
932 | (let ((id (spam-fetch-field-message-id-fast article)) | |
933 | (subject (spam-fetch-field-subject-fast article)) | |
934 | (sender (spam-fetch-field-from-fast article))) | |
935 | (unless (and spam-log-to-registry | |
936 | (spam-log-registered-p id 'incoming)) | |
937 | (let* ((spam-split-symbolic-return t) | |
938 | (spam-split-symbolic-return-positive t) | |
939 | (split-return | |
940 | (with-temp-buffer | |
941 | (gnus-request-article-this-buffer | |
942 | article | |
943 | group) | |
944 | (if (or (null first-method) | |
945 | (equal first-method 'default)) | |
946 | (spam-split) | |
947 | (apply 'spam-split methods))))) | |
948 | (if (equal split-return 'spam) | |
949 | (gnus-summary-mark-article article gnus-spam-mark)) | |
950 | ||
951 | (when (and split-return spam-log-to-registry) | |
952 | (when (zerop (gnus-registry-group-count id)) | |
953 | (gnus-registry-add-group | |
954 | id group subject sender)) | |
955 | ||
956 | (spam-log-processing-to-registry | |
957 | id | |
958 | 'incoming | |
959 | split-return | |
960 | spam-split-last-successful-check | |
961 | group)))))) | |
962 | (if spam-autodetect-recheck-messages | |
963 | gnus-newsgroup-articles | |
964 | gnus-newsgroup-unseen))))) | |
965 | ||
966 | (defvar spam-registration-functions | |
967 | ;; first the ham register, second the spam register function | |
968 | ;; third the ham unregister, fourth the spam unregister function | |
969 | '((spam-use-blacklist nil | |
970 | spam-blacklist-register-routine | |
971 | nil | |
972 | spam-blacklist-unregister-routine) | |
973 | (spam-use-whitelist spam-whitelist-register-routine | |
974 | nil | |
975 | spam-whitelist-unregister-routine | |
976 | nil) | |
977 | (spam-use-BBDB spam-BBDB-register-routine | |
978 | nil | |
979 | spam-BBDB-unregister-routine | |
980 | nil) | |
981 | (spam-use-ifile spam-ifile-register-ham-routine | |
982 | spam-ifile-register-spam-routine | |
983 | spam-ifile-unregister-ham-routine | |
984 | spam-ifile-unregister-spam-routine) | |
985 | (spam-use-spamoracle spam-spamoracle-learn-ham | |
986 | spam-spamoracle-learn-spam | |
987 | spam-spamoracle-unlearn-ham | |
988 | spam-spamoracle-unlearn-spam) | |
989 | (spam-use-stat spam-stat-register-ham-routine | |
990 | spam-stat-register-spam-routine | |
991 | spam-stat-unregister-ham-routine | |
992 | spam-stat-unregister-spam-routine) | |
993 | ;; note that spam-use-gmane is not a legitimate check | |
994 | (spam-use-gmane nil | |
995 | spam-report-gmane-register-routine | |
996 | ;; does Gmane support unregistration? | |
997 | nil | |
998 | nil) | |
999 | (spam-use-bogofilter spam-bogofilter-register-ham-routine | |
1000 | spam-bogofilter-register-spam-routine | |
1001 | spam-bogofilter-unregister-ham-routine | |
1002 | spam-bogofilter-unregister-spam-routine)) | |
1003 | "The spam-registration-functions list contains pairs | |
1004 | associating a parameter variable with the ham and spam | |
1005 | registration functions, and the ham and spam unregistration | |
1006 | functions") | |
1007 | ||
1008 | (defun spam-classification-valid-p (classification) | |
1009 | (or (eq classification 'spam) | |
1010 | (eq classification 'ham))) | |
1011 | ||
1012 | (defun spam-process-type-valid-p (process-type) | |
1013 | (or (eq process-type 'incoming) | |
1014 | (eq process-type 'process))) | |
1015 | ||
1016 | (defun spam-registration-check-valid-p (check) | |
1017 | (assoc check spam-registration-functions)) | |
1018 | ||
1019 | (defun spam-unregistration-check-valid-p (check) | |
1020 | (assoc check spam-registration-functions)) | |
1021 | ||
1022 | (defun spam-registration-function (classification check) | |
1023 | (let ((flist (cdr-safe (assoc check spam-registration-functions)))) | |
1024 | (if (eq classification 'spam) | |
1025 | (nth 1 flist) | |
1026 | (nth 0 flist)))) | |
1027 | ||
1028 | (defun spam-unregistration-function (classification check) | |
1029 | (let ((flist (cdr-safe (assoc check spam-registration-functions)))) | |
1030 | (if (eq classification 'spam) | |
1031 | (nth 3 flist) | |
1032 | (nth 2 flist)))) | |
1033 | ||
1034 | (defun spam-list-articles (articles classification) | |
1035 | (let ((mark-check (if (eq classification 'spam) | |
1036 | 'spam-group-spam-mark-p | |
1037 | 'spam-group-ham-mark-p)) | |
1038 | list mark-cache-yes mark-cache-no) | |
1039 | (dolist (article articles) | |
1040 | (let ((mark (gnus-summary-article-mark article))) | |
1041 | (unless (memq mark mark-cache-no) | |
1042 | (if (memq mark mark-cache-yes) | |
1043 | (push article list) | |
1044 | ;; else, we have to actually check the mark | |
1045 | (if (funcall mark-check | |
1046 | gnus-newsgroup-name | |
1047 | mark) | |
1048 | (progn | |
1049 | (push article list) | |
1050 | (push mark mark-cache-yes)) | |
1051 | (push mark mark-cache-no)))))) | |
1052 | list)) | |
1053 | ||
1054 | (defun spam-register-routine (classification | |
1055 | check | |
1056 | &optional unregister | |
1057 | specific-articles) | |
1058 | (when (and (spam-classification-valid-p classification) | |
1059 | (spam-registration-check-valid-p check)) | |
1060 | (let* ((register-function | |
1061 | (spam-registration-function classification check)) | |
1062 | (unregister-function | |
1063 | (spam-unregistration-function classification check)) | |
1064 | (run-function (if unregister | |
1065 | unregister-function | |
1066 | register-function)) | |
1067 | (log-function (if unregister | |
1068 | 'spam-log-undo-registration | |
1069 | 'spam-log-processing-to-registry)) | |
1070 | article articles) | |
1071 | ||
1072 | (when run-function | |
1073 | ;; make list of articles, using specific-articles if given | |
1074 | (setq articles (or specific-articles | |
1075 | (spam-list-articles | |
1076 | gnus-newsgroup-articles | |
1077 | classification))) | |
1078 | ;; process them | |
1079 | (gnus-message 5 "%s %d %s articles with classification %s, check %s" | |
1080 | (if unregister "Unregistering" "Registering") | |
1081 | (length articles) | |
1082 | (if specific-articles "specific" "") | |
1083 | (symbol-name classification) | |
1084 | (symbol-name check)) | |
1085 | (funcall run-function articles) | |
1086 | ;; now log all the registrations (or undo them, depending on unregister) | |
1087 | (dolist (article articles) | |
1088 | (funcall log-function | |
1089 | (spam-fetch-field-message-id-fast article) | |
1090 | 'process | |
1091 | classification | |
1092 | check | |
1093 | gnus-newsgroup-name)))))) | |
1094 | ||
1095 | ;;; log a ham- or spam-processor invocation to the registry | |
1096 | (defun spam-log-processing-to-registry (id type classification check group) | |
1097 | (when spam-log-to-registry | |
1098 | (if (and (stringp id) | |
1099 | (stringp group) | |
1100 | (spam-process-type-valid-p type) | |
1101 | (spam-classification-valid-p classification) | |
1102 | (spam-registration-check-valid-p check)) | |
1103 | (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type))) | |
1104 | (cell (list classification check group))) | |
1105 | (push cell cell-list) | |
1106 | (gnus-registry-store-extra-entry | |
1107 | id | |
1108 | type | |
1109 | cell-list)) | |
1110 | ||
1111 | (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group" | |
1112 | "spam-log-processing-to-registry"))))) | |
1113 | ||
1114 | ;;; check if a ham- or spam-processor registration has been done | |
1115 | (defun spam-log-registered-p (id type) | |
1116 | (when spam-log-to-registry | |
1117 | (if (and (stringp id) | |
1118 | (spam-process-type-valid-p type)) | |
1119 | (cdr-safe (gnus-registry-fetch-extra id type)) | |
1120 | (progn | |
1121 | (gnus-message 5 (format "%s called with bad ID, type, classification, or check" | |
1122 | "spam-log-registered-p")) | |
1123 | nil)))) | |
1124 | ||
1125 | ;;; check if a ham- or spam-processor registration needs to be undone | |
1126 | (defun spam-log-unregistration-needed-p (id type classification check) | |
1127 | (when spam-log-to-registry | |
1128 | (if (and (stringp id) | |
1129 | (spam-process-type-valid-p type) | |
1130 | (spam-classification-valid-p classification) | |
1131 | (spam-registration-check-valid-p check)) | |
1132 | (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type))) | |
1133 | found) | |
1134 | (dolist (cell cell-list) | |
1135 | (unless found | |
1136 | (when (and (eq classification (nth 0 cell)) | |
1137 | (eq check (nth 1 cell))) | |
1138 | (setq found t)))) | |
1139 | found) | |
1140 | (progn | |
1141 | (gnus-message 5 (format "%s called with bad ID, type, classification, or check" | |
1142 | "spam-log-unregistration-needed-p")) | |
1143 | nil)))) | |
1144 | ||
1145 | ||
1146 | ;;; undo a ham- or spam-processor registration (the group is not used) | |
1147 | (defun spam-log-undo-registration (id type classification check &optional group) | |
1148 | (when (and spam-log-to-registry | |
1149 | (spam-log-unregistration-needed-p id type classification check)) | |
1150 | (if (and (stringp id) | |
1151 | (spam-process-type-valid-p type) | |
1152 | (spam-classification-valid-p classification) | |
1153 | (spam-registration-check-valid-p check)) | |
1154 | (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type))) | |
1155 | new-cell-list found) | |
1156 | (dolist (cell cell-list) | |
1157 | (unless (and (eq classification (nth 0 cell)) | |
1158 | (eq check (nth 1 cell))) | |
1159 | (push cell new-cell-list))) | |
1160 | (gnus-registry-store-extra-entry | |
1161 | id | |
1162 | type | |
1163 | new-cell-list)) | |
1164 | (progn | |
1165 | (gnus-message 5 (format "%s called with bad ID, type, check, or group" | |
1166 | "spam-log-undo-registration")) | |
1167 | nil)))) | |
1168 | ||
1169 | ;;; set up IMAP widening if it's necessary | |
1170 | (defun spam-setup-widening () | |
1171 | (dolist (check spam-list-of-statistical-checks) | |
1172 | (when (symbol-value check) | |
1173 | (setq nnimap-split-download-body-default t)))) | |
1174 | ||
1175 | \f | |
1176 | ;;;; Regex body | |
1177 | ||
1178 | (defun spam-check-regex-body () | |
1179 | (let ((spam-regex-headers-ham spam-regex-body-ham) | |
1180 | (spam-regex-headers-spam spam-regex-body-spam)) | |
1181 | (spam-check-regex-headers t))) | |
1182 | ||
1183 | \f | |
1184 | ;;;; Regex headers | |
1185 | ||
1186 | (defun spam-check-regex-headers (&optional body) | |
1187 | (let ((type (if body "body" "header")) | |
1188 | (spam-split-group (if spam-split-symbolic-return | |
1189 | 'spam | |
1190 | spam-split-group)) | |
1191 | ret found) | |
1192 | (dolist (h-regex spam-regex-headers-ham) | |
1193 | (unless found | |
1194 | (goto-char (point-min)) | |
1195 | (when (re-search-forward h-regex nil t) | |
1196 | (message "Ham regex %s search positive." type) | |
1197 | (setq found t)))) | |
1198 | (dolist (s-regex spam-regex-headers-spam) | |
1199 | (unless found | |
1200 | (goto-char (point-min)) | |
1201 | (when (re-search-forward s-regex nil t) | |
1202 | (message "Spam regex %s search positive." type) | |
1203 | (setq found t) | |
1204 | (setq ret spam-split-group)))) | |
1205 | ret)) | |
1206 | ||
1207 | \f | |
1208 | ;;;; Blackholes. | |
1209 | ||
1210 | (defun spam-reverse-ip-string (ip) | |
1211 | (when (stringp ip) | |
1212 | (mapconcat 'identity | |
1213 | (nreverse (split-string ip "\\.")) | |
1214 | "."))) | |
1215 | ||
1216 | (defun spam-check-blackholes () | |
1217 | "Check the Received headers for blackholed relays." | |
1218 | (let ((headers (nnmail-fetch-field "received")) | |
1219 | (spam-split-group (if spam-split-symbolic-return | |
1220 | 'spam | |
1221 | spam-split-group)) | |
1222 | ips matches) | |
1223 | (when headers | |
1224 | (with-temp-buffer | |
1225 | (insert headers) | |
1226 | (goto-char (point-min)) | |
1227 | (gnus-message 5 "Checking headers for relay addresses") | |
1228 | (while (re-search-forward | |
1229 | "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t) | |
1230 | (gnus-message 9 "Blackhole search found host IP %s." (match-string 1)) | |
1231 | (push (spam-reverse-ip-string (match-string 1)) | |
1232 | ips))) | |
1233 | (dolist (server spam-blackhole-servers) | |
1234 | (dolist (ip ips) | |
1235 | (unless (and spam-blackhole-good-server-regex | |
1236 | ;; match the good-server-regex against the reversed (again) IP string | |
1237 | (string-match | |
1238 | spam-blackhole-good-server-regex | |
1239 | (spam-reverse-ip-string ip))) | |
1240 | (unless matches | |
1241 | (let ((query-string (concat ip "." server))) | |
1242 | (if spam-use-dig | |
1243 | (let ((query-result (query-dig query-string))) | |
1244 | (when query-result | |
1245 | (gnus-message 5 "(DIG): positive blackhole check '%s'" | |
1246 | query-result) | |
1247 | (push (list ip server query-result) | |
1248 | matches))) | |
1249 | ;; else, if not using dig.el | |
1250 | (when (query-dns query-string) | |
1251 | (gnus-message 5 "positive blackhole check") | |
1252 | (push (list ip server (query-dns query-string 'TXT)) | |
1253 | matches))))))))) | |
1254 | (when matches | |
1255 | spam-split-group))) | |
1256 | \f | |
1257 | ;;;; Hashcash. | |
1258 | ||
ad136a7c MB |
1259 | (eval-when-compile |
1260 | (autoload 'mail-check-payment "hashcash")) | |
1261 | ||
23f87bed MB |
1262 | (condition-case nil |
1263 | (progn | |
1264 | (require 'hashcash) | |
1265 | ||
1266 | (defun spam-check-hashcash () | |
1267 | "Check the headers for hashcash payments." | |
1268 | (mail-check-payment))) ;mail-check-payment returns a boolean | |
1269 | ||
ad136a7c | 1270 | (file-error)) |
23f87bed MB |
1271 | \f |
1272 | ;;;; BBDB | |
1273 | ||
1274 | ;;; original idea for spam-check-BBDB from Alexander Kotelnikov | |
1275 | ;;; <sacha@giotto.sj.ru> | |
1276 | ||
1277 | ;; all this is done inside a condition-case to trap errors | |
1278 | ||
ad136a7c MB |
1279 | (eval-when-compile |
1280 | (autoload 'bbdb-buffer "bbdb") | |
1281 | (autoload 'bbdb-create-internal "bbdb") | |
1282 | (autoload 'bbdb-search-simple "bbdb")) | |
1283 | ||
1284 | (eval-and-compile | |
1285 | (when (condition-case nil | |
1286 | (progn | |
1287 | (require 'bbdb) | |
1288 | (require 'bbdb-com)) | |
1289 | (file-error | |
1290 | (defalias 'spam-BBDB-register-routine 'ignore) | |
1291 | (defalias 'spam-enter-ham-BBDB 'ignore) | |
1292 | nil)) | |
1293 | ||
1294 | (defun spam-enter-ham-BBDB (addresses &optional remove) | |
1295 | "Enter an address into the BBDB; implies ham (non-spam) sender" | |
1296 | (dolist (from addresses) | |
1297 | (when (stringp from) | |
1298 | (let* ((parsed-address (gnus-extract-address-components from)) | |
1299 | (name (or (nth 0 parsed-address) "Ham Sender")) | |
1300 | (remove-function (if remove | |
1301 | 'bbdb-delete-record-internal | |
1302 | 'ignore)) | |
1303 | (net-address (nth 1 parsed-address)) | |
1304 | (record (and net-address | |
1305 | (bbdb-search-simple nil net-address)))) | |
1306 | (when net-address | |
1307 | (gnus-message 5 "%s address %s %s BBDB" | |
1308 | (if remove "Deleting" "Adding") | |
1309 | from | |
1310 | (if remove "from" "to")) | |
1311 | (if record | |
1312 | (funcall remove-function record) | |
1313 | (bbdb-create-internal name nil net-address nil nil | |
1314 | "ham sender added by spam.el"))))))) | |
1315 | ||
1316 | (defun spam-BBDB-register-routine (articles &optional unregister) | |
1317 | (let (addresses) | |
1318 | (dolist (article articles) | |
1319 | (when (stringp (spam-fetch-field-from-fast article)) | |
1320 | (push (spam-fetch-field-from-fast article) addresses))) | |
1321 | ;; now do the register/unregister action | |
1322 | (spam-enter-ham-BBDB addresses unregister))) | |
1323 | ||
1324 | (defun spam-BBDB-unregister-routine (articles) | |
1325 | (spam-BBDB-register-routine articles t)) | |
1326 | ||
1327 | (defun spam-check-BBDB () | |
1328 | "Mail from people in the BBDB is classified as ham or non-spam" | |
1329 | (let ((who (nnmail-fetch-field "from")) | |
1330 | (spam-split-group (if spam-split-symbolic-return | |
1331 | 'spam | |
1332 | spam-split-group))) | |
1333 | (when who | |
1334 | (setq who (nth 1 (gnus-extract-address-components who))) | |
1335 | (if (bbdb-search-simple nil who) | |
1336 | t | |
1337 | (if spam-use-BBDB-exclusive | |
1338 | spam-split-group | |
1339 | nil))))))) | |
23f87bed MB |
1340 | |
1341 | \f | |
1342 | ;;;; ifile | |
1343 | ||
1344 | ;;; check the ifile backend; return nil if the mail was NOT classified | |
1345 | ;;; as spam | |
1346 | ||
1347 | (defun spam-get-ifile-database-parameter () | |
1348 | "Get the command-line parameter for ifile's database from | |
1349 | spam-ifile-database-path." | |
1350 | (if spam-ifile-database-path | |
1351 | (format "--db-file=%s" spam-ifile-database-path) | |
1352 | nil)) | |
1353 | ||
1354 | (defun spam-check-ifile () | |
1355 | "Check the ifile backend for the classification of this message." | |
1356 | (let ((article-buffer-name (buffer-name)) | |
1357 | (spam-split-group (if spam-split-symbolic-return | |
1358 | 'spam | |
1359 | spam-split-group)) | |
1360 | category return) | |
1361 | (with-temp-buffer | |
1362 | (let ((temp-buffer-name (buffer-name)) | |
1363 | (db-param (spam-get-ifile-database-parameter))) | |
1364 | (save-excursion | |
1365 | (set-buffer article-buffer-name) | |
1366 | (apply 'call-process-region | |
1367 | (point-min) (point-max) spam-ifile-path | |
1368 | nil temp-buffer-name nil "-c" | |
1369 | (if db-param `(,db-param "-q") `("-q")))) | |
1370 | ;; check the return now (we're back in the temp buffer) | |
1371 | (goto-char (point-min)) | |
1372 | (if (not (eobp)) | |
1373 | (setq category (buffer-substring (point) (spam-point-at-eol)))) | |
1374 | (when (not (zerop (length category))) ; we need a category here | |
1375 | (if spam-ifile-all-categories | |
1376 | (setq return category) | |
1377 | ;; else, if spam-ifile-all-categories is not set... | |
1378 | (when (string-equal spam-ifile-spam-category category) | |
1379 | (setq return spam-split-group)))))) ; note return is nil otherwise | |
1380 | return)) | |
1381 | ||
1382 | (defun spam-ifile-register-with-ifile (articles category &optional unregister) | |
1383 | "Register an article, given as a string, with a category. | |
1384 | Uses `gnus-newsgroup-name' if category is nil (for ham registration)." | |
1385 | (let ((category (or category gnus-newsgroup-name)) | |
1386 | (add-or-delete-option (if unregister "-d" "-i")) | |
1387 | (db (spam-get-ifile-database-parameter)) | |
1388 | parameters) | |
1389 | (with-temp-buffer | |
1390 | (dolist (article articles) | |
1391 | (let ((article-string (spam-get-article-as-string article))) | |
1392 | (when (stringp article-string) | |
1393 | (insert article-string)))) | |
1394 | (apply 'call-process-region | |
1395 | (point-min) (point-max) spam-ifile-path | |
1396 | nil nil nil | |
1397 | add-or-delete-option category | |
1398 | (if db `(,db "-h") `("-h")))))) | |
1399 | ||
1400 | (defun spam-ifile-register-spam-routine (articles &optional unregister) | |
1401 | (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister)) | |
1402 | ||
1403 | (defun spam-ifile-unregister-spam-routine (articles) | |
1404 | (spam-ifile-register-spam-routine articles t)) | |
1405 | ||
1406 | (defun spam-ifile-register-ham-routine (articles &optional unregister) | |
1407 | (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister)) | |
1408 | ||
1409 | (defun spam-ifile-unregister-ham-routine (articles) | |
1410 | (spam-ifile-register-ham-routine articles t)) | |
1411 | ||
1412 | \f | |
1413 | ;;;; spam-stat | |
1414 | ||
ad136a7c MB |
1415 | (eval-when-compile |
1416 | (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat") | |
1417 | (autoload 'spam-stat-buffer-change-to-spam "spam-stat") | |
1418 | (autoload 'spam-stat-buffer-is-non-spam "spam-stat") | |
1419 | (autoload 'spam-stat-buffer-is-spam "spam-stat") | |
1420 | (autoload 'spam-stat-load "spam-stat") | |
1421 | (autoload 'spam-stat-save "spam-stat") | |
1422 | (autoload 'spam-stat-split-fancy "spam-stat")) | |
1423 | ||
1424 | (eval-and-compile | |
1425 | (when (condition-case nil | |
1426 | (let ((spam-stat-install-hooks nil)) | |
1427 | (require 'spam-stat)) | |
1428 | (file-error | |
1429 | (defalias 'spam-stat-register-ham-routine 'ignore) | |
1430 | (defalias 'spam-stat-register-spam-routine 'ignore) | |
1431 | nil)) | |
1432 | ||
1433 | (defun spam-check-stat () | |
1434 | "Check the spam-stat backend for the classification of this message" | |
1435 | (let ((spam-split-group (if spam-split-symbolic-return | |
1436 | 'spam | |
1437 | spam-split-group)) | |
1438 | (spam-stat-split-fancy-spam-group spam-split-group) ; override | |
1439 | (spam-stat-buffer (buffer-name)) ; stat the current buffer | |
1440 | category return) | |
1441 | (spam-stat-split-fancy))) | |
1442 | ||
1443 | (defun spam-stat-register-spam-routine (articles &optional unregister) | |
1444 | (dolist (article articles) | |
1445 | (let ((article-string (spam-get-article-as-string article))) | |
1446 | (with-temp-buffer | |
1447 | (insert article-string) | |
1448 | (if unregister | |
1449 | (spam-stat-buffer-change-to-non-spam) | |
23f87bed MB |
1450 | (spam-stat-buffer-is-spam)))))) |
1451 | ||
ad136a7c MB |
1452 | (defun spam-stat-unregister-spam-routine (articles) |
1453 | (spam-stat-register-spam-routine articles t)) | |
23f87bed | 1454 | |
ad136a7c MB |
1455 | (defun spam-stat-register-ham-routine (articles &optional unregister) |
1456 | (dolist (article articles) | |
1457 | (let ((article-string (spam-get-article-as-string article))) | |
1458 | (with-temp-buffer | |
1459 | (insert article-string) | |
1460 | (if unregister | |
1461 | (spam-stat-buffer-change-to-spam) | |
23f87bed MB |
1462 | (spam-stat-buffer-is-non-spam)))))) |
1463 | ||
ad136a7c MB |
1464 | (defun spam-stat-unregister-ham-routine (articles) |
1465 | (spam-stat-register-ham-routine articles t)) | |
1466 | ||
1467 | (defun spam-maybe-spam-stat-load () | |
1468 | (when spam-use-stat (spam-stat-load))) | |
1469 | ||
1470 | (defun spam-maybe-spam-stat-save () | |
1471 | (when spam-use-stat (spam-stat-save))))) | |
23f87bed MB |
1472 | |
1473 | \f | |
1474 | ||
1475 | ;;;; Blacklists and whitelists. | |
1476 | ||
1477 | (defvar spam-whitelist-cache nil) | |
1478 | (defvar spam-blacklist-cache nil) | |
1479 | ||
1480 | (defun spam-kill-whole-line () | |
1481 | (beginning-of-line) | |
1482 | (let ((kill-whole-line t)) | |
1483 | (kill-line))) | |
1484 | ||
1485 | ;;; address can be a list, too | |
1486 | (defun spam-enter-whitelist (address &optional remove) | |
1487 | "Enter ADDRESS (list or single) into the whitelist. | |
1488 | With a non-nil REMOVE, remove them." | |
1489 | (interactive "sAddress: ") | |
1490 | (spam-enter-list address spam-whitelist remove) | |
1491 | (setq spam-whitelist-cache nil)) | |
1492 | ||
1493 | ;;; address can be a list, too | |
1494 | (defun spam-enter-blacklist (address &optional remove) | |
1495 | "Enter ADDRESS (list or single) into the blacklist. | |
1496 | With a non-nil REMOVE, remove them." | |
1497 | (interactive "sAddress: ") | |
1498 | (spam-enter-list address spam-blacklist remove) | |
1499 | (setq spam-blacklist-cache nil)) | |
1500 | ||
1501 | (defun spam-enter-list (addresses file &optional remove) | |
1502 | "Enter ADDRESSES into the given FILE. | |
1503 | Either the whitelist or the blacklist files can be used. With | |
1504 | REMOVE not nil, remove the ADDRESSES." | |
1505 | (if (stringp addresses) | |
1506 | (spam-enter-list (list addresses) file remove) | |
1507 | ;; else, we have a list of addresses here | |
1508 | (unless (file-exists-p (file-name-directory file)) | |
1509 | (make-directory (file-name-directory file) t)) | |
1510 | (save-excursion | |
1511 | (set-buffer | |
1512 | (find-file-noselect file)) | |
1513 | (dolist (a addresses) | |
1514 | (when (stringp a) | |
1515 | (goto-char (point-min)) | |
1516 | (if (re-search-forward (regexp-quote a) nil t) | |
1517 | ;; found the address | |
1518 | (when remove | |
1519 | (spam-kill-whole-line)) | |
1520 | ;; else, the address was not found | |
1521 | (unless remove | |
1522 | (goto-char (point-max)) | |
1523 | (unless (bobp) | |
1524 | (insert "\n")) | |
1525 | (insert a "\n"))))) | |
1526 | (save-buffer)))) | |
1527 | ||
1528 | ;;; returns t if the sender is in the whitelist, nil or | |
1529 | ;;; spam-split-group otherwise | |
1530 | (defun spam-check-whitelist () | |
1531 | ;; FIXME! Should it detect when file timestamps change? | |
1532 | (let ((spam-split-group (if spam-split-symbolic-return | |
1533 | 'spam | |
1534 | spam-split-group))) | |
1535 | (unless spam-whitelist-cache | |
1536 | (setq spam-whitelist-cache (spam-parse-list spam-whitelist))) | |
1537 | (if (spam-from-listed-p spam-whitelist-cache) | |
1538 | t | |
1539 | (if spam-use-whitelist-exclusive | |
1540 | spam-split-group | |
1541 | nil)))) | |
1542 | ||
1543 | (defun spam-check-blacklist () | |
1544 | ;; FIXME! Should it detect when file timestamps change? | |
1545 | (let ((spam-split-group (if spam-split-symbolic-return | |
1546 | 'spam | |
1547 | spam-split-group))) | |
1548 | (unless spam-blacklist-cache | |
1549 | (setq spam-blacklist-cache (spam-parse-list spam-blacklist))) | |
1550 | (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))) | |
1551 | ||
1552 | (defun spam-parse-list (file) | |
1553 | (when (file-readable-p file) | |
1554 | (let (contents address) | |
1555 | (with-temp-buffer | |
1556 | (insert-file-contents file) | |
1557 | (while (not (eobp)) | |
1558 | (setq address (buffer-substring (point) (spam-point-at-eol))) | |
1559 | (forward-line 1) | |
1560 | ;; insert the e-mail address if detected, otherwise the raw data | |
1561 | (unless (zerop (length address)) | |
1562 | (let ((pure-address (nth 1 (gnus-extract-address-components address)))) | |
1563 | (push (or pure-address address) contents))))) | |
1564 | (nreverse contents)))) | |
1565 | ||
1566 | (defun spam-from-listed-p (cache) | |
1567 | (let ((from (nnmail-fetch-field "from")) | |
1568 | found) | |
1569 | (while cache | |
1570 | (let ((address (pop cache))) | |
1571 | (unless (zerop (length address)) ; 0 for a nil address too | |
1572 | (setq address (regexp-quote address)) | |
1573 | ;; fix regexp-quote's treatment of user-intended regexes | |
1574 | (while (string-match "\\\\\\*" address) | |
1575 | (setq address (replace-match ".*" t t address)))) | |
1576 | (when (and address (string-match address from)) | |
1577 | (setq found t | |
1578 | cache nil)))) | |
1579 | found)) | |
1580 | ||
1581 | (defun spam-filelist-register-routine (articles blacklist &optional unregister) | |
1582 | (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist)) | |
1583 | (declassification (if blacklist 'ham 'spam)) | |
1584 | (enter-function | |
1585 | (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist)) | |
1586 | (remove-function | |
1587 | (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist)) | |
1588 | from addresses unregister-list) | |
1589 | (dolist (article articles) | |
1590 | (let ((from (spam-fetch-field-from-fast article)) | |
1591 | (id (spam-fetch-field-message-id-fast article)) | |
1592 | sender-ignored) | |
1593 | (when (stringp from) | |
1594 | (dolist (ignore-regex spam-blacklist-ignored-regexes) | |
1595 | (when (and (not sender-ignored) | |
1596 | (stringp ignore-regex) | |
1597 | (string-match ignore-regex from)) | |
1598 | (setq sender-ignored t))) | |
1599 | ;; remember the messages we need to unregister, unless remove is set | |
1600 | (when (and | |
1601 | (null unregister) | |
1602 | (spam-log-unregistration-needed-p | |
1603 | id 'process declassification de-symbol)) | |
1604 | (push from unregister-list)) | |
1605 | (unless sender-ignored | |
1606 | (push from addresses))))) | |
1607 | ||
1608 | (if unregister | |
1609 | (funcall enter-function addresses t) ; unregister all these addresses | |
1610 | ;; else, register normally and unregister what we need to | |
1611 | (funcall remove-function unregister-list t) | |
1612 | (dolist (article unregister-list) | |
1613 | (spam-log-undo-registration | |
1614 | (spam-fetch-field-message-id-fast article) | |
1615 | 'process | |
1616 | declassification | |
1617 | de-symbol)) | |
1618 | (funcall enter-function addresses nil)))) | |
1619 | ||
1620 | (defun spam-blacklist-unregister-routine (articles) | |
1621 | (spam-blacklist-register-routine articles t)) | |
1622 | ||
1623 | (defun spam-blacklist-register-routine (articles &optional unregister) | |
1624 | (spam-filelist-register-routine articles t unregister)) | |
1625 | ||
1626 | (defun spam-whitelist-unregister-routine (articles) | |
1627 | (spam-whitelist-register-routine articles t)) | |
1628 | ||
1629 | (defun spam-whitelist-register-routine (articles &optional unregister) | |
1630 | (spam-filelist-register-routine articles nil unregister)) | |
1631 | ||
1632 | \f | |
1633 | ;;;; Spam-report glue | |
1634 | (defun spam-report-gmane-register-routine (articles) | |
1635 | (when articles | |
1636 | (apply 'spam-report-gmane articles))) | |
1637 | ||
1638 | \f | |
1639 | ;;;; Bogofilter | |
1640 | (defun spam-check-bogofilter-headers (&optional score) | |
1641 | (let ((header (nnmail-fetch-field spam-bogofilter-header)) | |
1642 | (spam-split-group (if spam-split-symbolic-return | |
1643 | 'spam | |
1644 | spam-split-group))) | |
1645 | (when header ; return nil when no header | |
1646 | (if score ; scoring mode | |
1647 | (if (string-match "spamicity=\\([0-9.]+\\)" header) | |
1648 | (match-string 1 header) | |
1649 | "0") | |
1650 | ;; spam detection mode | |
1651 | (when (string-match spam-bogofilter-bogosity-positive-spam-header | |
1652 | header) | |
1653 | spam-split-group))))) | |
1654 | ||
1655 | ;; return something sensible if the score can't be determined | |
1656 | (defun spam-bogofilter-score () | |
1657 | "Get the Bogofilter spamicity score" | |
1658 | (interactive) | |
1659 | (save-window-excursion | |
1660 | (gnus-summary-show-article t) | |
1661 | (set-buffer gnus-article-buffer) | |
1662 | (let ((score (or (spam-check-bogofilter-headers t) | |
1663 | (spam-check-bogofilter t)))) | |
1664 | (message "Spamicity score %s" score) | |
1665 | (or score "0")) | |
1666 | (gnus-summary-show-article))) | |
1667 | ||
1668 | (defun spam-check-bogofilter (&optional score) | |
1669 | "Check the Bogofilter backend for the classification of this message" | |
1670 | (let ((article-buffer-name (buffer-name)) | |
1671 | (db spam-bogofilter-database-directory) | |
1672 | return) | |
1673 | (with-temp-buffer | |
1674 | (let ((temp-buffer-name (buffer-name))) | |
1675 | (save-excursion | |
1676 | (set-buffer article-buffer-name) | |
1677 | (apply 'call-process-region | |
1678 | (point-min) (point-max) | |
1679 | spam-bogofilter-path | |
1680 | nil temp-buffer-name nil | |
1681 | (if db `("-d" ,db "-v") `("-v")))) | |
1682 | (setq return (spam-check-bogofilter-headers score)))) | |
1683 | return)) | |
1684 | ||
1685 | (defun spam-bogofilter-register-with-bogofilter (articles | |
1686 | spam | |
1687 | &optional unregister) | |
1688 | "Register an article, given as a string, as spam or non-spam." | |
1689 | (dolist (article articles) | |
1690 | (let ((article-string (spam-get-article-as-string article)) | |
1691 | (db spam-bogofilter-database-directory) | |
1692 | (switch (if unregister | |
1693 | (if spam | |
1694 | spam-bogofilter-spam-strong-switch | |
1695 | spam-bogofilter-ham-strong-switch) | |
1696 | (if spam | |
1697 | spam-bogofilter-spam-switch | |
1698 | spam-bogofilter-ham-switch)))) | |
1699 | (when (stringp article-string) | |
1700 | (with-temp-buffer | |
1701 | (insert article-string) | |
1702 | ||
1703 | (apply 'call-process-region | |
1704 | (point-min) (point-max) | |
1705 | spam-bogofilter-path | |
1706 | nil nil nil switch | |
1707 | (if db `("-d" ,db "-v") `("-v")))))))) | |
1708 | ||
1709 | (defun spam-bogofilter-register-spam-routine (articles &optional unregister) | |
1710 | (spam-bogofilter-register-with-bogofilter articles t unregister)) | |
1711 | ||
1712 | (defun spam-bogofilter-unregister-spam-routine (articles) | |
1713 | (spam-bogofilter-register-spam-routine articles t)) | |
1714 | ||
1715 | (defun spam-bogofilter-register-ham-routine (articles &optional unregister) | |
1716 | (spam-bogofilter-register-with-bogofilter articles nil unregister)) | |
1717 | ||
1718 | (defun spam-bogofilter-unregister-ham-routine (articles) | |
1719 | (spam-bogofilter-register-ham-routine articles t)) | |
1720 | ||
1721 | ||
1722 | \f | |
1723 | ;;;; spamoracle | |
1724 | (defun spam-check-spamoracle () | |
1725 | "Run spamoracle on an article to determine whether it's spam." | |
1726 | (let ((article-buffer-name (buffer-name)) | |
1727 | (spam-split-group (if spam-split-symbolic-return | |
1728 | 'spam | |
1729 | spam-split-group))) | |
1730 | (with-temp-buffer | |
1731 | (let ((temp-buffer-name (buffer-name))) | |
1732 | (save-excursion | |
1733 | (set-buffer article-buffer-name) | |
1734 | (let ((status | |
1735 | (apply 'call-process-region | |
1736 | (point-min) (point-max) | |
1737 | spam-spamoracle-binary | |
1738 | nil temp-buffer-name nil | |
1739 | (if spam-spamoracle-database | |
1740 | `("-f" ,spam-spamoracle-database "mark") | |
1741 | '("mark"))))) | |
1742 | (if (eq 0 status) | |
1743 | (progn | |
1744 | (set-buffer temp-buffer-name) | |
1745 | (goto-char (point-min)) | |
1746 | (when (re-search-forward "^X-Spam: yes;" nil t) | |
1747 | spam-split-group)) | |
c1d7d285 | 1748 | (error "Error running spamoracle: %s" status)))))))) |
23f87bed MB |
1749 | |
1750 | (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister) | |
1751 | "Run spamoracle in training mode." | |
1752 | (with-temp-buffer | |
1753 | (let ((temp-buffer-name (buffer-name))) | |
1754 | (save-excursion | |
1755 | (goto-char (point-min)) | |
1756 | (dolist (article articles) | |
1757 | (insert (spam-get-article-as-string article))) | |
1758 | (let* ((arg (if (spam-xor unregister article-is-spam-p) | |
1759 | "-spam" | |
1760 | "-good")) | |
1761 | (status | |
1762 | (apply 'call-process-region | |
1763 | (point-min) (point-max) | |
1764 | spam-spamoracle-binary | |
1765 | nil temp-buffer-name nil | |
1766 | (if spam-spamoracle-database | |
1767 | `("-f" ,spam-spamoracle-database | |
1768 | "add" ,arg) | |
1769 | `("add" ,arg))))) | |
c1d7d285 MB |
1770 | (unless (eq 0 status) |
1771 | (error "Error running spamoracle: %s" status))))))) | |
23f87bed MB |
1772 | |
1773 | (defun spam-spamoracle-learn-ham (articles &optional unregister) | |
1774 | (spam-spamoracle-learn articles nil unregister)) | |
1775 | ||
1776 | (defun spam-spamoracle-unlearn-ham (articles &optional unregister) | |
1777 | (spam-spamoracle-learn-ham articles t)) | |
1778 | ||
1779 | (defun spam-spamoracle-learn-spam (articles &optional unregister) | |
1780 | (spam-spamoracle-learn articles t unregister)) | |
1781 | ||
1782 | (defun spam-spamoracle-unlearn-spam (articles &optional unregister) | |
1783 | (spam-spamoracle-learn-spam articles t)) | |
1784 | ||
1785 | \f | |
1786 | ;;;; Hooks | |
1787 | ||
1788 | ;;;###autoload | |
1789 | (defun spam-initialize () | |
1790 | "Install the spam.el hooks and do other initialization" | |
1791 | (interactive) | |
1792 | (setq spam-install-hooks t) | |
1793 | ;; TODO: How do we redo this every time spam-face is customized? | |
1794 | (push '((eq mark gnus-spam-mark) . spam-face) | |
1795 | gnus-summary-highlight) | |
1796 | ;; Add hooks for loading and saving the spam stats | |
1797 | (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save) | |
1798 | (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load) | |
1799 | (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load) | |
1800 | (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit) | |
1801 | (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare) | |
1802 | (add-hook 'gnus-get-new-news-hook 'spam-setup-widening) | |
1803 | (add-hook 'gnus-summary-prepare-hook 'spam-find-spam)) | |
1804 | ||
1805 | (defun spam-unload-hook () | |
1806 | "Uninstall the spam.el hooks" | |
1807 | (interactive) | |
1808 | (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save) | |
1809 | (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load) | |
1810 | (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load) | |
1811 | (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit) | |
1812 | (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare) | |
1813 | (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening) | |
1814 | (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam)) | |
1815 | ||
e3502063 RS |
1816 | (add-hook 'spam-unload-hook 'spam-unload-hook) |
1817 | ||
23f87bed MB |
1818 | (when spam-install-hooks |
1819 | (spam-initialize)) | |
1820 | ||
1821 | (provide 'spam) | |
1822 | ||
23f87bed MB |
1823 | ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f |
1824 | ;;; spam.el ends here |