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