shr: render <em> as italic, not bold
[emacs.git] / lisp / gnus / spam.el
blobc3be15adc1a0a92449faeeb3250e0e8a72ad3b68
1 ;;; spam.el --- Identifying spam
3 ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Maintainer: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: network, spam, mail, bogofilter, BBDB, dspam, dig, whitelist, blacklist, gmane, hashcash, spamassassin, bsfilter, ifile, stat, crm114, spamoracle
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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: cross-server splitting, remote processing, training through files
37 ;;; Code:
39 ;;{{{ compilation directives and autoloads/requires
41 ;; For Emacs <22.2 and XEmacs.
42 (eval-and-compile
43 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
45 (eval-when-compile (require 'cl))
47 (require 'message) ;for the message-fetch-field functions
48 (require 'gnus-sum)
49 (require 'gnus-uu) ; because of key prefix issues
50 ;;; for the definitions of group content classification and spam processors
51 (require 'gnus)
53 (eval-when-compile (require 'spam-report))
54 (eval-when-compile (require 'hashcash))
56 ;; for nnimap-split-download-body-default
57 (eval-when-compile (require 'nnimap))
59 ;; autoload query-dig
60 (autoload 'query-dig "dig")
62 ;; autoload spam-report
63 (eval-and-compile
64 (autoload 'spam-report-gmane "spam-report")
65 (autoload 'spam-report-gmane-spam "spam-report")
66 (autoload 'spam-report-gmane-ham "spam-report")
67 (autoload 'spam-report-resend "spam-report"))
69 ;; autoload gnus-registry
70 (autoload 'gnus-registry-group-count "gnus-registry")
71 (autoload 'gnus-registry-get-id-key "gnus-registry")
72 (autoload 'gnus-registry-set-id-key "gnus-registry")
73 (autoload 'gnus-registry-handle-action "gnus-registry")
75 ;; autoload dns-query
76 (autoload 'dns-query "dns")
78 ;;}}}
80 ;;{{{ Main parameters.
81 (defvar spam-backends nil
82 "List of spam.el backends with all the pertinent data.
83 Populated by `spam-install-backend-super'.")
85 (defgroup spam nil
86 "Spam configuration."
87 :version "22.1"
88 :group 'mail
89 :group 'news)
91 (defcustom spam-summary-exit-behavior 'default
92 "Exit behavior at the time of summary exit.
93 Note that setting the `spam-use-move' or `spam-use-copy' backends on
94 a group through group/topic parameters overrides this mechanism."
95 :type '(choice
96 (const
97 'default
98 :tag "Move spam out of all groups and ham out of spam groups.")
99 (const
100 'move-all
101 :tag "Move spam out of all groups and ham out of all groups.")
102 (const
103 'move-none
104 :tag "Never move spam or ham out of any groups."))
105 :group 'spam)
107 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
108 "Directory for spam whitelists and blacklists."
109 :type 'directory
110 :group 'spam)
112 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
113 "Whether new messages in a spam group should get the spam-mark."
114 :type 'boolean
115 ;; :version "22.1" ;; Gnus 5.10.8 / No Gnus 0.3
116 :group 'spam)
118 (defcustom spam-log-to-registry nil
119 "Whether spam/ham processing should be logged in the registry."
120 :type 'boolean
121 :group 'spam)
123 (defcustom spam-split-symbolic-return nil
124 "Whether `spam-split' should work with symbols or group names."
125 :type 'boolean
126 :group 'spam)
128 (defcustom spam-split-symbolic-return-positive nil
129 "Whether `spam-split' should ALWAYS work with symbols or group names.
130 Do not set this if you use `spam-split' in a fancy split method."
131 :type 'boolean
132 :group 'spam)
134 (defcustom spam-mark-only-unseen-as-spam t
135 "Whether only unseen articles should be marked as spam in spam groups.
136 When nil, all unread articles in a spam group are marked as
137 spam. Set this if you want to leave an article unread in a spam group
138 without losing it to the automatic spam-marking process."
139 :type 'boolean
140 :group 'spam)
142 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
143 "Whether ham should be marked unread before it's moved.
144 The article is moved out of a spam group according to `ham-process-destination'.
145 This variable is an official entry in the international Longest Variable Name
146 Competition."
147 :type 'boolean
148 :group 'spam)
150 (defcustom spam-disable-spam-split-during-ham-respool nil
151 "Whether `spam-split' should be ignored while resplitting ham.
152 This is useful to prevent ham from ending up in the same spam
153 group after the resplit. Don't set this to t if you have `spam-split' as the
154 last rule in your split configuration."
155 :type 'boolean
156 :group 'spam)
158 (defcustom spam-autodetect-recheck-messages nil
159 "Should spam.el recheck all messages when autodetecting?
160 Normally this is nil, so only unseen messages will be checked."
161 :type 'boolean
162 :group 'spam)
164 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
165 "The location of the whitelist.
166 The file format is one regular expression per line.
167 The regular expression is matched against the address."
168 :type 'file
169 :group 'spam)
171 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
172 "The location of the blacklist.
173 The file format is one regular expression per line.
174 The regular expression is matched against the address."
175 :type 'file
176 :group 'spam)
178 (defcustom spam-use-dig t
179 "Whether `query-dig' should be used instead of `dns-query'."
180 :type 'boolean
181 :group 'spam)
183 (defcustom spam-use-gmane-xref nil
184 "Whether the Gmane spam xref should be used by `spam-split'."
185 :type 'boolean
186 :group 'spam)
188 (defcustom spam-use-blacklist nil
189 "Whether the blacklist should be used by `spam-split'."
190 :type 'boolean
191 :group 'spam)
193 (defcustom spam-blacklist-ignored-regexes nil
194 "Regular expressions that the blacklist should ignore."
195 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
196 :group 'spam)
198 (defcustom spam-use-whitelist nil
199 "Whether the whitelist should be used by `spam-split'."
200 :type 'boolean
201 :group 'spam)
203 (defcustom spam-use-whitelist-exclusive nil
204 "Whether whitelist-exclusive should be used by `spam-split'.
205 Exclusive whitelisting means that all messages from senders not in the whitelist
206 are considered spam."
207 :type 'boolean
208 :group 'spam)
210 (defcustom spam-use-blackholes nil
211 "Whether blackholes should be used by `spam-split'."
212 :type 'boolean
213 :group 'spam)
215 (defcustom spam-use-hashcash nil
216 "Whether hashcash payments should be detected by `spam-split'."
217 :type 'boolean
218 :group 'spam)
220 (defcustom spam-use-regex-headers nil
221 "Whether a header regular expression match should be used by `spam-split'.
222 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
223 :type 'boolean
224 :group 'spam)
226 (defcustom spam-use-regex-body nil
227 "Whether a body regular expression match should be used by `spam-split'.
228 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
229 :type 'boolean
230 :group 'spam)
232 (defcustom spam-use-bogofilter-headers nil
233 "Whether bogofilter headers should be used by `spam-split'.
234 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
235 :type 'boolean
236 :group 'spam)
238 (defcustom spam-use-bogofilter nil
239 "Whether bogofilter should be invoked by `spam-split'.
240 Enable this if you want Gnus to invoke Bogofilter on new messages."
241 :type 'boolean
242 :group 'spam)
244 (defcustom spam-use-bsfilter-headers nil
245 "Whether bsfilter headers should be used by `spam-split'.
246 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
247 :type 'boolean
248 :group 'spam)
250 (defcustom spam-use-bsfilter nil
251 "Whether bsfilter should be invoked by `spam-split'.
252 Enable this if you want Gnus to invoke Bsfilter on new messages."
253 :type 'boolean
254 :group 'spam)
256 (defcustom spam-use-BBDB nil
257 "Whether BBDB should be used by `spam-split'."
258 :type 'boolean
259 :group 'spam)
261 (defcustom spam-use-BBDB-exclusive nil
262 "Whether BBDB-exclusive should be used by `spam-split'.
263 Exclusive BBDB means that all messages from senders not in the BBDB are
264 considered spam."
265 :type 'boolean
266 :group 'spam)
268 (defcustom spam-use-ifile nil
269 "Whether ifile should be used by `spam-split'."
270 :type 'boolean
271 :group 'spam)
273 (defcustom spam-use-stat nil
274 "Whether `spam-stat' should be used by `spam-split'."
275 :type 'boolean
276 :group 'spam)
278 (defcustom spam-use-spamoracle nil
279 "Whether spamoracle should be used by `spam-split'."
280 :type 'boolean
281 :group 'spam)
283 (defcustom spam-use-spamassassin nil
284 "Whether spamassassin should be invoked by `spam-split'.
285 Enable this if you want Gnus to invoke SpamAssassin on new messages."
286 :type 'boolean
287 :group 'spam)
289 (defcustom spam-use-spamassassin-headers nil
290 "Whether spamassassin headers should be checked by `spam-split'.
291 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
292 them."
293 :type 'boolean
294 :group 'spam)
296 (defcustom spam-use-crm114 nil
297 "Whether the CRM114 Mailfilter should be used by `spam-split'."
298 :type 'boolean
299 :group 'spam)
301 (defcustom spam-install-hooks (or
302 spam-use-dig
303 spam-use-gmane-xref
304 spam-use-blacklist
305 spam-use-whitelist
306 spam-use-whitelist-exclusive
307 spam-use-blackholes
308 spam-use-hashcash
309 spam-use-regex-headers
310 spam-use-regex-body
311 spam-use-bogofilter
312 spam-use-bogofilter-headers
313 spam-use-spamassassin
314 spam-use-spamassassin-headers
315 spam-use-bsfilter
316 spam-use-bsfilter-headers
317 spam-use-BBDB
318 spam-use-BBDB-exclusive
319 spam-use-ifile
320 spam-use-stat
321 spam-use-spamoracle
322 spam-use-crm114)
323 "Whether the spam hooks should be installed.
324 Default to t if one of the spam-use-* variables is set."
325 :group 'spam
326 :type 'boolean)
328 (defcustom spam-split-group "spam"
329 "Group name where incoming spam should be put by `spam-split'."
330 :type 'string
331 :group 'spam)
333 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
334 ;;; not regular expressions
335 (defcustom spam-junk-mailgroups (cons
336 spam-split-group
337 '("mail.junk" "poste.pourriel"))
338 "Mailgroups with spam contents.
339 All unmarked article in such group receive the spam mark on group entry."
340 :type '(repeat (string :tag "Group"))
341 :group 'spam)
344 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
345 "The group where spam xrefs can be found on Gmane.
346 Only meaningful if you enable `spam-use-gmane-xref'."
347 :type 'string
348 :group 'spam)
350 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
351 "dev.null.dk" "relays.visi.com")
352 "List of blackhole servers.
353 Only meaningful if you enable `spam-use-blackholes'."
354 :type '(repeat (string :tag "Server"))
355 :group 'spam)
357 (defcustom spam-blackhole-good-server-regex nil
358 "String matching IP addresses that should not be checked in the blackholes.
359 Only meaningful if you enable `spam-use-blackholes'."
360 :type '(radio (const nil) regexp)
361 :group 'spam)
363 (defface spam
364 '((((class color) (type tty) (background dark))
365 (:foreground "gray80" :background "gray50"))
366 (((class color) (type tty) (background light))
367 (:foreground "gray50" :background "gray80"))
368 (((class color) (background dark))
369 (:foreground "ivory2"))
370 (((class color) (background light))
371 (:foreground "ivory4"))
372 (t :inverse-video t))
373 "Face for spam-marked articles."
374 :group 'spam)
375 ;; backward-compatibility alias
376 (put 'spam-face 'face-alias 'spam)
377 (put 'spam-face 'obsolete-face "22.1")
379 (defcustom spam-face 'spam
380 "Face for spam-marked articles."
381 :type 'face
382 :group 'spam)
384 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
385 "Regular expression for positive header spam matches.
386 Only meaningful if you enable `spam-use-regex-headers'."
387 :type '(repeat (regexp :tag "Regular expression to match spam header"))
388 :group 'spam)
390 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
391 "Regular expression for positive header ham matches.
392 Only meaningful if you enable `spam-use-regex-headers'."
393 :type '(repeat (regexp :tag "Regular expression to match ham header"))
394 :group 'spam)
396 (defcustom spam-regex-body-spam '()
397 "Regular expression for positive body spam matches.
398 Only meaningful if you enable `spam-use-regex-body'."
399 :type '(repeat (regexp :tag "Regular expression to match spam body"))
400 :group 'spam)
402 (defcustom spam-regex-body-ham '()
403 "Regular expression for positive body ham matches.
404 Only meaningful if you enable `spam-use-regex-body'."
405 :type '(repeat (regexp :tag "Regular expression to match ham body"))
406 :group 'spam)
408 (defcustom spam-summary-score-preferred-header nil
409 "Preferred header to use for `spam-summary-score'."
410 :type '(choice :tag "Header name"
411 (symbol :tag "SpamAssassin etc" X-Spam-Status)
412 (symbol :tag "Bogofilter" X-Bogosity)
413 (const :tag "No preference, take best guess." nil))
414 :group 'spam)
416 (defgroup spam-ifile nil
417 "Spam ifile configuration."
418 :group 'spam)
420 (make-obsolete-variable 'spam-ifile-path 'spam-ifile-program
421 "Gnus 5.10.9 (Emacs 22.1)")
422 (defcustom spam-ifile-program (executable-find "ifile")
423 "Name of the ifile program."
424 :type '(choice (file :tag "Location of ifile")
425 (const :tag "ifile is not installed"))
426 :group 'spam-ifile)
428 (make-obsolete-variable 'spam-ifile-database-path 'spam-ifile-database
429 "Gnus 5.10.9 (Emacs 22.1)")
430 (defcustom spam-ifile-database nil
431 "File name of the ifile database."
432 :type '(choice (file :tag "Location of the ifile database")
433 (const :tag "Use the default"))
434 :group 'spam-ifile)
436 (defcustom spam-ifile-spam-category "spam"
437 "Name of the spam ifile category."
438 :type 'string
439 :group 'spam-ifile)
441 (defcustom spam-ifile-ham-category nil
442 "Name of the ham ifile category.
443 If nil, the current group name will be used."
444 :type '(choice (string :tag "Use a fixed category")
445 (const :tag "Use the current group name"))
446 :group 'spam-ifile)
448 (defcustom spam-ifile-all-categories nil
449 "Whether the ifile check will return all categories, or just spam.
450 Set this to t if you want to use the `spam-split' invocation of ifile as
451 your main source of newsgroup names."
452 :type 'boolean
453 :group 'spam-ifile)
455 (defgroup spam-bogofilter nil
456 "Spam bogofilter configuration."
457 :group 'spam)
459 (make-obsolete-variable 'spam-bogofilter-path 'spam-bogofilter-program
460 "Gnus 5.10.9 (Emacs 22.1)")
461 (defcustom spam-bogofilter-program (executable-find "bogofilter")
462 "Name of the Bogofilter program."
463 :type '(choice (file :tag "Location of bogofilter")
464 (const :tag "Bogofilter is not installed"))
465 :group 'spam-bogofilter)
467 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
469 (defcustom spam-bogofilter-header "X-Bogosity"
470 "The header that Bogofilter inserts in messages."
471 :type 'string
472 :group 'spam-bogofilter)
474 (defcustom spam-bogofilter-spam-switch "-s"
475 "The switch that Bogofilter uses to register spam messages."
476 :type 'string
477 :group 'spam-bogofilter)
479 (defcustom spam-bogofilter-ham-switch "-n"
480 "The switch that Bogofilter uses to register ham messages."
481 :type 'string
482 :group 'spam-bogofilter)
484 (defcustom spam-bogofilter-spam-strong-switch "-S"
485 "The switch that Bogofilter uses to unregister ham messages."
486 :type 'string
487 :group 'spam-bogofilter)
489 (defcustom spam-bogofilter-ham-strong-switch "-N"
490 "The switch that Bogofilter uses to unregister spam messages."
491 :type 'string
492 :group 'spam-bogofilter)
494 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
495 "The regex on `spam-bogofilter-header' for positive spam identification."
496 :type 'regexp
497 :group 'spam-bogofilter)
499 (defcustom spam-bogofilter-database-directory nil
500 "Location of the Bogofilter database.
501 When nil, use the default location."
502 :type '(choice (directory
503 :tag "Location of the Bogofilter database directory")
504 (const :tag "Use the default"))
505 :group 'spam-bogofilter)
507 (defgroup spam-bsfilter nil
508 "Spam bsfilter configuration."
509 :group 'spam)
511 (make-obsolete-variable 'spam-bsfilter-path 'spam-bsfilter-program
512 "Gnus 5.10.9 (Emacs 22.1)")
513 (defcustom spam-bsfilter-program (executable-find "bsfilter")
514 "Name of the Bsfilter program."
515 :type '(choice (file :tag "Location of bsfilter")
516 (const :tag "Bsfilter is not installed"))
517 :group 'spam-bsfilter)
519 (defcustom spam-bsfilter-header "X-Spam-Flag"
520 "The header inserted by Bsfilter to flag spam."
521 :type 'string
522 :group 'spam-bsfilter)
524 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
525 "The header that Bsfilter inserts in messages."
526 :type 'string
527 :group 'spam-bsfilter)
529 (defcustom spam-bsfilter-spam-switch "--add-spam"
530 "The switch that Bsfilter uses to register spam messages."
531 :type 'string
532 :group 'spam-bsfilter)
534 (defcustom spam-bsfilter-ham-switch "--add-clean"
535 "The switch that Bsfilter uses to register ham messages."
536 :type 'string
537 :group 'spam-bsfilter)
539 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
540 "The switch that Bsfilter uses to unregister ham messages."
541 :type 'string
542 :group 'spam-bsfilter)
544 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
545 "The switch that Bsfilter uses to unregister spam messages."
546 :type 'string
547 :group 'spam-bsfilter)
549 (defcustom spam-bsfilter-database-directory nil
550 "Directory path of the Bsfilter databases."
551 :type '(choice (directory
552 :tag "Location of the Bsfilter database directory")
553 (const :tag "Use the default"))
554 :group 'spam-bsfilter)
556 (defgroup spam-spamoracle nil
557 "Spam spamoracle configuration."
558 :group 'spam)
560 (defcustom spam-spamoracle-database nil
561 "Location of spamoracle database file.
562 When nil, use the default spamoracle database."
563 :type '(choice (directory :tag "Location of spamoracle database file.")
564 (const :tag "Use the default"))
565 :group 'spam-spamoracle)
567 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
568 "Location of the spamoracle binary."
569 :type '(choice (directory :tag "Location of the spamoracle binary")
570 (const :tag "Use the default"))
571 :group 'spam-spamoracle)
573 (defgroup spam-spamassassin nil
574 "Spam SpamAssassin configuration."
575 :group 'spam)
577 (make-obsolete-variable 'spam-spamassassin-path
578 'spam-spamassassin-program "Gnus 5.10.9 (Emacs 22.1)")
579 (defcustom spam-assassin-program (executable-find "spamassassin")
580 "Name of the spamassassin program.
581 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
582 spamd man pages for more information on these programs."
583 :type '(choice (file :tag "Location of spamc")
584 (const :tag "spamassassin is not installed"))
585 :group 'spam-spamassassin)
587 (defcustom spam-spamassassin-arguments ()
588 "Arguments to pass to the spamassassin executable.
589 This must be a list. For example, `(\"-C\" \"configfile\")'."
590 :type '(restricted-sexp :match-alternatives (listp))
591 :group 'spam-spamassassin)
593 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
594 "The header inserted by SpamAssassin to flag spam."
595 :type 'string
596 :group 'spam-spamassassin)
598 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
599 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
600 identification"
601 :type 'string
602 :group 'spam-spamassassin)
604 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
605 "The header inserted by SpamAssassin, giving extended scoring information"
606 :type 'string
607 :group 'spam-spamassassin)
609 (make-obsolete-variable 'spam-sa-learn-path 'spam-sa-learn-program
610 "Gnus 5.10.9 (Emacs 22.1)")
611 (defcustom spam-sa-learn-program (executable-find "sa-learn")
612 "Name of the sa-learn program."
613 :type '(choice (file :tag "Location of spamassassin")
614 (const :tag "spamassassin is not installed"))
615 :group 'spam-spamassassin)
617 (defcustom spam-sa-learn-rebuild t
618 "Whether sa-learn should rebuild the database every time it is called
619 Enable this if you want sa-learn to rebuild the database automatically. Doing
620 this will slightly increase the running time of the spam registration process.
621 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
622 order for SpamAssassin to recognize the new registered spam."
623 :type 'boolean
624 :group 'spam-spamassassin)
626 (defcustom spam-sa-learn-spam-switch "--spam"
627 "The switch that sa-learn uses to register spam messages."
628 :type 'string
629 :group 'spam-spamassassin)
631 (defcustom spam-sa-learn-ham-switch "--ham"
632 "The switch that sa-learn uses to register ham messages."
633 :type 'string
634 :group 'spam-spamassassin)
636 (defcustom spam-sa-learn-unregister-switch "--forget"
637 "The switch that sa-learn uses to unregister messages messages."
638 :type 'string
639 :group 'spam-spamassassin)
641 (defgroup spam-crm114 nil
642 "Spam CRM114 Mailfilter configuration."
643 :group 'spam)
645 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
646 "File path of the CRM114 Mailfilter executable program."
647 :type '(choice (file :tag "Location of CRM114 Mailfilter")
648 (const :tag "CRM114 Mailfilter is not installed"))
649 :group 'spam-crm114)
651 (defcustom spam-crm114-header "X-CRM114-Status"
652 "The header that CRM114 Mailfilter inserts in messages."
653 :type 'string
654 :group 'spam-crm114)
656 (defcustom spam-crm114-spam-switch "--learnspam"
657 "The switch that CRM114 Mailfilter uses to register spam messages."
658 :type 'string
659 :group 'spam-crm114)
661 (defcustom spam-crm114-ham-switch "--learnnonspam"
662 "The switch that CRM114 Mailfilter uses to register ham messages."
663 :type 'string
664 :group 'spam-crm114)
666 (defcustom spam-crm114-spam-strong-switch "--unlearn"
667 "The switch that CRM114 Mailfilter uses to unregister ham messages."
668 :type 'string
669 :group 'spam-crm114)
671 (defcustom spam-crm114-ham-strong-switch "--unlearn"
672 "The switch that CRM114 Mailfilter uses to unregister spam messages."
673 :type 'string
674 :group 'spam-crm114)
676 (defcustom spam-crm114-positive-spam-header "^SPAM"
677 "The regex on `spam-crm114-header' for positive spam identification."
678 :type 'regexp
679 :group 'spam-crm114)
681 (defcustom spam-crm114-database-directory nil
682 "Directory path of the CRM114 Mailfilter databases."
683 :type '(choice (directory
684 :tag "Location of the CRM114 Mailfilter database directory")
685 (const :tag "Use the default"))
686 :group 'spam-crm114)
688 ;;; Key bindings for spam control.
690 (gnus-define-keys gnus-summary-mode-map
691 "St" spam-generic-score
692 "Sx" gnus-summary-mark-as-spam
693 "Mst" spam-generic-score
694 "Msx" gnus-summary-mark-as-spam
695 "\M-d" gnus-summary-mark-as-spam
696 "$" gnus-summary-mark-as-spam)
698 (defvar spam-cache-lookups t
699 "Whether spam.el will try to cache lookups using `spam-caches'.")
701 (defvar spam-caches (make-hash-table
702 :size 10
703 :test 'equal)
704 "Cache of spam detection entries.")
706 (defvar spam-old-articles nil
707 "List of old ham and spam articles, generated when a group is entered.")
709 (defvar spam-split-disabled nil
710 "If non-nil, `spam-split' is disabled, and always returns nil.")
712 (defvar spam-split-last-successful-check nil
713 "Internal variable.
714 `spam-split' will set this to nil or a spam-use-XYZ check if it
715 finds ham or spam.")
717 ;; internal variables for backends
718 ;; TODO: find a way to create these on the fly in spam-install-backend-super
719 (defvar spam-use-copy nil)
720 (defvar spam-use-move nil)
721 (defvar spam-use-gmane nil)
722 (defvar spam-use-resend nil)
724 ;;}}}
726 ;;{{{ convenience functions
728 (defun spam-clear-cache (symbol)
729 "Clear the `spam-caches' entry for a check."
730 (remhash symbol spam-caches))
732 (defun spam-xor (a b)
733 "Logical A xor B."
734 (and (or a b) (not (and a b))))
736 (defun spam-set-difference (list1 list2)
737 "Return a set difference of LIST1 and LIST2.
738 When either list is nil, the other is returned."
739 (if (and list1 list2)
740 ;; we have two non-nil lists
741 (progn
742 (dolist (item (append list1 list2))
743 (when (and (memq item list1) (memq item list2))
744 (setq list1 (delq item list1))
745 (setq list2 (delq item list2))))
746 (append list1 list2))
747 ;; if either of the lists was nil, return the other one
748 (if list1 list1 list2)))
750 (defun spam-group-ham-mark-p (group mark &optional spam)
751 "Checks if MARK is considered a ham mark in GROUP."
752 (when (stringp group)
753 (let* ((marks (spam-group-ham-marks group spam))
754 (marks (if (symbolp mark)
755 marks
756 (mapcar 'symbol-value marks))))
757 (memq mark marks))))
759 (defun spam-group-spam-mark-p (group mark)
760 "Checks if MARK is considered a spam mark in GROUP."
761 (spam-group-ham-mark-p group mark t))
763 (defun spam-group-ham-marks (group &optional spam)
764 "In GROUP, get all the ham marks."
765 (when (stringp group)
766 (let* ((marks (if spam
767 (gnus-parameter-spam-marks group)
768 (gnus-parameter-ham-marks group)))
769 (marks (car marks))
770 (marks (if (listp (car marks)) (car marks) marks)))
771 marks)))
773 (defun spam-group-spam-marks (group)
774 "In GROUP, get all the spam marks."
775 (spam-group-ham-marks group t))
777 (defun spam-group-spam-contents-p (group)
778 "Is GROUP a spam group?"
779 (if (and (stringp group) (< 0 (length group)))
780 (or (member group spam-junk-mailgroups)
781 (memq 'gnus-group-spam-classification-spam
782 (gnus-parameter-spam-contents group)))
783 nil))
785 (defun spam-group-ham-contents-p (group)
786 "Is GROUP a ham group?"
787 (if (stringp group)
788 (memq 'gnus-group-spam-classification-ham
789 (gnus-parameter-spam-contents group))
790 nil))
792 (defun spam-classifications ()
793 "Return list of valid classifications"
794 '(spam ham))
796 (defun spam-classification-valid-p (classification)
797 "Is CLASSIFICATION a valid spam/ham classification?"
798 (memq classification (spam-classifications)))
800 (defun spam-backend-properties ()
801 "Return list of valid classifications."
802 '(statistical mover check hrf srf huf suf))
804 (defun spam-backend-property-valid-p (property)
805 "Is PROPERTY a valid backend property?"
806 (memq property (spam-backend-properties)))
808 (defun spam-backend-function-type-valid-p (type)
809 (or (eq type 'registration)
810 (eq type 'unregistration)))
812 (defun spam-process-type-valid-p (process-type)
813 (or (eq process-type 'incoming)
814 (eq process-type 'process)))
816 (defun spam-list-articles (articles classification)
817 (let ((mark-check (if (eq classification 'spam)
818 'spam-group-spam-mark-p
819 'spam-group-ham-mark-p))
820 alist mark-cache-yes mark-cache-no)
821 (dolist (article articles)
822 (let ((mark (gnus-summary-article-mark article)))
823 (unless (or (memq mark mark-cache-yes)
824 (memq mark mark-cache-no))
825 (if (funcall mark-check
826 gnus-newsgroup-name
827 mark)
828 (push mark mark-cache-yes)
829 (push mark mark-cache-no)))
830 (when (memq mark mark-cache-yes)
831 (push article alist))))
832 alist))
834 ;;}}}
836 ;;{{{ backend installation functions and procedures
838 (defun spam-install-backend-super (backend &rest properties)
839 "Install BACKEND for spam.el.
840 Accepts incoming CHECK, ham registration function HRF, spam
841 registration function SRF, ham unregistration function HUF, spam
842 unregistration function SUF, and an indication whether the
843 backend is STATISTICAL."
844 (setq spam-backends (add-to-list 'spam-backends backend))
845 (while properties
846 (let ((property (pop properties))
847 (value (pop properties)))
848 (if (spam-backend-property-valid-p property)
849 (put backend property value)
850 (gnus-error
852 "spam-install-backend-super got an invalid property %s"
853 property)))))
855 (defun spam-backend-list (&optional type)
856 "Return a list of all the backend symbols, constrained by TYPE.
857 When TYPE is 'non-mover, only non-mover backends are returned.
858 When TYPE is 'mover, only mover backends are returned."
859 (let (list)
860 (dolist (backend spam-backends)
861 (when (or
862 (null type) ;either no type was requested
863 ;; or the type is 'mover and the backend is a mover
864 (and
865 (eq type 'mover)
866 (spam-backend-mover-p backend))
867 ;; or the type is 'non-mover and the backend is not a mover
868 (and
869 (eq type 'non-mover)
870 (not (spam-backend-mover-p backend))))
871 (push backend list)))
872 list))
874 (defun spam-backend-check (backend)
875 "Get the check function for BACKEND.
876 Each individual check may return nil, t, or a mailgroup name.
877 The value nil means that the check does not yield a decision, and
878 so, that further checks are needed. The value t means that the
879 message is definitely not spam, and that further spam checks
880 should be inhibited. Otherwise, a mailgroup name or the symbol
881 'spam (depending on `spam-split-symbolic-return') is returned where
882 the mail should go, and further checks are also inhibited. The
883 usual mailgroup name is the value of `spam-split-group', meaning
884 that the message is definitely a spam."
885 (get backend 'check))
887 (defun spam-backend-valid-p (backend)
888 "Is BACKEND valid?"
889 (member backend (spam-backend-list)))
891 (defun spam-backend-info (backend)
892 "Return information about BACKEND."
893 (if (spam-backend-valid-p backend)
894 (let (info)
895 (setq info (format "Backend %s has the following properties:\n"
896 backend))
897 (dolist (property (spam-backend-properties))
898 (setq info (format "%s%s=%s\n"
899 info
900 property
901 (get backend property))))
902 info)
903 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
904 backend)))
906 (defun spam-backend-function (backend classification type)
907 "Get the BACKEND function for CLASSIFICATION and TYPE.
908 TYPE is 'registration or 'unregistration.
909 CLASSIFICATION is 'ham or 'spam."
910 (if (and
911 (spam-classification-valid-p classification)
912 (spam-backend-function-type-valid-p type))
913 (let ((retrieval
914 (intern
915 (format "spam-backend-%s-%s-function"
916 classification
917 type))))
918 (funcall retrieval backend))
919 (gnus-error
921 "%s was passed invalid backend %s, classification %s, or type %s"
922 "spam-backend-function"
923 backend
924 classification
925 type)))
927 (defun spam-backend-article-list-property (classification
928 &optional unregister)
929 "Property name of article list with CLASSIFICATION and UNREGISTER."
930 (let* ((r (if unregister "unregister" "register"))
931 (prop (format "%s-%s" classification r)))
932 prop))
934 (defun spam-backend-get-article-todo-list (backend
935 classification
936 &optional unregister)
937 "Get the articles to be processed for BACKEND and CLASSIFICATION.
938 With UNREGISTER, get articles to be unregistered.
939 This is a temporary storage function - nothing here persists."
940 (get
941 backend
942 (intern (spam-backend-article-list-property classification unregister))))
944 (defun spam-backend-put-article-todo-list (backend classification list
945 &optional unregister)
946 "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
947 With UNREGISTER, set articles to be unregistered.
948 This is a temporary storage function - nothing here persists."
949 (put
950 backend
951 (intern (spam-backend-article-list-property classification unregister))
952 list))
954 (defun spam-backend-ham-registration-function (backend)
955 "Get the ham registration function for BACKEND."
956 (get backend 'hrf))
958 (defun spam-backend-spam-registration-function (backend)
959 "Get the spam registration function for BACKEND."
960 (get backend 'srf))
962 (defun spam-backend-ham-unregistration-function (backend)
963 "Get the ham unregistration function for BACKEND."
964 (get backend 'huf))
966 (defun spam-backend-spam-unregistration-function (backend)
967 "Get the spam unregistration function for BACKEND."
968 (get backend 'suf))
970 (defun spam-backend-statistical-p (backend)
971 "Is BACKEND statistical?"
972 (get backend 'statistical))
974 (defun spam-backend-mover-p (backend)
975 "Is BACKEND a mover?"
976 (get backend 'mover))
978 (defun spam-install-backend-alias (backend alias)
979 "Add ALIAS to an existing BACKEND.
980 The previous backend settings for ALIAS are erased."
982 ;; install alias with no properties at first
983 (spam-install-backend-super alias)
985 (dolist (property (spam-backend-properties))
986 (put alias property (get backend property))))
988 (defun spam-install-checkonly-backend (backend check)
989 "Install a BACKEND than can only CHECK for spam."
990 (spam-install-backend-super backend 'check check))
992 (defun spam-install-mover-backend (backend hrf srf huf suf)
993 "Install a BACKEND than can move articles at summary exit.
994 Accepts ham registration function HRF, spam registration function
995 SRF, ham unregistration function HUF, spam unregistration
996 function SUF. The backend has no incoming check and can't be
997 statistical."
998 (spam-install-backend-super
999 backend
1000 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
1002 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
1003 "Install a BACKEND than has no check.
1004 Accepts ham registration function HRF, spam registration function
1005 SRF, ham unregistration function HUF, spam unregistration
1006 function SUF. The backend has no incoming check and can't be
1007 statistical (it could be, but in practice that doesn't happen)."
1008 (spam-install-backend-super
1009 backend
1010 'hrf hrf 'srf srf 'huf huf 'suf suf))
1012 (defun spam-install-backend (backend check hrf srf huf suf)
1013 "Install a BACKEND.
1014 Accepts incoming CHECK, ham registration function HRF, spam
1015 registration function SRF, ham unregistration function HUF, spam
1016 unregistration function SUF. The backend won't be
1017 statistical (use `spam-install-statistical-backend' for that)."
1018 (spam-install-backend-super
1019 backend
1020 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
1022 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
1023 "Install a BACKEND.
1024 Accepts incoming CHECK, ham registration function HRF, spam
1025 registration function SRF, ham unregistration function HUF, spam
1026 unregistration function SUF. The backend will be
1027 statistical (use `spam-install-backend' for non-statistical
1028 backends)."
1029 (spam-install-backend-super
1030 backend
1031 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1033 (defun spam-install-statistical-checkonly-backend (backend check)
1034 "Install a statistical BACKEND than can only CHECK for spam."
1035 (spam-install-backend-super
1036 backend
1037 'check check 'statistical t))
1039 ;;}}}
1041 ;;{{{ backend installations
1042 (spam-install-checkonly-backend 'spam-use-blackholes
1043 'spam-check-blackholes)
1045 (spam-install-checkonly-backend 'spam-use-hashcash
1046 'spam-check-hashcash)
1048 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1049 'spam-check-spamassassin-headers)
1051 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1052 'spam-check-bogofilter-headers)
1054 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1055 'spam-check-bsfilter-headers)
1057 (spam-install-checkonly-backend 'spam-use-gmane-xref
1058 'spam-check-gmane-xref)
1060 (spam-install-checkonly-backend 'spam-use-regex-headers
1061 'spam-check-regex-headers)
1063 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1064 'spam-check-regex-body)
1066 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy)
1067 (spam-install-mover-backend 'spam-use-move
1068 'spam-move-ham-routine
1069 'spam-move-spam-routine
1071 nil)
1073 (spam-install-nocheck-backend 'spam-use-copy
1074 'spam-copy-ham-routine
1075 'spam-copy-spam-routine
1077 nil)
1079 (spam-install-nocheck-backend 'spam-use-gmane
1080 'spam-report-gmane-unregister-routine
1081 'spam-report-gmane-register-routine
1082 'spam-report-gmane-register-routine
1083 'spam-report-gmane-unregister-routine)
1085 (spam-install-nocheck-backend 'spam-use-resend
1086 'spam-report-resend-register-ham-routine
1087 'spam-report-resend-register-routine
1089 nil)
1091 (spam-install-backend 'spam-use-BBDB
1092 'spam-check-BBDB
1093 'spam-BBDB-register-routine
1095 'spam-BBDB-unregister-routine
1096 nil)
1098 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1100 (spam-install-backend 'spam-use-blacklist
1101 'spam-check-blacklist
1103 'spam-blacklist-register-routine
1105 'spam-blacklist-unregister-routine)
1107 (spam-install-backend 'spam-use-whitelist
1108 'spam-check-whitelist
1109 'spam-whitelist-register-routine
1111 'spam-whitelist-unregister-routine
1112 nil)
1114 (spam-install-statistical-backend 'spam-use-ifile
1115 'spam-check-ifile
1116 'spam-ifile-register-ham-routine
1117 'spam-ifile-register-spam-routine
1118 'spam-ifile-unregister-ham-routine
1119 'spam-ifile-unregister-spam-routine)
1121 (spam-install-statistical-backend 'spam-use-spamoracle
1122 'spam-check-spamoracle
1123 'spam-spamoracle-learn-ham
1124 'spam-spamoracle-learn-spam
1125 'spam-spamoracle-unlearn-ham
1126 'spam-spamoracle-unlearn-spam)
1128 (spam-install-statistical-backend 'spam-use-stat
1129 'spam-check-stat
1130 'spam-stat-register-ham-routine
1131 'spam-stat-register-spam-routine
1132 'spam-stat-unregister-ham-routine
1133 'spam-stat-unregister-spam-routine)
1135 (spam-install-statistical-backend 'spam-use-spamassassin
1136 'spam-check-spamassassin
1137 'spam-spamassassin-register-ham-routine
1138 'spam-spamassassin-register-spam-routine
1139 'spam-spamassassin-unregister-ham-routine
1140 'spam-spamassassin-unregister-spam-routine)
1142 (spam-install-statistical-backend 'spam-use-bogofilter
1143 'spam-check-bogofilter
1144 'spam-bogofilter-register-ham-routine
1145 'spam-bogofilter-register-spam-routine
1146 'spam-bogofilter-unregister-ham-routine
1147 'spam-bogofilter-unregister-spam-routine)
1149 (spam-install-statistical-backend 'spam-use-bsfilter
1150 'spam-check-bsfilter
1151 'spam-bsfilter-register-ham-routine
1152 'spam-bsfilter-register-spam-routine
1153 'spam-bsfilter-unregister-ham-routine
1154 'spam-bsfilter-unregister-spam-routine)
1156 (spam-install-statistical-backend 'spam-use-crm114
1157 'spam-check-crm114
1158 'spam-crm114-register-ham-routine
1159 'spam-crm114-register-spam-routine
1160 'spam-crm114-unregister-ham-routine
1161 'spam-crm114-unregister-spam-routine)
1162 ;;}}}
1164 ;;{{{ scoring and summary formatting
1165 (defun spam-necessary-extra-headers ()
1166 "Return the extra headers spam.el thinks are necessary."
1167 (let (list)
1168 (when (or spam-use-spamassassin
1169 spam-use-spamassassin-headers
1170 spam-use-regex-headers)
1171 (push 'X-Spam-Status list))
1172 (when (or spam-use-bogofilter
1173 spam-use-regex-headers)
1174 (push 'X-Bogosity list))
1175 (when (or spam-use-crm114
1176 spam-use-regex-headers)
1177 (push 'X-CRM114-Status list))
1178 list))
1180 (defun spam-user-format-function-S (headers)
1181 (when headers
1182 (format "%3.2f"
1183 (spam-summary-score headers spam-summary-score-preferred-header))))
1185 (defun spam-article-sort-by-spam-status (h1 h2)
1186 "Sort articles by score."
1187 (let (result)
1188 (dolist (header (spam-necessary-extra-headers))
1189 (let ((s1 (spam-summary-score h1 header))
1190 (s2 (spam-summary-score h2 header)))
1191 (unless (= s1 s2)
1192 (setq result (< s1 s2))
1193 (return))))
1194 result))
1196 (defvar spam-spamassassin-score-regexp
1197 ".*\\b\\(?:score\\|hits\\)=\\(-?[0-9.]+\\)"
1198 "Regexp matching SpamAssassin score header.
1199 The first group must match the number.")
1201 (defun spam-extra-header-to-number (header headers)
1202 "Transform an extra HEADER to a number, using list of HEADERS.
1203 Note this has to be fast."
1204 (let ((header-content (gnus-extra-header header headers)))
1205 (if header-content
1206 (cond
1207 ((eq header 'X-Spam-Status)
1208 (string-to-number (gnus-replace-in-string
1209 header-content
1210 spam-spamassassin-score-regexp
1211 "\\1")))
1212 ;; for CRM checking, it's probably faster to just do the string match
1213 ((string-match "( pR: \\([0-9.-]+\\)" header-content)
1214 (- (string-to-number (match-string 1 header-content))))
1215 ((eq header 'X-Bogosity)
1216 (string-to-number (gnus-replace-in-string
1217 (gnus-replace-in-string
1218 header-content
1219 ".*spamicity=" "")
1220 ",.*" "")))
1221 (t nil))
1222 nil)))
1224 (defun spam-summary-score (headers &optional specific-header)
1225 "Score an article for the summary buffer, as fast as possible.
1226 With SPECIFIC-HEADER, returns only that header's score.
1227 Will not return a nil score."
1228 (let (score)
1229 (dolist (header
1230 (if specific-header
1231 (list specific-header)
1232 (spam-necessary-extra-headers)))
1233 (setq score
1234 (spam-extra-header-to-number header headers))
1235 (when score
1236 (return)))
1237 (or score 0)))
1239 (defun spam-generic-score (&optional recheck)
1240 "Invoke whatever scoring method we can."
1241 (interactive "P")
1242 (cond
1243 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1244 (spam-spamassassin-score recheck))
1245 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1246 (spam-bsfilter-score recheck))
1247 (spam-use-crm114
1248 (spam-crm114-score))
1249 (t (spam-bogofilter-score recheck))))
1250 ;;}}}
1252 ;;{{{ set up widening, processor checks
1254 ;;; set up IMAP widening if it's necessary
1255 (defun spam-setup-widening ()
1256 (when (spam-widening-needed-p)
1257 (setq nnimap-split-download-body-default t)))
1259 (defun spam-widening-needed-p (&optional force-symbols)
1260 (let (found)
1261 (dolist (backend (spam-backend-list))
1262 (when (and (spam-backend-statistical-p backend)
1263 (or (symbol-value backend)
1264 (memq backend force-symbols)))
1265 (setq found backend)))
1266 found))
1268 (defvar spam-list-of-processors
1269 ;; note the nil processors are not defined in gnus.el
1270 '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
1271 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
1272 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
1273 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
1274 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
1275 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
1276 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1277 (gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane) ;; Buggy?
1278 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
1279 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
1280 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
1281 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
1282 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
1283 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
1284 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
1285 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
1286 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
1287 "The OBSOLETE `spam-list-of-processors' list.
1288 This list contains pairs associating the obsolete ham/spam exit
1289 processor variables with a classification and a spam-use-*
1290 variable. When the processor variable is nil, just the
1291 classification and spam-use-* check variable are used. This is
1292 superseded by the new spam backend code, so it's only consulted
1293 for backwards compatibility.")
1294 (make-obsolete-variable 'spam-list-of-processors nil "22.1")
1296 (defun spam-group-processor-p (group backend &optional classification)
1297 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1298 Also accepts the obsolete processors, which can be found in
1299 gnus.el and in spam-list-of-processors. In the case of mover
1300 backends, checks the setting of `spam-summary-exit-behavior' in
1301 addition to the set values for the group."
1302 (if (and (stringp group)
1303 (symbolp backend))
1304 (let ((old-style (assq backend spam-list-of-processors))
1305 (parameters (nth 0 (gnus-parameter-spam-process group)))
1306 found)
1307 (if old-style ; old-style processor
1308 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1309 ;; now search for the parameter
1310 (dolist (parameter parameters)
1311 (when (and (null found)
1312 (listp parameter)
1313 (eq classification (nth 0 parameter))
1314 (eq backend (nth 1 parameter)))
1315 (setq found t)))
1317 ;; now, if the parameter was not found, do the
1318 ;; spam-summary-exit-behavior-logic for mover backends
1319 (unless found
1320 (when (spam-backend-mover-p backend)
1321 (setq
1322 found
1323 (cond
1324 ((eq spam-summary-exit-behavior 'move-all) t)
1325 ((eq spam-summary-exit-behavior 'move-none) nil)
1326 ((eq spam-summary-exit-behavior 'default)
1327 (or (eq classification 'spam) ;move spam out of all groups
1328 ;; move ham out of spam groups
1329 (and (eq classification 'ham)
1330 (spam-group-spam-contents-p group))))
1331 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1332 spam-summary-exit-behavior))))))
1334 found))
1335 nil))
1337 ;;}}}
1339 ;;{{{ Summary entry and exit processing.
1341 (defun spam-mark-junk-as-spam-routine ()
1342 ;; check the global list of group names spam-junk-mailgroups and the
1343 ;; group parameters
1344 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1345 (gnus-message 6 "Marking %s articles as spam"
1346 (if spam-mark-only-unseen-as-spam
1347 "unseen"
1348 "unread"))
1349 (let ((articles (if spam-mark-only-unseen-as-spam
1350 gnus-newsgroup-unseen
1351 gnus-newsgroup-unreads)))
1352 (if spam-mark-new-messages-in-spam-group-as-spam
1353 (dolist (article articles)
1354 (gnus-summary-mark-article article gnus-spam-mark))
1355 (gnus-message 9 "Did not mark new messages as spam.")))))
1357 (defun spam-summary-prepare ()
1358 (setq spam-old-articles
1359 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1360 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1361 (spam-mark-junk-as-spam-routine))
1363 ;; The spam processors are invoked for any group, spam or ham or neither
1364 (defun spam-summary-prepare-exit ()
1365 (unless gnus-group-is-exiting-without-update-p
1366 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1368 ;; before we begin, remove any article limits
1369 ; (ignore-errors
1370 ; (gnus-summary-pop-limit t))
1372 ;; first of all, unregister any articles that are no longer ham or spam
1373 ;; we have to iterate over the processors, or else we'll be too slow
1374 (dolist (classification (spam-classifications))
1375 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1376 (new-articles (spam-list-articles
1377 gnus-newsgroup-articles
1378 classification))
1379 (changed-articles (spam-set-difference new-articles old-articles)))
1380 ;; now that we have the changed articles, we go through the processors
1381 (dolist (backend (spam-backend-list))
1382 (let (unregister-list)
1383 (dolist (article changed-articles)
1384 (let ((id (spam-fetch-field-message-id-fast article)))
1385 (when (spam-log-unregistration-needed-p
1386 id 'process classification backend)
1387 (push article unregister-list))))
1388 ;; call spam-register-routine with specific articles to unregister,
1389 ;; when there are articles to unregister and the check is enabled
1390 (when (and unregister-list (symbol-value backend))
1391 (spam-backend-put-article-todo-list backend
1392 classification
1393 unregister-list
1394 t))))))
1396 ;; do the non-moving backends first, then the moving ones
1397 (dolist (backend-type '(non-mover mover))
1398 (dolist (classification (spam-classifications))
1399 (dolist (backend (spam-backend-list backend-type))
1400 (when (spam-group-processor-p
1401 gnus-newsgroup-name
1402 backend
1403 classification)
1404 (spam-backend-put-article-todo-list backend
1405 classification
1406 (spam-list-articles
1407 gnus-newsgroup-articles
1408 classification))))))
1410 (spam-resolve-registrations-routine) ; do the registrations now
1412 ;; we mark all the leftover spam articles as expired at the end
1413 (dolist (article (spam-list-articles
1414 gnus-newsgroup-articles
1415 'spam))
1416 (gnus-summary-mark-article article gnus-expirable-mark)))
1418 (setq spam-old-articles nil))
1420 ;;}}}
1422 ;;{{{ spam-use-move and spam-use-copy backend support functions
1424 (defun spam-copy-or-move-routine (copy groups articles classification)
1426 (when (and (car-safe groups) (listp (car-safe groups)))
1427 (setq groups (pop groups)))
1429 (unless (listp groups)
1430 (setq groups (list groups)))
1432 ;; remove the current process mark
1433 (gnus-summary-kill-process-mark)
1435 (let ((backend-supports-deletions
1436 (gnus-check-backend-function
1437 'request-move-article gnus-newsgroup-name))
1438 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1439 article mark deletep respool valid-move-destinations)
1441 (when (member 'respool groups)
1442 (setq respool t) ; boolean for later
1443 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1445 ;; exclude invalid move destinations
1446 (dolist (group groups)
1447 (unless
1449 (and
1450 (eq classification 'spam)
1451 (spam-group-spam-contents-p gnus-newsgroup-name)
1452 (spam-group-spam-contents-p group)
1453 (gnus-message
1455 "Sorry, can't move spam from spam group %s to spam group %s"
1456 gnus-newsgroup-name
1457 group))
1458 (and
1459 (eq classification 'ham)
1460 (spam-group-ham-contents-p gnus-newsgroup-name)
1461 (spam-group-ham-contents-p group)
1462 (gnus-message
1464 "Sorry, can't move ham from ham group %s to ham group %s"
1465 gnus-newsgroup-name
1466 group)))
1467 (push group valid-move-destinations)))
1469 (setq groups (nreverse valid-move-destinations))
1471 ;; now do the actual move
1472 (dolist (group groups)
1474 (when (and articles (stringp group))
1476 ;; first, mark the article with the process mark and, if needed,
1477 ;; the unread or expired mark (for ham and spam respectively)
1478 (dolist (article articles)
1479 (when (and (eq classification 'ham)
1480 spam-mark-ham-unread-before-move-from-spam-group)
1481 (gnus-message 9 "Marking ham article %d unread before move"
1482 article)
1483 (gnus-summary-mark-article article gnus-unread-mark))
1484 (when (and (eq classification 'spam)
1485 (not copy))
1486 (gnus-message 9 "Marking spam article %d expirable before move"
1487 article)
1488 (gnus-summary-mark-article article gnus-expirable-mark))
1489 (gnus-summary-set-process-mark article)
1491 (if respool ; respooling is with a "fake" group
1492 (let ((spam-split-disabled
1493 (or spam-split-disabled
1494 (and (eq classification 'ham)
1495 spam-disable-spam-split-during-ham-respool))))
1496 (gnus-message 9 "Respooling article %d with method %s"
1497 article respool-method)
1498 (gnus-summary-respool-article nil respool-method))
1499 ;; else, we are not respooling
1500 (if (or (not backend-supports-deletions)
1501 (> (length groups) 1))
1502 (progn ; if copying, copy and set deletep
1503 (gnus-message 9 "Copying article %d to group %s"
1504 article group)
1505 (gnus-summary-copy-article nil group)
1506 (setq deletep t))
1507 (gnus-message 9 "Moving article %d to group %s"
1508 article group)
1509 (gnus-summary-move-article nil group)))))) ; else move articles
1511 ;; now delete the articles, unless a) copy is t, and there was a copy done
1512 ;; b) a move was done to a single group
1513 ;; c) backend-supports-deletions is nil
1514 (unless copy
1515 (when (and deletep backend-supports-deletions)
1516 (dolist (article articles)
1517 (gnus-summary-set-process-mark article)
1518 (gnus-message 9 "Deleting article %d" article))
1519 (when articles
1520 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1521 (gnus-summary-delete-article nil)))))
1522 (gnus-summary-yank-process-mark)
1523 (length articles)))
1525 (defun spam-copy-spam-routine (articles)
1526 (spam-copy-or-move-routine
1528 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1529 articles
1530 'spam))
1532 (defun spam-move-spam-routine (articles)
1533 (spam-copy-or-move-routine
1535 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1536 articles
1537 'spam))
1539 (defun spam-copy-ham-routine (articles)
1540 (spam-copy-or-move-routine
1542 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1543 articles
1544 'ham))
1546 (defun spam-move-ham-routine (articles)
1547 (spam-copy-or-move-routine
1549 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1550 articles
1551 'ham))
1553 ;;}}}
1555 ;;{{{ article and field retrieval code
1556 (defun spam-get-article-as-string (article)
1557 (when (numberp article)
1558 (with-temp-buffer
1559 (gnus-request-article-this-buffer
1560 article
1561 gnus-newsgroup-name)
1562 (buffer-string))))
1564 ;; disabled for now
1565 ;; (defun spam-get-article-as-filename (article)
1566 ;; (let ((article-filename))
1567 ;; (when (numberp article)
1568 ;; (nnml-possibly-change-directory
1569 ;; (gnus-group-real-name gnus-newsgroup-name))
1570 ;; (setq article-filename (expand-file-name
1571 ;; (int-to-string article) nnml-current-directory)))
1572 ;; (if (file-exists-p article-filename)
1573 ;; article-filename
1574 ;; nil)))
1576 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1577 "Fetch a FIELD for ARTICLE with the internal `gnus-data-list' function.
1578 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1579 When FIELD is 'number, ARTICLE can be any number (since we want
1580 to find it out)."
1581 (when (numberp article)
1582 (let* ((data-header (or prepared-data-header
1583 (spam-fetch-article-header article))))
1584 (cond
1585 ((not (arrayp data-header))
1586 (gnus-message 6 "Article %d has a nil data header" article))
1587 ((equal field 'number)
1588 (mail-header-number data-header))
1589 ((equal field 'from)
1590 (mail-header-from data-header))
1591 ((equal field 'message-id)
1592 (mail-header-message-id data-header))
1593 ((equal field 'subject)
1594 (mail-header-subject data-header))
1595 ((equal field 'references)
1596 (mail-header-references data-header))
1597 ((equal field 'date)
1598 (mail-header-date data-header))
1599 ((equal field 'xref)
1600 (mail-header-xref data-header))
1601 ((equal field 'extra)
1602 (mail-header-extra data-header))
1604 (gnus-error
1606 "spam-fetch-field-fast: unknown field %s requested"
1607 field)
1608 nil)))))
1610 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1611 (spam-fetch-field-fast article 'from prepared-data-header))
1613 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1614 (spam-fetch-field-fast article 'subject prepared-data-header))
1616 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1617 (spam-fetch-field-fast article 'message-id prepared-data-header))
1619 (defun spam-generate-fake-headers (article)
1620 (let ((dh (spam-fetch-article-header article)))
1621 (if dh
1622 (concat
1623 (format
1624 ;; 80-character limit makes for strange constructs
1625 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1626 "Date: %s\nReferences: %s\nXref: %s\n")
1627 (spam-fetch-field-fast article 'from dh)
1628 (spam-fetch-field-fast article 'subject dh)
1629 (spam-fetch-field-fast article 'message-id dh)
1630 (spam-fetch-field-fast article 'date dh)
1631 (spam-fetch-field-fast article 'references dh)
1632 (spam-fetch-field-fast article 'xref dh))
1633 (when (spam-fetch-field-fast article 'extra dh)
1634 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1635 (gnus-message
1637 "spam-generate-fake-headers: article %d didn't have a valid header"
1638 article))))
1640 (defun spam-fetch-article-header (article)
1641 (with-current-buffer gnus-summary-buffer
1642 (gnus-read-header article)
1643 (nth 3 (assq article gnus-newsgroup-data))))
1644 ;;}}}
1646 ;;{{{ Spam determination.
1648 (defun spam-split (&rest specific-checks)
1649 "Split this message into the `spam' group if it is spam.
1650 This function can be used as an entry in the variable `nnmail-split-fancy',
1651 for example like this: (: spam-split). It can take checks as
1652 parameters. A string as a parameter will set the
1653 `spam-split-group' to that string.
1655 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1656 (interactive)
1657 (setq spam-split-last-successful-check nil)
1658 (unless spam-split-disabled
1659 (let ((spam-split-group-choice spam-split-group))
1660 (dolist (check specific-checks)
1661 (when (stringp check)
1662 (setq spam-split-group-choice check)
1663 (setq specific-checks (delq check specific-checks))))
1665 (let ((spam-split-group spam-split-group-choice)
1666 (widening-needed-check (spam-widening-needed-p specific-checks)))
1667 (save-excursion
1668 (save-restriction
1669 (when widening-needed-check
1670 (widen)
1671 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1672 widening-needed-check))
1673 (let ((backends (spam-backend-list))
1674 decision)
1675 (while (and backends (not decision))
1676 (let* ((backend (pop backends))
1677 (check-function (spam-backend-check backend))
1678 (spam-split-group (if spam-split-symbolic-return
1679 'spam
1680 spam-split-group)))
1681 (when (or
1682 ;; either, given specific checks, this is one of them
1683 (memq backend specific-checks)
1684 ;; or, given no specific checks, spam-use-CHECK is set
1685 (and (null specific-checks) (symbol-value backend)))
1686 (gnus-message 6 "spam-split: calling the %s function"
1687 check-function)
1688 (setq decision (funcall check-function))
1689 ;; if we got a decision at all, save the current check
1690 (when decision
1691 (setq spam-split-last-successful-check backend))
1693 (when (eq decision 'spam)
1694 (unless spam-split-symbolic-return
1695 (gnus-error
1697 (format "spam-split got %s but %s is nil"
1698 decision
1699 spam-split-symbolic-return)))))))
1700 (if (eq decision t)
1701 (if spam-split-symbolic-return-positive 'ham nil)
1702 decision))))))))
1704 (defun spam-find-spam ()
1705 "Detect spam in the current newsgroup using `spam-split'."
1706 (interactive)
1708 (let* ((group gnus-newsgroup-name)
1709 (autodetect (gnus-parameter-spam-autodetect group))
1710 (methods (gnus-parameter-spam-autodetect-methods group))
1711 (first-method (nth 0 methods))
1712 (articles (if spam-autodetect-recheck-messages
1713 gnus-newsgroup-articles
1714 gnus-newsgroup-unseen))
1715 article-cannot-be-faked)
1718 (dolist (backend methods)
1719 (when (spam-backend-statistical-p backend)
1720 (setq article-cannot-be-faked t)
1721 (return)))
1723 (when (memq 'default methods)
1724 (setq article-cannot-be-faked t))
1726 (when (and autodetect
1727 (not (equal first-method 'none)))
1728 (mapcar
1729 (lambda (article)
1730 (let ((id (spam-fetch-field-message-id-fast article))
1731 (subject (spam-fetch-field-subject-fast article))
1732 (sender (spam-fetch-field-from-fast article))
1733 registry-lookup)
1735 (unless id
1736 (gnus-message 6 "Article %d has no message ID!" article))
1738 (when (and id spam-log-to-registry)
1739 (setq registry-lookup (spam-log-registration-type id 'incoming))
1740 (when registry-lookup
1741 (gnus-message
1743 "spam-find-spam: message %s was already registered incoming"
1744 id)))
1746 (let* ((spam-split-symbolic-return t)
1747 (spam-split-symbolic-return-positive t)
1748 (fake-headers (spam-generate-fake-headers article))
1749 (split-return
1750 (or registry-lookup
1751 (with-temp-buffer
1752 (if article-cannot-be-faked
1753 (gnus-request-article-this-buffer
1754 article
1755 group)
1756 ;; else, we fake the article
1757 (when fake-headers (insert fake-headers)))
1758 (if (or (null first-method)
1759 (equal first-method 'default))
1760 (spam-split)
1761 (apply 'spam-split methods))))))
1762 (if (equal split-return 'spam)
1763 (gnus-summary-mark-article article gnus-spam-mark))
1765 (when (and id split-return spam-log-to-registry)
1766 (when (zerop (gnus-registry-group-count id))
1767 (gnus-registry-handle-action id nil group subject sender))
1769 (unless registry-lookup
1770 (spam-log-processing-to-registry
1772 'incoming
1773 split-return
1774 spam-split-last-successful-check
1775 group))))))
1776 articles))))
1778 ;;}}}
1780 ;;{{{ registration/unregistration functions
1782 (defun spam-resolve-registrations-routine ()
1783 "Go through the backends and register or unregister articles as needed."
1784 (dolist (backend-type '(non-mover mover))
1785 (dolist (classification (spam-classifications))
1786 (dolist (backend (spam-backend-list backend-type))
1787 (let ((rlist (spam-backend-get-article-todo-list
1788 backend classification))
1789 (ulist (spam-backend-get-article-todo-list
1790 backend classification t))
1791 (delcount 0))
1793 ;; clear the old lists right away
1794 (spam-backend-put-article-todo-list backend
1795 classification
1797 nil)
1798 (spam-backend-put-article-todo-list backend
1799 classification
1803 ;; eliminate duplicates
1804 (dolist (article (copy-sequence ulist))
1805 (when (memq article rlist)
1806 (incf delcount)
1807 (setq rlist (delq article rlist))
1808 (setq ulist (delq article ulist))))
1810 (unless (zerop delcount)
1811 (gnus-message
1813 "%d messages did not have to unregister and then register"
1814 delcount))
1816 ;; unregister articles
1817 (unless (zerop (length ulist))
1818 (let ((num (spam-unregister-routine classification backend ulist)))
1819 (when (> num 0)
1820 (gnus-message
1822 "%d %s messages were unregistered by backend %s."
1824 classification
1825 backend))))
1827 ;; register articles
1828 (unless (zerop (length rlist))
1829 (let ((num (spam-register-routine classification backend rlist)))
1830 (when (> num 0)
1831 (gnus-message
1833 "%d %s messages were registered by backend %s."
1835 classification
1836 backend)))))))))
1838 (defun spam-unregister-routine (classification
1839 backend
1840 specific-articles)
1841 (spam-register-routine classification backend specific-articles t))
1843 (defun spam-register-routine (classification
1844 backend
1845 specific-articles
1846 &optional unregister)
1847 (when (and (spam-classification-valid-p classification)
1848 (spam-backend-valid-p backend))
1849 (let* ((register-function
1850 (spam-backend-function backend classification 'registration))
1851 (unregister-function
1852 (spam-backend-function backend classification 'unregistration))
1853 (run-function (if unregister
1854 unregister-function
1855 register-function))
1856 (log-function (if unregister
1857 'spam-log-undo-registration
1858 'spam-log-processing-to-registry))
1859 article articles)
1861 (when run-function
1862 ;; make list of articles, using specific-articles if given
1863 (setq articles (or specific-articles
1864 (spam-list-articles
1865 gnus-newsgroup-articles
1866 classification)))
1867 ;; process them
1868 (when (> (length articles) 0)
1869 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1870 (if unregister "Unregistering" "Registering")
1871 (length articles)
1872 (if specific-articles "specific" "")
1873 classification
1874 backend)
1875 (funcall run-function articles)
1876 ;; now log all the registrations (or undo them, depending on
1877 ;; unregister)
1878 (dolist (article articles)
1879 (funcall log-function
1880 (spam-fetch-field-message-id-fast article)
1881 'process
1882 classification
1883 backend
1884 gnus-newsgroup-name))))
1885 ;; return the number of articles processed
1886 (length articles))))
1888 ;;; log a ham- or spam-processor invocation to the registry
1889 (defun spam-log-processing-to-registry (id type classification backend group)
1890 (when spam-log-to-registry
1891 (if (and (stringp id)
1892 (stringp group)
1893 (spam-process-type-valid-p type)
1894 (spam-classification-valid-p classification)
1895 (spam-backend-valid-p backend))
1896 (let ((cell-list (gnus-registry-get-id-key id type))
1897 (cell (list classification backend group)))
1898 (push cell cell-list)
1899 (gnus-registry-set-id-key id type cell-list))
1901 (gnus-error
1903 (format
1904 "%s call with bad ID, type, classification, spam-backend, or group"
1905 "spam-log-processing-to-registry")))))
1907 ;;; check if a ham- or spam-processor registration has been done
1908 (defun spam-log-registered-p (id type)
1909 (when spam-log-to-registry
1910 (if (and (stringp id)
1911 (spam-process-type-valid-p type))
1912 (gnus-registry-get-id-key id type)
1913 (progn
1914 (gnus-error
1916 (format "%s called with bad ID, type, classification, or spam-backend"
1917 "spam-log-registered-p"))
1918 nil))))
1920 ;;; check what a ham- or spam-processor registration says
1921 ;;; returns nil if conflicting registrations are found
1922 (defun spam-log-registration-type (id type)
1923 (let ((count 0)
1924 decision)
1925 (dolist (reg (spam-log-registered-p id type))
1926 (let ((classification (nth 0 reg)))
1927 (when (spam-classification-valid-p classification)
1928 (when (and decision
1929 (not (eq classification decision)))
1930 (setq count (+ 1 count)))
1931 (setq decision classification))))
1932 (if (< 0 count)
1934 decision)))
1937 ;;; check if a ham- or spam-processor registration needs to be undone
1938 (defun spam-log-unregistration-needed-p (id type classification backend)
1939 (when spam-log-to-registry
1940 (if (and (stringp id)
1941 (spam-process-type-valid-p type)
1942 (spam-classification-valid-p classification)
1943 (spam-backend-valid-p backend))
1944 (let ((cell-list (gnus-registry-get-id-key id type))
1945 found)
1946 (dolist (cell cell-list)
1947 (unless found
1948 (when (and (eq classification (nth 0 cell))
1949 (eq backend (nth 1 cell)))
1950 (setq found t))))
1951 found)
1952 (progn
1953 (gnus-error
1955 (format "%s called with bad ID, type, classification, or spam-backend"
1956 "spam-log-unregistration-needed-p"))
1957 nil))))
1960 ;;; undo a ham- or spam-processor registration (the group is not used)
1961 (defun spam-log-undo-registration (id type classification backend
1962 &optional group)
1963 (when (and spam-log-to-registry
1964 (spam-log-unregistration-needed-p id type classification backend))
1965 (if (and (stringp id)
1966 (spam-process-type-valid-p type)
1967 (spam-classification-valid-p classification)
1968 (spam-backend-valid-p backend))
1969 (let ((cell-list (gnus-registry-get-id-key id type))
1970 new-cell-list found)
1971 (dolist (cell cell-list)
1972 (unless (and (eq classification (nth 0 cell))
1973 (eq backend (nth 1 cell)))
1974 (push cell new-cell-list)))
1975 (gnus-registry-set-id-key id type new-cell-list))
1976 (progn
1977 (gnus-error 7 (format
1978 "%s call with bad ID, type, spam-backend, or group"
1979 "spam-log-undo-registration"))
1980 nil))))
1982 ;;}}}
1984 ;;{{{ backend functions
1986 ;;{{{ Gmane xrefs
1987 (defun spam-check-gmane-xref ()
1988 (let ((header (or
1989 (message-fetch-field "Xref")
1990 (message-fetch-field "Newsgroups"))))
1991 (when header ; return nil when no header
1992 (when (string-match spam-gmane-xref-spam-group
1993 header)
1994 spam-split-group))))
1996 ;;}}}
1998 ;;{{{ Regex body
2000 (defun spam-check-regex-body ()
2001 (let ((spam-regex-headers-ham spam-regex-body-ham)
2002 (spam-regex-headers-spam spam-regex-body-spam))
2003 (spam-check-regex-headers t)))
2005 ;;}}}
2007 ;;{{{ Regex headers
2009 (defun spam-check-regex-headers (&optional body)
2010 (let ((type (if body "body" "header"))
2011 ret found)
2012 (dolist (h-regex spam-regex-headers-ham)
2013 (unless found
2014 (goto-char (point-min))
2015 (when (re-search-forward h-regex nil t)
2016 (message "Ham regex %s search positive." type)
2017 (setq found t))))
2018 (dolist (s-regex spam-regex-headers-spam)
2019 (unless found
2020 (goto-char (point-min))
2021 (when (re-search-forward s-regex nil t)
2022 (message "Spam regex %s search positive." type)
2023 (setq found t)
2024 (setq ret spam-split-group))))
2025 ret))
2027 ;;}}}
2029 ;;{{{ Blackholes.
2031 (defun spam-reverse-ip-string (ip)
2032 (when (stringp ip)
2033 (mapconcat 'identity
2034 (nreverse (split-string ip "\\."))
2035 ".")))
2037 (defun spam-check-blackholes ()
2038 "Check the Received headers for blackholed relays."
2039 (let ((headers (message-fetch-field "received"))
2040 ips matches)
2041 (when headers
2042 (with-temp-buffer
2043 (insert headers)
2044 (goto-char (point-min))
2045 (gnus-message 6 "Checking headers for relay addresses")
2046 (while (re-search-forward
2047 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
2048 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
2049 (push (spam-reverse-ip-string (match-string 1))
2050 ips)))
2051 (dolist (server spam-blackhole-servers)
2052 (dolist (ip ips)
2053 (unless (and spam-blackhole-good-server-regex
2054 ;; match against the reversed (again) IP string
2055 (string-match
2056 spam-blackhole-good-server-regex
2057 (spam-reverse-ip-string ip)))
2058 (unless matches
2059 (let ((query-string (concat ip "." server)))
2060 (if spam-use-dig
2061 (let ((query-result (query-dig query-string)))
2062 (when query-result
2063 (gnus-message 6 "(DIG): positive blackhole check '%s'"
2064 query-result)
2065 (push (list ip server query-result)
2066 matches)))
2067 ;; else, if not using dig.el
2068 (when (dns-query query-string)
2069 (gnus-message 6 "positive blackhole check")
2070 (push (list ip server (dns-query query-string 'TXT))
2071 matches)))))))))
2072 (when matches
2073 spam-split-group)))
2074 ;;}}}
2076 ;;{{{ Hashcash.
2078 (defun spam-check-hashcash ()
2079 "Check the headers for hashcash payments."
2080 (ignore-errors (mail-check-payment))) ;mail-check-payment returns a boolean
2082 ;;}}}
2084 ;;{{{ BBDB
2086 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2087 ;;; <sacha@giotto.sj.ru>
2089 ;; all this is done inside a condition-case to trap errors
2091 ;; Autoloaded in message, which we require.
2092 (declare-function gnus-extract-address-components "gnus-util" (from))
2094 (eval-and-compile
2095 (when (condition-case nil
2096 (progn
2097 (require 'bbdb)
2098 (require 'bbdb-com))
2099 (file-error
2100 ;; `bbdb-records' should not be bound as an autoload function
2101 ;; before loading bbdb because of `bbdb-hashtable-size'.
2102 (defalias 'bbdb-buffer 'ignore)
2103 (defalias 'bbdb-create-internal 'ignore)
2104 (defalias 'bbdb-records 'ignore)
2105 (defalias 'spam-BBDB-register-routine 'ignore)
2106 (defalias 'spam-enter-ham-BBDB 'ignore)
2107 (defalias 'spam-exists-in-BBDB-p 'ignore)
2108 (defalias 'bbdb-gethash 'ignore)
2109 nil))
2111 ;; when the BBDB changes, we want to clear out our cache
2112 (defun spam-clear-cache-BBDB (&rest immaterial)
2113 (spam-clear-cache 'spam-use-BBDB))
2115 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2117 (defun spam-enter-ham-BBDB (addresses &optional remove)
2118 "Enter an address into the BBDB; implies ham (non-spam) sender"
2119 (dolist (from addresses)
2120 (when (stringp from)
2121 (let* ((parsed-address (gnus-extract-address-components from))
2122 (name (or (nth 0 parsed-address) "Ham Sender"))
2123 (remove-function (if remove
2124 'bbdb-delete-record-internal
2125 'ignore))
2126 (net-address (nth 1 parsed-address))
2127 (record (and net-address
2128 (spam-exists-in-BBDB-p net-address))))
2129 (when net-address
2130 (gnus-message 6 "%s address %s %s BBDB"
2131 (if remove "Deleting" "Adding")
2132 from
2133 (if remove "from" "to"))
2134 (if record
2135 (funcall remove-function record)
2136 (bbdb-create-internal name nil net-address nil nil
2137 "ham sender added by spam.el")))))))
2139 (defun spam-BBDB-register-routine (articles &optional unregister)
2140 (let (addresses)
2141 (dolist (article articles)
2142 (when (stringp (spam-fetch-field-from-fast article))
2143 (push (spam-fetch-field-from-fast article) addresses)))
2144 ;; now do the register/unregister action
2145 (spam-enter-ham-BBDB addresses unregister)))
2147 (defun spam-BBDB-unregister-routine (articles)
2148 (spam-BBDB-register-routine articles t))
2150 (defsubst spam-exists-in-BBDB-p (net)
2151 (when (and (stringp net) (not (zerop (length net))))
2152 (bbdb-records)
2153 (bbdb-gethash (downcase net))))
2155 (defun spam-check-BBDB ()
2156 "Mail from people in the BBDB is classified as ham or non-spam"
2157 (let ((net (message-fetch-field "from")))
2158 (when net
2159 (setq net (nth 1 (gnus-extract-address-components net)))
2160 (if (spam-exists-in-BBDB-p net)
2162 (if spam-use-BBDB-exclusive
2163 spam-split-group
2164 nil)))))))
2166 ;;}}}
2168 ;;{{{ ifile
2170 ;;; check the ifile backend; return nil if the mail was NOT classified
2171 ;;; as spam
2174 (defun spam-get-ifile-database-parameter ()
2175 "Return the command-line parameter for ifile's database.
2176 See `spam-ifile-database'."
2177 (if spam-ifile-database
2178 (format "--db-file=%s" spam-ifile-database)
2179 nil))
2181 (defun spam-check-ifile ()
2182 "Check the ifile backend for the classification of this message."
2183 (let ((article-buffer-name (buffer-name))
2184 category return)
2185 (with-temp-buffer
2186 (let ((temp-buffer-name (buffer-name))
2187 (db-param (spam-get-ifile-database-parameter)))
2188 (with-current-buffer article-buffer-name
2189 (apply 'call-process-region
2190 (point-min) (point-max) spam-ifile-program
2191 nil temp-buffer-name nil "-c"
2192 (if db-param `(,db-param "-q") `("-q"))))
2193 ;; check the return now (we're back in the temp buffer)
2194 (goto-char (point-min))
2195 (if (not (eobp))
2196 (setq category (buffer-substring (point) (point-at-eol))))
2197 (when (not (zerop (length category))) ; we need a category here
2198 (if spam-ifile-all-categories
2199 (setq return category)
2200 ;; else, if spam-ifile-all-categories is not set...
2201 (when (string-equal spam-ifile-spam-category category)
2202 (setq return spam-split-group)))))) ; note return is nil otherwise
2203 return))
2205 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2206 "Register an article, given as a string, with a category.
2207 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2208 (let ((category (or category gnus-newsgroup-name))
2209 (add-or-delete-option (if unregister "-d" "-i"))
2210 (db (spam-get-ifile-database-parameter))
2211 parameters)
2212 (with-temp-buffer
2213 (dolist (article articles)
2214 (let ((article-string (spam-get-article-as-string article)))
2215 (when (stringp article-string)
2216 (insert article-string))))
2217 (apply 'call-process-region
2218 (point-min) (point-max) spam-ifile-program
2219 nil nil nil
2220 add-or-delete-option category
2221 (if db `(,db "-h") `("-h"))))))
2223 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2224 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2226 (defun spam-ifile-unregister-spam-routine (articles)
2227 (spam-ifile-register-spam-routine articles t))
2229 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2230 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2232 (defun spam-ifile-unregister-ham-routine (articles)
2233 (spam-ifile-register-ham-routine articles t))
2235 ;;}}}
2237 ;;{{{ spam-stat
2239 (eval-when-compile
2240 (autoload 'spam-stat-buffer-change-to-non-spam "spam-stat")
2241 (autoload 'spam-stat-buffer-change-to-spam "spam-stat")
2242 (autoload 'spam-stat-buffer-is-non-spam "spam-stat")
2243 (autoload 'spam-stat-buffer-is-spam "spam-stat")
2244 (autoload 'spam-stat-load "spam-stat")
2245 (autoload 'spam-stat-save "spam-stat")
2246 (autoload 'spam-stat-split-fancy "spam-stat"))
2248 (require 'spam-stat)
2250 (defun spam-check-stat ()
2251 "Check the spam-stat backend for the classification of this message"
2252 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2253 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2254 category return)
2255 (spam-stat-split-fancy)))
2257 (defun spam-stat-register-spam-routine (articles &optional unregister)
2258 (dolist (article articles)
2259 (let ((article-string (spam-get-article-as-string article)))
2260 (with-temp-buffer
2261 (insert article-string)
2262 (if unregister
2263 (spam-stat-buffer-change-to-non-spam)
2264 (spam-stat-buffer-is-spam))))))
2266 (defun spam-stat-unregister-spam-routine (articles)
2267 (spam-stat-register-spam-routine articles t))
2269 (defun spam-stat-register-ham-routine (articles &optional unregister)
2270 (dolist (article articles)
2271 (let ((article-string (spam-get-article-as-string article)))
2272 (with-temp-buffer
2273 (insert article-string)
2274 (if unregister
2275 (spam-stat-buffer-change-to-spam)
2276 (spam-stat-buffer-is-non-spam))))))
2278 (defun spam-stat-unregister-ham-routine (articles)
2279 (spam-stat-register-ham-routine articles t))
2281 (defun spam-maybe-spam-stat-load ()
2282 (when spam-use-stat (spam-stat-load)))
2284 (defun spam-maybe-spam-stat-save ()
2285 (when spam-use-stat (spam-stat-save)))
2287 ;;}}}
2289 ;;{{{ Blacklists and whitelists.
2291 (defvar spam-whitelist-cache nil)
2292 (defvar spam-blacklist-cache nil)
2294 (defun spam-kill-whole-line ()
2295 (beginning-of-line)
2296 (let ((kill-whole-line t))
2297 (kill-line)))
2299 ;;; address can be a list, too
2300 (defun spam-enter-whitelist (address &optional remove)
2301 "Enter ADDRESS (list or single) into the whitelist.
2302 With a non-nil REMOVE, remove them."
2303 (interactive "sAddress: ")
2304 (spam-enter-list address spam-whitelist remove)
2305 (setq spam-whitelist-cache nil)
2306 (spam-clear-cache 'spam-use-whitelist))
2308 ;;; address can be a list, too
2309 (defun spam-enter-blacklist (address &optional remove)
2310 "Enter ADDRESS (list or single) into the blacklist.
2311 With a non-nil REMOVE, remove them."
2312 (interactive "sAddress: ")
2313 (spam-enter-list address spam-blacklist remove)
2314 (setq spam-blacklist-cache nil)
2315 (spam-clear-cache 'spam-use-whitelist))
2317 (defun spam-enter-list (addresses file &optional remove)
2318 "Enter ADDRESSES into the given FILE.
2319 Either the whitelist or the blacklist files can be used.
2320 With a non-nil REMOVE, remove the ADDRESSES."
2321 (if (stringp addresses)
2322 (spam-enter-list (list addresses) file remove)
2323 ;; else, we have a list of addresses here
2324 (unless (file-exists-p (file-name-directory file))
2325 (make-directory (file-name-directory file) t))
2326 (with-current-buffer
2327 (find-file-noselect file)
2328 (dolist (a addresses)
2329 (when (stringp a)
2330 (goto-char (point-min))
2331 (if (re-search-forward (regexp-quote a) nil t)
2332 ;; found the address
2333 (when remove
2334 (spam-kill-whole-line))
2335 ;; else, the address was not found
2336 (unless remove
2337 (goto-char (point-max))
2338 (unless (bobp)
2339 (insert "\n"))
2340 (insert a "\n")))))
2341 (save-buffer))))
2343 (defun spam-filelist-build-cache (type)
2344 (let ((cache (if (eq type 'spam-use-blacklist)
2345 spam-blacklist-cache
2346 spam-whitelist-cache))
2347 parsed-cache)
2348 (unless (gethash type spam-caches)
2349 (while cache
2350 (let ((address (pop cache)))
2351 (unless (zerop (length address)) ; 0 for a nil address too
2352 (setq address (regexp-quote address))
2353 ;; fix regexp-quote's treatment of user-intended regexes
2354 (while (string-match "\\\\\\*" address)
2355 (setq address (replace-match ".*" t t address))))
2356 (push address parsed-cache)))
2357 (puthash type parsed-cache spam-caches))))
2359 (defun spam-filelist-check-cache (type from)
2360 (when (stringp from)
2361 (spam-filelist-build-cache type)
2362 (let (found)
2363 (dolist (address (gethash type spam-caches))
2364 (when (and address (string-match address from))
2365 (setq found t)
2366 (return)))
2367 found)))
2369 ;;; returns t if the sender is in the whitelist, nil or
2370 ;;; spam-split-group otherwise
2371 (defun spam-check-whitelist ()
2372 ;; FIXME! Should it detect when file timestamps change?
2373 (unless spam-whitelist-cache
2374 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2375 (if (spam-from-listed-p 'spam-use-whitelist)
2377 (if spam-use-whitelist-exclusive
2378 spam-split-group
2379 nil)))
2381 (defun spam-check-blacklist ()
2382 ;; FIXME! Should it detect when file timestamps change?
2383 (unless spam-blacklist-cache
2384 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2385 (and (spam-from-listed-p 'spam-use-blacklist)
2386 spam-split-group))
2388 (defun spam-parse-list (file)
2389 (when (file-readable-p file)
2390 (let (contents address)
2391 (with-temp-buffer
2392 (insert-file-contents file)
2393 (while (not (eobp))
2394 (setq address (buffer-substring (point) (point-at-eol)))
2395 (forward-line 1)
2396 ;; insert the e-mail address if detected, otherwise the raw data
2397 (unless (zerop (length address))
2398 (let ((pure-address
2399 (nth 1 (gnus-extract-address-components address))))
2400 (push (or pure-address address) contents)))))
2401 (nreverse contents))))
2403 (defun spam-from-listed-p (type)
2404 (let ((from (message-fetch-field "from"))
2405 found)
2406 (spam-filelist-check-cache type from)))
2408 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2409 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2410 (declassification (if blacklist 'ham 'spam))
2411 (enter-function
2412 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2413 (remove-function
2414 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2415 from addresses unregister-list article-unregister-list)
2416 (dolist (article articles)
2417 (let ((from (spam-fetch-field-from-fast article))
2418 (id (spam-fetch-field-message-id-fast article))
2419 sender-ignored)
2420 (when (stringp from)
2421 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2422 (when (and (not sender-ignored)
2423 (stringp ignore-regex)
2424 (string-match ignore-regex from))
2425 (setq sender-ignored t)))
2426 ;; remember the messages we need to unregister, unless remove is set
2427 (when (and
2428 (null unregister)
2429 (spam-log-unregistration-needed-p
2430 id 'process declassification de-symbol))
2431 (push article article-unregister-list)
2432 (push from unregister-list))
2433 (unless sender-ignored
2434 (push from addresses)))))
2436 (if unregister
2437 (funcall enter-function addresses t) ; unregister all these addresses
2438 ;; else, register normally and unregister what we need to
2439 (funcall remove-function unregister-list t)
2440 (dolist (article article-unregister-list)
2441 (spam-log-undo-registration
2442 (spam-fetch-field-message-id-fast article)
2443 'process
2444 declassification
2445 de-symbol))
2446 (funcall enter-function addresses nil))))
2448 (defun spam-blacklist-unregister-routine (articles)
2449 (spam-blacklist-register-routine articles t))
2451 (defun spam-blacklist-register-routine (articles &optional unregister)
2452 (spam-filelist-register-routine articles t unregister))
2454 (defun spam-whitelist-unregister-routine (articles)
2455 (spam-whitelist-register-routine articles t))
2457 (defun spam-whitelist-register-routine (articles &optional unregister)
2458 (spam-filelist-register-routine articles nil unregister))
2460 ;;}}}
2462 ;;{{{ Spam-report glue (gmane and resend reporting)
2463 (defun spam-report-gmane-register-routine (articles)
2464 (when articles
2465 (apply 'spam-report-gmane-spam articles)))
2467 (defun spam-report-gmane-unregister-routine (articles)
2468 (when articles
2469 (apply 'spam-report-gmane-ham articles)))
2471 (defun spam-report-resend-register-ham-routine (articles)
2472 (spam-report-resend-register-routine articles t))
2474 (defun spam-report-resend-register-routine (articles &optional ham)
2475 (let* ((resend-to-gp
2476 (if ham
2477 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2478 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2479 (spam-report-resend-to (or (car-safe resend-to-gp)
2480 spam-report-resend-to)))
2481 (spam-report-resend articles ham)))
2483 ;;}}}
2485 ;;{{{ Bogofilter
2486 (defun spam-check-bogofilter-headers (&optional score)
2487 (let ((header (message-fetch-field spam-bogofilter-header)))
2488 (when header ; return nil when no header
2489 (if score ; scoring mode
2490 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2491 (match-string 1 header)
2492 "0")
2493 ;; spam detection mode
2494 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2495 header)
2496 spam-split-group)))))
2498 ;; return something sensible if the score can't be determined
2499 (defun spam-bogofilter-score (&optional recheck)
2500 "Get the Bogofilter spamicity score."
2501 (interactive "P")
2502 (save-window-excursion
2503 (gnus-summary-show-article t)
2504 (set-buffer gnus-article-buffer)
2505 (let ((score (or (unless recheck
2506 (spam-check-bogofilter-headers t))
2507 (spam-check-bogofilter t))))
2508 (gnus-summary-show-article)
2509 (message "Spamicity score %s" score)
2510 (or score "0"))))
2512 (defun spam-verify-bogofilter ()
2513 "Verify the Bogofilter version is sufficient."
2514 (when (eq spam-bogofilter-valid 'unknown)
2515 (setq spam-bogofilter-valid
2516 (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2517 (shell-command-to-string
2518 (format "%s -V" spam-bogofilter-program))))))
2519 spam-bogofilter-valid)
2521 (defun spam-check-bogofilter (&optional score)
2522 "Check the Bogofilter backend for the classification of this message."
2523 (if (spam-verify-bogofilter)
2524 (let ((article-buffer-name (buffer-name))
2525 (db spam-bogofilter-database-directory)
2526 return)
2527 (with-temp-buffer
2528 (let ((temp-buffer-name (buffer-name)))
2529 (with-current-buffer article-buffer-name
2530 (apply 'call-process-region
2531 (point-min) (point-max)
2532 spam-bogofilter-program
2533 nil temp-buffer-name nil
2534 (if db `("-d" ,db "-v") `("-v"))))
2535 (setq return (spam-check-bogofilter-headers score))))
2536 return)
2537 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2539 (defun spam-bogofilter-register-with-bogofilter (articles
2540 spam
2541 &optional unregister)
2542 "Register an article, given as a string, as spam or non-spam."
2543 (if (spam-verify-bogofilter)
2544 (dolist (article articles)
2545 (let ((article-string (spam-get-article-as-string article))
2546 (db spam-bogofilter-database-directory)
2547 (switch (if unregister
2548 (if spam
2549 spam-bogofilter-spam-strong-switch
2550 spam-bogofilter-ham-strong-switch)
2551 (if spam
2552 spam-bogofilter-spam-switch
2553 spam-bogofilter-ham-switch))))
2554 (when (stringp article-string)
2555 (with-temp-buffer
2556 (insert article-string)
2558 (apply 'call-process-region
2559 (point-min) (point-max)
2560 spam-bogofilter-program
2561 nil nil nil switch
2562 (if db `("-d" ,db "-v") `("-v")))))))
2563 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2565 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2566 (spam-bogofilter-register-with-bogofilter articles t unregister))
2568 (defun spam-bogofilter-unregister-spam-routine (articles)
2569 (spam-bogofilter-register-spam-routine articles t))
2571 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2572 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2574 (defun spam-bogofilter-unregister-ham-routine (articles)
2575 (spam-bogofilter-register-ham-routine articles t))
2578 ;;}}}
2580 ;;{{{ spamoracle
2581 (defun spam-check-spamoracle ()
2582 "Run spamoracle on an article to determine whether it's spam."
2583 (let ((article-buffer-name (buffer-name)))
2584 (with-temp-buffer
2585 (let ((temp-buffer-name (buffer-name)))
2586 (with-current-buffer article-buffer-name
2587 (let ((status
2588 (apply 'call-process-region
2589 (point-min) (point-max)
2590 spam-spamoracle-binary
2591 nil temp-buffer-name nil
2592 (if spam-spamoracle-database
2593 `("-f" ,spam-spamoracle-database "mark")
2594 '("mark")))))
2595 (if (eq 0 status)
2596 (progn
2597 (set-buffer temp-buffer-name)
2598 (goto-char (point-min))
2599 (when (re-search-forward "^X-Spam: yes;" nil t)
2600 spam-split-group))
2601 (error "Error running spamoracle: %s" status))))))))
2603 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2604 "Run spamoracle in training mode."
2605 (with-temp-buffer
2606 (let ((temp-buffer-name (buffer-name)))
2607 (save-excursion
2608 (goto-char (point-min))
2609 (dolist (article articles)
2610 (insert (spam-get-article-as-string article)))
2611 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2612 "-spam"
2613 "-good"))
2614 (status
2615 (apply 'call-process-region
2616 (point-min) (point-max)
2617 spam-spamoracle-binary
2618 nil temp-buffer-name nil
2619 (if spam-spamoracle-database
2620 `("-f" ,spam-spamoracle-database
2621 "add" ,arg)
2622 `("add" ,arg)))))
2623 (unless (eq 0 status)
2624 (error "Error running spamoracle: %s" status)))))))
2626 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2627 (spam-spamoracle-learn articles nil unregister))
2629 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2630 (spam-spamoracle-learn-ham articles t))
2632 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2633 (spam-spamoracle-learn articles t unregister))
2635 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2636 (spam-spamoracle-learn-spam articles t))
2638 ;;}}}
2640 ;;{{{ SpamAssassin
2641 ;;; based mostly on the bogofilter code
2642 (defun spam-check-spamassassin-headers (&optional score)
2643 "Check the SpamAssassin headers for the classification of this message."
2644 (if score ; scoring mode
2645 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2646 (when header
2647 (if (string-match spam-spamassassin-score-regexp header)
2648 (match-string 1 header)
2649 "0")))
2650 ;; spam detection mode
2651 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2652 (when header ; return nil when no header
2653 (when (string-match spam-spamassassin-positive-spam-flag-header
2654 header)
2655 spam-split-group)))))
2657 (defun spam-check-spamassassin (&optional score)
2658 "Check the SpamAssassin backend for the classification of this message."
2659 (let ((article-buffer-name (buffer-name)))
2660 (with-temp-buffer
2661 (let ((temp-buffer-name (buffer-name)))
2662 (with-current-buffer article-buffer-name
2663 (apply 'call-process-region
2664 (point-min) (point-max) spam-assassin-program
2665 nil temp-buffer-name nil spam-spamassassin-arguments))
2666 ;; check the return now (we're back in the temp buffer)
2667 (goto-char (point-min))
2668 (spam-check-spamassassin-headers score)))))
2670 ;; return something sensible if the score can't be determined
2671 (defun spam-spamassassin-score (&optional recheck)
2672 "Get the SpamAssassin score"
2673 (interactive "P")
2674 (save-window-excursion
2675 (gnus-summary-show-article t)
2676 (set-buffer gnus-article-buffer)
2677 (let ((score (or (unless recheck
2678 (spam-check-spamassassin-headers t))
2679 (spam-check-spamassassin t))))
2680 (gnus-summary-show-article)
2681 (message "SpamAssassin score %s" score)
2682 (or score "0"))))
2684 (defun spam-spamassassin-register-with-sa-learn (articles spam
2685 &optional unregister)
2686 "Register articles with spamassassin's sa-learn as spam or non-spam."
2687 (if articles
2688 (let ((action (if unregister spam-sa-learn-unregister-switch
2689 (if spam spam-sa-learn-spam-switch
2690 spam-sa-learn-ham-switch)))
2691 (summary-buffer-name (buffer-name)))
2692 (with-temp-buffer
2693 ;; group the articles into mbox format
2694 (dolist (article articles)
2695 (let (article-string)
2696 (with-current-buffer summary-buffer-name
2697 (setq article-string (spam-get-article-as-string article)))
2698 (when (stringp article-string)
2699 ;; mbox separator
2700 (insert (concat "From nobody " (current-time-string) "\n"))
2701 (insert article-string)
2702 (insert "\n"))))
2703 ;; call sa-learn on all messages at the same time
2704 (apply 'call-process-region
2705 (point-min) (point-max)
2706 spam-sa-learn-program
2707 nil nil nil "--mbox"
2708 (if spam-sa-learn-rebuild
2709 (list action)
2710 `("--no-rebuild" ,action)))))))
2712 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2713 (spam-spamassassin-register-with-sa-learn articles t unregister))
2715 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2716 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2718 (defun spam-spamassassin-unregister-spam-routine (articles)
2719 (spam-spamassassin-register-with-sa-learn articles t t))
2721 (defun spam-spamassassin-unregister-ham-routine (articles)
2722 (spam-spamassassin-register-with-sa-learn articles nil t))
2724 ;;}}}
2726 ;;{{{ Bsfilter
2727 ;;; based mostly on the bogofilter code
2728 (defun spam-check-bsfilter-headers (&optional score)
2729 (if score
2730 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2731 "0")
2732 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2733 (when header ; return nil when no header
2734 (when (string-match "YES" header)
2735 spam-split-group)))))
2737 ;; return something sensible if the score can't be determined
2738 (defun spam-bsfilter-score (&optional recheck)
2739 "Get the Bsfilter spamicity score."
2740 (interactive "P")
2741 (save-window-excursion
2742 (gnus-summary-show-article t)
2743 (set-buffer gnus-article-buffer)
2744 (let ((score (or (unless recheck
2745 (spam-check-bsfilter-headers t))
2746 (spam-check-bsfilter t))))
2747 (gnus-summary-show-article)
2748 (message "Spamicity score %s" score)
2749 (or score "0"))))
2751 (defun spam-check-bsfilter (&optional score)
2752 "Check the Bsfilter backend for the classification of this message."
2753 (let ((article-buffer-name (buffer-name))
2754 (dir spam-bsfilter-database-directory)
2755 return)
2756 (with-temp-buffer
2757 (let ((temp-buffer-name (buffer-name)))
2758 (with-current-buffer article-buffer-name
2759 (apply 'call-process-region
2760 (point-min) (point-max)
2761 spam-bsfilter-program
2762 nil temp-buffer-name nil
2763 "--pipe"
2764 "--insert-flag"
2765 "--insert-probability"
2766 (when dir
2767 (list "--homedir" dir))))
2768 (setq return (spam-check-bsfilter-headers score))))
2769 return))
2771 (defun spam-bsfilter-register-with-bsfilter (articles
2772 spam
2773 &optional unregister)
2774 "Register an article, given as a string, as spam or non-spam."
2775 (dolist (article articles)
2776 (let ((article-string (spam-get-article-as-string article))
2777 (switch (if unregister
2778 (if spam
2779 spam-bsfilter-spam-strong-switch
2780 spam-bsfilter-ham-strong-switch)
2781 (if spam
2782 spam-bsfilter-spam-switch
2783 spam-bsfilter-ham-switch))))
2784 (when (stringp article-string)
2785 (with-temp-buffer
2786 (insert article-string)
2787 (apply 'call-process-region
2788 (point-min) (point-max)
2789 spam-bsfilter-program
2790 nil nil nil switch
2791 "--update"
2792 (when spam-bsfilter-database-directory
2793 (list "--homedir"
2794 spam-bsfilter-database-directory))))))))
2796 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2797 (spam-bsfilter-register-with-bsfilter articles t unregister))
2799 (defun spam-bsfilter-unregister-spam-routine (articles)
2800 (spam-bsfilter-register-spam-routine articles t))
2802 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2803 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2805 (defun spam-bsfilter-unregister-ham-routine (articles)
2806 (spam-bsfilter-register-ham-routine articles t))
2808 ;;}}}
2810 ;;{{{ CRM114 Mailfilter
2811 (defun spam-check-crm114-headers (&optional score)
2812 (let ((header (message-fetch-field spam-crm114-header)))
2813 (when header ; return nil when no header
2814 (if score ; scoring mode
2815 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2816 (match-string 1 header)
2817 "0")
2818 ;; spam detection mode
2819 (when (string-match spam-crm114-positive-spam-header
2820 header)
2821 spam-split-group)))))
2823 ;; return something sensible if the score can't be determined
2824 (defun spam-crm114-score ()
2825 "Get the CRM114 Mailfilter pR."
2826 (interactive)
2827 (save-window-excursion
2828 (gnus-summary-show-article t)
2829 (set-buffer gnus-article-buffer)
2830 (let ((score (or (spam-check-crm114-headers t)
2831 (spam-check-crm114 t))))
2832 (gnus-summary-show-article)
2833 (message "pR: %s" score)
2834 (or score "0"))))
2836 (defun spam-check-crm114 (&optional score)
2837 "Check the CRM114 Mailfilter backend for the classification of this message."
2838 (let ((article-buffer-name (buffer-name))
2839 (db spam-crm114-database-directory)
2840 return)
2841 (with-temp-buffer
2842 (let ((temp-buffer-name (buffer-name)))
2843 (with-current-buffer article-buffer-name
2844 (apply 'call-process-region
2845 (point-min) (point-max)
2846 spam-crm114-program
2847 nil temp-buffer-name nil
2848 (when db (list (concat "--fileprefix=" db)))))
2849 (setq return (spam-check-crm114-headers score))))
2850 return))
2852 (defun spam-crm114-register-with-crm114 (articles
2853 spam
2854 &optional unregister)
2855 "Register an article, given as a string, as spam or non-spam."
2856 (dolist (article articles)
2857 (let ((article-string (spam-get-article-as-string article))
2858 (db spam-crm114-database-directory)
2859 (switch (if unregister
2860 (if spam
2861 spam-crm114-spam-strong-switch
2862 spam-crm114-ham-strong-switch)
2863 (if spam
2864 spam-crm114-spam-switch
2865 spam-crm114-ham-switch))))
2866 (when (stringp article-string)
2867 (with-temp-buffer
2868 (insert article-string)
2870 (apply 'call-process-region
2871 (point-min) (point-max)
2872 spam-crm114-program
2873 nil nil nil
2874 (when db (list switch (concat "--fileprefix=" db)))))))))
2876 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2877 (spam-crm114-register-with-crm114 articles t unregister))
2879 (defun spam-crm114-unregister-spam-routine (articles)
2880 (spam-crm114-register-spam-routine articles t))
2882 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2883 (spam-crm114-register-with-crm114 articles nil unregister))
2885 (defun spam-crm114-unregister-ham-routine (articles)
2886 (spam-crm114-register-ham-routine articles t))
2888 ;;}}}
2890 ;;}}}
2892 ;;{{{ Hooks
2894 ;;;###autoload
2895 (defun spam-initialize (&rest symbols)
2896 "Install the spam.el hooks and do other initialization.
2897 When SYMBOLS is given, set those variables to t. This is so you
2898 can call `spam-initialize' before you set spam-use-* variables on
2899 explicitly, and matters only if you need the extra headers
2900 installed through `spam-necessary-extra-headers'."
2901 (interactive)
2903 (dolist (var symbols)
2904 (set var t))
2906 (dolist (header (spam-necessary-extra-headers))
2907 (add-to-list 'nnmail-extra-headers header)
2908 (add-to-list 'gnus-extra-headers header))
2910 (setq spam-install-hooks t)
2911 ;; TODO: How do we redo this every time the `spam' face is customized?
2912 (push '((eq mark gnus-spam-mark) . spam)
2913 gnus-summary-highlight)
2914 ;; Add hooks for loading and saving the spam stats
2915 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2916 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2917 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2918 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2919 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2920 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2921 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2923 (defun spam-unload-hook ()
2924 "Uninstall the spam.el hooks."
2925 (interactive)
2926 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2927 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2928 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2929 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2930 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2931 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2932 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2934 (add-hook 'spam-unload-hook 'spam-unload-hook)
2936 (when spam-install-hooks
2937 (spam-initialize))
2938 ;;}}}
2940 (provide 'spam)
2942 ;;; spam.el ends here