]> code.delx.au - gnu-emacs/blob - lisp/gnus/spam.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-78
[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-mark-p (group mark &optional spam)
471 (when (stringp group)
472 (let* ((marks (spam-group-ham-marks group spam))
473 (marks (if (symbolp mark)
474 marks
475 (mapcar 'symbol-value marks))))
476 (memq mark marks))))
477
478 (defun spam-group-spam-mark-p (group mark)
479 (spam-group-ham-mark-p group mark t))
480
481 (defun spam-group-ham-marks (group &optional spam)
482 (when (stringp group)
483 (let* ((marks (if spam
484 (gnus-parameter-spam-marks group)
485 (gnus-parameter-ham-marks group)))
486 (marks (car marks))
487 (marks (if (listp (car marks)) (car marks) marks)))
488 marks)))
489
490 (defun spam-group-spam-marks (group)
491 (spam-group-ham-marks group t))
492
493 (defun spam-group-spam-contents-p (group)
494 (if (stringp group)
495 (or (member group spam-junk-mailgroups)
496 (memq 'gnus-group-spam-classification-spam
497 (gnus-parameter-spam-contents group)))
498 nil))
499
500 (defun spam-group-ham-contents-p (group)
501 (if (stringp group)
502 (memq 'gnus-group-spam-classification-ham
503 (gnus-parameter-spam-contents group))
504 nil))
505
506 (defvar spam-list-of-processors
507 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
508 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
509 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
510 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
511 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
512 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
513 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
514 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
515 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
516 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
517 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
518 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
519 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
520 "The spam-list-of-processors list contains pairs associating a
521 ham/spam exit processor variable with a classification and a
522 spam-use-* variable.")
523
524 (defun spam-group-processor-p (group processor)
525 (if (and (stringp group)
526 (symbolp processor))
527 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
528 (spam-group-processor-multiple-p
529 group
530 (cdr-safe (assoc processor spam-list-of-processors))))
531 nil))
532
533 (defun spam-group-processor-multiple-p (group processor-info)
534 (let* ((classification (nth 0 processor-info))
535 (check (nth 1 processor-info))
536 (parameters (nth 0 (gnus-parameter-spam-process group)))
537 found)
538 (dolist (parameter parameters)
539 (when (and (null found)
540 (listp parameter)
541 (eq classification (nth 0 parameter))
542 (eq check (nth 1 parameter)))
543 (setq found t)))
544 found))
545
546 (defun spam-group-spam-processor-report-gmane-p (group)
547 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
548
549 (defun spam-group-spam-processor-bogofilter-p (group)
550 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
551
552 (defun spam-group-spam-processor-blacklist-p (group)
553 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
554
555 (defun spam-group-spam-processor-ifile-p (group)
556 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
557
558 (defun spam-group-ham-processor-ifile-p (group)
559 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
560
561 (defun spam-group-spam-processor-spamoracle-p (group)
562 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
563
564 (defun spam-group-ham-processor-bogofilter-p (group)
565 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
566
567 (defun spam-group-spam-processor-stat-p (group)
568 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
569
570 (defun spam-group-ham-processor-stat-p (group)
571 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
572
573 (defun spam-group-ham-processor-whitelist-p (group)
574 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
575
576 (defun spam-group-ham-processor-BBDB-p (group)
577 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
578
579 (defun spam-group-ham-processor-copy-p (group)
580 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
581
582 (defun spam-group-ham-processor-spamoracle-p (group)
583 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
584
585 ;;; Summary entry and exit processing.
586
587 (defun spam-summary-prepare ()
588 (setq spam-old-ham-articles
589 (spam-list-articles gnus-newsgroup-articles 'ham))
590 (setq spam-old-spam-articles
591 (spam-list-articles gnus-newsgroup-articles 'spam))
592 (spam-mark-junk-as-spam-routine))
593
594 ;; The spam processors are invoked for any group, spam or ham or neither
595 (defun spam-summary-prepare-exit ()
596 (unless gnus-group-is-exiting-without-update-p
597 (gnus-message 6 "Exiting summary buffer and applying spam rules")
598
599 ;; first of all, unregister any articles that are no longer ham or spam
600 ;; we have to iterate over the processors, or else we'll be too slow
601 (dolist (classification '(spam ham))
602 (let* ((old-articles (if (eq classification 'spam)
603 spam-old-spam-articles
604 spam-old-ham-articles))
605 (new-articles (spam-list-articles
606 gnus-newsgroup-articles
607 classification))
608 (changed-articles (gnus-set-difference old-articles new-articles)))
609 ;; now that we have the changed articles, we go through the processors
610 (dolist (processor-param spam-list-of-processors)
611 (let ((processor (nth 0 processor-param))
612 (processor-classification (nth 1 processor-param))
613 (check (nth 2 processor-param))
614 unregister-list)
615 (dolist (article changed-articles)
616 (let ((id (spam-fetch-field-message-id-fast article)))
617 (when (spam-log-unregistration-needed-p
618 id 'process classification check)
619 (push article unregister-list))))
620 ;; call spam-register-routine with specific articles to unregister,
621 ;; when there are articles to unregister and the check is enabled
622 (when (and unregister-list (symbol-value check))
623 (spam-register-routine classification check t unregister-list))))))
624
625 ;; find all the spam processors applicable to this group
626 (dolist (processor-param spam-list-of-processors)
627 (let ((processor (nth 0 processor-param))
628 (classification (nth 1 processor-param))
629 (check (nth 2 processor-param)))
630 (when (and (eq 'spam classification)
631 (spam-group-processor-p gnus-newsgroup-name processor))
632 (spam-register-routine classification check))))
633
634 (if spam-move-spam-nonspam-groups-only
635 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
636 (spam-mark-spam-as-expired-and-move-routine
637 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
638 (gnus-message 5 "Marking spam as expired and moving it to %s"
639 gnus-newsgroup-name)
640 (spam-mark-spam-as-expired-and-move-routine
641 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
642
643 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
644 ;; expire spam, in case the above did not expire them
645 (gnus-message 5 "Marking spam as expired without moving it")
646 (spam-mark-spam-as-expired-and-move-routine nil)
647
648 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
649 (and (spam-group-spam-contents-p gnus-newsgroup-name)
650 spam-process-ham-in-spam-groups)
651 spam-process-ham-in-nonham-groups)
652 ;; find all the ham processors applicable to this group
653 (dolist (processor-param spam-list-of-processors)
654 (let ((processor (nth 0 processor-param))
655 (classification (nth 1 processor-param))
656 (check (nth 2 processor-param)))
657 (when (and (eq 'ham classification)
658 (spam-group-processor-p gnus-newsgroup-name processor))
659 (spam-register-routine classification check)))))
660
661 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
662 (gnus-message 5 "Copying ham")
663 (spam-ham-copy-routine
664 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
665
666 ;; now move all ham articles out of spam groups
667 (when (spam-group-spam-contents-p gnus-newsgroup-name)
668 (gnus-message 5 "Moving ham messages from spam group")
669 (spam-ham-move-routine
670 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
671
672 (setq spam-old-ham-articles nil)
673 (setq spam-old-spam-articles nil))
674
675 (defun spam-mark-junk-as-spam-routine ()
676 ;; check the global list of group names spam-junk-mailgroups and the
677 ;; group parameters
678 (when (spam-group-spam-contents-p gnus-newsgroup-name)
679 (gnus-message 5 "Marking %s articles as spam"
680 (if spam-mark-only-unseen-as-spam
681 "unseen"
682 "unread"))
683 (let ((articles (if spam-mark-only-unseen-as-spam
684 gnus-newsgroup-unseen
685 gnus-newsgroup-unreads)))
686 (dolist (article articles)
687 (gnus-summary-mark-article article gnus-spam-mark)))))
688
689 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
690 (if (and (car-safe groups) (listp (car-safe groups)))
691 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
692 (gnus-summary-kill-process-mark)
693 (let ((articles gnus-newsgroup-articles)
694 (backend-supports-deletions
695 (gnus-check-backend-function
696 'request-move-article gnus-newsgroup-name))
697 article tomove deletep)
698 (dolist (article articles)
699 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
700 (gnus-summary-mark-article article gnus-expirable-mark)
701 (push article tomove)))
702
703 ;; now do the actual copies
704 (dolist (group groups)
705 (when (and tomove
706 (stringp group))
707 (dolist (article tomove)
708 (gnus-summary-set-process-mark article))
709 (when tomove
710 (if (or (not backend-supports-deletions)
711 (> (length groups) 1))
712 (progn
713 (gnus-summary-copy-article nil group)
714 (setq deletep t))
715 (gnus-summary-move-article nil group)))))
716
717 ;; now delete the articles, if there was a copy done, and the
718 ;; backend allows it
719 (when (and deletep backend-supports-deletions)
720 (dolist (article tomove)
721 (gnus-summary-set-process-mark article))
722 (when tomove
723 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
724 (gnus-summary-delete-article nil))))
725
726 (gnus-summary-yank-process-mark))))
727
728 (defun spam-ham-copy-or-move-routine (copy groups)
729 (gnus-summary-kill-process-mark)
730 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
731 (backend-supports-deletions
732 (gnus-check-backend-function
733 'request-move-article gnus-newsgroup-name))
734 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
735 article mark todo deletep respool)
736
737 (when (member 'respool groups)
738 (setq respool t) ; boolean for later
739 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
740
741 ;; now do the actual move
742 (dolist (group groups)
743 (when (and todo (stringp group))
744 (dolist (article todo)
745 (when spam-mark-ham-unread-before-move-from-spam-group
746 (gnus-summary-mark-article article gnus-unread-mark))
747 (gnus-summary-set-process-mark article))
748
749 (if respool ; respooling is with a "fake" group
750 (let ((spam-split-disabled
751 (or spam-split-disabled
752 spam-disable-spam-split-during-ham-respool)))
753 (gnus-summary-respool-article nil respool-method))
754 (if (or (not backend-supports-deletions) ; else, we are not respooling
755 (> (length groups) 1))
756 (progn ; if copying, copy and set deletep
757 (gnus-summary-copy-article nil group)
758 (setq deletep t))
759 (gnus-summary-move-article nil group))))) ; else move articles
760
761 ;; now delete the articles, unless a) copy is t, and there was a copy done
762 ;; b) a move was done to a single group
763 ;; c) backend-supports-deletions is nil
764 (unless copy
765 (when (and deletep backend-supports-deletions)
766 (dolist (article todo)
767 (gnus-summary-set-process-mark article))
768 (when todo
769 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
770 (gnus-summary-delete-article nil))))))
771
772 (gnus-summary-yank-process-mark))
773
774 (defun spam-ham-copy-routine (&rest groups)
775 (if (and (car-safe groups) (listp (car-safe groups)))
776 (apply 'spam-ham-copy-routine (car groups))
777 (spam-ham-copy-or-move-routine t groups)))
778
779 (defun spam-ham-move-routine (&rest groups)
780 (if (and (car-safe groups) (listp (car-safe groups)))
781 (apply 'spam-ham-move-routine (car groups))
782 (spam-ham-copy-or-move-routine nil groups)))
783
784 (eval-and-compile
785 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
786 'point-at-eol
787 'line-end-position)))
788
789 (defun spam-get-article-as-string (article)
790 (let ((article-buffer (spam-get-article-as-buffer article))
791 article-string)
792 (when article-buffer
793 (save-window-excursion
794 (set-buffer article-buffer)
795 (setq article-string (buffer-string))))
796 article-string))
797
798 (defun spam-get-article-as-buffer (article)
799 (let ((article-buffer))
800 (when (numberp article)
801 (save-window-excursion
802 (gnus-summary-goto-subject article)
803 (gnus-summary-show-article t)
804 (setq article-buffer (get-buffer gnus-article-buffer))))
805 article-buffer))
806
807 ;; disabled for now
808 ;; (defun spam-get-article-as-filename (article)
809 ;; (let ((article-filename))
810 ;; (when (numberp article)
811 ;; (nnml-possibly-change-directory
812 ;; (gnus-group-real-name gnus-newsgroup-name))
813 ;; (setq article-filename (expand-file-name
814 ;; (int-to-string article) nnml-current-directory)))
815 ;; (if (file-exists-p article-filename)
816 ;; article-filename
817 ;; nil)))
818
819 (defun spam-fetch-field-from-fast (article)
820 "Fetch the `from' field quickly, using the internal gnus-data-list function"
821 (if (and (numberp article)
822 (assoc article (gnus-data-list nil)))
823 (mail-header-from
824 (gnus-data-header (assoc article (gnus-data-list nil))))
825 nil))
826
827 (defun spam-fetch-field-subject-fast (article)
828 "Fetch the `subject' field quickly, using the internal
829 gnus-data-list function"
830 (if (and (numberp article)
831 (assoc article (gnus-data-list nil)))
832 (mail-header-subject
833 (gnus-data-header (assoc article (gnus-data-list nil))))
834 nil))
835
836 (defun spam-fetch-field-message-id-fast (article)
837 "Fetch the `Message-ID' field quickly, using the internal
838 gnus-data-list function"
839 (if (and (numberp article)
840 (assoc article (gnus-data-list nil)))
841 (mail-header-message-id
842 (gnus-data-header (assoc article (gnus-data-list nil))))
843 nil))
844
845 \f
846 ;;;; Spam determination.
847
848 (defvar spam-list-of-checks
849 '((spam-use-blacklist . spam-check-blacklist)
850 (spam-use-regex-headers . spam-check-regex-headers)
851 (spam-use-regex-body . spam-check-regex-body)
852 (spam-use-whitelist . spam-check-whitelist)
853 (spam-use-BBDB . spam-check-BBDB)
854 (spam-use-ifile . spam-check-ifile)
855 (spam-use-spamoracle . spam-check-spamoracle)
856 (spam-use-stat . spam-check-stat)
857 (spam-use-blackholes . spam-check-blackholes)
858 (spam-use-hashcash . spam-check-hashcash)
859 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
860 (spam-use-bogofilter . spam-check-bogofilter))
861 "The spam-list-of-checks list contains pairs associating a
862 parameter variable with a spam checking function. If the
863 parameter variable is true, then the checking function is called,
864 and its value decides what happens. Each individual check may
865 return nil, t, or a mailgroup name. The value nil means that the
866 check does not yield a decision, and so, that further checks are
867 needed. The value t means that the message is definitely not
868 spam, and that further spam checks should be inhibited.
869 Otherwise, a mailgroup name or the symbol 'spam (depending on
870 spam-split-symbolic-return) is returned where the mail should go,
871 and further checks are also inhibited. The usual mailgroup name
872 is the value of `spam-split-group', meaning that the message is
873 definitely a spam.")
874
875 (defvar spam-list-of-statistical-checks
876 '(spam-use-ifile
877 spam-use-regex-body
878 spam-use-stat
879 spam-use-bogofilter
880 spam-use-spamoracle)
881 "The spam-list-of-statistical-checks list contains all the mail
882 splitters that need to have the full message body available.")
883
884 ;;;TODO: modify to invoke self with each check if invoked without specifics
885 (defun spam-split (&rest specific-checks)
886 "Split this message into the `spam' group if it is spam.
887 This function can be used as an entry in the variable `nnmail-split-fancy',
888 for example like this: (: spam-split). It can take checks as
889 parameters. A string as a parameter will set the
890 spam-split-group to that string.
891
892 See the Info node `(gnus)Fancy Mail Splitting' for more details."
893 (interactive)
894 (setq spam-split-last-successful-check nil)
895 (unless spam-split-disabled
896 (let ((spam-split-group-choice spam-split-group))
897 (dolist (check specific-checks)
898 (when (stringp check)
899 (setq spam-split-group-choice check)
900 (setq specific-checks (delq check specific-checks))))
901
902 (let ((spam-split-group spam-split-group-choice))
903 (save-excursion
904 (save-restriction
905 (dolist (check spam-list-of-statistical-checks)
906 (when (and (symbolp check) (symbol-value check))
907 (widen)
908 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
909 (symbol-name check))
910 (return)))
911 ;; (progn (widen) (debug (buffer-string)))
912 (let ((list-of-checks spam-list-of-checks)
913 decision)
914 (while (and list-of-checks (not decision))
915 (let ((pair (pop list-of-checks)))
916 (when (and (symbol-value (car pair))
917 (or (null specific-checks)
918 (memq (car pair) specific-checks)))
919 (gnus-message 5 "spam-split: calling the %s function"
920 (symbol-name (cdr pair)))
921 (setq decision (funcall (cdr pair)))
922 ;; if we got a decision at all, save the current check
923 (when decision
924 (setq spam-split-last-successful-check (car pair)))
925
926 (when (eq decision 'spam)
927 (if spam-split-symbolic-return
928 (setq decision spam-split-group)
929 (gnus-error
930 5
931 (format "spam-split got %s but %s is nil"
932 (symbol-name decision)
933 (symbol-name spam-split-symbolic-return))))))))
934 (if (eq decision t)
935 (if spam-split-symbolic-return-positive 'ham nil)
936 decision))))))))
937
938 (defun spam-find-spam ()
939 "This function will detect spam in the current newsgroup using spam-split."
940 (interactive)
941
942 (let* ((group gnus-newsgroup-name)
943 (autodetect (gnus-parameter-spam-autodetect group))
944 (methods (gnus-parameter-spam-autodetect-methods group))
945 (first-method (nth 0 methods)))
946 (when (and autodetect
947 (not (equal first-method 'none)))
948 (mapcar
949 (lambda (article)
950 (let ((id (spam-fetch-field-message-id-fast article))
951 (subject (spam-fetch-field-subject-fast article))
952 (sender (spam-fetch-field-from-fast article)))
953 (unless (and spam-log-to-registry
954 (spam-log-registered-p id 'incoming))
955 (let* ((spam-split-symbolic-return t)
956 (spam-split-symbolic-return-positive t)
957 (split-return
958 (with-temp-buffer
959 (gnus-request-article-this-buffer
960 article
961 group)
962 (if (or (null first-method)
963 (equal first-method 'default))
964 (spam-split)
965 (apply 'spam-split methods)))))
966 (if (equal split-return 'spam)
967 (gnus-summary-mark-article article gnus-spam-mark))
968
969 (when (and split-return spam-log-to-registry)
970 (when (zerop (gnus-registry-group-count id))
971 (gnus-registry-add-group
972 id group subject sender))
973
974 (spam-log-processing-to-registry
975 id
976 'incoming
977 split-return
978 spam-split-last-successful-check
979 group))))))
980 (if spam-autodetect-recheck-messages
981 gnus-newsgroup-articles
982 gnus-newsgroup-unseen)))))
983
984 (defvar spam-registration-functions
985 ;; first the ham register, second the spam register function
986 ;; third the ham unregister, fourth the spam unregister function
987 '((spam-use-blacklist nil
988 spam-blacklist-register-routine
989 nil
990 spam-blacklist-unregister-routine)
991 (spam-use-whitelist spam-whitelist-register-routine
992 nil
993 spam-whitelist-unregister-routine
994 nil)
995 (spam-use-BBDB spam-BBDB-register-routine
996 nil
997 spam-BBDB-unregister-routine
998 nil)
999 (spam-use-ifile spam-ifile-register-ham-routine
1000 spam-ifile-register-spam-routine
1001 spam-ifile-unregister-ham-routine
1002 spam-ifile-unregister-spam-routine)
1003 (spam-use-spamoracle spam-spamoracle-learn-ham
1004 spam-spamoracle-learn-spam
1005 spam-spamoracle-unlearn-ham
1006 spam-spamoracle-unlearn-spam)
1007 (spam-use-stat spam-stat-register-ham-routine
1008 spam-stat-register-spam-routine
1009 spam-stat-unregister-ham-routine
1010 spam-stat-unregister-spam-routine)
1011 ;; note that spam-use-gmane is not a legitimate check
1012 (spam-use-gmane nil
1013 spam-report-gmane-register-routine
1014 ;; does Gmane support unregistration?
1015 nil
1016 nil)
1017 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1018 spam-bogofilter-register-spam-routine
1019 spam-bogofilter-unregister-ham-routine
1020 spam-bogofilter-unregister-spam-routine))
1021 "The spam-registration-functions list contains pairs
1022 associating a parameter variable with the ham and spam
1023 registration functions, and the ham and spam unregistration
1024 functions")
1025
1026 (defun spam-classification-valid-p (classification)
1027 (or (eq classification 'spam)
1028 (eq classification 'ham)))
1029
1030 (defun spam-process-type-valid-p (process-type)
1031 (or (eq process-type 'incoming)
1032 (eq process-type 'process)))
1033
1034 (defun spam-registration-check-valid-p (check)
1035 (assoc check spam-registration-functions))
1036
1037 (defun spam-unregistration-check-valid-p (check)
1038 (assoc check spam-registration-functions))
1039
1040 (defun spam-registration-function (classification check)
1041 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1042 (if (eq classification 'spam)
1043 (nth 1 flist)
1044 (nth 0 flist))))
1045
1046 (defun spam-unregistration-function (classification check)
1047 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1048 (if (eq classification 'spam)
1049 (nth 3 flist)
1050 (nth 2 flist))))
1051
1052 (defun spam-list-articles (articles classification)
1053 (let ((mark-check (if (eq classification 'spam)
1054 'spam-group-spam-mark-p
1055 'spam-group-ham-mark-p))
1056 list mark-cache-yes mark-cache-no)
1057 (dolist (article articles)
1058 (let ((mark (gnus-summary-article-mark article)))
1059 (unless (memq mark mark-cache-no)
1060 (if (memq mark mark-cache-yes)
1061 (push article list)
1062 ;; else, we have to actually check the mark
1063 (if (funcall mark-check
1064 gnus-newsgroup-name
1065 mark)
1066 (progn
1067 (push article list)
1068 (push mark mark-cache-yes))
1069 (push mark mark-cache-no))))))
1070 list))
1071
1072 (defun spam-register-routine (classification
1073 check
1074 &optional unregister
1075 specific-articles)
1076 (when (and (spam-classification-valid-p classification)
1077 (spam-registration-check-valid-p check))
1078 (let* ((register-function
1079 (spam-registration-function classification check))
1080 (unregister-function
1081 (spam-unregistration-function classification check))
1082 (run-function (if unregister
1083 unregister-function
1084 register-function))
1085 (log-function (if unregister
1086 'spam-log-undo-registration
1087 'spam-log-processing-to-registry))
1088 article articles)
1089
1090 (when run-function
1091 ;; make list of articles, using specific-articles if given
1092 (setq articles (or specific-articles
1093 (spam-list-articles
1094 gnus-newsgroup-articles
1095 classification)))
1096 ;; process them
1097 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1098 (if unregister "Unregistering" "Registering")
1099 (length articles)
1100 (if specific-articles "specific" "")
1101 (symbol-name classification)
1102 (symbol-name check))
1103 (funcall run-function articles)
1104 ;; now log all the registrations (or undo them, depending on unregister)
1105 (dolist (article articles)
1106 (funcall log-function
1107 (spam-fetch-field-message-id-fast article)
1108 'process
1109 classification
1110 check
1111 gnus-newsgroup-name))))))
1112
1113 ;;; log a ham- or spam-processor invocation to the registry
1114 (defun spam-log-processing-to-registry (id type classification check group)
1115 (when spam-log-to-registry
1116 (if (and (stringp id)
1117 (stringp group)
1118 (spam-process-type-valid-p type)
1119 (spam-classification-valid-p classification)
1120 (spam-registration-check-valid-p check))
1121 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1122 (cell (list classification check group)))
1123 (push cell cell-list)
1124 (gnus-registry-store-extra-entry
1125 id
1126 type
1127 cell-list))
1128
1129 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1130 "spam-log-processing-to-registry")))))
1131
1132 ;;; check if a ham- or spam-processor registration has been done
1133 (defun spam-log-registered-p (id type)
1134 (when spam-log-to-registry
1135 (if (and (stringp id)
1136 (spam-process-type-valid-p type))
1137 (cdr-safe (gnus-registry-fetch-extra id type))
1138 (progn
1139 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1140 "spam-log-registered-p"))
1141 nil))))
1142
1143 ;;; check if a ham- or spam-processor registration needs to be undone
1144 (defun spam-log-unregistration-needed-p (id type classification check)
1145 (when spam-log-to-registry
1146 (if (and (stringp id)
1147 (spam-process-type-valid-p type)
1148 (spam-classification-valid-p classification)
1149 (spam-registration-check-valid-p check))
1150 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1151 found)
1152 (dolist (cell cell-list)
1153 (unless found
1154 (when (and (eq classification (nth 0 cell))
1155 (eq check (nth 1 cell)))
1156 (setq found t))))
1157 found)
1158 (progn
1159 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1160 "spam-log-unregistration-needed-p"))
1161 nil))))
1162
1163
1164 ;;; undo a ham- or spam-processor registration (the group is not used)
1165 (defun spam-log-undo-registration (id type classification check &optional group)
1166 (when (and spam-log-to-registry
1167 (spam-log-unregistration-needed-p id type classification check))
1168 (if (and (stringp id)
1169 (spam-process-type-valid-p type)
1170 (spam-classification-valid-p classification)
1171 (spam-registration-check-valid-p check))
1172 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1173 new-cell-list found)
1174 (dolist (cell cell-list)
1175 (unless (and (eq classification (nth 0 cell))
1176 (eq check (nth 1 cell)))
1177 (push cell new-cell-list)))
1178 (gnus-registry-store-extra-entry
1179 id
1180 type
1181 new-cell-list))
1182 (progn
1183 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1184 "spam-log-undo-registration"))
1185 nil))))
1186
1187 ;;; set up IMAP widening if it's necessary
1188 (defun spam-setup-widening ()
1189 (dolist (check spam-list-of-statistical-checks)
1190 (when (symbol-value check)
1191 (setq nnimap-split-download-body-default t))))
1192
1193 \f
1194 ;;;; Regex body
1195
1196 (defun spam-check-regex-body ()
1197 (let ((spam-regex-headers-ham spam-regex-body-ham)
1198 (spam-regex-headers-spam spam-regex-body-spam))
1199 (spam-check-regex-headers t)))
1200
1201 \f
1202 ;;;; Regex headers
1203
1204 (defun spam-check-regex-headers (&optional body)
1205 (let ((type (if body "body" "header"))
1206 (spam-split-group (if spam-split-symbolic-return
1207 'spam
1208 spam-split-group))
1209 ret found)
1210 (dolist (h-regex spam-regex-headers-ham)
1211 (unless found
1212 (goto-char (point-min))
1213 (when (re-search-forward h-regex nil t)
1214 (message "Ham regex %s search positive." type)
1215 (setq found t))))
1216 (dolist (s-regex spam-regex-headers-spam)
1217 (unless found
1218 (goto-char (point-min))
1219 (when (re-search-forward s-regex nil t)
1220 (message "Spam regex %s search positive." type)
1221 (setq found t)
1222 (setq ret spam-split-group))))
1223 ret))
1224
1225 \f
1226 ;;;; Blackholes.
1227
1228 (defun spam-reverse-ip-string (ip)
1229 (when (stringp ip)
1230 (mapconcat 'identity
1231 (nreverse (split-string ip "\\."))
1232 ".")))
1233
1234 (defun spam-check-blackholes ()
1235 "Check the Received headers for blackholed relays."
1236 (let ((headers (nnmail-fetch-field "received"))
1237 (spam-split-group (if spam-split-symbolic-return
1238 'spam
1239 spam-split-group))
1240 ips matches)
1241 (when headers
1242 (with-temp-buffer
1243 (insert headers)
1244 (goto-char (point-min))
1245 (gnus-message 5 "Checking headers for relay addresses")
1246 (while (re-search-forward
1247 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1248 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1249 (push (spam-reverse-ip-string (match-string 1))
1250 ips)))
1251 (dolist (server spam-blackhole-servers)
1252 (dolist (ip ips)
1253 (unless (and spam-blackhole-good-server-regex
1254 ;; match the good-server-regex against the reversed (again) IP string
1255 (string-match
1256 spam-blackhole-good-server-regex
1257 (spam-reverse-ip-string ip)))
1258 (unless matches
1259 (let ((query-string (concat ip "." server)))
1260 (if spam-use-dig
1261 (let ((query-result (query-dig query-string)))
1262 (when query-result
1263 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1264 query-result)
1265 (push (list ip server query-result)
1266 matches)))
1267 ;; else, if not using dig.el
1268 (when (query-dns query-string)
1269 (gnus-message 5 "positive blackhole check")
1270 (push (list ip server (query-dns query-string 'TXT))
1271 matches)))))))))
1272 (when matches
1273 spam-split-group)))
1274 \f
1275 ;;;; Hashcash.
1276
1277 (eval-when-compile
1278 (autoload 'mail-check-payment "hashcash"))
1279
1280 (condition-case nil
1281 (progn
1282 (require 'hashcash)
1283
1284 (defun spam-check-hashcash ()
1285 "Check the headers for hashcash payments."
1286 (mail-check-payment))) ;mail-check-payment returns a boolean
1287
1288 (file-error))
1289 \f
1290 ;;;; BBDB
1291
1292 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1293 ;;; <sacha@giotto.sj.ru>
1294
1295 ;; all this is done inside a condition-case to trap errors
1296
1297 (eval-when-compile
1298 (autoload 'bbdb-buffer "bbdb")
1299 (autoload 'bbdb-create-internal "bbdb")
1300 (autoload 'bbdb-search-simple "bbdb"))
1301
1302 (eval-and-compile
1303 (when (condition-case nil
1304 (progn
1305 (require 'bbdb)
1306 (require 'bbdb-com))
1307 (file-error
1308 (defalias 'spam-BBDB-register-routine 'ignore)
1309 (defalias 'spam-enter-ham-BBDB 'ignore)
1310 nil))
1311
1312 (defun spam-enter-ham-BBDB (addresses &optional remove)
1313 "Enter an address into the BBDB; implies ham (non-spam) sender"
1314 (dolist (from addresses)
1315 (when (stringp from)
1316 (let* ((parsed-address (gnus-extract-address-components from))
1317 (name (or (nth 0 parsed-address) "Ham Sender"))
1318 (remove-function (if remove
1319 'bbdb-delete-record-internal
1320 'ignore))
1321 (net-address (nth 1 parsed-address))
1322 (record (and net-address
1323 (bbdb-search-simple nil net-address))))
1324 (when net-address
1325 (gnus-message 5 "%s address %s %s BBDB"
1326 (if remove "Deleting" "Adding")
1327 from
1328 (if remove "from" "to"))
1329 (if record
1330 (funcall remove-function record)
1331 (bbdb-create-internal name nil net-address nil nil
1332 "ham sender added by spam.el")))))))
1333
1334 (defun spam-BBDB-register-routine (articles &optional unregister)
1335 (let (addresses)
1336 (dolist (article articles)
1337 (when (stringp (spam-fetch-field-from-fast article))
1338 (push (spam-fetch-field-from-fast article) addresses)))
1339 ;; now do the register/unregister action
1340 (spam-enter-ham-BBDB addresses unregister)))
1341
1342 (defun spam-BBDB-unregister-routine (articles)
1343 (spam-BBDB-register-routine articles t))
1344
1345 (defun spam-check-BBDB ()
1346 "Mail from people in the BBDB is classified as ham or non-spam"
1347 (let ((who (nnmail-fetch-field "from"))
1348 (spam-split-group (if spam-split-symbolic-return
1349 'spam
1350 spam-split-group)))
1351 (when who
1352 (setq who (nth 1 (gnus-extract-address-components who)))
1353 (if (bbdb-search-simple nil who)
1354 t
1355 (if spam-use-BBDB-exclusive
1356 spam-split-group
1357 nil)))))))
1358
1359 \f
1360 ;;;; ifile
1361
1362 ;;; check the ifile backend; return nil if the mail was NOT classified
1363 ;;; as spam
1364
1365 (defun spam-get-ifile-database-parameter ()
1366 "Get the command-line parameter for ifile's database from
1367 spam-ifile-database-path."
1368 (if spam-ifile-database-path
1369 (format "--db-file=%s" spam-ifile-database-path)
1370 nil))
1371
1372 (defun spam-check-ifile ()
1373 "Check the ifile backend for the classification of this message."
1374 (let ((article-buffer-name (buffer-name))
1375 (spam-split-group (if spam-split-symbolic-return
1376 'spam
1377 spam-split-group))
1378 category return)
1379 (with-temp-buffer
1380 (let ((temp-buffer-name (buffer-name))
1381 (db-param (spam-get-ifile-database-parameter)))
1382 (save-excursion
1383 (set-buffer article-buffer-name)
1384 (apply 'call-process-region
1385 (point-min) (point-max) spam-ifile-path
1386 nil temp-buffer-name nil "-c"
1387 (if db-param `(,db-param "-q") `("-q"))))
1388 ;; check the return now (we're back in the temp buffer)
1389 (goto-char (point-min))
1390 (if (not (eobp))
1391 (setq category (buffer-substring (point) (spam-point-at-eol))))
1392 (when (not (zerop (length category))) ; we need a category here
1393 (if spam-ifile-all-categories
1394 (setq return category)
1395 ;; else, if spam-ifile-all-categories is not set...
1396 (when (string-equal spam-ifile-spam-category category)
1397 (setq return spam-split-group)))))) ; note return is nil otherwise
1398 return))
1399
1400 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1401 "Register an article, given as a string, with a category.
1402 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1403 (let ((category (or category gnus-newsgroup-name))
1404 (add-or-delete-option (if unregister "-d" "-i"))
1405 (db (spam-get-ifile-database-parameter))
1406 parameters)
1407 (with-temp-buffer
1408 (dolist (article articles)
1409 (let ((article-string (spam-get-article-as-string article)))
1410 (when (stringp article-string)
1411 (insert article-string))))
1412 (apply 'call-process-region
1413 (point-min) (point-max) spam-ifile-path
1414 nil nil nil
1415 add-or-delete-option category
1416 (if db `(,db "-h") `("-h"))))))
1417
1418 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1419 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1420
1421 (defun spam-ifile-unregister-spam-routine (articles)
1422 (spam-ifile-register-spam-routine articles t))
1423
1424 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1425 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1426
1427 (defun spam-ifile-unregister-ham-routine (articles)
1428 (spam-ifile-register-ham-routine articles t))
1429
1430 \f
1431 ;;;; spam-stat
1432
1433 (eval-when-compile
1434 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
1435 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
1436 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
1437 (autoload 'spam-stat-buffer-is-spam "spam-stat")
1438 (autoload 'spam-stat-load "spam-stat")
1439 (autoload 'spam-stat-save "spam-stat")
1440 (autoload 'spam-stat-split-fancy "spam-stat"))
1441
1442 (eval-and-compile
1443 (when (condition-case nil
1444 (let ((spam-stat-install-hooks nil))
1445 (require 'spam-stat))
1446 (file-error
1447 (defalias 'spam-stat-register-ham-routine 'ignore)
1448 (defalias 'spam-stat-register-spam-routine 'ignore)
1449 nil))
1450
1451 (defun spam-check-stat ()
1452 "Check the spam-stat backend for the classification of this message"
1453 (let ((spam-split-group (if spam-split-symbolic-return
1454 'spam
1455 spam-split-group))
1456 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1457 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1458 category return)
1459 (spam-stat-split-fancy)))
1460
1461 (defun spam-stat-register-spam-routine (articles &optional unregister)
1462 (dolist (article articles)
1463 (let ((article-string (spam-get-article-as-string article)))
1464 (with-temp-buffer
1465 (insert article-string)
1466 (if unregister
1467 (spam-stat-buffer-change-to-non-spam)
1468 (spam-stat-buffer-is-spam))))))
1469
1470 (defun spam-stat-unregister-spam-routine (articles)
1471 (spam-stat-register-spam-routine articles t))
1472
1473 (defun spam-stat-register-ham-routine (articles &optional unregister)
1474 (dolist (article articles)
1475 (let ((article-string (spam-get-article-as-string article)))
1476 (with-temp-buffer
1477 (insert article-string)
1478 (if unregister
1479 (spam-stat-buffer-change-to-spam)
1480 (spam-stat-buffer-is-non-spam))))))
1481
1482 (defun spam-stat-unregister-ham-routine (articles)
1483 (spam-stat-register-ham-routine articles t))
1484
1485 (defun spam-maybe-spam-stat-load ()
1486 (when spam-use-stat (spam-stat-load)))
1487
1488 (defun spam-maybe-spam-stat-save ()
1489 (when spam-use-stat (spam-stat-save)))))
1490
1491 \f
1492
1493 ;;;; Blacklists and whitelists.
1494
1495 (defvar spam-whitelist-cache nil)
1496 (defvar spam-blacklist-cache nil)
1497
1498 (defun spam-kill-whole-line ()
1499 (beginning-of-line)
1500 (let ((kill-whole-line t))
1501 (kill-line)))
1502
1503 ;;; address can be a list, too
1504 (defun spam-enter-whitelist (address &optional remove)
1505 "Enter ADDRESS (list or single) into the whitelist.
1506 With a non-nil REMOVE, remove them."
1507 (interactive "sAddress: ")
1508 (spam-enter-list address spam-whitelist remove)
1509 (setq spam-whitelist-cache nil))
1510
1511 ;;; address can be a list, too
1512 (defun spam-enter-blacklist (address &optional remove)
1513 "Enter ADDRESS (list or single) into the blacklist.
1514 With a non-nil REMOVE, remove them."
1515 (interactive "sAddress: ")
1516 (spam-enter-list address spam-blacklist remove)
1517 (setq spam-blacklist-cache nil))
1518
1519 (defun spam-enter-list (addresses file &optional remove)
1520 "Enter ADDRESSES into the given FILE.
1521 Either the whitelist or the blacklist files can be used. With
1522 REMOVE not nil, remove the ADDRESSES."
1523 (if (stringp addresses)
1524 (spam-enter-list (list addresses) file remove)
1525 ;; else, we have a list of addresses here
1526 (unless (file-exists-p (file-name-directory file))
1527 (make-directory (file-name-directory file) t))
1528 (save-excursion
1529 (set-buffer
1530 (find-file-noselect file))
1531 (dolist (a addresses)
1532 (when (stringp a)
1533 (goto-char (point-min))
1534 (if (re-search-forward (regexp-quote a) nil t)
1535 ;; found the address
1536 (when remove
1537 (spam-kill-whole-line))
1538 ;; else, the address was not found
1539 (unless remove
1540 (goto-char (point-max))
1541 (unless (bobp)
1542 (insert "\n"))
1543 (insert a "\n")))))
1544 (save-buffer))))
1545
1546 ;;; returns t if the sender is in the whitelist, nil or
1547 ;;; spam-split-group otherwise
1548 (defun spam-check-whitelist ()
1549 ;; FIXME! Should it detect when file timestamps change?
1550 (let ((spam-split-group (if spam-split-symbolic-return
1551 'spam
1552 spam-split-group)))
1553 (unless spam-whitelist-cache
1554 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1555 (if (spam-from-listed-p spam-whitelist-cache)
1556 t
1557 (if spam-use-whitelist-exclusive
1558 spam-split-group
1559 nil))))
1560
1561 (defun spam-check-blacklist ()
1562 ;; FIXME! Should it detect when file timestamps change?
1563 (let ((spam-split-group (if spam-split-symbolic-return
1564 'spam
1565 spam-split-group)))
1566 (unless spam-blacklist-cache
1567 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1568 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1569
1570 (defun spam-parse-list (file)
1571 (when (file-readable-p file)
1572 (let (contents address)
1573 (with-temp-buffer
1574 (insert-file-contents file)
1575 (while (not (eobp))
1576 (setq address (buffer-substring (point) (spam-point-at-eol)))
1577 (forward-line 1)
1578 ;; insert the e-mail address if detected, otherwise the raw data
1579 (unless (zerop (length address))
1580 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1581 (push (or pure-address address) contents)))))
1582 (nreverse contents))))
1583
1584 (defun spam-from-listed-p (cache)
1585 (let ((from (nnmail-fetch-field "from"))
1586 found)
1587 (while cache
1588 (let ((address (pop cache)))
1589 (unless (zerop (length address)) ; 0 for a nil address too
1590 (setq address (regexp-quote address))
1591 ;; fix regexp-quote's treatment of user-intended regexes
1592 (while (string-match "\\\\\\*" address)
1593 (setq address (replace-match ".*" t t address))))
1594 (when (and address (string-match address from))
1595 (setq found t
1596 cache nil))))
1597 found))
1598
1599 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1600 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1601 (declassification (if blacklist 'ham 'spam))
1602 (enter-function
1603 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1604 (remove-function
1605 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1606 from addresses unregister-list)
1607 (dolist (article articles)
1608 (let ((from (spam-fetch-field-from-fast article))
1609 (id (spam-fetch-field-message-id-fast article))
1610 sender-ignored)
1611 (when (stringp from)
1612 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1613 (when (and (not sender-ignored)
1614 (stringp ignore-regex)
1615 (string-match ignore-regex from))
1616 (setq sender-ignored t)))
1617 ;; remember the messages we need to unregister, unless remove is set
1618 (when (and
1619 (null unregister)
1620 (spam-log-unregistration-needed-p
1621 id 'process declassification de-symbol))
1622 (push from unregister-list))
1623 (unless sender-ignored
1624 (push from addresses)))))
1625
1626 (if unregister
1627 (funcall enter-function addresses t) ; unregister all these addresses
1628 ;; else, register normally and unregister what we need to
1629 (funcall remove-function unregister-list t)
1630 (dolist (article unregister-list)
1631 (spam-log-undo-registration
1632 (spam-fetch-field-message-id-fast article)
1633 'process
1634 declassification
1635 de-symbol))
1636 (funcall enter-function addresses nil))))
1637
1638 (defun spam-blacklist-unregister-routine (articles)
1639 (spam-blacklist-register-routine articles t))
1640
1641 (defun spam-blacklist-register-routine (articles &optional unregister)
1642 (spam-filelist-register-routine articles t unregister))
1643
1644 (defun spam-whitelist-unregister-routine (articles)
1645 (spam-whitelist-register-routine articles t))
1646
1647 (defun spam-whitelist-register-routine (articles &optional unregister)
1648 (spam-filelist-register-routine articles nil unregister))
1649
1650 \f
1651 ;;;; Spam-report glue
1652 (defun spam-report-gmane-register-routine (articles)
1653 (when articles
1654 (apply 'spam-report-gmane articles)))
1655
1656 \f
1657 ;;;; Bogofilter
1658 (defun spam-check-bogofilter-headers (&optional score)
1659 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1660 (spam-split-group (if spam-split-symbolic-return
1661 'spam
1662 spam-split-group)))
1663 (when header ; return nil when no header
1664 (if score ; scoring mode
1665 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1666 (match-string 1 header)
1667 "0")
1668 ;; spam detection mode
1669 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1670 header)
1671 spam-split-group)))))
1672
1673 ;; return something sensible if the score can't be determined
1674 (defun spam-bogofilter-score ()
1675 "Get the Bogofilter spamicity score"
1676 (interactive)
1677 (save-window-excursion
1678 (gnus-summary-show-article t)
1679 (set-buffer gnus-article-buffer)
1680 (let ((score (or (spam-check-bogofilter-headers t)
1681 (spam-check-bogofilter t))))
1682 (message "Spamicity score %s" score)
1683 (or score "0"))
1684 (gnus-summary-show-article)))
1685
1686 (defun spam-check-bogofilter (&optional score)
1687 "Check the Bogofilter backend for the classification of this message"
1688 (let ((article-buffer-name (buffer-name))
1689 (db spam-bogofilter-database-directory)
1690 return)
1691 (with-temp-buffer
1692 (let ((temp-buffer-name (buffer-name)))
1693 (save-excursion
1694 (set-buffer article-buffer-name)
1695 (apply 'call-process-region
1696 (point-min) (point-max)
1697 spam-bogofilter-path
1698 nil temp-buffer-name nil
1699 (if db `("-d" ,db "-v") `("-v"))))
1700 (setq return (spam-check-bogofilter-headers score))))
1701 return))
1702
1703 (defun spam-bogofilter-register-with-bogofilter (articles
1704 spam
1705 &optional unregister)
1706 "Register an article, given as a string, as spam or non-spam."
1707 (dolist (article articles)
1708 (let ((article-string (spam-get-article-as-string article))
1709 (db spam-bogofilter-database-directory)
1710 (switch (if unregister
1711 (if spam
1712 spam-bogofilter-spam-strong-switch
1713 spam-bogofilter-ham-strong-switch)
1714 (if spam
1715 spam-bogofilter-spam-switch
1716 spam-bogofilter-ham-switch))))
1717 (when (stringp article-string)
1718 (with-temp-buffer
1719 (insert article-string)
1720
1721 (apply 'call-process-region
1722 (point-min) (point-max)
1723 spam-bogofilter-path
1724 nil nil nil switch
1725 (if db `("-d" ,db "-v") `("-v"))))))))
1726
1727 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1728 (spam-bogofilter-register-with-bogofilter articles t unregister))
1729
1730 (defun spam-bogofilter-unregister-spam-routine (articles)
1731 (spam-bogofilter-register-spam-routine articles t))
1732
1733 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1734 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1735
1736 (defun spam-bogofilter-unregister-ham-routine (articles)
1737 (spam-bogofilter-register-ham-routine articles t))
1738
1739
1740 \f
1741 ;;;; spamoracle
1742 (defun spam-check-spamoracle ()
1743 "Run spamoracle on an article to determine whether it's spam."
1744 (let ((article-buffer-name (buffer-name))
1745 (spam-split-group (if spam-split-symbolic-return
1746 'spam
1747 spam-split-group)))
1748 (with-temp-buffer
1749 (let ((temp-buffer-name (buffer-name)))
1750 (save-excursion
1751 (set-buffer article-buffer-name)
1752 (let ((status
1753 (apply 'call-process-region
1754 (point-min) (point-max)
1755 spam-spamoracle-binary
1756 nil temp-buffer-name nil
1757 (if spam-spamoracle-database
1758 `("-f" ,spam-spamoracle-database "mark")
1759 '("mark")))))
1760 (if (eq 0 status)
1761 (progn
1762 (set-buffer temp-buffer-name)
1763 (goto-char (point-min))
1764 (when (re-search-forward "^X-Spam: yes;" nil t)
1765 spam-split-group))
1766 (error "Error running spamoracle: %s" status))))))))
1767
1768 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1769 "Run spamoracle in training mode."
1770 (with-temp-buffer
1771 (let ((temp-buffer-name (buffer-name)))
1772 (save-excursion
1773 (goto-char (point-min))
1774 (dolist (article articles)
1775 (insert (spam-get-article-as-string article)))
1776 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1777 "-spam"
1778 "-good"))
1779 (status
1780 (apply 'call-process-region
1781 (point-min) (point-max)
1782 spam-spamoracle-binary
1783 nil temp-buffer-name nil
1784 (if spam-spamoracle-database
1785 `("-f" ,spam-spamoracle-database
1786 "add" ,arg)
1787 `("add" ,arg)))))
1788 (unless (eq 0 status)
1789 (error "Error running spamoracle: %s" status)))))))
1790
1791 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1792 (spam-spamoracle-learn articles nil unregister))
1793
1794 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1795 (spam-spamoracle-learn-ham articles t))
1796
1797 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1798 (spam-spamoracle-learn articles t unregister))
1799
1800 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1801 (spam-spamoracle-learn-spam articles t))
1802
1803 \f
1804 ;;;; Hooks
1805
1806 ;;;###autoload
1807 (defun spam-initialize ()
1808 "Install the spam.el hooks and do other initialization"
1809 (interactive)
1810 (setq spam-install-hooks t)
1811 ;; TODO: How do we redo this every time the `spam' face is customized?
1812 (push '((eq mark gnus-spam-mark) . spam)
1813 gnus-summary-highlight)
1814 ;; Add hooks for loading and saving the spam stats
1815 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1816 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1817 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1818 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1819 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1820 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1821 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1822
1823 (defun spam-unload-hook ()
1824 "Uninstall the spam.el hooks"
1825 (interactive)
1826 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1827 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1828 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1829 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1830 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1831 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1832 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1833
1834 (add-hook 'spam-unload-hook 'spam-unload-hook)
1835
1836 (when spam-install-hooks
1837 (spam-initialize))
1838
1839 (provide 'spam)
1840
1841 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
1842 ;;; spam.el ends here