Add isearch-yank-symbol-or-char
[emacs.git] / lisp / gnus / spam.el
blobde8aa77efc9ea5ee607a6a2918adb340cba23f9e
1 ;;; spam.el --- Identifying spam
3 ;; Copyright (C) 2002-2018 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 <https://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 (eval-when-compile (require 'cl))
43 (require 'message) ;for the message-fetch-field functions
44 (require 'gnus-sum)
45 (require 'gnus-uu) ; because of key prefix issues
46 ;;; for the definitions of group content classification and spam processors
47 (require 'gnus)
49 (eval-when-compile (require 'hashcash))
51 ;; for nnimap-split-download-body-default
52 (eval-when-compile (require 'nnimap))
54 ;; autoload query-dig
55 (autoload 'query-dig "dig")
57 ;; autoload spam-report
58 (autoload 'spam-report-gmane "spam-report")
59 (autoload 'spam-report-gmane-spam "spam-report")
60 (autoload 'spam-report-gmane-ham "spam-report")
61 (autoload 'spam-report-resend "spam-report")
63 ;; autoload gnus-registry
64 (autoload 'gnus-registry-group-count "gnus-registry")
65 (autoload 'gnus-registry-get-id-key "gnus-registry")
66 (autoload 'gnus-registry-set-id-key "gnus-registry")
67 (autoload 'gnus-registry-handle-action "gnus-registry")
69 ;; autoload dns-query
70 (autoload 'dns-query "dns")
72 ;;}}}
74 ;;{{{ Main parameters.
75 (defvar spam-backends nil
76 "List of spam.el backends with all the pertinent data.
77 Populated by `spam-install-backend-super'.")
79 (defgroup spam nil
80 "Spam configuration."
81 :version "22.1"
82 :group 'mail
83 :group 'news)
85 (defcustom spam-summary-exit-behavior 'default
86 "Exit behavior at the time of summary exit.
87 Note that setting the `spam-use-move' or `spam-use-copy' backends on
88 a group through group/topic parameters overrides this mechanism."
89 :type '(choice
90 (const
91 :tag "Move spam out of all groups and ham out of spam groups"
92 default)
93 (const
94 :tag "Move spam out of all groups and ham out of all groups"
95 move-all)
96 (const
97 :tag "Never move spam or ham out of any groups"
98 move-none))
99 :group 'spam)
101 (defcustom spam-directory (nnheader-concat gnus-directory "spam/")
102 "Directory for spam whitelists and blacklists."
103 :type 'directory
104 :group 'spam)
106 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
107 "Whether new messages in a spam group should get the spam-mark."
108 :type 'boolean
109 ;; :version "22.1" ;; Gnus 5.10.8 / No Gnus 0.3
110 :group 'spam)
112 (defcustom spam-log-to-registry nil
113 "Whether spam/ham processing should be logged in the registry."
114 :type 'boolean
115 :group 'spam)
117 (defcustom spam-split-symbolic-return nil
118 "Whether `spam-split' should work with symbols or group names."
119 :type 'boolean
120 :group 'spam)
122 (defcustom spam-split-symbolic-return-positive nil
123 "Whether `spam-split' should ALWAYS work with symbols or group names.
124 Do not set this if you use `spam-split' in a fancy split method."
125 :type 'boolean
126 :group 'spam)
128 (defcustom spam-mark-only-unseen-as-spam t
129 "Whether only unseen articles should be marked as spam in spam groups.
130 When nil, all unread articles in a spam group are marked as
131 spam. Set this if you want to leave an article unread in a spam group
132 without losing it to the automatic spam-marking process."
133 :type 'boolean
134 :group 'spam)
136 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
137 "Whether ham should be marked unread before it's moved.
138 The article is moved out of a spam group according to `ham-process-destination'.
139 This variable is an official entry in the international Longest Variable Name
140 Competition."
141 :type 'boolean
142 :group 'spam)
144 (defcustom spam-disable-spam-split-during-ham-respool nil
145 "Whether `spam-split' should be ignored while resplitting ham.
146 This is useful to prevent ham from ending up in the same spam
147 group after the resplit. Don't set this to t if you have `spam-split' as the
148 last rule in your split configuration."
149 :type 'boolean
150 :group 'spam)
152 (defcustom spam-autodetect-recheck-messages nil
153 "Should spam.el recheck all messages when autodetecting?
154 Normally this is nil, so only unseen messages will be checked."
155 :type 'boolean
156 :group 'spam)
158 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
159 "The location of the whitelist.
160 The file format is one regular expression per line.
161 The regular expression is matched against the address."
162 :type 'file
163 :group 'spam)
165 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
166 "The location of the blacklist.
167 The file format is one regular expression per line.
168 The regular expression is matched against the address."
169 :type 'file
170 :group 'spam)
172 (defcustom spam-use-dig t
173 "Whether `query-dig' should be used instead of `dns-query'."
174 :type 'boolean
175 :group 'spam)
177 (defcustom spam-use-gmane-xref nil
178 "Whether the Gmane spam xref should be used by `spam-split'."
179 :type 'boolean
180 :group 'spam)
182 (defcustom spam-use-blacklist nil
183 "Whether the blacklist should be used by `spam-split'."
184 :type 'boolean
185 :group 'spam)
187 (defcustom spam-blacklist-ignored-regexes nil
188 "Regular expressions that the blacklist should ignore."
189 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
190 :group 'spam)
192 (defcustom spam-use-whitelist nil
193 "Whether the whitelist should be used by `spam-split'."
194 :type 'boolean
195 :group 'spam)
197 (defcustom spam-use-whitelist-exclusive nil
198 "Whether whitelist-exclusive should be used by `spam-split'.
199 Exclusive whitelisting means that all messages from senders not in the whitelist
200 are considered spam."
201 :type 'boolean
202 :group 'spam)
204 (defcustom spam-use-blackholes nil
205 "Whether blackholes should be used by `spam-split'."
206 :type 'boolean
207 :group 'spam)
209 (defcustom spam-use-hashcash nil
210 "Whether hashcash payments should be detected by `spam-split'."
211 :type 'boolean
212 :group 'spam)
214 (defcustom spam-use-regex-headers nil
215 "Whether a header regular expression match should be used by `spam-split'.
216 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
217 :type 'boolean
218 :group 'spam)
220 (defcustom spam-use-regex-body nil
221 "Whether a body regular expression match should be used by `spam-split'.
222 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
223 :type 'boolean
224 :group 'spam)
226 (defcustom spam-use-bogofilter-headers nil
227 "Whether bogofilter headers should be used by `spam-split'.
228 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
229 :type 'boolean
230 :group 'spam)
232 (defcustom spam-use-bogofilter nil
233 "Whether bogofilter should be invoked by `spam-split'.
234 Enable this if you want Gnus to invoke Bogofilter on new messages."
235 :type 'boolean
236 :group 'spam)
238 (defcustom spam-use-bsfilter-headers nil
239 "Whether bsfilter headers should be used by `spam-split'.
240 Enable this if you pre-process messages with Bsfilter BEFORE Gnus sees them."
241 :type 'boolean
242 :group 'spam)
244 (defcustom spam-use-bsfilter nil
245 "Whether bsfilter should be invoked by `spam-split'.
246 Enable this if you want Gnus to invoke Bsfilter on new messages."
247 :type 'boolean
248 :group 'spam)
250 (defcustom spam-use-BBDB nil
251 "Whether BBDB should be used by `spam-split'."
252 :type 'boolean
253 :group 'spam)
255 (defcustom spam-use-BBDB-exclusive nil
256 "Whether BBDB-exclusive should be used by `spam-split'.
257 Exclusive BBDB means that all messages from senders not in the BBDB are
258 considered spam."
259 :type 'boolean
260 :group 'spam)
262 (defcustom spam-use-ifile nil
263 "Whether ifile should be used by `spam-split'."
264 :type 'boolean
265 :group 'spam)
267 (defcustom spam-use-stat nil
268 "Whether `spam-stat' should be used by `spam-split'."
269 :type 'boolean
270 :group 'spam)
272 (defcustom spam-use-spamoracle nil
273 "Whether spamoracle should be used by `spam-split'."
274 :type 'boolean
275 :group 'spam)
277 (defcustom spam-use-spamassassin nil
278 "Whether spamassassin should be invoked by `spam-split'.
279 Enable this if you want Gnus to invoke SpamAssassin on new messages."
280 :type 'boolean
281 :group 'spam)
283 (defcustom spam-use-spamassassin-headers nil
284 "Whether spamassassin headers should be checked by `spam-split'.
285 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
286 them."
287 :type 'boolean
288 :group 'spam)
290 (defcustom spam-use-crm114 nil
291 "Whether the CRM114 Mailfilter should be used by `spam-split'."
292 :type 'boolean
293 :group 'spam)
295 (defcustom spam-install-hooks (or
296 spam-use-dig
297 spam-use-gmane-xref
298 spam-use-blacklist
299 spam-use-whitelist
300 spam-use-whitelist-exclusive
301 spam-use-blackholes
302 spam-use-hashcash
303 spam-use-regex-headers
304 spam-use-regex-body
305 spam-use-bogofilter
306 spam-use-bogofilter-headers
307 spam-use-spamassassin
308 spam-use-spamassassin-headers
309 spam-use-bsfilter
310 spam-use-bsfilter-headers
311 spam-use-BBDB
312 spam-use-BBDB-exclusive
313 spam-use-ifile
314 spam-use-stat
315 spam-use-spamoracle
316 spam-use-crm114)
317 "Whether the spam hooks should be installed.
318 Default to t if one of the spam-use-* variables is set."
319 :group 'spam
320 :type 'boolean)
322 (defcustom spam-split-group "spam"
323 "Group name where incoming spam should be put by `spam-split'."
324 :type 'string
325 :group 'spam)
327 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
328 ;;; not regular expressions
329 (defcustom spam-junk-mailgroups (cons
330 spam-split-group
331 '("mail.junk" "poste.pourriel"))
332 "Mailgroups with spam contents.
333 All unmarked article in such group receive the spam mark on group entry."
334 :type '(repeat (string :tag "Group"))
335 :group 'spam)
338 (defcustom spam-gmane-xref-spam-group "gmane.spam.detected"
339 "The group where spam xrefs can be found on Gmane.
340 Only meaningful if you enable `spam-use-gmane-xref'."
341 :type 'string
342 :group 'spam)
344 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
345 "dev.null.dk" "relays.visi.com")
346 "List of blackhole servers.
347 Only meaningful if you enable `spam-use-blackholes'."
348 :type '(repeat (string :tag "Server"))
349 :group 'spam)
351 (defcustom spam-blackhole-good-server-regex nil
352 "String matching IP addresses that should not be checked in the blackholes.
353 Only meaningful if you enable `spam-use-blackholes'."
354 :type '(radio (const nil) regexp)
355 :group 'spam)
357 (defface spam
358 '((((class color) (type tty) (background dark))
359 (:foreground "gray80" :background "gray50"))
360 (((class color) (type tty) (background light))
361 (:foreground "gray50" :background "gray80"))
362 (((class color) (background dark))
363 (:foreground "ivory2"))
364 (((class color) (background light))
365 (:foreground "ivory4"))
366 (t :inverse-video t))
367 "Face for spam-marked articles."
368 :group 'spam)
370 (defcustom spam-face 'spam
371 "Face for spam-marked articles."
372 :type 'face
373 :group 'spam)
375 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
376 "Regular expression for positive header spam matches.
377 Only meaningful if you enable `spam-use-regex-headers'."
378 :type '(repeat (regexp :tag "Regular expression to match spam header"))
379 :group 'spam)
381 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
382 "Regular expression for positive header ham matches.
383 Only meaningful if you enable `spam-use-regex-headers'."
384 :type '(repeat (regexp :tag "Regular expression to match ham header"))
385 :group 'spam)
387 (defcustom spam-regex-body-spam '()
388 "Regular expression for positive body spam matches.
389 Only meaningful if you enable `spam-use-regex-body'."
390 :type '(repeat (regexp :tag "Regular expression to match spam body"))
391 :group 'spam)
393 (defcustom spam-regex-body-ham '()
394 "Regular expression for positive body ham matches.
395 Only meaningful if you enable `spam-use-regex-body'."
396 :type '(repeat (regexp :tag "Regular expression to match ham body"))
397 :group 'spam)
399 (defcustom spam-summary-score-preferred-header nil
400 "Preferred header to use for `spam-summary-score'."
401 :type '(choice :tag "Header name"
402 (symbol :tag "SpamAssassin etc" X-Spam-Status)
403 (symbol :tag "Bogofilter" X-Bogosity)
404 (const :tag "No preference, take best guess." nil))
405 :group 'spam)
407 (defgroup spam-ifile nil
408 "Spam ifile configuration."
409 :group 'spam)
411 (make-obsolete-variable 'spam-ifile-path 'spam-ifile-program
412 "Gnus 5.10.9 (Emacs 22.1)")
413 (defcustom spam-ifile-program (executable-find "ifile")
414 "Name of the ifile program."
415 :type '(choice (file :tag "Location of ifile")
416 (const :tag "ifile is not installed"))
417 :group 'spam-ifile)
419 (make-obsolete-variable 'spam-ifile-database-path 'spam-ifile-database
420 "Gnus 5.10.9 (Emacs 22.1)")
421 (defcustom spam-ifile-database nil
422 "File name of the ifile database."
423 :type '(choice (file :tag "Location of the ifile database")
424 (const :tag "Use the default"))
425 :group 'spam-ifile)
427 (defcustom spam-ifile-spam-category "spam"
428 "Name of the spam ifile category."
429 :type 'string
430 :group 'spam-ifile)
432 (defcustom spam-ifile-ham-category nil
433 "Name of the ham ifile category.
434 If nil, the current group name will be used."
435 :type '(choice (string :tag "Use a fixed category")
436 (const :tag "Use the current group name"))
437 :group 'spam-ifile)
439 (defcustom spam-ifile-all-categories nil
440 "Whether the ifile check will return all categories, or just spam.
441 Set this to t if you want to use the `spam-split' invocation of ifile as
442 your main source of newsgroup names."
443 :type 'boolean
444 :group 'spam-ifile)
446 (defgroup spam-bogofilter nil
447 "Spam bogofilter configuration."
448 :group 'spam)
450 (make-obsolete-variable 'spam-bogofilter-path 'spam-bogofilter-program
451 "Gnus 5.10.9 (Emacs 22.1)")
452 (defcustom spam-bogofilter-program (executable-find "bogofilter")
453 "Name of the Bogofilter program."
454 :type '(choice (file :tag "Location of bogofilter")
455 (const :tag "Bogofilter is not installed"))
456 :group 'spam-bogofilter)
458 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
460 (defcustom spam-bogofilter-header "X-Bogosity"
461 "The header that Bogofilter inserts in messages."
462 :type 'string
463 :group 'spam-bogofilter)
465 (defcustom spam-bogofilter-spam-switch "-s"
466 "The switch that Bogofilter uses to register spam messages."
467 :type 'string
468 :group 'spam-bogofilter)
470 (defcustom spam-bogofilter-ham-switch "-n"
471 "The switch that Bogofilter uses to register ham messages."
472 :type 'string
473 :group 'spam-bogofilter)
475 (defcustom spam-bogofilter-spam-strong-switch "-S"
476 "The switch that Bogofilter uses to unregister ham messages."
477 :type 'string
478 :group 'spam-bogofilter)
480 (defcustom spam-bogofilter-ham-strong-switch "-N"
481 "The switch that Bogofilter uses to unregister spam messages."
482 :type 'string
483 :group 'spam-bogofilter)
485 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
486 "The regex on `spam-bogofilter-header' for positive spam identification."
487 :type 'regexp
488 :group 'spam-bogofilter)
490 (defcustom spam-bogofilter-database-directory nil
491 "Location of the Bogofilter database.
492 When nil, use the default location."
493 :type '(choice (directory
494 :tag "Location of the Bogofilter database directory")
495 (const :tag "Use the default"))
496 :group 'spam-bogofilter)
498 (defgroup spam-bsfilter nil
499 "Spam bsfilter configuration."
500 :group 'spam)
502 (make-obsolete-variable 'spam-bsfilter-path 'spam-bsfilter-program
503 "Gnus 5.10.9 (Emacs 22.1)")
504 (defcustom spam-bsfilter-program (executable-find "bsfilter")
505 "Name of the Bsfilter program."
506 :type '(choice (file :tag "Location of bsfilter")
507 (const :tag "Bsfilter is not installed"))
508 :group 'spam-bsfilter)
510 (defcustom spam-bsfilter-header "X-Spam-Flag"
511 "The header inserted by Bsfilter to flag spam."
512 :type 'string
513 :group 'spam-bsfilter)
515 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
516 "The header that Bsfilter inserts in messages."
517 :type 'string
518 :group 'spam-bsfilter)
520 (defcustom spam-bsfilter-spam-switch "--add-spam"
521 "The switch that Bsfilter uses to register spam messages."
522 :type 'string
523 :group 'spam-bsfilter)
525 (defcustom spam-bsfilter-ham-switch "--add-clean"
526 "The switch that Bsfilter uses to register ham messages."
527 :type 'string
528 :group 'spam-bsfilter)
530 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
531 "The switch that Bsfilter uses to unregister ham messages."
532 :type 'string
533 :group 'spam-bsfilter)
535 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
536 "The switch that Bsfilter uses to unregister spam messages."
537 :type 'string
538 :group 'spam-bsfilter)
540 (defcustom spam-bsfilter-database-directory nil
541 "Directory path of the Bsfilter databases."
542 :type '(choice (directory
543 :tag "Location of the Bsfilter database directory")
544 (const :tag "Use the default"))
545 :group 'spam-bsfilter)
547 (defgroup spam-spamoracle nil
548 "Spam spamoracle configuration."
549 :group 'spam)
551 (defcustom spam-spamoracle-database nil
552 "Location of spamoracle database file.
553 When nil, use the default spamoracle database."
554 :type '(choice (directory :tag "Location of spamoracle database file.")
555 (const :tag "Use the default"))
556 :group 'spam-spamoracle)
558 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
559 "Location of the spamoracle binary."
560 :type '(choice (directory :tag "Location of the spamoracle binary")
561 (const :tag "Use the default"))
562 :group 'spam-spamoracle)
564 (defgroup spam-spamassassin nil
565 "Spam SpamAssassin configuration."
566 :group 'spam)
568 (make-obsolete-variable 'spam-spamassassin-path
569 'spam-spamassassin-program "Gnus 5.10.9 (Emacs 22.1)")
570 (defcustom spam-assassin-program (executable-find "spamassassin")
571 "Name of the spamassassin program.
572 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
573 spamd man pages for more information on these programs."
574 :type '(choice (file :tag "Location of spamc")
575 (const :tag "spamassassin is not installed"))
576 :group 'spam-spamassassin)
578 (defcustom spam-spamassassin-arguments ()
579 "Arguments to pass to the spamassassin executable.
580 This must be a list. For example, `(\"-C\" \"configfile\")'."
581 :type '(restricted-sexp :match-alternatives (listp))
582 :group 'spam-spamassassin)
584 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
585 "The header inserted by SpamAssassin to flag spam."
586 :type 'string
587 :group 'spam-spamassassin)
589 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
590 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
591 identification"
592 :type 'string
593 :group 'spam-spamassassin)
595 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
596 "The header inserted by SpamAssassin, giving extended scoring information"
597 :type 'string
598 :group 'spam-spamassassin)
600 (make-obsolete-variable 'spam-sa-learn-path 'spam-sa-learn-program
601 "Gnus 5.10.9 (Emacs 22.1)")
602 (defcustom spam-sa-learn-program (executable-find "sa-learn")
603 "Name of the sa-learn program."
604 :type '(choice (file :tag "Location of spamassassin")
605 (const :tag "spamassassin is not installed"))
606 :group 'spam-spamassassin)
608 (defcustom spam-sa-learn-rebuild t
609 "Whether sa-learn should rebuild the database every time it is called
610 Enable this if you want sa-learn to rebuild the database automatically. Doing
611 this will slightly increase the running time of the spam registration process.
612 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
613 order for SpamAssassin to recognize the new registered spam."
614 :type 'boolean
615 :group 'spam-spamassassin)
617 (defcustom spam-sa-learn-spam-switch "--spam"
618 "The switch that sa-learn uses to register spam messages."
619 :type 'string
620 :group 'spam-spamassassin)
622 (defcustom spam-sa-learn-ham-switch "--ham"
623 "The switch that sa-learn uses to register ham messages."
624 :type 'string
625 :group 'spam-spamassassin)
627 (defcustom spam-sa-learn-unregister-switch "--forget"
628 "The switch that sa-learn uses to unregister messages."
629 :type 'string
630 :group 'spam-spamassassin)
632 (defgroup spam-crm114 nil
633 "Spam CRM114 Mailfilter configuration."
634 :group 'spam)
636 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
637 "File path of the CRM114 Mailfilter executable program."
638 :type '(choice (file :tag "Location of CRM114 Mailfilter")
639 (const :tag "CRM114 Mailfilter is not installed"))
640 :group 'spam-crm114)
642 (defcustom spam-crm114-header "X-CRM114-Status"
643 "The header that CRM114 Mailfilter inserts in messages."
644 :type 'string
645 :group 'spam-crm114)
647 (defcustom spam-crm114-spam-switch "--learnspam"
648 "The switch that CRM114 Mailfilter uses to register spam messages."
649 :type 'string
650 :group 'spam-crm114)
652 (defcustom spam-crm114-ham-switch "--learnnonspam"
653 "The switch that CRM114 Mailfilter uses to register ham messages."
654 :type 'string
655 :group 'spam-crm114)
657 (defcustom spam-crm114-spam-strong-switch "--unlearn"
658 "The switch that CRM114 Mailfilter uses to unregister ham messages."
659 :type 'string
660 :group 'spam-crm114)
662 (defcustom spam-crm114-ham-strong-switch "--unlearn"
663 "The switch that CRM114 Mailfilter uses to unregister spam messages."
664 :type 'string
665 :group 'spam-crm114)
667 (defcustom spam-crm114-positive-spam-header "^SPAM"
668 "The regex on `spam-crm114-header' for positive spam identification."
669 :type 'regexp
670 :group 'spam-crm114)
672 (defcustom spam-crm114-database-directory nil
673 "Directory path of the CRM114 Mailfilter databases."
674 :type '(choice (directory
675 :tag "Location of the CRM114 Mailfilter database directory")
676 (const :tag "Use the default"))
677 :group 'spam-crm114)
679 ;;; Key bindings for spam control.
681 (gnus-define-keys gnus-summary-mode-map
682 "St" spam-generic-score
683 "Sx" gnus-summary-mark-as-spam
684 "Mst" spam-generic-score
685 "Msx" gnus-summary-mark-as-spam
686 "\M-d" gnus-summary-mark-as-spam
687 "$" gnus-summary-mark-as-spam)
689 (defvar spam-cache-lookups t
690 "Whether spam.el will try to cache lookups using `spam-caches'.")
692 (defvar spam-caches (make-hash-table
693 :size 10
694 :test 'equal)
695 "Cache of spam detection entries.")
697 (defvar spam-old-articles nil
698 "List of old ham and spam articles, generated when a group is entered.")
700 (defvar spam-split-disabled nil
701 "If non-nil, `spam-split' is disabled, and always returns nil.")
703 (defvar spam-split-last-successful-check nil
704 "Internal variable.
705 `spam-split' will set this to nil or a spam-use-XYZ check if it
706 finds ham or spam.")
708 ;; internal variables for backends
709 ;; TODO: find a way to create these on the fly in spam-install-backend-super
710 (defvar spam-use-copy nil)
711 (defvar spam-use-move nil)
712 (defvar spam-use-gmane nil)
713 (defvar spam-use-resend nil)
715 ;;}}}
717 ;;{{{ convenience functions
719 (defun spam-clear-cache (symbol)
720 "Clear the `spam-caches' entry for a check."
721 (remhash symbol spam-caches))
723 (defun spam-xor (a b)
724 "Logical A xor B."
725 (and (or a b) (not (and a b))))
727 (defun spam-set-difference (list1 list2)
728 "Return a set difference of LIST1 and LIST2.
729 When either list is nil, the other is returned."
730 (if (and list1 list2)
731 ;; we have two non-nil lists
732 (progn
733 (dolist (item (append list1 list2))
734 (when (and (memq item list1) (memq item list2))
735 (setq list1 (delq item list1))
736 (setq list2 (delq item list2))))
737 (append list1 list2))
738 ;; if either of the lists was nil, return the other one
739 (if list1 list1 list2)))
741 (defun spam-group-ham-mark-p (group mark &optional spam)
742 "Checks if MARK is considered a ham mark in GROUP."
743 (when (stringp group)
744 (let* ((marks (spam-group-ham-marks group spam))
745 (marks (if (symbolp mark)
746 marks
747 (mapcar 'symbol-value marks))))
748 (memq mark marks))))
750 (defun spam-group-spam-mark-p (group mark)
751 "Checks if MARK is considered a spam mark in GROUP."
752 (spam-group-ham-mark-p group mark t))
754 (defun spam-group-ham-marks (group &optional spam)
755 "In GROUP, get all the ham marks."
756 (when (stringp group)
757 (let* ((marks (if spam
758 (gnus-parameter-spam-marks group)
759 (gnus-parameter-ham-marks group)))
760 (marks (car marks))
761 (marks (if (listp (car marks)) (car marks) marks)))
762 marks)))
764 (defun spam-group-spam-marks (group)
765 "In GROUP, get all the spam marks."
766 (spam-group-ham-marks group t))
768 (defun spam-group-spam-contents-p (group)
769 "Is GROUP a spam group?"
770 (if (and (stringp group) (< 0 (length group)))
771 (or (member group spam-junk-mailgroups)
772 (memq 'gnus-group-spam-classification-spam
773 (gnus-parameter-spam-contents group)))
774 nil))
776 (defun spam-group-ham-contents-p (group)
777 "Is GROUP a ham group?"
778 (if (stringp group)
779 (memq 'gnus-group-spam-classification-ham
780 (gnus-parameter-spam-contents group))
781 nil))
783 (defun spam-classifications ()
784 "Return list of valid classifications"
785 '(spam ham))
787 (defun spam-classification-valid-p (classification)
788 "Is CLASSIFICATION a valid spam/ham classification?"
789 (memq classification (spam-classifications)))
791 (defun spam-backend-properties ()
792 "Return list of valid classifications."
793 '(statistical mover check hrf srf huf suf))
795 (defun spam-backend-property-valid-p (property)
796 "Is PROPERTY a valid backend property?"
797 (memq property (spam-backend-properties)))
799 (defun spam-backend-function-type-valid-p (type)
800 (or (eq type 'registration)
801 (eq type 'unregistration)))
803 (defun spam-process-type-valid-p (process-type)
804 (or (eq process-type 'incoming)
805 (eq process-type 'process)))
807 (defun spam-list-articles (articles classification)
808 (let ((mark-check (if (eq classification 'spam)
809 'spam-group-spam-mark-p
810 'spam-group-ham-mark-p))
811 alist mark-cache-yes mark-cache-no)
812 (dolist (article articles)
813 (let ((mark (gnus-summary-article-mark article)))
814 (unless (or (memq mark mark-cache-yes)
815 (memq mark mark-cache-no))
816 (if (funcall mark-check
817 gnus-newsgroup-name
818 mark)
819 (push mark mark-cache-yes)
820 (push mark mark-cache-no)))
821 (when (memq mark mark-cache-yes)
822 (push article alist))))
823 alist))
825 ;;}}}
827 ;;{{{ backend installation functions and procedures
829 (defun spam-install-backend-super (backend &rest properties)
830 "Install BACKEND for spam.el.
831 Accepts incoming CHECK, ham registration function HRF, spam
832 registration function SRF, ham unregistration function HUF, spam
833 unregistration function SUF, and an indication whether the
834 backend is STATISTICAL."
835 (setq spam-backends (add-to-list 'spam-backends backend))
836 (while properties
837 (let ((property (pop properties))
838 (value (pop properties)))
839 (if (spam-backend-property-valid-p property)
840 (put backend property value)
841 (gnus-error
843 "spam-install-backend-super got an invalid property %s"
844 property)))))
846 (defun spam-backend-list (&optional type)
847 "Return a list of all the backend symbols, constrained by TYPE.
848 When TYPE is 'non-mover, only non-mover backends are returned.
849 When TYPE is 'mover, only mover backends are returned."
850 (let (list)
851 (dolist (backend spam-backends)
852 (when (or
853 (null type) ;either no type was requested
854 ;; or the type is 'mover and the backend is a mover
855 (and
856 (eq type 'mover)
857 (spam-backend-mover-p backend))
858 ;; or the type is 'non-mover and the backend is not a mover
859 (and
860 (eq type 'non-mover)
861 (not (spam-backend-mover-p backend))))
862 (push backend list)))
863 list))
865 (defun spam-backend-check (backend)
866 "Get the check function for BACKEND.
867 Each individual check may return nil, t, or a mailgroup name.
868 The value nil means that the check does not yield a decision, and
869 so, that further checks are needed. The value t means that the
870 message is definitely not spam, and that further spam checks
871 should be inhibited. Otherwise, a mailgroup name or the symbol
872 'spam (depending on `spam-split-symbolic-return') is returned where
873 the mail should go, and further checks are also inhibited. The
874 usual mailgroup name is the value of `spam-split-group', meaning
875 that the message is definitely a spam."
876 (get backend 'check))
878 (defun spam-backend-valid-p (backend)
879 "Is BACKEND valid?"
880 (member backend (spam-backend-list)))
882 (defun spam-backend-info (backend)
883 "Return information about BACKEND."
884 (if (spam-backend-valid-p backend)
885 (let (info)
886 (setq info (format "Backend %s has the following properties:\n"
887 backend))
888 (dolist (property (spam-backend-properties))
889 (setq info (format "%s%s=%s\n"
890 info
891 property
892 (get backend property))))
893 info)
894 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
895 backend)))
897 (defun spam-backend-function (backend classification type)
898 "Get the BACKEND function for CLASSIFICATION and TYPE.
899 TYPE is 'registration or 'unregistration.
900 CLASSIFICATION is 'ham or 'spam."
901 (if (and
902 (spam-classification-valid-p classification)
903 (spam-backend-function-type-valid-p type))
904 (let ((retrieval
905 (intern
906 (format "spam-backend-%s-%s-function"
907 classification
908 type))))
909 (funcall retrieval backend))
910 (gnus-error
912 "%s was passed invalid backend %s, classification %s, or type %s"
913 "spam-backend-function"
914 backend
915 classification
916 type)))
918 (defun spam-backend-article-list-property (classification
919 &optional unregister)
920 "Property name of article list with CLASSIFICATION and UNREGISTER."
921 (let* ((r (if unregister "unregister" "register"))
922 (prop (format "%s-%s" classification r)))
923 prop))
925 (defun spam-backend-get-article-todo-list (backend
926 classification
927 &optional unregister)
928 "Get the articles to be processed for BACKEND and CLASSIFICATION.
929 With UNREGISTER, get articles to be unregistered.
930 This is a temporary storage function - nothing here persists."
931 (get
932 backend
933 (intern (spam-backend-article-list-property classification unregister))))
935 (defun spam-backend-put-article-todo-list (backend classification list
936 &optional unregister)
937 "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
938 With UNREGISTER, set articles to be unregistered.
939 This is a temporary storage function - nothing here persists."
940 (put
941 backend
942 (intern (spam-backend-article-list-property classification unregister))
943 list))
945 (defun spam-backend-ham-registration-function (backend)
946 "Get the ham registration function for BACKEND."
947 (get backend 'hrf))
949 (defun spam-backend-spam-registration-function (backend)
950 "Get the spam registration function for BACKEND."
951 (get backend 'srf))
953 (defun spam-backend-ham-unregistration-function (backend)
954 "Get the ham unregistration function for BACKEND."
955 (get backend 'huf))
957 (defun spam-backend-spam-unregistration-function (backend)
958 "Get the spam unregistration function for BACKEND."
959 (get backend 'suf))
961 (defun spam-backend-statistical-p (backend)
962 "Is BACKEND statistical?"
963 (get backend 'statistical))
965 (defun spam-backend-mover-p (backend)
966 "Is BACKEND a mover?"
967 (get backend 'mover))
969 (defun spam-install-backend-alias (backend alias)
970 "Add ALIAS to an existing BACKEND.
971 The previous backend settings for ALIAS are erased."
973 ;; install alias with no properties at first
974 (spam-install-backend-super alias)
976 (dolist (property (spam-backend-properties))
977 (put alias property (get backend property))))
979 (defun spam-install-checkonly-backend (backend check)
980 "Install a BACKEND than can only CHECK for spam."
981 (spam-install-backend-super backend 'check check))
983 (defun spam-install-mover-backend (backend hrf srf huf suf)
984 "Install a BACKEND than can move articles at summary exit.
985 Accepts ham registration function HRF, spam registration function
986 SRF, ham unregistration function HUF, spam unregistration
987 function SUF. The backend has no incoming check and can't be
988 statistical."
989 (spam-install-backend-super
990 backend
991 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
993 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
994 "Install a BACKEND than has no check.
995 Accepts ham registration function HRF, spam registration function
996 SRF, ham unregistration function HUF, spam unregistration
997 function SUF. The backend has no incoming check and can't be
998 statistical (it could be, but in practice that doesn't happen)."
999 (spam-install-backend-super
1000 backend
1001 'hrf hrf 'srf srf 'huf huf 'suf suf))
1003 (defun spam-install-backend (backend check hrf srf huf suf)
1004 "Install a BACKEND.
1005 Accepts incoming CHECK, ham registration function HRF, spam
1006 registration function SRF, ham unregistration function HUF, spam
1007 unregistration function SUF. The backend won't be
1008 statistical (use `spam-install-statistical-backend' for that)."
1009 (spam-install-backend-super
1010 backend
1011 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
1013 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
1014 "Install a BACKEND.
1015 Accepts incoming CHECK, ham registration function HRF, spam
1016 registration function SRF, ham unregistration function HUF, spam
1017 unregistration function SUF. The backend will be
1018 statistical (use `spam-install-backend' for non-statistical
1019 backends)."
1020 (spam-install-backend-super
1021 backend
1022 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1024 (defun spam-install-statistical-checkonly-backend (backend check)
1025 "Install a statistical BACKEND than can only CHECK for spam."
1026 (spam-install-backend-super
1027 backend
1028 'check check 'statistical t))
1030 ;;}}}
1032 ;;{{{ backend installations
1033 (spam-install-checkonly-backend 'spam-use-blackholes
1034 'spam-check-blackholes)
1036 (spam-install-checkonly-backend 'spam-use-hashcash
1037 'spam-check-hashcash)
1039 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1040 'spam-check-spamassassin-headers)
1042 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1043 'spam-check-bogofilter-headers)
1045 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1046 'spam-check-bsfilter-headers)
1048 (spam-install-checkonly-backend 'spam-use-gmane-xref
1049 'spam-check-gmane-xref)
1051 (spam-install-checkonly-backend 'spam-use-regex-headers
1052 'spam-check-regex-headers)
1054 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1055 'spam-check-regex-body)
1057 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy)
1058 (spam-install-mover-backend 'spam-use-move
1059 'spam-move-ham-routine
1060 'spam-move-spam-routine
1062 nil)
1064 (spam-install-nocheck-backend 'spam-use-copy
1065 'spam-copy-ham-routine
1066 'spam-copy-spam-routine
1068 nil)
1070 (spam-install-nocheck-backend 'spam-use-gmane
1071 'spam-report-gmane-unregister-routine
1072 'spam-report-gmane-register-routine
1073 'spam-report-gmane-register-routine
1074 'spam-report-gmane-unregister-routine)
1076 (spam-install-nocheck-backend 'spam-use-resend
1077 'spam-report-resend-register-ham-routine
1078 'spam-report-resend-register-routine
1080 nil)
1082 (spam-install-backend 'spam-use-BBDB
1083 'spam-check-BBDB
1084 'spam-BBDB-register-routine
1086 'spam-BBDB-unregister-routine
1087 nil)
1089 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1091 (spam-install-backend 'spam-use-blacklist
1092 'spam-check-blacklist
1094 'spam-blacklist-register-routine
1096 'spam-blacklist-unregister-routine)
1098 (spam-install-backend 'spam-use-whitelist
1099 'spam-check-whitelist
1100 'spam-whitelist-register-routine
1102 'spam-whitelist-unregister-routine
1103 nil)
1105 (spam-install-statistical-backend 'spam-use-ifile
1106 'spam-check-ifile
1107 'spam-ifile-register-ham-routine
1108 'spam-ifile-register-spam-routine
1109 'spam-ifile-unregister-ham-routine
1110 'spam-ifile-unregister-spam-routine)
1112 (spam-install-statistical-backend 'spam-use-spamoracle
1113 'spam-check-spamoracle
1114 'spam-spamoracle-learn-ham
1115 'spam-spamoracle-learn-spam
1116 'spam-spamoracle-unlearn-ham
1117 'spam-spamoracle-unlearn-spam)
1119 (spam-install-statistical-backend 'spam-use-stat
1120 'spam-check-stat
1121 'spam-stat-register-ham-routine
1122 'spam-stat-register-spam-routine
1123 'spam-stat-unregister-ham-routine
1124 'spam-stat-unregister-spam-routine)
1126 (spam-install-statistical-backend 'spam-use-spamassassin
1127 'spam-check-spamassassin
1128 'spam-spamassassin-register-ham-routine
1129 'spam-spamassassin-register-spam-routine
1130 'spam-spamassassin-unregister-ham-routine
1131 'spam-spamassassin-unregister-spam-routine)
1133 (spam-install-statistical-backend 'spam-use-bogofilter
1134 'spam-check-bogofilter
1135 'spam-bogofilter-register-ham-routine
1136 'spam-bogofilter-register-spam-routine
1137 'spam-bogofilter-unregister-ham-routine
1138 'spam-bogofilter-unregister-spam-routine)
1140 (spam-install-statistical-backend 'spam-use-bsfilter
1141 'spam-check-bsfilter
1142 'spam-bsfilter-register-ham-routine
1143 'spam-bsfilter-register-spam-routine
1144 'spam-bsfilter-unregister-ham-routine
1145 'spam-bsfilter-unregister-spam-routine)
1147 (spam-install-statistical-backend 'spam-use-crm114
1148 'spam-check-crm114
1149 'spam-crm114-register-ham-routine
1150 'spam-crm114-register-spam-routine
1151 'spam-crm114-unregister-ham-routine
1152 'spam-crm114-unregister-spam-routine)
1153 ;;}}}
1155 ;;{{{ scoring and summary formatting
1156 (defun spam-necessary-extra-headers ()
1157 "Return the extra headers spam.el thinks are necessary."
1158 (let (list)
1159 (when (or spam-use-spamassassin
1160 spam-use-spamassassin-headers
1161 spam-use-regex-headers)
1162 (push 'X-Spam-Status list))
1163 (when (or spam-use-bogofilter
1164 spam-use-regex-headers)
1165 (push 'X-Bogosity list))
1166 (when (or spam-use-crm114
1167 spam-use-regex-headers)
1168 (push 'X-CRM114-Status list))
1169 list))
1171 (defun spam-user-format-function-S (headers)
1172 (when headers
1173 (format "%3.2f"
1174 (spam-summary-score headers spam-summary-score-preferred-header))))
1176 (defun spam-article-sort-by-spam-status (h1 h2)
1177 "Sort articles by score."
1178 (let (result)
1179 (dolist (header (spam-necessary-extra-headers))
1180 (let ((s1 (spam-summary-score h1 header))
1181 (s2 (spam-summary-score h2 header)))
1182 (unless (= s1 s2)
1183 (setq result (< s1 s2))
1184 (return))))
1185 result))
1187 (defvar spam-spamassassin-score-regexp
1188 ".*\\b\\(?:score\\|hits\\)=\\(-?[0-9.]+\\)"
1189 "Regexp matching SpamAssassin score header.
1190 The first group must match the number.")
1192 (defun spam-extra-header-to-number (header headers)
1193 "Transform an extra HEADER to a number, using list of HEADERS.
1194 Note this has to be fast."
1195 (let ((header-content (gnus-extra-header header headers)))
1196 (if header-content
1197 (cond
1198 ((eq header 'X-Spam-Status)
1199 (string-to-number (replace-regexp-in-string
1200 spam-spamassassin-score-regexp
1201 "\\1"
1202 header-content)))
1203 ;; for CRM checking, it's probably faster to just do the string match
1204 ((string-match "( pR: \\([0-9.-]+\\)" header-content)
1205 (- (string-to-number (match-string 1 header-content))))
1206 ((eq header 'X-Bogosity)
1207 (string-to-number (replace-regexp-in-string
1208 ",.*" ""
1209 (replace-regexp-in-string
1210 ".*spamicity=" ""
1211 header-content))))
1212 (t nil))
1213 nil)))
1215 (defun spam-summary-score (headers &optional specific-header)
1216 "Score an article for the summary buffer, as fast as possible.
1217 With SPECIFIC-HEADER, returns only that header's score.
1218 Will not return a nil score."
1219 (let (score)
1220 (dolist (header
1221 (if specific-header
1222 (list specific-header)
1223 (spam-necessary-extra-headers)))
1224 (setq score
1225 (spam-extra-header-to-number header headers))
1226 (when score
1227 (return)))
1228 (or score 0)))
1230 (defun spam-generic-score (&optional recheck)
1231 "Invoke whatever scoring method we can."
1232 (interactive "P")
1233 (cond
1234 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1235 (spam-spamassassin-score recheck))
1236 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1237 (spam-bsfilter-score recheck))
1238 (spam-use-crm114
1239 (spam-crm114-score))
1240 (t (spam-bogofilter-score recheck))))
1241 ;;}}}
1243 ;;{{{ set up widening, processor checks
1245 ;;; set up IMAP widening if it's necessary
1246 (defun spam-setup-widening ()
1247 (when (spam-widening-needed-p)
1248 (setq nnimap-split-download-body-default t)))
1250 (defun spam-widening-needed-p (&optional force-symbols)
1251 (let (found)
1252 (dolist (backend (spam-backend-list))
1253 (when (and (spam-backend-statistical-p backend)
1254 (or (symbol-value backend)
1255 (memq backend force-symbols)))
1256 (setq found backend)))
1257 found))
1259 (defvar spam-list-of-processors
1260 ;; note the nil processors are not defined in gnus.el
1261 '((gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
1262 (gnus-group-spam-exit-processor-bsfilter spam spam-use-bsfilter)
1263 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
1264 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
1265 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
1266 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
1267 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spamassassin)
1268 (gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane) ;; Buggy?
1269 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
1270 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
1271 (gnus-group-ham-exit-processor-bsfilter ham spam-use-bsfilter)
1272 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
1273 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
1274 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
1275 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
1276 (gnus-group-ham-exit-processor-spamassassin ham spam-use-spamassassin)
1277 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
1278 "The OBSOLETE `spam-list-of-processors' list.
1279 This list contains pairs associating the obsolete ham/spam exit
1280 processor variables with a classification and a spam-use-*
1281 variable. When the processor variable is nil, just the
1282 classification and spam-use-* check variable are used. This is
1283 superseded by the new spam backend code, so it's only consulted
1284 for backwards compatibility.")
1285 (make-obsolete-variable 'spam-list-of-processors nil "22.1")
1287 (defun spam-group-processor-p (group backend &optional classification)
1288 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1289 Also accepts the obsolete processors, which can be found in
1290 gnus.el and in spam-list-of-processors. In the case of mover
1291 backends, checks the setting of `spam-summary-exit-behavior' in
1292 addition to the set values for the group."
1293 (if (and (stringp group)
1294 (symbolp backend))
1295 (let ((old-style (assq backend spam-list-of-processors))
1296 (parameters (nth 0 (gnus-parameter-spam-process group)))
1297 found)
1298 (if old-style ; old-style processor
1299 (spam-group-processor-p group (nth 2 old-style) (nth 1 old-style))
1300 ;; now search for the parameter
1301 (dolist (parameter parameters)
1302 (when (and (null found)
1303 (listp parameter)
1304 (eq classification (nth 0 parameter))
1305 (eq backend (nth 1 parameter)))
1306 (setq found t)))
1308 ;; now, if the parameter was not found, do the
1309 ;; spam-summary-exit-behavior-logic for mover backends
1310 (unless found
1311 (when (spam-backend-mover-p backend)
1312 (setq
1313 found
1314 (cond
1315 ((eq spam-summary-exit-behavior 'move-all) t)
1316 ((eq spam-summary-exit-behavior 'move-none) nil)
1317 ((eq spam-summary-exit-behavior 'default)
1318 (or (eq classification 'spam) ;move spam out of all groups
1319 ;; move ham out of spam groups
1320 (and (eq classification 'ham)
1321 (spam-group-spam-contents-p group))))
1322 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1323 spam-summary-exit-behavior))))))
1325 found))
1326 nil))
1328 ;;}}}
1330 ;;{{{ Summary entry and exit processing.
1332 (defun spam-mark-junk-as-spam-routine ()
1333 ;; check the global list of group names spam-junk-mailgroups and the
1334 ;; group parameters
1335 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1336 (gnus-message 6 "Marking %s articles as spam"
1337 (if spam-mark-only-unseen-as-spam
1338 "unseen"
1339 "unread"))
1340 (let ((articles (if spam-mark-only-unseen-as-spam
1341 gnus-newsgroup-unseen
1342 gnus-newsgroup-unreads)))
1343 (if spam-mark-new-messages-in-spam-group-as-spam
1344 (dolist (article articles)
1345 (gnus-summary-mark-article article gnus-spam-mark))
1346 (gnus-message 9 "Did not mark new messages as spam.")))))
1348 (defun spam-summary-prepare ()
1349 (setq spam-old-articles
1350 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1351 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1352 (spam-mark-junk-as-spam-routine))
1354 ;; The spam processors are invoked for any group, spam or ham or neither
1355 (defun spam-summary-prepare-exit ()
1356 (unless gnus-group-is-exiting-without-update-p
1357 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1359 ;; before we begin, remove any article limits
1360 ; (ignore-errors
1361 ; (gnus-summary-pop-limit t))
1363 ;; first of all, unregister any articles that are no longer ham or spam
1364 ;; we have to iterate over the processors, or else we'll be too slow
1365 (dolist (classification (spam-classifications))
1366 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1367 (new-articles (spam-list-articles
1368 gnus-newsgroup-articles
1369 classification))
1370 (changed-articles (spam-set-difference new-articles old-articles)))
1371 ;; now that we have the changed articles, we go through the processors
1372 (dolist (backend (spam-backend-list))
1373 (let (unregister-list)
1374 (dolist (article changed-articles)
1375 (let ((id (spam-fetch-field-message-id-fast article)))
1376 (when (spam-log-unregistration-needed-p
1377 id 'process classification backend)
1378 (push article unregister-list))))
1379 ;; call spam-register-routine with specific articles to unregister,
1380 ;; when there are articles to unregister and the check is enabled
1381 (when (and unregister-list (symbol-value backend))
1382 (spam-backend-put-article-todo-list backend
1383 classification
1384 unregister-list
1385 t))))))
1387 ;; do the non-moving backends first, then the moving ones
1388 (dolist (backend-type '(non-mover mover))
1389 (dolist (classification (spam-classifications))
1390 (dolist (backend (spam-backend-list backend-type))
1391 (when (spam-group-processor-p
1392 gnus-newsgroup-name
1393 backend
1394 classification)
1395 (spam-backend-put-article-todo-list backend
1396 classification
1397 (spam-list-articles
1398 gnus-newsgroup-articles
1399 classification))))))
1401 (spam-resolve-registrations-routine) ; do the registrations now
1403 ;; we mark all the leftover spam articles as expired at the end
1404 (dolist (article (spam-list-articles
1405 gnus-newsgroup-articles
1406 'spam))
1407 (gnus-summary-mark-article article gnus-expirable-mark)))
1409 (setq spam-old-articles nil))
1411 ;;}}}
1413 ;;{{{ spam-use-move and spam-use-copy backend support functions
1415 (defun spam-copy-or-move-routine (copy groups articles classification)
1417 (when (and (car-safe groups) (listp (car-safe groups)))
1418 (setq groups (pop groups)))
1420 (unless (listp groups)
1421 (setq groups (list groups)))
1423 ;; remove the current process mark
1424 (gnus-summary-kill-process-mark)
1426 (let ((backend-supports-deletions
1427 (gnus-check-backend-function
1428 'request-move-article gnus-newsgroup-name))
1429 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1430 article mark deletep respool valid-move-destinations)
1432 (when (member 'respool groups)
1433 (setq respool t) ; boolean for later
1434 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1436 ;; exclude invalid move destinations
1437 (dolist (group groups)
1438 (unless
1440 (and
1441 (eq classification 'spam)
1442 (spam-group-spam-contents-p gnus-newsgroup-name)
1443 (spam-group-spam-contents-p group)
1444 (gnus-message
1446 "Sorry, can't move spam from spam group %s to spam group %s"
1447 gnus-newsgroup-name
1448 group))
1449 (and
1450 (eq classification 'ham)
1451 (spam-group-ham-contents-p gnus-newsgroup-name)
1452 (spam-group-ham-contents-p group)
1453 (gnus-message
1455 "Sorry, can't move ham from ham group %s to ham group %s"
1456 gnus-newsgroup-name
1457 group)))
1458 (push group valid-move-destinations)))
1460 (setq groups (nreverse valid-move-destinations))
1462 ;; now do the actual move
1463 (dolist (group groups)
1465 (when (and articles (stringp group))
1467 ;; first, mark the article with the process mark and, if needed,
1468 ;; the unread or expired mark (for ham and spam respectively)
1469 (dolist (article articles)
1470 (when (and (eq classification 'ham)
1471 spam-mark-ham-unread-before-move-from-spam-group)
1472 (gnus-message 9 "Marking ham article %d unread before move"
1473 article)
1474 (gnus-summary-mark-article article gnus-unread-mark))
1475 (when (and (eq classification 'spam)
1476 (not copy))
1477 (gnus-message 9 "Marking spam article %d expirable before move"
1478 article)
1479 (gnus-summary-mark-article article gnus-expirable-mark))
1480 (gnus-summary-set-process-mark article)
1482 (if respool ; respooling is with a "fake" group
1483 (let ((spam-split-disabled
1484 (or spam-split-disabled
1485 (and (eq classification 'ham)
1486 spam-disable-spam-split-during-ham-respool))))
1487 (gnus-message 9 "Respooling article %d with method %s"
1488 article respool-method)
1489 (gnus-summary-respool-article nil respool-method))
1490 ;; else, we are not respooling
1491 (if (or (not backend-supports-deletions)
1492 (> (length groups) 1))
1493 (progn ; if copying, copy and set deletep
1494 (gnus-message 9 "Copying article %d to group %s"
1495 article group)
1496 (gnus-summary-copy-article nil group)
1497 (setq deletep t))
1498 (gnus-message 9 "Moving article %d to group %s"
1499 article group)
1500 (gnus-summary-move-article nil group)))))) ; else move articles
1502 ;; now delete the articles, unless a) copy is t, and there was a copy done
1503 ;; b) a move was done to a single group
1504 ;; c) backend-supports-deletions is nil
1505 (unless copy
1506 (when (and deletep backend-supports-deletions)
1507 (dolist (article articles)
1508 (gnus-summary-set-process-mark article)
1509 (gnus-message 9 "Deleting article %d" article))
1510 (when articles
1511 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1512 (gnus-summary-delete-article nil)))))
1513 (gnus-summary-yank-process-mark)
1514 (length articles)))
1516 (defun spam-copy-spam-routine (articles)
1517 (spam-copy-or-move-routine
1519 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1520 articles
1521 'spam))
1523 (defun spam-move-spam-routine (articles)
1524 (spam-copy-or-move-routine
1526 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1527 articles
1528 'spam))
1530 (defun spam-copy-ham-routine (articles)
1531 (spam-copy-or-move-routine
1533 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1534 articles
1535 'ham))
1537 (defun spam-move-ham-routine (articles)
1538 (spam-copy-or-move-routine
1540 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1541 articles
1542 'ham))
1544 ;;}}}
1546 ;;{{{ article and field retrieval code
1547 (defun spam-get-article-as-string (article)
1548 (when (numberp article)
1549 (with-temp-buffer
1550 (gnus-request-article-this-buffer
1551 article
1552 gnus-newsgroup-name)
1553 (buffer-string))))
1555 ;; disabled for now
1556 ;; (defun spam-get-article-as-filename (article)
1557 ;; (let ((article-filename))
1558 ;; (when (numberp article)
1559 ;; (nnml-possibly-change-directory
1560 ;; (gnus-group-real-name gnus-newsgroup-name))
1561 ;; (setq article-filename (expand-file-name
1562 ;; (int-to-string article) nnml-current-directory)))
1563 ;; (if (file-exists-p article-filename)
1564 ;; article-filename
1565 ;; nil)))
1567 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1568 "Fetch a FIELD for ARTICLE with the internal `gnus-data-list' function.
1569 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1570 When FIELD is 'number, ARTICLE can be any number (since we want
1571 to find it out)."
1572 (when (numberp article)
1573 (let* ((data-header (or prepared-data-header
1574 (spam-fetch-article-header article))))
1575 (cond
1576 ((not (arrayp data-header))
1577 (gnus-message 6 "Article %d has a nil data header" article))
1578 ((equal field 'number)
1579 (mail-header-number data-header))
1580 ((equal field 'from)
1581 (mail-header-from data-header))
1582 ((equal field 'message-id)
1583 (mail-header-message-id data-header))
1584 ((equal field 'subject)
1585 (mail-header-subject data-header))
1586 ((equal field 'references)
1587 (mail-header-references data-header))
1588 ((equal field 'date)
1589 (mail-header-date data-header))
1590 ((equal field 'xref)
1591 (mail-header-xref data-header))
1592 ((equal field 'extra)
1593 (mail-header-extra data-header))
1595 (gnus-error
1597 "spam-fetch-field-fast: unknown field %s requested"
1598 field)
1599 nil)))))
1601 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1602 (spam-fetch-field-fast article 'from prepared-data-header))
1604 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1605 (spam-fetch-field-fast article 'subject prepared-data-header))
1607 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1608 (spam-fetch-field-fast article 'message-id prepared-data-header))
1610 (defun spam-generate-fake-headers (article)
1611 (let ((dh (spam-fetch-article-header article)))
1612 (if dh
1613 (concat
1614 (format
1615 ;; 80-character limit makes for strange constructs
1616 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1617 "Date: %s\nReferences: %s\nXref: %s\n")
1618 (spam-fetch-field-fast article 'from dh)
1619 (spam-fetch-field-fast article 'subject dh)
1620 (spam-fetch-field-fast article 'message-id dh)
1621 (spam-fetch-field-fast article 'date dh)
1622 (spam-fetch-field-fast article 'references dh)
1623 (spam-fetch-field-fast article 'xref dh))
1624 (when (spam-fetch-field-fast article 'extra dh)
1625 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1626 (gnus-message
1628 "spam-generate-fake-headers: article %d didn't have a valid header"
1629 article))))
1631 (defun spam-fetch-article-header (article)
1632 (with-current-buffer gnus-summary-buffer
1633 (gnus-read-header article)
1634 (nth 3 (assq article gnus-newsgroup-data))))
1635 ;;}}}
1637 ;;{{{ Spam determination.
1639 (defun spam-split (&rest specific-checks)
1640 "Split this message into the `spam' group if it is spam.
1641 This function can be used as an entry in the variable `nnmail-split-fancy',
1642 for example like this: (: spam-split). It can take checks as
1643 parameters. A string as a parameter will set the
1644 `spam-split-group' to that string.
1646 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1647 (interactive)
1648 (setq spam-split-last-successful-check nil)
1649 (unless spam-split-disabled
1650 (let ((spam-split-group-choice spam-split-group))
1651 (dolist (check specific-checks)
1652 (when (stringp check)
1653 (setq spam-split-group-choice check)
1654 (setq specific-checks (delq check specific-checks))))
1656 (let ((spam-split-group spam-split-group-choice)
1657 (widening-needed-check (spam-widening-needed-p specific-checks)))
1658 (save-excursion
1659 (save-restriction
1660 (when widening-needed-check
1661 (widen)
1662 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1663 widening-needed-check))
1664 (let ((backends (spam-backend-list))
1665 decision)
1666 (while (and backends (not decision))
1667 (let* ((backend (pop backends))
1668 (check-function (spam-backend-check backend))
1669 (spam-split-group (if spam-split-symbolic-return
1670 'spam
1671 spam-split-group)))
1672 (when (or
1673 ;; either, given specific checks, this is one of them
1674 (memq backend specific-checks)
1675 ;; or, given no specific checks, spam-use-CHECK is set
1676 (and (null specific-checks) (symbol-value backend)))
1677 (gnus-message 6 "spam-split: calling the %s function"
1678 check-function)
1679 (setq decision (funcall check-function))
1680 ;; if we got a decision at all, save the current check
1681 (when decision
1682 (setq spam-split-last-successful-check backend))
1684 (when (eq decision 'spam)
1685 (unless spam-split-symbolic-return
1686 (gnus-error
1688 (format "spam-split got %s but %s is nil"
1689 decision
1690 spam-split-symbolic-return)))))))
1691 (if (eq decision t)
1692 (if spam-split-symbolic-return-positive 'ham nil)
1693 decision))))))))
1695 (defun spam-find-spam ()
1696 "Detect spam in the current newsgroup using `spam-split'."
1697 (interactive)
1699 (let* ((group gnus-newsgroup-name)
1700 (autodetect (gnus-parameter-spam-autodetect group))
1701 (methods (gnus-parameter-spam-autodetect-methods group))
1702 (first-method (nth 0 methods))
1703 (articles (if spam-autodetect-recheck-messages
1704 gnus-newsgroup-articles
1705 gnus-newsgroup-unseen))
1706 article-cannot-be-faked)
1709 (dolist (backend methods)
1710 (when (spam-backend-statistical-p backend)
1711 (setq article-cannot-be-faked t)
1712 (return)))
1714 (when (memq 'default methods)
1715 (setq article-cannot-be-faked t))
1717 (when (and autodetect
1718 (not (equal first-method 'none)))
1719 (mapcar
1720 (lambda (article)
1721 (let ((id (spam-fetch-field-message-id-fast article))
1722 (subject (spam-fetch-field-subject-fast article))
1723 (sender (spam-fetch-field-from-fast article))
1724 registry-lookup)
1726 (unless id
1727 (gnus-message 6 "Article %d has no message ID!" article))
1729 (when (and id spam-log-to-registry)
1730 (setq registry-lookup (spam-log-registration-type id 'incoming))
1731 (when registry-lookup
1732 (gnus-message
1734 "spam-find-spam: message %s was already registered incoming"
1735 id)))
1737 (let* ((spam-split-symbolic-return t)
1738 (spam-split-symbolic-return-positive t)
1739 (fake-headers (spam-generate-fake-headers article))
1740 (split-return
1741 (or registry-lookup
1742 (with-temp-buffer
1743 (if article-cannot-be-faked
1744 (gnus-request-article-this-buffer
1745 article
1746 group)
1747 ;; else, we fake the article
1748 (when fake-headers (insert fake-headers)))
1749 (if (or (null first-method)
1750 (equal first-method 'default))
1751 (spam-split)
1752 (apply 'spam-split methods))))))
1753 (if (equal split-return 'spam)
1754 (gnus-summary-mark-article article gnus-spam-mark))
1756 (when (and id split-return spam-log-to-registry)
1757 (when (zerop (gnus-registry-group-count id))
1758 (gnus-registry-handle-action id nil group subject sender))
1760 (unless registry-lookup
1761 (spam-log-processing-to-registry
1763 'incoming
1764 split-return
1765 spam-split-last-successful-check
1766 group))))))
1767 articles))))
1769 ;;}}}
1771 ;;{{{ registration/unregistration functions
1773 (defun spam-resolve-registrations-routine ()
1774 "Go through the backends and register or unregister articles as needed."
1775 (dolist (backend-type '(non-mover mover))
1776 (dolist (classification (spam-classifications))
1777 (dolist (backend (spam-backend-list backend-type))
1778 (let ((rlist (spam-backend-get-article-todo-list
1779 backend classification))
1780 (ulist (spam-backend-get-article-todo-list
1781 backend classification t))
1782 (delcount 0))
1784 ;; clear the old lists right away
1785 (spam-backend-put-article-todo-list backend
1786 classification
1788 nil)
1789 (spam-backend-put-article-todo-list backend
1790 classification
1794 ;; eliminate duplicates
1795 (dolist (article (copy-sequence ulist))
1796 (when (memq article rlist)
1797 (incf delcount)
1798 (setq rlist (delq article rlist))
1799 (setq ulist (delq article ulist))))
1801 (unless (zerop delcount)
1802 (gnus-message
1804 "%d messages did not have to unregister and then register"
1805 delcount))
1807 ;; unregister articles
1808 (unless (zerop (length ulist))
1809 (let ((num (spam-unregister-routine classification backend ulist)))
1810 (when (> num 0)
1811 (gnus-message
1813 "%d %s messages were unregistered by backend %s."
1815 classification
1816 backend))))
1818 ;; register articles
1819 (unless (zerop (length rlist))
1820 (let ((num (spam-register-routine classification backend rlist)))
1821 (when (> num 0)
1822 (gnus-message
1824 "%d %s messages were registered by backend %s."
1826 classification
1827 backend)))))))))
1829 (defun spam-unregister-routine (classification
1830 backend
1831 specific-articles)
1832 (spam-register-routine classification backend specific-articles t))
1834 (defun spam-register-routine (classification
1835 backend
1836 specific-articles
1837 &optional unregister)
1838 (when (and (spam-classification-valid-p classification)
1839 (spam-backend-valid-p backend))
1840 (let* ((register-function
1841 (spam-backend-function backend classification 'registration))
1842 (unregister-function
1843 (spam-backend-function backend classification 'unregistration))
1844 (run-function (if unregister
1845 unregister-function
1846 register-function))
1847 (log-function (if unregister
1848 'spam-log-undo-registration
1849 'spam-log-processing-to-registry))
1850 article articles)
1852 (when run-function
1853 ;; make list of articles, using specific-articles if given
1854 (setq articles (or specific-articles
1855 (spam-list-articles
1856 gnus-newsgroup-articles
1857 classification)))
1858 ;; process them
1859 (when (> (length articles) 0)
1860 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1861 (if unregister "Unregistering" "Registering")
1862 (length articles)
1863 (if specific-articles "specific" "")
1864 classification
1865 backend)
1866 (funcall run-function articles)
1867 ;; now log all the registrations (or undo them, depending on
1868 ;; unregister)
1869 (dolist (article articles)
1870 (funcall log-function
1871 (spam-fetch-field-message-id-fast article)
1872 'process
1873 classification
1874 backend
1875 gnus-newsgroup-name))))
1876 ;; return the number of articles processed
1877 (length articles))))
1879 ;;; log a ham- or spam-processor invocation to the registry
1880 (defun spam-log-processing-to-registry (id type classification backend group)
1881 (when spam-log-to-registry
1882 (if (and (stringp id)
1883 (stringp group)
1884 (spam-process-type-valid-p type)
1885 (spam-classification-valid-p classification)
1886 (spam-backend-valid-p backend))
1887 (let ((cell-list (gnus-registry-get-id-key id type))
1888 (cell (list classification backend group)))
1889 (push cell cell-list)
1890 (gnus-registry-set-id-key id type cell-list))
1892 (gnus-error
1894 (format
1895 "%s call with bad ID, type, classification, spam-backend, or group"
1896 "spam-log-processing-to-registry")))))
1898 ;;; check if a ham- or spam-processor registration has been done
1899 (defun spam-log-registered-p (id type)
1900 (when spam-log-to-registry
1901 (if (and (stringp id)
1902 (spam-process-type-valid-p type))
1903 (gnus-registry-get-id-key id type)
1904 (progn
1905 (gnus-error
1907 (format "%s called with bad ID, type, classification, or spam-backend"
1908 "spam-log-registered-p"))
1909 nil))))
1911 ;;; check what a ham- or spam-processor registration says
1912 ;;; returns nil if conflicting registrations are found
1913 (defun spam-log-registration-type (id type)
1914 (let ((count 0)
1915 decision)
1916 (dolist (reg (spam-log-registered-p id type))
1917 (let ((classification (nth 0 reg)))
1918 (when (spam-classification-valid-p classification)
1919 (when (and decision
1920 (not (eq classification decision)))
1921 (setq count (+ 1 count)))
1922 (setq decision classification))))
1923 (if (< 0 count)
1925 decision)))
1928 ;;; check if a ham- or spam-processor registration needs to be undone
1929 (defun spam-log-unregistration-needed-p (id type classification backend)
1930 (when spam-log-to-registry
1931 (if (and (stringp id)
1932 (spam-process-type-valid-p type)
1933 (spam-classification-valid-p classification)
1934 (spam-backend-valid-p backend))
1935 (let ((cell-list (gnus-registry-get-id-key id type))
1936 found)
1937 (dolist (cell cell-list)
1938 (unless found
1939 (when (and (eq classification (nth 0 cell))
1940 (eq backend (nth 1 cell)))
1941 (setq found t))))
1942 found)
1943 (progn
1944 (gnus-error
1946 (format "%s called with bad ID, type, classification, or spam-backend"
1947 "spam-log-unregistration-needed-p"))
1948 nil))))
1951 ;;; undo a ham- or spam-processor registration (the group is not used)
1952 (defun spam-log-undo-registration (id type classification backend
1953 &optional group)
1954 (when (and spam-log-to-registry
1955 (spam-log-unregistration-needed-p id type classification backend))
1956 (if (and (stringp id)
1957 (spam-process-type-valid-p type)
1958 (spam-classification-valid-p classification)
1959 (spam-backend-valid-p backend))
1960 (let ((cell-list (gnus-registry-get-id-key id type))
1961 new-cell-list found)
1962 (dolist (cell cell-list)
1963 (unless (and (eq classification (nth 0 cell))
1964 (eq backend (nth 1 cell)))
1965 (push cell new-cell-list)))
1966 (gnus-registry-set-id-key id type new-cell-list))
1967 (progn
1968 (gnus-error 7 (format
1969 "%s call with bad ID, type, spam-backend, or group"
1970 "spam-log-undo-registration"))
1971 nil))))
1973 ;;}}}
1975 ;;{{{ backend functions
1977 ;;{{{ Gmane xrefs
1978 (defun spam-check-gmane-xref ()
1979 (let ((header (or
1980 (message-fetch-field "Xref")
1981 (message-fetch-field "Newsgroups"))))
1982 (when header ; return nil when no header
1983 (when (string-match spam-gmane-xref-spam-group
1984 header)
1985 spam-split-group))))
1987 ;;}}}
1989 ;;{{{ Regex body
1991 (defun spam-check-regex-body ()
1992 (let ((spam-regex-headers-ham spam-regex-body-ham)
1993 (spam-regex-headers-spam spam-regex-body-spam))
1994 (spam-check-regex-headers t)))
1996 ;;}}}
1998 ;;{{{ Regex headers
2000 (defun spam-check-regex-headers (&optional body)
2001 (let ((type (if body "body" "header"))
2002 ret found)
2003 (dolist (h-regex spam-regex-headers-ham)
2004 (unless found
2005 (goto-char (point-min))
2006 (when (re-search-forward h-regex nil t)
2007 (message "Ham regex %s search positive." type)
2008 (setq found t))))
2009 (dolist (s-regex spam-regex-headers-spam)
2010 (unless found
2011 (goto-char (point-min))
2012 (when (re-search-forward s-regex nil t)
2013 (message "Spam regex %s search positive." type)
2014 (setq found t)
2015 (setq ret spam-split-group))))
2016 ret))
2018 ;;}}}
2020 ;;{{{ Blackholes.
2022 (defun spam-reverse-ip-string (ip)
2023 (when (stringp ip)
2024 (mapconcat 'identity
2025 (nreverse (split-string ip "\\."))
2026 ".")))
2028 (defun spam-check-blackholes ()
2029 "Check the Received headers for blackholed relays."
2030 (let ((headers (message-fetch-field "received"))
2031 ips matches)
2032 (when headers
2033 (with-temp-buffer
2034 (insert headers)
2035 (goto-char (point-min))
2036 (gnus-message 6 "Checking headers for relay addresses")
2037 (while (re-search-forward
2038 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
2039 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
2040 (push (spam-reverse-ip-string (match-string 1))
2041 ips)))
2042 (dolist (server spam-blackhole-servers)
2043 (dolist (ip ips)
2044 (unless (and spam-blackhole-good-server-regex
2045 ;; match against the reversed (again) IP string
2046 (string-match
2047 spam-blackhole-good-server-regex
2048 (spam-reverse-ip-string ip)))
2049 (unless matches
2050 (let ((query-string (concat ip "." server)))
2051 (if spam-use-dig
2052 (let ((query-result (query-dig query-string)))
2053 (when query-result
2054 (gnus-message 6 "(DIG): positive blackhole check `%s'"
2055 query-result)
2056 (push (list ip server query-result)
2057 matches)))
2058 ;; else, if not using dig.el
2059 (when (dns-query query-string)
2060 (gnus-message 6 "positive blackhole check")
2061 (push (list ip server (dns-query query-string 'TXT))
2062 matches)))))))))
2063 (when matches
2064 spam-split-group)))
2065 ;;}}}
2067 ;;{{{ Hashcash.
2069 (defun spam-check-hashcash ()
2070 "Check the headers for hashcash payments."
2071 (ignore-errors (mail-check-payment))) ;mail-check-payment returns a boolean
2073 ;;}}}
2075 ;;{{{ BBDB
2077 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2078 ;;; <sacha@giotto.sj.ru>
2080 ;; all this is done inside a condition-case to trap errors
2082 ;; Autoloaded in message, which we require.
2083 (declare-function gnus-extract-address-components "gnus-util" (from))
2085 (eval-and-compile
2086 (condition-case nil
2087 (progn
2088 (require 'bbdb)
2089 (require 'bbdb-com))
2090 (file-error
2091 ;; `bbdb-records' should not be bound as an autoload function
2092 ;; before loading bbdb because of `bbdb-hashtable-size'.
2093 (defalias 'bbdb-buffer 'ignore)
2094 (defalias 'bbdb-create-internal 'ignore)
2095 (defalias 'bbdb-records 'ignore)
2096 (defalias 'spam-BBDB-register-routine 'ignore)
2097 (defalias 'spam-enter-ham-BBDB 'ignore)
2098 (defalias 'spam-exists-in-BBDB-p 'ignore)
2099 (defalias 'bbdb-gethash 'ignore)
2100 nil)))
2102 (eval-and-compile
2103 (when (featurep 'bbdb-com)
2104 ;; when the BBDB changes, we want to clear out our cache
2105 (defun spam-clear-cache-BBDB (&rest immaterial)
2106 (spam-clear-cache 'spam-use-BBDB))
2108 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2110 (defun spam-enter-ham-BBDB (addresses &optional remove)
2111 "Enter an address into the BBDB; implies ham (non-spam) sender"
2112 (dolist (from addresses)
2113 (when (stringp from)
2114 (let* ((parsed-address (gnus-extract-address-components from))
2115 (name (or (nth 0 parsed-address) "Ham Sender"))
2116 (remove-function (if remove
2117 'bbdb-delete-record-internal
2118 'ignore))
2119 (net-address (nth 1 parsed-address))
2120 (record (and net-address
2121 (spam-exists-in-BBDB-p net-address))))
2122 (when net-address
2123 (gnus-message 6 "%s address %s %s BBDB"
2124 (if remove "Deleting" "Adding")
2125 from
2126 (if remove "from" "to"))
2127 (if record
2128 (funcall remove-function record)
2129 (bbdb-create-internal name nil net-address nil nil
2130 "ham sender added by spam.el")))))))
2132 (defun spam-BBDB-register-routine (articles &optional unregister)
2133 (let (addresses)
2134 (dolist (article articles)
2135 (when (stringp (spam-fetch-field-from-fast article))
2136 (push (spam-fetch-field-from-fast article) addresses)))
2137 ;; now do the register/unregister action
2138 (spam-enter-ham-BBDB addresses unregister)))
2140 (defun spam-BBDB-unregister-routine (articles)
2141 (spam-BBDB-register-routine articles t))
2143 (defsubst spam-exists-in-BBDB-p (net)
2144 (when (and (stringp net) (not (zerop (length net))))
2145 (bbdb-records)
2146 (bbdb-gethash (downcase net))))
2148 (defun spam-check-BBDB ()
2149 "Mail from people in the BBDB is classified as ham or non-spam"
2150 (let ((net (message-fetch-field "from")))
2151 (when net
2152 (setq net (nth 1 (gnus-extract-address-components net)))
2153 (if (spam-exists-in-BBDB-p net)
2155 (if spam-use-BBDB-exclusive
2156 spam-split-group
2157 nil)))))))
2159 ;;}}}
2161 ;;{{{ ifile
2163 ;;; check the ifile backend; return nil if the mail was NOT classified
2164 ;;; as spam
2167 (defun spam-get-ifile-database-parameter ()
2168 "Return the command-line parameter for ifile's database.
2169 See `spam-ifile-database'."
2170 (if spam-ifile-database
2171 (format "--db-file=%s" spam-ifile-database)
2172 nil))
2174 (defun spam-check-ifile ()
2175 "Check the ifile backend for the classification of this message."
2176 (let ((article-buffer-name (buffer-name))
2177 category return)
2178 (with-temp-buffer
2179 (let ((temp-buffer-name (buffer-name))
2180 (db-param (spam-get-ifile-database-parameter)))
2181 (with-current-buffer article-buffer-name
2182 (apply 'call-process-region
2183 (point-min) (point-max) spam-ifile-program
2184 nil temp-buffer-name nil "-c"
2185 (if db-param `(,db-param "-q") `("-q"))))
2186 ;; check the return now (we're back in the temp buffer)
2187 (goto-char (point-min))
2188 (if (not (eobp))
2189 (setq category (buffer-substring (point) (point-at-eol))))
2190 (when (not (zerop (length category))) ; we need a category here
2191 (if spam-ifile-all-categories
2192 (setq return category)
2193 ;; else, if spam-ifile-all-categories is not set...
2194 (when (string-equal spam-ifile-spam-category category)
2195 (setq return spam-split-group)))))) ; note return is nil otherwise
2196 return))
2198 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2199 "Register an article, given as a string, with a category.
2200 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2201 (let ((category (or category gnus-newsgroup-name))
2202 (add-or-delete-option (if unregister "-d" "-i"))
2203 (db (spam-get-ifile-database-parameter))
2204 parameters)
2205 (with-temp-buffer
2206 (dolist (article articles)
2207 (let ((article-string (spam-get-article-as-string article)))
2208 (when (stringp article-string)
2209 (insert article-string))))
2210 (apply 'call-process-region
2211 (point-min) (point-max) spam-ifile-program
2212 nil nil nil
2213 add-or-delete-option category
2214 (if db `(,db "-h") `("-h"))))))
2216 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2217 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2219 (defun spam-ifile-unregister-spam-routine (articles)
2220 (spam-ifile-register-spam-routine articles t))
2222 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2223 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2225 (defun spam-ifile-unregister-ham-routine (articles)
2226 (spam-ifile-register-ham-routine articles t))
2228 ;;}}}
2230 ;;{{{ spam-stat
2232 (require 'spam-stat)
2234 (defun spam-check-stat ()
2235 "Check the spam-stat backend for the classification of this message"
2236 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2237 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2238 category return)
2239 (spam-stat-split-fancy)))
2241 (defun spam-stat-register-spam-routine (articles &optional unregister)
2242 (dolist (article articles)
2243 (let ((article-string (spam-get-article-as-string article)))
2244 (with-temp-buffer
2245 (insert article-string)
2246 (if unregister
2247 (spam-stat-buffer-change-to-non-spam)
2248 (spam-stat-buffer-is-spam))))))
2250 (defun spam-stat-unregister-spam-routine (articles)
2251 (spam-stat-register-spam-routine articles t))
2253 (defun spam-stat-register-ham-routine (articles &optional unregister)
2254 (dolist (article articles)
2255 (let ((article-string (spam-get-article-as-string article)))
2256 (with-temp-buffer
2257 (insert article-string)
2258 (if unregister
2259 (spam-stat-buffer-change-to-spam)
2260 (spam-stat-buffer-is-non-spam))))))
2262 (defun spam-stat-unregister-ham-routine (articles)
2263 (spam-stat-register-ham-routine articles t))
2265 (defun spam-maybe-spam-stat-load ()
2266 (when spam-use-stat (spam-stat-load)))
2268 (defun spam-maybe-spam-stat-save ()
2269 (when spam-use-stat (spam-stat-save)))
2271 ;;}}}
2273 ;;{{{ Blacklists and whitelists.
2275 (defvar spam-whitelist-cache nil)
2276 (defvar spam-blacklist-cache nil)
2278 (defun spam-kill-whole-line ()
2279 (beginning-of-line)
2280 (let ((kill-whole-line t))
2281 (kill-line)))
2283 ;;; address can be a list, too
2284 (defun spam-enter-whitelist (address &optional remove)
2285 "Enter ADDRESS (list or single) into the whitelist.
2286 With a non-nil REMOVE, remove them."
2287 (interactive "sAddress: ")
2288 (spam-enter-list address spam-whitelist remove)
2289 (setq spam-whitelist-cache nil)
2290 (spam-clear-cache 'spam-use-whitelist))
2292 ;;; address can be a list, too
2293 (defun spam-enter-blacklist (address &optional remove)
2294 "Enter ADDRESS (list or single) into the blacklist.
2295 With a non-nil REMOVE, remove them."
2296 (interactive "sAddress: ")
2297 (spam-enter-list address spam-blacklist remove)
2298 (setq spam-blacklist-cache nil)
2299 (spam-clear-cache 'spam-use-whitelist))
2301 (defun spam-enter-list (addresses file &optional remove)
2302 "Enter ADDRESSES into the given FILE.
2303 Either the whitelist or the blacklist files can be used.
2304 With a non-nil REMOVE, remove the ADDRESSES."
2305 (if (stringp addresses)
2306 (spam-enter-list (list addresses) file remove)
2307 ;; else, we have a list of addresses here
2308 (unless (file-exists-p (file-name-directory file))
2309 (make-directory (file-name-directory file) t))
2310 (with-current-buffer
2311 (find-file-noselect file)
2312 (dolist (a addresses)
2313 (when (stringp a)
2314 (goto-char (point-min))
2315 (if (re-search-forward (regexp-quote a) nil t)
2316 ;; found the address
2317 (when remove
2318 (spam-kill-whole-line))
2319 ;; else, the address was not found
2320 (unless remove
2321 (goto-char (point-max))
2322 (unless (bobp)
2323 (insert "\n"))
2324 (insert a "\n")))))
2325 (save-buffer))))
2327 (defun spam-filelist-build-cache (type)
2328 (let ((cache (if (eq type 'spam-use-blacklist)
2329 spam-blacklist-cache
2330 spam-whitelist-cache))
2331 parsed-cache)
2332 (unless (gethash type spam-caches)
2333 (while cache
2334 (let ((address (pop cache)))
2335 (unless (zerop (length address)) ; 0 for a nil address too
2336 (setq address (regexp-quote address))
2337 ;; fix regexp-quote's treatment of user-intended regexes
2338 (while (string-match "\\\\\\*" address)
2339 (setq address (replace-match ".*" t t address))))
2340 (push address parsed-cache)))
2341 (puthash type parsed-cache spam-caches))))
2343 (defun spam-filelist-check-cache (type from)
2344 (when (stringp from)
2345 (spam-filelist-build-cache type)
2346 (let (found)
2347 (dolist (address (gethash type spam-caches))
2348 (when (and address (string-match address from))
2349 (setq found t)
2350 (return)))
2351 found)))
2353 ;;; returns t if the sender is in the whitelist, nil or
2354 ;;; spam-split-group otherwise
2355 (defun spam-check-whitelist ()
2356 ;; FIXME! Should it detect when file timestamps change?
2357 (unless spam-whitelist-cache
2358 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2359 (if (spam-from-listed-p 'spam-use-whitelist)
2361 (if spam-use-whitelist-exclusive
2362 spam-split-group
2363 nil)))
2365 (defun spam-check-blacklist ()
2366 ;; FIXME! Should it detect when file timestamps change?
2367 (unless spam-blacklist-cache
2368 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2369 (and (spam-from-listed-p 'spam-use-blacklist)
2370 spam-split-group))
2372 (defun spam-parse-list (file)
2373 (when (file-readable-p file)
2374 (let (contents address)
2375 (with-temp-buffer
2376 (insert-file-contents file)
2377 (while (not (eobp))
2378 (setq address (buffer-substring (point) (point-at-eol)))
2379 (forward-line 1)
2380 ;; insert the e-mail address if detected, otherwise the raw data
2381 (unless (zerop (length address))
2382 (let ((pure-address
2383 (nth 1 (gnus-extract-address-components address))))
2384 (push (or pure-address address) contents)))))
2385 (nreverse contents))))
2387 (defun spam-from-listed-p (type)
2388 (let ((from (message-fetch-field "from"))
2389 found)
2390 (spam-filelist-check-cache type from)))
2392 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2393 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2394 (declassification (if blacklist 'ham 'spam))
2395 (enter-function
2396 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2397 (remove-function
2398 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2399 from addresses unregister-list article-unregister-list)
2400 (dolist (article articles)
2401 (let ((from (spam-fetch-field-from-fast article))
2402 (id (spam-fetch-field-message-id-fast article))
2403 sender-ignored)
2404 (when (stringp from)
2405 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2406 (when (and (not sender-ignored)
2407 (stringp ignore-regex)
2408 (string-match ignore-regex from))
2409 (setq sender-ignored t)))
2410 ;; remember the messages we need to unregister, unless remove is set
2411 (when (and
2412 (null unregister)
2413 (spam-log-unregistration-needed-p
2414 id 'process declassification de-symbol))
2415 (push article article-unregister-list)
2416 (push from unregister-list))
2417 (unless sender-ignored
2418 (push from addresses)))))
2420 (if unregister
2421 (funcall enter-function addresses t) ; unregister all these addresses
2422 ;; else, register normally and unregister what we need to
2423 (funcall remove-function unregister-list t)
2424 (dolist (article article-unregister-list)
2425 (spam-log-undo-registration
2426 (spam-fetch-field-message-id-fast article)
2427 'process
2428 declassification
2429 de-symbol))
2430 (funcall enter-function addresses nil))))
2432 (defun spam-blacklist-unregister-routine (articles)
2433 (spam-blacklist-register-routine articles t))
2435 (defun spam-blacklist-register-routine (articles &optional unregister)
2436 (spam-filelist-register-routine articles t unregister))
2438 (defun spam-whitelist-unregister-routine (articles)
2439 (spam-whitelist-register-routine articles t))
2441 (defun spam-whitelist-register-routine (articles &optional unregister)
2442 (spam-filelist-register-routine articles nil unregister))
2444 ;;}}}
2446 ;;{{{ Spam-report glue (gmane and resend reporting)
2447 (defun spam-report-gmane-register-routine (articles)
2448 (when articles
2449 (apply 'spam-report-gmane-spam articles)))
2451 (defun spam-report-gmane-unregister-routine (articles)
2452 (when articles
2453 (apply 'spam-report-gmane-ham articles)))
2455 (defun spam-report-resend-register-ham-routine (articles)
2456 (spam-report-resend-register-routine articles t))
2458 (defvar spam-report-resend-to)
2460 (defun spam-report-resend-register-routine (articles &optional ham)
2461 (require 'spam-report)
2462 (let* ((resend-to-gp
2463 (if ham
2464 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2465 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2466 (spam-report-resend-to (or (car-safe resend-to-gp)
2467 spam-report-resend-to)))
2468 (spam-report-resend articles ham)))
2470 ;;}}}
2472 ;;{{{ Bogofilter
2473 (defun spam-check-bogofilter-headers (&optional score)
2474 (let ((header (message-fetch-field spam-bogofilter-header)))
2475 (when header ; return nil when no header
2476 (if score ; scoring mode
2477 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2478 (match-string 1 header)
2479 "0")
2480 ;; spam detection mode
2481 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2482 header)
2483 spam-split-group)))))
2485 ;; return something sensible if the score can't be determined
2486 (defun spam-bogofilter-score (&optional recheck)
2487 "Get the Bogofilter spamicity score."
2488 (interactive "P")
2489 (save-window-excursion
2490 (gnus-summary-show-article t)
2491 (set-buffer gnus-article-buffer)
2492 (let ((score (or (unless recheck
2493 (spam-check-bogofilter-headers t))
2494 (spam-check-bogofilter t))))
2495 (gnus-summary-show-article)
2496 (message "Spamicity score %s" score)
2497 (or score "0"))))
2499 (defun spam-verify-bogofilter ()
2500 "Verify the Bogofilter version is sufficient."
2501 (when (eq spam-bogofilter-valid 'unknown)
2502 (setq spam-bogofilter-valid
2503 (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2504 (shell-command-to-string
2505 (format "%s -V" spam-bogofilter-program))))))
2506 spam-bogofilter-valid)
2508 (defun spam-check-bogofilter (&optional score)
2509 "Check the Bogofilter backend for the classification of this message."
2510 (if (spam-verify-bogofilter)
2511 (let ((article-buffer-name (buffer-name))
2512 (db spam-bogofilter-database-directory)
2513 return)
2514 (with-temp-buffer
2515 (let ((temp-buffer-name (buffer-name)))
2516 (with-current-buffer article-buffer-name
2517 (apply 'call-process-region
2518 (point-min) (point-max)
2519 spam-bogofilter-program
2520 nil temp-buffer-name nil
2521 (if db `("-d" ,db "-v") `("-v"))))
2522 (setq return (spam-check-bogofilter-headers score))))
2523 return)
2524 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2526 (defun spam-bogofilter-register-with-bogofilter (articles
2527 spam
2528 &optional unregister)
2529 "Register an article, given as a string, as spam or non-spam."
2530 (if (spam-verify-bogofilter)
2531 (dolist (article articles)
2532 (let ((article-string (spam-get-article-as-string article))
2533 (db spam-bogofilter-database-directory)
2534 (switch (if unregister
2535 (if spam
2536 spam-bogofilter-spam-strong-switch
2537 spam-bogofilter-ham-strong-switch)
2538 (if spam
2539 spam-bogofilter-spam-switch
2540 spam-bogofilter-ham-switch))))
2541 (when (stringp article-string)
2542 (with-temp-buffer
2543 (insert article-string)
2545 (apply 'call-process-region
2546 (point-min) (point-max)
2547 spam-bogofilter-program
2548 nil nil nil switch
2549 (if db `("-d" ,db "-v") `("-v")))))))
2550 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2552 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2553 (spam-bogofilter-register-with-bogofilter articles t unregister))
2555 (defun spam-bogofilter-unregister-spam-routine (articles)
2556 (spam-bogofilter-register-spam-routine articles t))
2558 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2559 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2561 (defun spam-bogofilter-unregister-ham-routine (articles)
2562 (spam-bogofilter-register-ham-routine articles t))
2565 ;;}}}
2567 ;;{{{ spamoracle
2568 (defun spam-check-spamoracle ()
2569 "Run spamoracle on an article to determine whether it's spam."
2570 (let ((article-buffer-name (buffer-name)))
2571 (with-temp-buffer
2572 (let ((temp-buffer-name (buffer-name)))
2573 (with-current-buffer article-buffer-name
2574 (let ((status
2575 (apply 'call-process-region
2576 (point-min) (point-max)
2577 spam-spamoracle-binary
2578 nil temp-buffer-name nil
2579 (if spam-spamoracle-database
2580 `("-f" ,spam-spamoracle-database "mark")
2581 '("mark")))))
2582 (if (eq 0 status)
2583 (progn
2584 (set-buffer temp-buffer-name)
2585 (goto-char (point-min))
2586 (when (re-search-forward "^X-Spam: yes;" nil t)
2587 spam-split-group))
2588 (error "Error running spamoracle: %s" status))))))))
2590 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2591 "Run spamoracle in training mode."
2592 (with-temp-buffer
2593 (let ((temp-buffer-name (buffer-name)))
2594 (save-excursion
2595 (goto-char (point-min))
2596 (dolist (article articles)
2597 (insert (spam-get-article-as-string article)))
2598 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2599 "-spam"
2600 "-good"))
2601 (status
2602 (apply 'call-process-region
2603 (point-min) (point-max)
2604 spam-spamoracle-binary
2605 nil temp-buffer-name nil
2606 (if spam-spamoracle-database
2607 `("-f" ,spam-spamoracle-database
2608 "add" ,arg)
2609 `("add" ,arg)))))
2610 (unless (eq 0 status)
2611 (error "Error running spamoracle: %s" status)))))))
2613 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2614 (spam-spamoracle-learn articles nil unregister))
2616 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2617 (spam-spamoracle-learn-ham articles t))
2619 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2620 (spam-spamoracle-learn articles t unregister))
2622 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2623 (spam-spamoracle-learn-spam articles t))
2625 ;;}}}
2627 ;;{{{ SpamAssassin
2628 ;;; based mostly on the bogofilter code
2629 (defun spam-check-spamassassin-headers (&optional score)
2630 "Check the SpamAssassin headers for the classification of this message."
2631 (if score ; scoring mode
2632 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2633 (when header
2634 (if (string-match spam-spamassassin-score-regexp header)
2635 (match-string 1 header)
2636 "0")))
2637 ;; spam detection mode
2638 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2639 (when header ; return nil when no header
2640 (when (string-match spam-spamassassin-positive-spam-flag-header
2641 header)
2642 spam-split-group)))))
2644 (defun spam-check-spamassassin (&optional score)
2645 "Check the SpamAssassin backend for the classification of this message."
2646 (let ((article-buffer-name (buffer-name)))
2647 (with-temp-buffer
2648 (let ((temp-buffer-name (buffer-name)))
2649 (with-current-buffer article-buffer-name
2650 (apply 'call-process-region
2651 (point-min) (point-max) spam-assassin-program
2652 nil temp-buffer-name nil spam-spamassassin-arguments))
2653 ;; check the return now (we're back in the temp buffer)
2654 (goto-char (point-min))
2655 (spam-check-spamassassin-headers score)))))
2657 ;; return something sensible if the score can't be determined
2658 (defun spam-spamassassin-score (&optional recheck)
2659 "Get the SpamAssassin score"
2660 (interactive "P")
2661 (save-window-excursion
2662 (gnus-summary-show-article t)
2663 (set-buffer gnus-article-buffer)
2664 (let ((score (or (unless recheck
2665 (spam-check-spamassassin-headers t))
2666 (spam-check-spamassassin t))))
2667 (gnus-summary-show-article)
2668 (message "SpamAssassin score %s" score)
2669 (or score "0"))))
2671 (defun spam-spamassassin-register-with-sa-learn (articles spam
2672 &optional unregister)
2673 "Register articles with spamassassin's sa-learn as spam or non-spam."
2674 (if articles
2675 (let ((action (if unregister spam-sa-learn-unregister-switch
2676 (if spam spam-sa-learn-spam-switch
2677 spam-sa-learn-ham-switch)))
2678 (summary-buffer-name (buffer-name)))
2679 (with-temp-buffer
2680 ;; group the articles into mbox format
2681 (dolist (article articles)
2682 (let (article-string)
2683 (with-current-buffer summary-buffer-name
2684 (setq article-string (spam-get-article-as-string article)))
2685 (when (stringp article-string)
2686 ;; mbox separator
2687 (insert (concat "From nobody " (current-time-string) "\n"))
2688 (insert article-string)
2689 (insert "\n"))))
2690 ;; call sa-learn on all messages at the same time
2691 (apply 'call-process-region
2692 (point-min) (point-max)
2693 spam-sa-learn-program
2694 nil nil nil "--mbox"
2695 (if spam-sa-learn-rebuild
2696 (list action)
2697 `("--no-rebuild" ,action)))))))
2699 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2700 (spam-spamassassin-register-with-sa-learn articles t unregister))
2702 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2703 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2705 (defun spam-spamassassin-unregister-spam-routine (articles)
2706 (spam-spamassassin-register-with-sa-learn articles t t))
2708 (defun spam-spamassassin-unregister-ham-routine (articles)
2709 (spam-spamassassin-register-with-sa-learn articles nil t))
2711 ;;}}}
2713 ;;{{{ Bsfilter
2714 ;;; based mostly on the bogofilter code
2715 (defun spam-check-bsfilter-headers (&optional score)
2716 (if score
2717 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2718 "0")
2719 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2720 (when header ; return nil when no header
2721 (when (string-match "YES" header)
2722 spam-split-group)))))
2724 ;; return something sensible if the score can't be determined
2725 (defun spam-bsfilter-score (&optional recheck)
2726 "Get the Bsfilter spamicity score."
2727 (interactive "P")
2728 (save-window-excursion
2729 (gnus-summary-show-article t)
2730 (set-buffer gnus-article-buffer)
2731 (let ((score (or (unless recheck
2732 (spam-check-bsfilter-headers t))
2733 (spam-check-bsfilter t))))
2734 (gnus-summary-show-article)
2735 (message "Spamicity score %s" score)
2736 (or score "0"))))
2738 (defun spam-check-bsfilter (&optional score)
2739 "Check the Bsfilter backend for the classification of this message."
2740 (let ((article-buffer-name (buffer-name))
2741 (dir spam-bsfilter-database-directory)
2742 return)
2743 (with-temp-buffer
2744 (let ((temp-buffer-name (buffer-name)))
2745 (with-current-buffer article-buffer-name
2746 (apply 'call-process-region
2747 (point-min) (point-max)
2748 spam-bsfilter-program
2749 nil temp-buffer-name nil
2750 "--pipe"
2751 "--insert-flag"
2752 "--insert-probability"
2753 (when dir
2754 (list "--homedir" dir))))
2755 (setq return (spam-check-bsfilter-headers score))))
2756 return))
2758 (defun spam-bsfilter-register-with-bsfilter (articles
2759 spam
2760 &optional unregister)
2761 "Register an article, given as a string, as spam or non-spam."
2762 (dolist (article articles)
2763 (let ((article-string (spam-get-article-as-string article))
2764 (switch (if unregister
2765 (if spam
2766 spam-bsfilter-spam-strong-switch
2767 spam-bsfilter-ham-strong-switch)
2768 (if spam
2769 spam-bsfilter-spam-switch
2770 spam-bsfilter-ham-switch))))
2771 (when (stringp article-string)
2772 (with-temp-buffer
2773 (insert article-string)
2774 (apply 'call-process-region
2775 (point-min) (point-max)
2776 spam-bsfilter-program
2777 nil nil nil switch
2778 "--update"
2779 (when spam-bsfilter-database-directory
2780 (list "--homedir"
2781 spam-bsfilter-database-directory))))))))
2783 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2784 (spam-bsfilter-register-with-bsfilter articles t unregister))
2786 (defun spam-bsfilter-unregister-spam-routine (articles)
2787 (spam-bsfilter-register-spam-routine articles t))
2789 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2790 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2792 (defun spam-bsfilter-unregister-ham-routine (articles)
2793 (spam-bsfilter-register-ham-routine articles t))
2795 ;;}}}
2797 ;;{{{ CRM114 Mailfilter
2798 (defun spam-check-crm114-headers (&optional score)
2799 (let ((header (message-fetch-field spam-crm114-header)))
2800 (when header ; return nil when no header
2801 (if score ; scoring mode
2802 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2803 (match-string 1 header)
2804 "0")
2805 ;; spam detection mode
2806 (when (string-match spam-crm114-positive-spam-header
2807 header)
2808 spam-split-group)))))
2810 ;; return something sensible if the score can't be determined
2811 (defun spam-crm114-score ()
2812 "Get the CRM114 Mailfilter pR."
2813 (interactive)
2814 (save-window-excursion
2815 (gnus-summary-show-article t)
2816 (set-buffer gnus-article-buffer)
2817 (let ((score (or (spam-check-crm114-headers t)
2818 (spam-check-crm114 t))))
2819 (gnus-summary-show-article)
2820 (message "pR: %s" score)
2821 (or score "0"))))
2823 (defun spam-check-crm114 (&optional score)
2824 "Check the CRM114 Mailfilter backend for the classification of this message."
2825 (let ((article-buffer-name (buffer-name))
2826 (db spam-crm114-database-directory)
2827 return)
2828 (with-temp-buffer
2829 (let ((temp-buffer-name (buffer-name)))
2830 (with-current-buffer article-buffer-name
2831 (apply 'call-process-region
2832 (point-min) (point-max)
2833 spam-crm114-program
2834 nil temp-buffer-name nil
2835 (when db (list (concat "--fileprefix=" db)))))
2836 (setq return (spam-check-crm114-headers score))))
2837 return))
2839 (defun spam-crm114-register-with-crm114 (articles
2840 spam
2841 &optional unregister)
2842 "Register an article, given as a string, as spam or non-spam."
2843 (dolist (article articles)
2844 (let ((article-string (spam-get-article-as-string article))
2845 (db spam-crm114-database-directory)
2846 (switch (if unregister
2847 (if spam
2848 spam-crm114-spam-strong-switch
2849 spam-crm114-ham-strong-switch)
2850 (if spam
2851 spam-crm114-spam-switch
2852 spam-crm114-ham-switch))))
2853 (when (stringp article-string)
2854 (with-temp-buffer
2855 (insert article-string)
2857 (apply 'call-process-region
2858 (point-min) (point-max)
2859 spam-crm114-program
2860 nil nil nil
2861 (when db (list switch (concat "--fileprefix=" db)))))))))
2863 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2864 (spam-crm114-register-with-crm114 articles t unregister))
2866 (defun spam-crm114-unregister-spam-routine (articles)
2867 (spam-crm114-register-spam-routine articles t))
2869 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2870 (spam-crm114-register-with-crm114 articles nil unregister))
2872 (defun spam-crm114-unregister-ham-routine (articles)
2873 (spam-crm114-register-ham-routine articles t))
2875 ;;}}}
2877 ;;}}}
2879 ;;{{{ Hooks
2881 ;;;###autoload
2882 (defun spam-initialize (&rest symbols)
2883 "Install the spam.el hooks and do other initialization.
2884 When SYMBOLS is given, set those variables to t. This is so you
2885 can call `spam-initialize' before you set spam-use-* variables on
2886 explicitly, and matters only if you need the extra headers
2887 installed through `spam-necessary-extra-headers'."
2888 (interactive)
2890 (when spam-install-hooks
2891 (dolist (var symbols)
2892 (set var t))
2894 (dolist (header (spam-necessary-extra-headers))
2895 (add-to-list 'nnmail-extra-headers header)
2896 (add-to-list 'gnus-extra-headers header))
2898 ;; TODO: How do we redo this every time the `spam' face is customized?
2899 (push '((eq mark gnus-spam-mark) . spam)
2900 gnus-summary-highlight)
2901 ;; Add hooks for loading and saving the spam stats
2902 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2903 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2904 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2905 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2906 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2907 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2908 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam)
2909 ;; Don't install things more than once.
2910 (setq spam-install-hooks nil)))
2912 (defun spam-unload-hook ()
2913 "Uninstall the spam.el hooks."
2914 (interactive)
2915 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2916 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2917 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2918 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2919 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2920 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2921 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2923 (add-hook 'spam-unload-hook 'spam-unload-hook)
2925 ;;}}}
2927 (provide 'spam)
2929 ;;; spam.el ends here