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