Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / gnus / spam.el
blob710e0e83cf9f2434a75c6bf0211ca72605ae5684
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 (require 'message) ;for the message-fetch-field functions
42 (require 'gnus-sum)
43 (require 'gnus-uu) ; because of key prefix issues
44 ;;; for the definitions of group content classification and spam processors
45 (require 'gnus)
47 (eval-when-compile (require 'hashcash))
49 ;; for nnimap-split-download-body-default
50 (eval-when-compile (require 'nnimap))
52 (eval-when-compile (require 'cl-lib))
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 (defcustom spam-ifile-program (executable-find "ifile")
412 "Name of the ifile program."
413 :type '(choice (file :tag "Location of ifile")
414 (const :tag "ifile is not installed"))
415 :group 'spam-ifile)
417 (defcustom spam-ifile-database nil
418 "File name of the ifile database."
419 :type '(choice (file :tag "Location of the ifile database")
420 (const :tag "Use the default"))
421 :group 'spam-ifile)
423 (defcustom spam-ifile-spam-category "spam"
424 "Name of the spam ifile category."
425 :type 'string
426 :group 'spam-ifile)
428 (defcustom spam-ifile-ham-category nil
429 "Name of the ham ifile category.
430 If nil, the current group name will be used."
431 :type '(choice (string :tag "Use a fixed category")
432 (const :tag "Use the current group name"))
433 :group 'spam-ifile)
435 (defcustom spam-ifile-all-categories nil
436 "Whether the ifile check will return all categories, or just spam.
437 Set this to t if you want to use the `spam-split' invocation of ifile as
438 your main source of newsgroup names."
439 :type 'boolean
440 :group 'spam-ifile)
442 (defgroup spam-bogofilter nil
443 "Spam bogofilter configuration."
444 :group 'spam)
446 (defcustom spam-bogofilter-program (executable-find "bogofilter")
447 "Name of the Bogofilter program."
448 :type '(choice (file :tag "Location of bogofilter")
449 (const :tag "Bogofilter is not installed"))
450 :group 'spam-bogofilter)
452 (defvar spam-bogofilter-valid 'unknown "Is the bogofilter version valid?")
454 (defcustom spam-bogofilter-header "X-Bogosity"
455 "The header that Bogofilter inserts in messages."
456 :type 'string
457 :group 'spam-bogofilter)
459 (defcustom spam-bogofilter-spam-switch "-s"
460 "The switch that Bogofilter uses to register spam messages."
461 :type 'string
462 :group 'spam-bogofilter)
464 (defcustom spam-bogofilter-ham-switch "-n"
465 "The switch that Bogofilter uses to register ham messages."
466 :type 'string
467 :group 'spam-bogofilter)
469 (defcustom spam-bogofilter-spam-strong-switch "-S"
470 "The switch that Bogofilter uses to unregister ham messages."
471 :type 'string
472 :group 'spam-bogofilter)
474 (defcustom spam-bogofilter-ham-strong-switch "-N"
475 "The switch that Bogofilter uses to unregister spam messages."
476 :type 'string
477 :group 'spam-bogofilter)
479 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
480 "The regex on `spam-bogofilter-header' for positive spam identification."
481 :type 'regexp
482 :group 'spam-bogofilter)
484 (defcustom spam-bogofilter-database-directory nil
485 "Location of the Bogofilter database.
486 When nil, use the default location."
487 :type '(choice (directory
488 :tag "Location of the Bogofilter database directory")
489 (const :tag "Use the default"))
490 :group 'spam-bogofilter)
492 (defgroup spam-bsfilter nil
493 "Spam bsfilter configuration."
494 :group 'spam)
496 (defcustom spam-bsfilter-program (executable-find "bsfilter")
497 "Name of the Bsfilter program."
498 :type '(choice (file :tag "Location of bsfilter")
499 (const :tag "Bsfilter is not installed"))
500 :group 'spam-bsfilter)
502 (defcustom spam-bsfilter-header "X-Spam-Flag"
503 "The header inserted by Bsfilter to flag spam."
504 :type 'string
505 :group 'spam-bsfilter)
507 (defcustom spam-bsfilter-probability-header "X-Spam-Probability"
508 "The header that Bsfilter inserts in messages."
509 :type 'string
510 :group 'spam-bsfilter)
512 (defcustom spam-bsfilter-spam-switch "--add-spam"
513 "The switch that Bsfilter uses to register spam messages."
514 :type 'string
515 :group 'spam-bsfilter)
517 (defcustom spam-bsfilter-ham-switch "--add-clean"
518 "The switch that Bsfilter uses to register ham messages."
519 :type 'string
520 :group 'spam-bsfilter)
522 (defcustom spam-bsfilter-spam-strong-switch "--sub-spam"
523 "The switch that Bsfilter uses to unregister ham messages."
524 :type 'string
525 :group 'spam-bsfilter)
527 (defcustom spam-bsfilter-ham-strong-switch "--sub-clean"
528 "The switch that Bsfilter uses to unregister spam messages."
529 :type 'string
530 :group 'spam-bsfilter)
532 (defcustom spam-bsfilter-database-directory nil
533 "Directory path of the Bsfilter databases."
534 :type '(choice (directory
535 :tag "Location of the Bsfilter database directory")
536 (const :tag "Use the default"))
537 :group 'spam-bsfilter)
539 (defgroup spam-spamoracle nil
540 "Spam spamoracle configuration."
541 :group 'spam)
543 (defcustom spam-spamoracle-database nil
544 "Location of spamoracle database file.
545 When nil, use the default spamoracle database."
546 :type '(choice (directory :tag "Location of spamoracle database file.")
547 (const :tag "Use the default"))
548 :group 'spam-spamoracle)
550 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
551 "Location of the spamoracle binary."
552 :type '(choice (directory :tag "Location of the spamoracle binary")
553 (const :tag "Use the default"))
554 :group 'spam-spamoracle)
556 (defgroup spam-spamassassin nil
557 "Spam SpamAssassin configuration."
558 :group 'spam)
560 (defcustom spam-assassin-program (executable-find "spamassassin")
561 "Name of the spamassassin program.
562 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
563 spamd man pages for more information on these programs."
564 :type '(choice (file :tag "Location of spamc")
565 (const :tag "spamassassin is not installed"))
566 :group 'spam-spamassassin)
568 (defcustom spam-spamassassin-arguments ()
569 "Arguments to pass to the spamassassin executable.
570 This must be a list. For example, `(\"-C\" \"configfile\")'."
571 :type '(restricted-sexp :match-alternatives (listp))
572 :group 'spam-spamassassin)
574 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
575 "The header inserted by SpamAssassin to flag spam."
576 :type 'string
577 :group 'spam-spamassassin)
579 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
580 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
581 identification"
582 :type 'string
583 :group 'spam-spamassassin)
585 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
586 "The header inserted by SpamAssassin, giving extended scoring information"
587 :type 'string
588 :group 'spam-spamassassin)
590 (defcustom spam-sa-learn-program (executable-find "sa-learn")
591 "Name of the sa-learn program."
592 :type '(choice (file :tag "Location of spamassassin")
593 (const :tag "spamassassin is not installed"))
594 :group 'spam-spamassassin)
596 (defcustom spam-sa-learn-rebuild t
597 "Whether sa-learn should rebuild the database every time it is called
598 Enable this if you want sa-learn to rebuild the database automatically. Doing
599 this will slightly increase the running time of the spam registration process.
600 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
601 order for SpamAssassin to recognize the new registered spam."
602 :type 'boolean
603 :group 'spam-spamassassin)
605 (defcustom spam-sa-learn-spam-switch "--spam"
606 "The switch that sa-learn uses to register spam messages."
607 :type 'string
608 :group 'spam-spamassassin)
610 (defcustom spam-sa-learn-ham-switch "--ham"
611 "The switch that sa-learn uses to register ham messages."
612 :type 'string
613 :group 'spam-spamassassin)
615 (defcustom spam-sa-learn-unregister-switch "--forget"
616 "The switch that sa-learn uses to unregister messages."
617 :type 'string
618 :group 'spam-spamassassin)
620 (defgroup spam-crm114 nil
621 "Spam CRM114 Mailfilter configuration."
622 :group 'spam)
624 (defcustom spam-crm114-program (executable-find "mailfilter.crm")
625 "File path of the CRM114 Mailfilter executable program."
626 :type '(choice (file :tag "Location of CRM114 Mailfilter")
627 (const :tag "CRM114 Mailfilter is not installed"))
628 :group 'spam-crm114)
630 (defcustom spam-crm114-header "X-CRM114-Status"
631 "The header that CRM114 Mailfilter inserts in messages."
632 :type 'string
633 :group 'spam-crm114)
635 (defcustom spam-crm114-spam-switch "--learnspam"
636 "The switch that CRM114 Mailfilter uses to register spam messages."
637 :type 'string
638 :group 'spam-crm114)
640 (defcustom spam-crm114-ham-switch "--learnnonspam"
641 "The switch that CRM114 Mailfilter uses to register ham messages."
642 :type 'string
643 :group 'spam-crm114)
645 (defcustom spam-crm114-spam-strong-switch "--unlearn"
646 "The switch that CRM114 Mailfilter uses to unregister ham messages."
647 :type 'string
648 :group 'spam-crm114)
650 (defcustom spam-crm114-ham-strong-switch "--unlearn"
651 "The switch that CRM114 Mailfilter uses to unregister spam messages."
652 :type 'string
653 :group 'spam-crm114)
655 (defcustom spam-crm114-positive-spam-header "^SPAM"
656 "The regex on `spam-crm114-header' for positive spam identification."
657 :type 'regexp
658 :group 'spam-crm114)
660 (defcustom spam-crm114-database-directory nil
661 "Directory path of the CRM114 Mailfilter databases."
662 :type '(choice (directory
663 :tag "Location of the CRM114 Mailfilter database directory")
664 (const :tag "Use the default"))
665 :group 'spam-crm114)
667 ;;; Key bindings for spam control.
669 (gnus-define-keys gnus-summary-mode-map
670 "St" spam-generic-score
671 "Sx" gnus-summary-mark-as-spam
672 "Mst" spam-generic-score
673 "Msx" gnus-summary-mark-as-spam
674 "\M-d" gnus-summary-mark-as-spam
675 "$" gnus-summary-mark-as-spam)
677 (defvar spam-cache-lookups t
678 "Whether spam.el will try to cache lookups using `spam-caches'.")
680 (defvar spam-caches (make-hash-table
681 :size 10
682 :test 'equal)
683 "Cache of spam detection entries.")
685 (defvar spam-old-articles nil
686 "List of old ham and spam articles, generated when a group is entered.")
688 (defvar spam-split-disabled nil
689 "If non-nil, `spam-split' is disabled, and always returns nil.")
691 (defvar spam-split-last-successful-check nil
692 "Internal variable.
693 `spam-split' will set this to nil or a spam-use-XYZ check if it
694 finds ham or spam.")
696 ;; internal variables for backends
697 ;; TODO: find a way to create these on the fly in spam-install-backend-super
698 (defvar spam-use-copy nil)
699 (defvar spam-use-move nil)
700 (defvar spam-use-gmane nil)
701 (defvar spam-use-resend nil)
703 ;;}}}
705 ;;{{{ convenience functions
707 (defun spam-clear-cache (symbol)
708 "Clear the `spam-caches' entry for a check."
709 (remhash symbol spam-caches))
711 (defun spam-xor (a b)
712 "Logical A xor B."
713 (and (or a b) (not (and a b))))
715 (defun spam-set-difference (list1 list2)
716 "Return a set difference of LIST1 and LIST2.
717 When either list is nil, the other is returned."
718 (if (and list1 list2)
719 ;; we have two non-nil lists
720 (progn
721 (dolist (item (append list1 list2))
722 (when (and (memq item list1) (memq item list2))
723 (setq list1 (delq item list1))
724 (setq list2 (delq item list2))))
725 (append list1 list2))
726 ;; if either of the lists was nil, return the other one
727 (if list1 list1 list2)))
729 (defun spam-group-ham-mark-p (group mark &optional spam)
730 "Checks if MARK is considered a ham mark in GROUP."
731 (when (stringp group)
732 (let* ((marks (spam-group-ham-marks group spam))
733 (marks (if (symbolp mark)
734 marks
735 (mapcar 'symbol-value marks))))
736 (memq mark marks))))
738 (defun spam-group-spam-mark-p (group mark)
739 "Checks if MARK is considered a spam mark in GROUP."
740 (spam-group-ham-mark-p group mark t))
742 (defun spam-group-ham-marks (group &optional spam)
743 "In GROUP, get all the ham marks."
744 (when (stringp group)
745 (let* ((marks (if spam
746 (gnus-parameter-spam-marks group)
747 (gnus-parameter-ham-marks group)))
748 (marks (car marks))
749 (marks (if (listp (car marks)) (car marks) marks)))
750 marks)))
752 (defun spam-group-spam-marks (group)
753 "In GROUP, get all the spam marks."
754 (spam-group-ham-marks group t))
756 (defun spam-group-spam-contents-p (group)
757 "Is GROUP a spam group?"
758 (if (and (stringp group) (< 0 (length group)))
759 (or (member group spam-junk-mailgroups)
760 (memq 'gnus-group-spam-classification-spam
761 (gnus-parameter-spam-contents group)))
762 nil))
764 (defun spam-group-ham-contents-p (group)
765 "Is GROUP a ham group?"
766 (if (stringp group)
767 (memq 'gnus-group-spam-classification-ham
768 (gnus-parameter-spam-contents group))
769 nil))
771 (defun spam-classifications ()
772 "Return list of valid classifications"
773 '(spam ham))
775 (defun spam-classification-valid-p (classification)
776 "Is CLASSIFICATION a valid spam/ham classification?"
777 (memq classification (spam-classifications)))
779 (defun spam-backend-properties ()
780 "Return list of valid classifications."
781 '(statistical mover check hrf srf huf suf))
783 (defun spam-backend-property-valid-p (property)
784 "Is PROPERTY a valid backend property?"
785 (memq property (spam-backend-properties)))
787 (defun spam-backend-function-type-valid-p (type)
788 (or (eq type 'registration)
789 (eq type 'unregistration)))
791 (defun spam-process-type-valid-p (process-type)
792 (or (eq process-type 'incoming)
793 (eq process-type 'process)))
795 (defun spam-list-articles (articles classification)
796 (let ((mark-check (if (eq classification 'spam)
797 'spam-group-spam-mark-p
798 'spam-group-ham-mark-p))
799 alist mark-cache-yes mark-cache-no)
800 (dolist (article articles)
801 (let ((mark (gnus-summary-article-mark article)))
802 (unless (or (memq mark mark-cache-yes)
803 (memq mark mark-cache-no))
804 (if (funcall mark-check
805 gnus-newsgroup-name
806 mark)
807 (push mark mark-cache-yes)
808 (push mark mark-cache-no)))
809 (when (memq mark mark-cache-yes)
810 (push article alist))))
811 alist))
813 ;;}}}
815 ;;{{{ backend installation functions and procedures
817 (defun spam-install-backend-super (backend &rest properties)
818 "Install BACKEND for spam.el.
819 Accepts incoming CHECK, ham registration function HRF, spam
820 registration function SRF, ham unregistration function HUF, spam
821 unregistration function SUF, and an indication whether the
822 backend is STATISTICAL."
823 (setq spam-backends (add-to-list 'spam-backends backend))
824 (while properties
825 (let ((property (pop properties))
826 (value (pop properties)))
827 (if (spam-backend-property-valid-p property)
828 (put backend property value)
829 (gnus-error
831 "spam-install-backend-super got an invalid property %s"
832 property)))))
834 (defun spam-backend-list (&optional type)
835 "Return a list of all the backend symbols, constrained by TYPE.
836 When TYPE is 'non-mover, only non-mover backends are returned.
837 When TYPE is 'mover, only mover backends are returned."
838 (let (list)
839 (dolist (backend spam-backends)
840 (when (or
841 (null type) ;either no type was requested
842 ;; or the type is 'mover and the backend is a mover
843 (and
844 (eq type 'mover)
845 (spam-backend-mover-p backend))
846 ;; or the type is 'non-mover and the backend is not a mover
847 (and
848 (eq type 'non-mover)
849 (not (spam-backend-mover-p backend))))
850 (push backend list)))
851 list))
853 (defun spam-backend-check (backend)
854 "Get the check function for BACKEND.
855 Each individual check may return nil, t, or a mailgroup name.
856 The value nil means that the check does not yield a decision, and
857 so, that further checks are needed. The value t means that the
858 message is definitely not spam, and that further spam checks
859 should be inhibited. Otherwise, a mailgroup name or the symbol
860 'spam (depending on `spam-split-symbolic-return') is returned where
861 the mail should go, and further checks are also inhibited. The
862 usual mailgroup name is the value of `spam-split-group', meaning
863 that the message is definitely a spam."
864 (get backend 'check))
866 (defun spam-backend-valid-p (backend)
867 "Is BACKEND valid?"
868 (member backend (spam-backend-list)))
870 (defun spam-backend-info (backend)
871 "Return information about BACKEND."
872 (if (spam-backend-valid-p backend)
873 (let (info)
874 (setq info (format "Backend %s has the following properties:\n"
875 backend))
876 (dolist (property (spam-backend-properties))
877 (setq info (format "%s%s=%s\n"
878 info
879 property
880 (get backend property))))
881 info)
882 (gnus-error 5 "spam-backend-info was asked about an invalid backend %s"
883 backend)))
885 (defun spam-backend-function (backend classification type)
886 "Get the BACKEND function for CLASSIFICATION and TYPE.
887 TYPE is 'registration or 'unregistration.
888 CLASSIFICATION is 'ham or 'spam."
889 (if (and
890 (spam-classification-valid-p classification)
891 (spam-backend-function-type-valid-p type))
892 (let ((retrieval
893 (intern
894 (format "spam-backend-%s-%s-function"
895 classification
896 type))))
897 (funcall retrieval backend))
898 (gnus-error
900 "%s was passed invalid backend %s, classification %s, or type %s"
901 "spam-backend-function"
902 backend
903 classification
904 type)))
906 (defun spam-backend-article-list-property (classification
907 &optional unregister)
908 "Property name of article list with CLASSIFICATION and UNREGISTER."
909 (let* ((r (if unregister "unregister" "register"))
910 (prop (format "%s-%s" classification r)))
911 prop))
913 (defun spam-backend-get-article-todo-list (backend
914 classification
915 &optional unregister)
916 "Get the articles to be processed for BACKEND and CLASSIFICATION.
917 With UNREGISTER, get articles to be unregistered.
918 This is a temporary storage function - nothing here persists."
919 (get
920 backend
921 (intern (spam-backend-article-list-property classification unregister))))
923 (defun spam-backend-put-article-todo-list (backend classification list
924 &optional unregister)
925 "Set the LIST of articles to be processed for BACKEND and CLASSIFICATION.
926 With UNREGISTER, set articles to be unregistered.
927 This is a temporary storage function - nothing here persists."
928 (put
929 backend
930 (intern (spam-backend-article-list-property classification unregister))
931 list))
933 (defun spam-backend-ham-registration-function (backend)
934 "Get the ham registration function for BACKEND."
935 (get backend 'hrf))
937 (defun spam-backend-spam-registration-function (backend)
938 "Get the spam registration function for BACKEND."
939 (get backend 'srf))
941 (defun spam-backend-ham-unregistration-function (backend)
942 "Get the ham unregistration function for BACKEND."
943 (get backend 'huf))
945 (defun spam-backend-spam-unregistration-function (backend)
946 "Get the spam unregistration function for BACKEND."
947 (get backend 'suf))
949 (defun spam-backend-statistical-p (backend)
950 "Is BACKEND statistical?"
951 (get backend 'statistical))
953 (defun spam-backend-mover-p (backend)
954 "Is BACKEND a mover?"
955 (get backend 'mover))
957 (defun spam-install-backend-alias (backend alias)
958 "Add ALIAS to an existing BACKEND.
959 The previous backend settings for ALIAS are erased."
961 ;; install alias with no properties at first
962 (spam-install-backend-super alias)
964 (dolist (property (spam-backend-properties))
965 (put alias property (get backend property))))
967 (defun spam-install-checkonly-backend (backend check)
968 "Install a BACKEND than can only CHECK for spam."
969 (spam-install-backend-super backend 'check check))
971 (defun spam-install-mover-backend (backend hrf srf huf suf)
972 "Install a BACKEND than can move articles at summary exit.
973 Accepts ham registration function HRF, spam registration function
974 SRF, ham unregistration function HUF, spam unregistration
975 function SUF. The backend has no incoming check and can't be
976 statistical."
977 (spam-install-backend-super
978 backend
979 'hrf hrf 'srf srf 'huf huf 'suf suf 'mover t))
981 (defun spam-install-nocheck-backend (backend hrf srf huf suf)
982 "Install a BACKEND than has no check.
983 Accepts ham registration function HRF, spam registration function
984 SRF, ham unregistration function HUF, spam unregistration
985 function SUF. The backend has no incoming check and can't be
986 statistical (it could be, but in practice that doesn't happen)."
987 (spam-install-backend-super
988 backend
989 'hrf hrf 'srf srf 'huf huf 'suf suf))
991 (defun spam-install-backend (backend check hrf srf huf suf)
992 "Install a BACKEND.
993 Accepts incoming CHECK, ham registration function HRF, spam
994 registration function SRF, ham unregistration function HUF, spam
995 unregistration function SUF. The backend won't be
996 statistical (use `spam-install-statistical-backend' for that)."
997 (spam-install-backend-super
998 backend
999 'check check 'hrf hrf 'srf srf 'huf huf 'suf suf))
1001 (defun spam-install-statistical-backend (backend check hrf srf huf suf)
1002 "Install a BACKEND.
1003 Accepts incoming CHECK, ham registration function HRF, spam
1004 registration function SRF, ham unregistration function HUF, spam
1005 unregistration function SUF. The backend will be
1006 statistical (use `spam-install-backend' for non-statistical
1007 backends)."
1008 (spam-install-backend-super
1009 backend
1010 'check check 'statistical t 'hrf hrf 'srf srf 'huf huf 'suf suf))
1012 (defun spam-install-statistical-checkonly-backend (backend check)
1013 "Install a statistical BACKEND than can only CHECK for spam."
1014 (spam-install-backend-super
1015 backend
1016 'check check 'statistical t))
1018 ;;}}}
1020 ;;{{{ backend installations
1021 (spam-install-checkonly-backend 'spam-use-blackholes
1022 'spam-check-blackholes)
1024 (spam-install-checkonly-backend 'spam-use-hashcash
1025 'spam-check-hashcash)
1027 (spam-install-checkonly-backend 'spam-use-spamassassin-headers
1028 'spam-check-spamassassin-headers)
1030 (spam-install-checkonly-backend 'spam-use-bogofilter-headers
1031 'spam-check-bogofilter-headers)
1033 (spam-install-checkonly-backend 'spam-use-bsfilter-headers
1034 'spam-check-bsfilter-headers)
1036 (spam-install-checkonly-backend 'spam-use-gmane-xref
1037 'spam-check-gmane-xref)
1039 (spam-install-checkonly-backend 'spam-use-regex-headers
1040 'spam-check-regex-headers)
1042 (spam-install-statistical-checkonly-backend 'spam-use-regex-body
1043 'spam-check-regex-body)
1045 ;; TODO: NOTE: spam-use-ham-copy is now obsolete, use (ham spam-use-copy)
1046 (spam-install-mover-backend 'spam-use-move
1047 'spam-move-ham-routine
1048 'spam-move-spam-routine
1050 nil)
1052 (spam-install-nocheck-backend 'spam-use-copy
1053 'spam-copy-ham-routine
1054 'spam-copy-spam-routine
1056 nil)
1058 (spam-install-nocheck-backend 'spam-use-gmane
1059 'spam-report-gmane-unregister-routine
1060 'spam-report-gmane-register-routine
1061 'spam-report-gmane-register-routine
1062 'spam-report-gmane-unregister-routine)
1064 (spam-install-nocheck-backend 'spam-use-resend
1065 'spam-report-resend-register-ham-routine
1066 'spam-report-resend-register-routine
1068 nil)
1070 (spam-install-backend 'spam-use-BBDB
1071 'spam-check-BBDB
1072 'spam-BBDB-register-routine
1074 'spam-BBDB-unregister-routine
1075 nil)
1077 (spam-install-backend-alias 'spam-use-BBDB 'spam-use-BBDB-exclusive)
1079 (spam-install-backend 'spam-use-blacklist
1080 'spam-check-blacklist
1082 'spam-blacklist-register-routine
1084 'spam-blacklist-unregister-routine)
1086 (spam-install-backend 'spam-use-whitelist
1087 'spam-check-whitelist
1088 'spam-whitelist-register-routine
1090 'spam-whitelist-unregister-routine
1091 nil)
1093 (spam-install-statistical-backend 'spam-use-ifile
1094 'spam-check-ifile
1095 'spam-ifile-register-ham-routine
1096 'spam-ifile-register-spam-routine
1097 'spam-ifile-unregister-ham-routine
1098 'spam-ifile-unregister-spam-routine)
1100 (spam-install-statistical-backend 'spam-use-spamoracle
1101 'spam-check-spamoracle
1102 'spam-spamoracle-learn-ham
1103 'spam-spamoracle-learn-spam
1104 'spam-spamoracle-unlearn-ham
1105 'spam-spamoracle-unlearn-spam)
1107 (spam-install-statistical-backend 'spam-use-stat
1108 'spam-check-stat
1109 'spam-stat-register-ham-routine
1110 'spam-stat-register-spam-routine
1111 'spam-stat-unregister-ham-routine
1112 'spam-stat-unregister-spam-routine)
1114 (spam-install-statistical-backend 'spam-use-spamassassin
1115 'spam-check-spamassassin
1116 'spam-spamassassin-register-ham-routine
1117 'spam-spamassassin-register-spam-routine
1118 'spam-spamassassin-unregister-ham-routine
1119 'spam-spamassassin-unregister-spam-routine)
1121 (spam-install-statistical-backend 'spam-use-bogofilter
1122 'spam-check-bogofilter
1123 'spam-bogofilter-register-ham-routine
1124 'spam-bogofilter-register-spam-routine
1125 'spam-bogofilter-unregister-ham-routine
1126 'spam-bogofilter-unregister-spam-routine)
1128 (spam-install-statistical-backend 'spam-use-bsfilter
1129 'spam-check-bsfilter
1130 'spam-bsfilter-register-ham-routine
1131 'spam-bsfilter-register-spam-routine
1132 'spam-bsfilter-unregister-ham-routine
1133 'spam-bsfilter-unregister-spam-routine)
1135 (spam-install-statistical-backend 'spam-use-crm114
1136 'spam-check-crm114
1137 'spam-crm114-register-ham-routine
1138 'spam-crm114-register-spam-routine
1139 'spam-crm114-unregister-ham-routine
1140 'spam-crm114-unregister-spam-routine)
1141 ;;}}}
1143 ;;{{{ scoring and summary formatting
1144 (defun spam-necessary-extra-headers ()
1145 "Return the extra headers spam.el thinks are necessary."
1146 (let (list)
1147 (when (or spam-use-spamassassin
1148 spam-use-spamassassin-headers
1149 spam-use-regex-headers)
1150 (push 'X-Spam-Status list))
1151 (when (or spam-use-bogofilter
1152 spam-use-regex-headers)
1153 (push 'X-Bogosity list))
1154 (when (or spam-use-crm114
1155 spam-use-regex-headers)
1156 (push 'X-CRM114-Status list))
1157 list))
1159 (defun spam-user-format-function-S (headers)
1160 (when headers
1161 (format "%3.2f"
1162 (spam-summary-score headers spam-summary-score-preferred-header))))
1164 (defun spam-article-sort-by-spam-status (h1 h2)
1165 "Sort articles by score."
1166 (let (result)
1167 (cl-dolist (header (spam-necessary-extra-headers))
1168 (let ((s1 (spam-summary-score h1 header))
1169 (s2 (spam-summary-score h2 header)))
1170 (unless (= s1 s2)
1171 (setq result (< s1 s2))
1172 (cl-return))))
1173 result))
1175 (defvar spam-spamassassin-score-regexp
1176 ".*\\b\\(?:score\\|hits\\)=\\(-?[0-9.]+\\)"
1177 "Regexp matching SpamAssassin score header.
1178 The first group must match the number.")
1180 (defun spam-extra-header-to-number (header headers)
1181 "Transform an extra HEADER to a number, using list of HEADERS.
1182 Note this has to be fast."
1183 (let ((header-content (gnus-extra-header header headers)))
1184 (if header-content
1185 (cond
1186 ((eq header 'X-Spam-Status)
1187 (string-to-number (replace-regexp-in-string
1188 spam-spamassassin-score-regexp
1189 "\\1"
1190 header-content)))
1191 ;; for CRM checking, it's probably faster to just do the string match
1192 ((string-match "( pR: \\([0-9.-]+\\)" header-content)
1193 (- (string-to-number (match-string 1 header-content))))
1194 ((eq header 'X-Bogosity)
1195 (string-to-number (replace-regexp-in-string
1196 ",.*" ""
1197 (replace-regexp-in-string
1198 ".*spamicity=" ""
1199 header-content))))
1200 (t nil))
1201 nil)))
1203 (defun spam-summary-score (headers &optional specific-header)
1204 "Score an article for the summary buffer, as fast as possible.
1205 With SPECIFIC-HEADER, returns only that header's score.
1206 Will not return a nil score."
1207 (let (score)
1208 (cl-dolist (header
1209 (if specific-header
1210 (list specific-header)
1211 (spam-necessary-extra-headers)))
1212 (setq score
1213 (spam-extra-header-to-number header headers))
1214 (when score
1215 (cl-return)))
1216 (or score 0)))
1218 (defun spam-generic-score (&optional recheck)
1219 "Invoke whatever scoring method we can."
1220 (interactive "P")
1221 (cond
1222 ((or spam-use-spamassassin spam-use-spamassassin-headers)
1223 (spam-spamassassin-score recheck))
1224 ((or spam-use-bsfilter spam-use-bsfilter-headers)
1225 (spam-bsfilter-score recheck))
1226 (spam-use-crm114
1227 (spam-crm114-score))
1228 (t (spam-bogofilter-score recheck))))
1229 ;;}}}
1231 ;;{{{ set up widening, processor checks
1233 ;;; set up IMAP widening if it's necessary
1234 (defun spam-setup-widening ()
1235 (when (spam-widening-needed-p)
1236 (setq nnimap-split-download-body-default t)))
1238 (defun spam-widening-needed-p (&optional force-symbols)
1239 (let (found)
1240 (dolist (backend (spam-backend-list))
1241 (when (and (spam-backend-statistical-p backend)
1242 (or (symbol-value backend)
1243 (memq backend force-symbols)))
1244 (setq found backend)))
1245 found))
1247 (defun spam-group-processor-p (group backend &optional classification)
1248 "Checks if GROUP has a BACKEND with CLASSIFICATION registered.
1249 In the case of mover backends, checks the setting of
1250 `spam-summary-exit-behavior' in addition to the set values for the group."
1251 (if (and (stringp group)
1252 (symbolp backend))
1253 (let ((parameters (nth 0 (gnus-parameter-spam-process group)))
1254 found)
1255 ;; now search for the parameter
1256 (dolist (parameter parameters)
1257 (when (and (null found)
1258 (listp parameter)
1259 (eq classification (nth 0 parameter))
1260 (eq backend (nth 1 parameter)))
1261 (setq found t)))
1263 ;; now, if the parameter was not found, do the
1264 ;; spam-summary-exit-behavior-logic for mover backends
1265 (unless found
1266 (when (spam-backend-mover-p backend)
1267 (setq
1268 found
1269 (cond
1270 ((eq spam-summary-exit-behavior 'move-all) t)
1271 ((eq spam-summary-exit-behavior 'move-none) nil)
1272 ((eq spam-summary-exit-behavior 'default)
1273 (or (eq classification 'spam) ;move spam out of all groups
1274 ;; move ham out of spam groups
1275 (and (eq classification 'ham)
1276 (spam-group-spam-contents-p group))))
1277 (t (gnus-error 5 "Unknown spam-summary-exit-behavior: %s"
1278 spam-summary-exit-behavior))))))
1280 found)
1281 nil))
1283 ;;}}}
1285 ;;{{{ Summary entry and exit processing.
1287 (defun spam-mark-junk-as-spam-routine ()
1288 ;; check the global list of group names spam-junk-mailgroups and the
1289 ;; group parameters
1290 (when (spam-group-spam-contents-p gnus-newsgroup-name)
1291 (gnus-message 6 "Marking %s articles as spam"
1292 (if spam-mark-only-unseen-as-spam
1293 "unseen"
1294 "unread"))
1295 (let ((articles (if spam-mark-only-unseen-as-spam
1296 gnus-newsgroup-unseen
1297 gnus-newsgroup-unreads)))
1298 (if spam-mark-new-messages-in-spam-group-as-spam
1299 (dolist (article articles)
1300 (gnus-summary-mark-article article gnus-spam-mark))
1301 (gnus-message 9 "Did not mark new messages as spam.")))))
1303 (defun spam-summary-prepare ()
1304 (setq spam-old-articles
1305 (list (cons 'ham (spam-list-articles gnus-newsgroup-articles 'ham))
1306 (cons 'spam (spam-list-articles gnus-newsgroup-articles 'spam))))
1307 (spam-mark-junk-as-spam-routine))
1309 ;; The spam processors are invoked for any group, spam or ham or neither
1310 (defun spam-summary-prepare-exit ()
1311 (unless gnus-group-is-exiting-without-update-p
1312 (gnus-message 6 "Exiting summary buffer and applying spam rules")
1314 ;; before we begin, remove any article limits
1315 ; (ignore-errors
1316 ; (gnus-summary-pop-limit t))
1318 ;; first of all, unregister any articles that are no longer ham or spam
1319 ;; we have to iterate over the processors, or else we'll be too slow
1320 (dolist (classification (spam-classifications))
1321 (let* ((old-articles (cdr-safe (assq classification spam-old-articles)))
1322 (new-articles (spam-list-articles
1323 gnus-newsgroup-articles
1324 classification))
1325 (changed-articles (spam-set-difference new-articles old-articles)))
1326 ;; now that we have the changed articles, we go through the processors
1327 (dolist (backend (spam-backend-list))
1328 (let (unregister-list)
1329 (dolist (article changed-articles)
1330 (let ((id (spam-fetch-field-message-id-fast article)))
1331 (when (spam-log-unregistration-needed-p
1332 id 'process classification backend)
1333 (push article unregister-list))))
1334 ;; call spam-register-routine with specific articles to unregister,
1335 ;; when there are articles to unregister and the check is enabled
1336 (when (and unregister-list (symbol-value backend))
1337 (spam-backend-put-article-todo-list backend
1338 classification
1339 unregister-list
1340 t))))))
1342 ;; do the non-moving backends first, then the moving ones
1343 (dolist (backend-type '(non-mover mover))
1344 (dolist (classification (spam-classifications))
1345 (dolist (backend (spam-backend-list backend-type))
1346 (when (spam-group-processor-p
1347 gnus-newsgroup-name
1348 backend
1349 classification)
1350 (spam-backend-put-article-todo-list backend
1351 classification
1352 (spam-list-articles
1353 gnus-newsgroup-articles
1354 classification))))))
1356 (spam-resolve-registrations-routine) ; do the registrations now
1358 ;; we mark all the leftover spam articles as expired at the end
1359 (dolist (article (spam-list-articles
1360 gnus-newsgroup-articles
1361 'spam))
1362 (gnus-summary-mark-article article gnus-expirable-mark)))
1364 (setq spam-old-articles nil))
1366 ;;}}}
1368 ;;{{{ spam-use-move and spam-use-copy backend support functions
1370 (defun spam-copy-or-move-routine (copy groups articles classification)
1372 (when (and (car-safe groups) (listp (car-safe groups)))
1373 (setq groups (pop groups)))
1375 (unless (listp groups)
1376 (setq groups (list groups)))
1378 ;; remove the current process mark
1379 (gnus-summary-kill-process-mark)
1381 (let ((backend-supports-deletions
1382 (gnus-check-backend-function
1383 'request-move-article gnus-newsgroup-name))
1384 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
1385 article mark deletep respool valid-move-destinations)
1387 (when (member 'respool groups)
1388 (setq respool t) ; boolean for later
1389 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
1391 ;; exclude invalid move destinations
1392 (dolist (group groups)
1393 (unless
1395 (and
1396 (eq classification 'spam)
1397 (spam-group-spam-contents-p gnus-newsgroup-name)
1398 (spam-group-spam-contents-p group)
1399 (gnus-message
1401 "Sorry, can't move spam from spam group %s to spam group %s"
1402 gnus-newsgroup-name
1403 group))
1404 (and
1405 (eq classification 'ham)
1406 (spam-group-ham-contents-p gnus-newsgroup-name)
1407 (spam-group-ham-contents-p group)
1408 (gnus-message
1410 "Sorry, can't move ham from ham group %s to ham group %s"
1411 gnus-newsgroup-name
1412 group)))
1413 (push group valid-move-destinations)))
1415 (setq groups (nreverse valid-move-destinations))
1417 ;; now do the actual move
1418 (dolist (group groups)
1420 (when (and articles (stringp group))
1422 ;; first, mark the article with the process mark and, if needed,
1423 ;; the unread or expired mark (for ham and spam respectively)
1424 (dolist (article articles)
1425 (when (and (eq classification 'ham)
1426 spam-mark-ham-unread-before-move-from-spam-group)
1427 (gnus-message 9 "Marking ham article %d unread before move"
1428 article)
1429 (gnus-summary-mark-article article gnus-unread-mark))
1430 (when (and (eq classification 'spam)
1431 (not copy))
1432 (gnus-message 9 "Marking spam article %d expirable before move"
1433 article)
1434 (gnus-summary-mark-article article gnus-expirable-mark))
1435 (gnus-summary-set-process-mark article)
1437 (if respool ; respooling is with a "fake" group
1438 (let ((spam-split-disabled
1439 (or spam-split-disabled
1440 (and (eq classification 'ham)
1441 spam-disable-spam-split-during-ham-respool))))
1442 (gnus-message 9 "Respooling article %d with method %s"
1443 article respool-method)
1444 (gnus-summary-respool-article nil respool-method))
1445 ;; else, we are not respooling
1446 (if (or (not backend-supports-deletions)
1447 (> (length groups) 1))
1448 (progn ; if copying, copy and set deletep
1449 (gnus-message 9 "Copying article %d to group %s"
1450 article group)
1451 (gnus-summary-copy-article nil group)
1452 (setq deletep t))
1453 (gnus-message 9 "Moving article %d to group %s"
1454 article group)
1455 (gnus-summary-move-article nil group)))))) ; else move articles
1457 ;; now delete the articles, unless a) copy is t, and there was a copy done
1458 ;; b) a move was done to a single group
1459 ;; c) backend-supports-deletions is nil
1460 (unless copy
1461 (when (and deletep backend-supports-deletions)
1462 (dolist (article articles)
1463 (gnus-summary-set-process-mark article)
1464 (gnus-message 9 "Deleting article %d" article))
1465 (when articles
1466 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
1467 (gnus-summary-delete-article nil)))))
1468 (gnus-summary-yank-process-mark)
1469 (length articles)))
1471 (defun spam-copy-spam-routine (articles)
1472 (spam-copy-or-move-routine
1474 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1475 articles
1476 'spam))
1478 (defun spam-move-spam-routine (articles)
1479 (spam-copy-or-move-routine
1481 (gnus-parameter-spam-process-destination gnus-newsgroup-name)
1482 articles
1483 'spam))
1485 (defun spam-copy-ham-routine (articles)
1486 (spam-copy-or-move-routine
1488 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1489 articles
1490 'ham))
1492 (defun spam-move-ham-routine (articles)
1493 (spam-copy-or-move-routine
1495 (gnus-parameter-ham-process-destination gnus-newsgroup-name)
1496 articles
1497 'ham))
1499 ;;}}}
1501 ;;{{{ article and field retrieval code
1502 (defun spam-get-article-as-string (article)
1503 (when (numberp article)
1504 (with-temp-buffer
1505 (gnus-request-article-this-buffer
1506 article
1507 gnus-newsgroup-name)
1508 (buffer-string))))
1510 ;; disabled for now
1511 ;; (defun spam-get-article-as-filename (article)
1512 ;; (let ((article-filename))
1513 ;; (when (numberp article)
1514 ;; (nnml-possibly-change-directory
1515 ;; (gnus-group-real-name gnus-newsgroup-name))
1516 ;; (setq article-filename (expand-file-name
1517 ;; (int-to-string article) nnml-current-directory)))
1518 ;; (if (file-exists-p article-filename)
1519 ;; article-filename
1520 ;; nil)))
1522 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
1523 "Fetch a FIELD for ARTICLE with the internal `gnus-data-list' function.
1524 When PREPARED-DATA-HEADER is given, don't look in the Gnus data.
1525 When FIELD is 'number, ARTICLE can be any number (since we want
1526 to find it out)."
1527 (when (numberp article)
1528 (let* ((data-header (or prepared-data-header
1529 (spam-fetch-article-header article))))
1530 (cond
1531 ((not (arrayp data-header))
1532 (gnus-message 6 "Article %d has a nil data header" article))
1533 ((equal field 'number)
1534 (mail-header-number data-header))
1535 ((equal field 'from)
1536 (mail-header-from data-header))
1537 ((equal field 'message-id)
1538 (mail-header-message-id data-header))
1539 ((equal field 'subject)
1540 (mail-header-subject data-header))
1541 ((equal field 'references)
1542 (mail-header-references data-header))
1543 ((equal field 'date)
1544 (mail-header-date data-header))
1545 ((equal field 'xref)
1546 (mail-header-xref data-header))
1547 ((equal field 'extra)
1548 (mail-header-extra data-header))
1550 (gnus-error
1552 "spam-fetch-field-fast: unknown field %s requested"
1553 field)
1554 nil)))))
1556 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
1557 (spam-fetch-field-fast article 'from prepared-data-header))
1559 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
1560 (spam-fetch-field-fast article 'subject prepared-data-header))
1562 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
1563 (spam-fetch-field-fast article 'message-id prepared-data-header))
1565 (defun spam-generate-fake-headers (article)
1566 (let ((dh (spam-fetch-article-header article)))
1567 (if dh
1568 (concat
1569 (format
1570 ;; 80-character limit makes for strange constructs
1571 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
1572 "Date: %s\nReferences: %s\nXref: %s\n")
1573 (spam-fetch-field-fast article 'from dh)
1574 (spam-fetch-field-fast article 'subject dh)
1575 (spam-fetch-field-fast article 'message-id dh)
1576 (spam-fetch-field-fast article 'date dh)
1577 (spam-fetch-field-fast article 'references dh)
1578 (spam-fetch-field-fast article 'xref dh))
1579 (when (spam-fetch-field-fast article 'extra dh)
1580 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
1581 (gnus-message
1583 "spam-generate-fake-headers: article %d didn't have a valid header"
1584 article))))
1586 (defun spam-fetch-article-header (article)
1587 (with-current-buffer gnus-summary-buffer
1588 (gnus-read-header article)
1589 (nth 3 (assq article gnus-newsgroup-data))))
1590 ;;}}}
1592 ;;{{{ Spam determination.
1594 (defun spam-split (&rest specific-checks)
1595 "Split this message into the `spam' group if it is spam.
1596 This function can be used as an entry in the variable `nnmail-split-fancy',
1597 for example like this: (: spam-split). It can take checks as
1598 parameters. A string as a parameter will set the
1599 `spam-split-group' to that string.
1601 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1602 (interactive)
1603 (setq spam-split-last-successful-check nil)
1604 (unless spam-split-disabled
1605 (let ((spam-split-group-choice spam-split-group))
1606 (dolist (check specific-checks)
1607 (when (stringp check)
1608 (setq spam-split-group-choice check)
1609 (setq specific-checks (delq check specific-checks))))
1611 (let ((spam-split-group spam-split-group-choice)
1612 (widening-needed-check (spam-widening-needed-p specific-checks)))
1613 (save-excursion
1614 (save-restriction
1615 (when widening-needed-check
1616 (widen)
1617 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1618 widening-needed-check))
1619 (let ((backends (spam-backend-list))
1620 decision)
1621 (while (and backends (not decision))
1622 (let* ((backend (pop backends))
1623 (check-function (spam-backend-check backend))
1624 (spam-split-group (if spam-split-symbolic-return
1625 'spam
1626 spam-split-group)))
1627 (when (or
1628 ;; either, given specific checks, this is one of them
1629 (memq backend specific-checks)
1630 ;; or, given no specific checks, spam-use-CHECK is set
1631 (and (null specific-checks) (symbol-value backend)))
1632 (gnus-message 6 "spam-split: calling the %s function"
1633 check-function)
1634 (setq decision (funcall check-function))
1635 ;; if we got a decision at all, save the current check
1636 (when decision
1637 (setq spam-split-last-successful-check backend))
1639 (when (eq decision 'spam)
1640 (unless spam-split-symbolic-return
1641 (gnus-error
1643 (format "spam-split got %s but %s is nil"
1644 decision
1645 spam-split-symbolic-return)))))))
1646 (if (eq decision t)
1647 (if spam-split-symbolic-return-positive 'ham nil)
1648 decision))))))))
1650 (defun spam-find-spam ()
1651 "Detect spam in the current newsgroup using `spam-split'."
1652 (interactive)
1654 (let* ((group gnus-newsgroup-name)
1655 (autodetect (gnus-parameter-spam-autodetect group))
1656 (methods (gnus-parameter-spam-autodetect-methods group))
1657 (first-method (nth 0 methods))
1658 (articles (if spam-autodetect-recheck-messages
1659 gnus-newsgroup-articles
1660 gnus-newsgroup-unseen))
1661 article-cannot-be-faked)
1664 (cl-dolist (backend methods)
1665 (when (spam-backend-statistical-p backend)
1666 (setq article-cannot-be-faked t)
1667 (cl-return)))
1669 (when (memq 'default methods)
1670 (setq article-cannot-be-faked t))
1672 (when (and autodetect
1673 (not (equal first-method 'none)))
1674 (mapcar
1675 (lambda (article)
1676 (let ((id (spam-fetch-field-message-id-fast article))
1677 (subject (spam-fetch-field-subject-fast article))
1678 (sender (spam-fetch-field-from-fast article))
1679 registry-lookup)
1681 (unless id
1682 (gnus-message 6 "Article %d has no message ID!" article))
1684 (when (and id spam-log-to-registry)
1685 (setq registry-lookup (spam-log-registration-type id 'incoming))
1686 (when registry-lookup
1687 (gnus-message
1689 "spam-find-spam: message %s was already registered incoming"
1690 id)))
1692 (let* ((spam-split-symbolic-return t)
1693 (spam-split-symbolic-return-positive t)
1694 (fake-headers (spam-generate-fake-headers article))
1695 (split-return
1696 (or registry-lookup
1697 (with-temp-buffer
1698 (if article-cannot-be-faked
1699 (gnus-request-article-this-buffer
1700 article
1701 group)
1702 ;; else, we fake the article
1703 (when fake-headers (insert fake-headers)))
1704 (if (or (null first-method)
1705 (equal first-method 'default))
1706 (spam-split)
1707 (apply 'spam-split methods))))))
1708 (if (equal split-return 'spam)
1709 (gnus-summary-mark-article article gnus-spam-mark))
1711 (when (and id split-return spam-log-to-registry)
1712 (when (zerop (gnus-registry-group-count id))
1713 (gnus-registry-handle-action id nil group subject sender))
1715 (unless registry-lookup
1716 (spam-log-processing-to-registry
1718 'incoming
1719 split-return
1720 spam-split-last-successful-check
1721 group))))))
1722 articles))))
1724 ;;}}}
1726 ;;{{{ registration/unregistration functions
1728 (defun spam-resolve-registrations-routine ()
1729 "Go through the backends and register or unregister articles as needed."
1730 (dolist (backend-type '(non-mover mover))
1731 (dolist (classification (spam-classifications))
1732 (dolist (backend (spam-backend-list backend-type))
1733 (let ((rlist (spam-backend-get-article-todo-list
1734 backend classification))
1735 (ulist (spam-backend-get-article-todo-list
1736 backend classification t))
1737 (delcount 0))
1739 ;; clear the old lists right away
1740 (spam-backend-put-article-todo-list backend
1741 classification
1743 nil)
1744 (spam-backend-put-article-todo-list backend
1745 classification
1749 ;; eliminate duplicates
1750 (dolist (article (copy-sequence ulist))
1751 (when (memq article rlist)
1752 (cl-incf delcount)
1753 (setq rlist (delq article rlist))
1754 (setq ulist (delq article ulist))))
1756 (unless (zerop delcount)
1757 (gnus-message
1759 "%d messages did not have to unregister and then register"
1760 delcount))
1762 ;; unregister articles
1763 (unless (zerop (length ulist))
1764 (let ((num (spam-unregister-routine classification backend ulist)))
1765 (when (> num 0)
1766 (gnus-message
1768 "%d %s messages were unregistered by backend %s."
1770 classification
1771 backend))))
1773 ;; register articles
1774 (unless (zerop (length rlist))
1775 (let ((num (spam-register-routine classification backend rlist)))
1776 (when (> num 0)
1777 (gnus-message
1779 "%d %s messages were registered by backend %s."
1781 classification
1782 backend)))))))))
1784 (defun spam-unregister-routine (classification
1785 backend
1786 specific-articles)
1787 (spam-register-routine classification backend specific-articles t))
1789 (defun spam-register-routine (classification
1790 backend
1791 specific-articles
1792 &optional unregister)
1793 (when (and (spam-classification-valid-p classification)
1794 (spam-backend-valid-p backend))
1795 (let* ((register-function
1796 (spam-backend-function backend classification 'registration))
1797 (unregister-function
1798 (spam-backend-function backend classification 'unregistration))
1799 (run-function (if unregister
1800 unregister-function
1801 register-function))
1802 (log-function (if unregister
1803 'spam-log-undo-registration
1804 'spam-log-processing-to-registry))
1805 article articles)
1807 (when run-function
1808 ;; make list of articles, using specific-articles if given
1809 (setq articles (or specific-articles
1810 (spam-list-articles
1811 gnus-newsgroup-articles
1812 classification)))
1813 ;; process them
1814 (when (> (length articles) 0)
1815 (gnus-message 5 "%s %d %s articles as %s using backend %s"
1816 (if unregister "Unregistering" "Registering")
1817 (length articles)
1818 (if specific-articles "specific" "")
1819 classification
1820 backend)
1821 (funcall run-function articles)
1822 ;; now log all the registrations (or undo them, depending on
1823 ;; unregister)
1824 (dolist (article articles)
1825 (funcall log-function
1826 (spam-fetch-field-message-id-fast article)
1827 'process
1828 classification
1829 backend
1830 gnus-newsgroup-name))))
1831 ;; return the number of articles processed
1832 (length articles))))
1834 ;;; log a ham- or spam-processor invocation to the registry
1835 (defun spam-log-processing-to-registry (id type classification backend group)
1836 (when spam-log-to-registry
1837 (if (and (stringp id)
1838 (stringp group)
1839 (spam-process-type-valid-p type)
1840 (spam-classification-valid-p classification)
1841 (spam-backend-valid-p backend))
1842 (let ((cell-list (gnus-registry-get-id-key id type))
1843 (cell (list classification backend group)))
1844 (push cell cell-list)
1845 (gnus-registry-set-id-key id type cell-list))
1847 (gnus-error
1849 (format
1850 "%s call with bad ID, type, classification, spam-backend, or group"
1851 "spam-log-processing-to-registry")))))
1853 ;;; check if a ham- or spam-processor registration has been done
1854 (defun spam-log-registered-p (id type)
1855 (when spam-log-to-registry
1856 (if (and (stringp id)
1857 (spam-process-type-valid-p type))
1858 (gnus-registry-get-id-key id type)
1859 (progn
1860 (gnus-error
1862 (format "%s called with bad ID, type, classification, or spam-backend"
1863 "spam-log-registered-p"))
1864 nil))))
1866 ;;; check what a ham- or spam-processor registration says
1867 ;;; returns nil if conflicting registrations are found
1868 (defun spam-log-registration-type (id type)
1869 (let ((count 0)
1870 decision)
1871 (dolist (reg (spam-log-registered-p id type))
1872 (let ((classification (nth 0 reg)))
1873 (when (spam-classification-valid-p classification)
1874 (when (and decision
1875 (not (eq classification decision)))
1876 (setq count (+ 1 count)))
1877 (setq decision classification))))
1878 (if (< 0 count)
1880 decision)))
1883 ;;; check if a ham- or spam-processor registration needs to be undone
1884 (defun spam-log-unregistration-needed-p (id type classification backend)
1885 (when spam-log-to-registry
1886 (if (and (stringp id)
1887 (spam-process-type-valid-p type)
1888 (spam-classification-valid-p classification)
1889 (spam-backend-valid-p backend))
1890 (let ((cell-list (gnus-registry-get-id-key id type))
1891 found)
1892 (dolist (cell cell-list)
1893 (unless found
1894 (when (and (eq classification (nth 0 cell))
1895 (eq backend (nth 1 cell)))
1896 (setq found t))))
1897 found)
1898 (progn
1899 (gnus-error
1901 (format "%s called with bad ID, type, classification, or spam-backend"
1902 "spam-log-unregistration-needed-p"))
1903 nil))))
1906 ;;; undo a ham- or spam-processor registration (the group is not used)
1907 (defun spam-log-undo-registration (id type classification backend
1908 &optional group)
1909 (when (and spam-log-to-registry
1910 (spam-log-unregistration-needed-p id type classification backend))
1911 (if (and (stringp id)
1912 (spam-process-type-valid-p type)
1913 (spam-classification-valid-p classification)
1914 (spam-backend-valid-p backend))
1915 (let ((cell-list (gnus-registry-get-id-key id type))
1916 new-cell-list found)
1917 (dolist (cell cell-list)
1918 (unless (and (eq classification (nth 0 cell))
1919 (eq backend (nth 1 cell)))
1920 (push cell new-cell-list)))
1921 (gnus-registry-set-id-key id type new-cell-list))
1922 (progn
1923 (gnus-error 7 (format
1924 "%s call with bad ID, type, spam-backend, or group"
1925 "spam-log-undo-registration"))
1926 nil))))
1928 ;;}}}
1930 ;;{{{ backend functions
1932 ;;{{{ Gmane xrefs
1933 (defun spam-check-gmane-xref ()
1934 (let ((header (or
1935 (message-fetch-field "Xref")
1936 (message-fetch-field "Newsgroups"))))
1937 (when header ; return nil when no header
1938 (when (string-match spam-gmane-xref-spam-group
1939 header)
1940 spam-split-group))))
1942 ;;}}}
1944 ;;{{{ Regex body
1946 (defun spam-check-regex-body ()
1947 (let ((spam-regex-headers-ham spam-regex-body-ham)
1948 (spam-regex-headers-spam spam-regex-body-spam))
1949 (spam-check-regex-headers t)))
1951 ;;}}}
1953 ;;{{{ Regex headers
1955 (defun spam-check-regex-headers (&optional body)
1956 (let ((type (if body "body" "header"))
1957 ret found)
1958 (dolist (h-regex spam-regex-headers-ham)
1959 (unless found
1960 (goto-char (point-min))
1961 (when (re-search-forward h-regex nil t)
1962 (message "Ham regex %s search positive." type)
1963 (setq found t))))
1964 (dolist (s-regex spam-regex-headers-spam)
1965 (unless found
1966 (goto-char (point-min))
1967 (when (re-search-forward s-regex nil t)
1968 (message "Spam regex %s search positive." type)
1969 (setq found t)
1970 (setq ret spam-split-group))))
1971 ret))
1973 ;;}}}
1975 ;;{{{ Blackholes.
1977 (defun spam-reverse-ip-string (ip)
1978 (when (stringp ip)
1979 (mapconcat 'identity
1980 (nreverse (split-string ip "\\."))
1981 ".")))
1983 (defun spam-check-blackholes ()
1984 "Check the Received headers for blackholed relays."
1985 (let ((headers (message-fetch-field "received"))
1986 ips matches)
1987 (when headers
1988 (with-temp-buffer
1989 (insert headers)
1990 (goto-char (point-min))
1991 (gnus-message 6 "Checking headers for relay addresses")
1992 (while (re-search-forward
1993 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1994 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1995 (push (spam-reverse-ip-string (match-string 1))
1996 ips)))
1997 (dolist (server spam-blackhole-servers)
1998 (dolist (ip ips)
1999 (unless (and spam-blackhole-good-server-regex
2000 ;; match against the reversed (again) IP string
2001 (string-match
2002 spam-blackhole-good-server-regex
2003 (spam-reverse-ip-string ip)))
2004 (unless matches
2005 (let ((query-string (concat ip "." server)))
2006 (if spam-use-dig
2007 (let ((query-result (query-dig query-string)))
2008 (when query-result
2009 (gnus-message 6 "(DIG): positive blackhole check `%s'"
2010 query-result)
2011 (push (list ip server query-result)
2012 matches)))
2013 ;; else, if not using dig.el
2014 (when (dns-query query-string)
2015 (gnus-message 6 "positive blackhole check")
2016 (push (list ip server (dns-query query-string 'TXT))
2017 matches)))))))))
2018 (when matches
2019 spam-split-group)))
2020 ;;}}}
2022 ;;{{{ Hashcash.
2024 (defun spam-check-hashcash ()
2025 "Check the headers for hashcash payments."
2026 (ignore-errors (mail-check-payment))) ;mail-check-payment returns a boolean
2028 ;;}}}
2030 ;;{{{ BBDB
2032 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
2033 ;;; <sacha@giotto.sj.ru>
2035 ;; all this is done inside a condition-case to trap errors
2037 ;; Autoloaded in message, which we require.
2038 (declare-function gnus-extract-address-components "gnus-util" (from))
2040 (eval-and-compile
2041 (condition-case nil
2042 (progn
2043 (require 'bbdb)
2044 (require 'bbdb-com))
2045 (file-error
2046 ;; `bbdb-records' should not be bound as an autoload function
2047 ;; before loading bbdb because of `bbdb-hashtable-size'.
2048 (defalias 'bbdb-buffer 'ignore)
2049 (defalias 'bbdb-create-internal 'ignore)
2050 (defalias 'bbdb-records 'ignore)
2051 (defalias 'spam-BBDB-register-routine 'ignore)
2052 (defalias 'spam-enter-ham-BBDB 'ignore)
2053 (defalias 'spam-exists-in-BBDB-p 'ignore)
2054 (defalias 'bbdb-gethash 'ignore)
2055 nil)))
2057 (eval-and-compile
2058 (when (featurep 'bbdb-com)
2059 ;; when the BBDB changes, we want to clear out our cache
2060 (defun spam-clear-cache-BBDB (&rest immaterial)
2061 (spam-clear-cache 'spam-use-BBDB))
2063 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
2065 (defun spam-enter-ham-BBDB (addresses &optional remove)
2066 "Enter an address into the BBDB; implies ham (non-spam) sender"
2067 (dolist (from addresses)
2068 (when (stringp from)
2069 (let* ((parsed-address (gnus-extract-address-components from))
2070 (name (or (nth 0 parsed-address) "Ham Sender"))
2071 (remove-function (if remove
2072 'bbdb-delete-record-internal
2073 'ignore))
2074 (net-address (nth 1 parsed-address))
2075 (record (and net-address
2076 (spam-exists-in-BBDB-p net-address))))
2077 (when net-address
2078 (gnus-message 6 "%s address %s %s BBDB"
2079 (if remove "Deleting" "Adding")
2080 from
2081 (if remove "from" "to"))
2082 (if record
2083 (funcall remove-function record)
2084 (bbdb-create-internal name nil net-address nil nil
2085 "ham sender added by spam.el")))))))
2087 (defun spam-BBDB-register-routine (articles &optional unregister)
2088 (let (addresses)
2089 (dolist (article articles)
2090 (when (stringp (spam-fetch-field-from-fast article))
2091 (push (spam-fetch-field-from-fast article) addresses)))
2092 ;; now do the register/unregister action
2093 (spam-enter-ham-BBDB addresses unregister)))
2095 (defun spam-BBDB-unregister-routine (articles)
2096 (spam-BBDB-register-routine articles t))
2098 (defsubst spam-exists-in-BBDB-p (net)
2099 (when (and (stringp net) (not (zerop (length net))))
2100 (bbdb-records)
2101 (bbdb-gethash (downcase net))))
2103 (defun spam-check-BBDB ()
2104 "Mail from people in the BBDB is classified as ham or non-spam"
2105 (let ((net (message-fetch-field "from")))
2106 (when net
2107 (setq net (nth 1 (gnus-extract-address-components net)))
2108 (if (spam-exists-in-BBDB-p net)
2110 (if spam-use-BBDB-exclusive
2111 spam-split-group
2112 nil)))))))
2114 ;;}}}
2116 ;;{{{ ifile
2118 ;;; check the ifile backend; return nil if the mail was NOT classified
2119 ;;; as spam
2122 (defun spam-get-ifile-database-parameter ()
2123 "Return the command-line parameter for ifile's database.
2124 See `spam-ifile-database'."
2125 (if spam-ifile-database
2126 (format "--db-file=%s" spam-ifile-database)
2127 nil))
2129 (defun spam-check-ifile ()
2130 "Check the ifile backend for the classification of this message."
2131 (let ((article-buffer-name (buffer-name))
2132 category return)
2133 (with-temp-buffer
2134 (let ((temp-buffer-name (buffer-name))
2135 (db-param (spam-get-ifile-database-parameter)))
2136 (with-current-buffer article-buffer-name
2137 (apply 'call-process-region
2138 (point-min) (point-max) spam-ifile-program
2139 nil temp-buffer-name nil "-c"
2140 (if db-param `(,db-param "-q") `("-q"))))
2141 ;; check the return now (we're back in the temp buffer)
2142 (goto-char (point-min))
2143 (if (not (eobp))
2144 (setq category (buffer-substring (point) (point-at-eol))))
2145 (when (not (zerop (length category))) ; we need a category here
2146 (if spam-ifile-all-categories
2147 (setq return category)
2148 ;; else, if spam-ifile-all-categories is not set...
2149 (when (string-equal spam-ifile-spam-category category)
2150 (setq return spam-split-group)))))) ; note return is nil otherwise
2151 return))
2153 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
2154 "Register an article, given as a string, with a category.
2155 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
2156 (let ((category (or category gnus-newsgroup-name))
2157 (add-or-delete-option (if unregister "-d" "-i"))
2158 (db (spam-get-ifile-database-parameter))
2159 parameters)
2160 (with-temp-buffer
2161 (dolist (article articles)
2162 (let ((article-string (spam-get-article-as-string article)))
2163 (when (stringp article-string)
2164 (insert article-string))))
2165 (apply 'call-process-region
2166 (point-min) (point-max) spam-ifile-program
2167 nil nil nil
2168 add-or-delete-option category
2169 (if db `(,db "-h") `("-h"))))))
2171 (defun spam-ifile-register-spam-routine (articles &optional unregister)
2172 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
2174 (defun spam-ifile-unregister-spam-routine (articles)
2175 (spam-ifile-register-spam-routine articles t))
2177 (defun spam-ifile-register-ham-routine (articles &optional unregister)
2178 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
2180 (defun spam-ifile-unregister-ham-routine (articles)
2181 (spam-ifile-register-ham-routine articles t))
2183 ;;}}}
2185 ;;{{{ spam-stat
2187 (require 'spam-stat)
2189 (defun spam-check-stat ()
2190 "Check the spam-stat backend for the classification of this message"
2191 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
2192 (spam-stat-buffer (buffer-name)) ; stat the current buffer
2193 category return)
2194 (spam-stat-split-fancy)))
2196 (defun spam-stat-register-spam-routine (articles &optional unregister)
2197 (dolist (article articles)
2198 (let ((article-string (spam-get-article-as-string article)))
2199 (with-temp-buffer
2200 (insert article-string)
2201 (if unregister
2202 (spam-stat-buffer-change-to-non-spam)
2203 (spam-stat-buffer-is-spam))))))
2205 (defun spam-stat-unregister-spam-routine (articles)
2206 (spam-stat-register-spam-routine articles t))
2208 (defun spam-stat-register-ham-routine (articles &optional unregister)
2209 (dolist (article articles)
2210 (let ((article-string (spam-get-article-as-string article)))
2211 (with-temp-buffer
2212 (insert article-string)
2213 (if unregister
2214 (spam-stat-buffer-change-to-spam)
2215 (spam-stat-buffer-is-non-spam))))))
2217 (defun spam-stat-unregister-ham-routine (articles)
2218 (spam-stat-register-ham-routine articles t))
2220 (defun spam-maybe-spam-stat-load ()
2221 (when spam-use-stat (spam-stat-load)))
2223 (defun spam-maybe-spam-stat-save ()
2224 (when spam-use-stat (spam-stat-save)))
2226 ;;}}}
2228 ;;{{{ Blacklists and whitelists.
2230 (defvar spam-whitelist-cache nil)
2231 (defvar spam-blacklist-cache nil)
2233 (defun spam-kill-whole-line ()
2234 (beginning-of-line)
2235 (let ((kill-whole-line t))
2236 (kill-line)))
2238 ;;; address can be a list, too
2239 (defun spam-enter-whitelist (address &optional remove)
2240 "Enter ADDRESS (list or single) into the whitelist.
2241 With a non-nil REMOVE, remove them."
2242 (interactive "sAddress: ")
2243 (spam-enter-list address spam-whitelist remove)
2244 (setq spam-whitelist-cache nil)
2245 (spam-clear-cache 'spam-use-whitelist))
2247 ;;; address can be a list, too
2248 (defun spam-enter-blacklist (address &optional remove)
2249 "Enter ADDRESS (list or single) into the blacklist.
2250 With a non-nil REMOVE, remove them."
2251 (interactive "sAddress: ")
2252 (spam-enter-list address spam-blacklist remove)
2253 (setq spam-blacklist-cache nil)
2254 (spam-clear-cache 'spam-use-whitelist))
2256 (defun spam-enter-list (addresses file &optional remove)
2257 "Enter ADDRESSES into the given FILE.
2258 Either the whitelist or the blacklist files can be used.
2259 With a non-nil REMOVE, remove the ADDRESSES."
2260 (if (stringp addresses)
2261 (spam-enter-list (list addresses) file remove)
2262 ;; else, we have a list of addresses here
2263 (unless (file-exists-p (file-name-directory file))
2264 (make-directory (file-name-directory file) t))
2265 (with-current-buffer
2266 (find-file-noselect file)
2267 (dolist (a addresses)
2268 (when (stringp a)
2269 (goto-char (point-min))
2270 (if (re-search-forward (regexp-quote a) nil t)
2271 ;; found the address
2272 (when remove
2273 (spam-kill-whole-line))
2274 ;; else, the address was not found
2275 (unless remove
2276 (goto-char (point-max))
2277 (unless (bobp)
2278 (insert "\n"))
2279 (insert a "\n")))))
2280 (save-buffer))))
2282 (defun spam-filelist-build-cache (type)
2283 (let ((cache (if (eq type 'spam-use-blacklist)
2284 spam-blacklist-cache
2285 spam-whitelist-cache))
2286 parsed-cache)
2287 (unless (gethash type spam-caches)
2288 (while cache
2289 (let ((address (pop cache)))
2290 (unless (zerop (length address)) ; 0 for a nil address too
2291 (setq address (regexp-quote address))
2292 ;; fix regexp-quote's treatment of user-intended regexes
2293 (while (string-match "\\\\\\*" address)
2294 (setq address (replace-match ".*" t t address))))
2295 (push address parsed-cache)))
2296 (puthash type parsed-cache spam-caches))))
2298 (defun spam-filelist-check-cache (type from)
2299 (when (stringp from)
2300 (spam-filelist-build-cache type)
2301 (let (found)
2302 (cl-dolist (address (gethash type spam-caches))
2303 (when (and address (string-match address from))
2304 (setq found t)
2305 (cl-return)))
2306 found)))
2308 ;;; returns t if the sender is in the whitelist, nil or
2309 ;;; spam-split-group otherwise
2310 (defun spam-check-whitelist ()
2311 ;; FIXME! Should it detect when file timestamps change?
2312 (unless spam-whitelist-cache
2313 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
2314 (if (spam-from-listed-p 'spam-use-whitelist)
2316 (if spam-use-whitelist-exclusive
2317 spam-split-group
2318 nil)))
2320 (defun spam-check-blacklist ()
2321 ;; FIXME! Should it detect when file timestamps change?
2322 (unless spam-blacklist-cache
2323 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
2324 (and (spam-from-listed-p 'spam-use-blacklist)
2325 spam-split-group))
2327 (defun spam-parse-list (file)
2328 (when (file-readable-p file)
2329 (let (contents address)
2330 (with-temp-buffer
2331 (insert-file-contents file)
2332 (while (not (eobp))
2333 (setq address (buffer-substring (point) (point-at-eol)))
2334 (forward-line 1)
2335 ;; insert the e-mail address if detected, otherwise the raw data
2336 (unless (zerop (length address))
2337 (let ((pure-address
2338 (nth 1 (gnus-extract-address-components address))))
2339 (push (or pure-address address) contents)))))
2340 (nreverse contents))))
2342 (defun spam-from-listed-p (type)
2343 (let ((from (message-fetch-field "from"))
2344 found)
2345 (spam-filelist-check-cache type from)))
2347 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
2348 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
2349 (declassification (if blacklist 'ham 'spam))
2350 (enter-function
2351 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
2352 (remove-function
2353 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
2354 from addresses unregister-list article-unregister-list)
2355 (dolist (article articles)
2356 (let ((from (spam-fetch-field-from-fast article))
2357 (id (spam-fetch-field-message-id-fast article))
2358 sender-ignored)
2359 (when (stringp from)
2360 (dolist (ignore-regex spam-blacklist-ignored-regexes)
2361 (when (and (not sender-ignored)
2362 (stringp ignore-regex)
2363 (string-match ignore-regex from))
2364 (setq sender-ignored t)))
2365 ;; remember the messages we need to unregister, unless remove is set
2366 (when (and
2367 (null unregister)
2368 (spam-log-unregistration-needed-p
2369 id 'process declassification de-symbol))
2370 (push article article-unregister-list)
2371 (push from unregister-list))
2372 (unless sender-ignored
2373 (push from addresses)))))
2375 (if unregister
2376 (funcall enter-function addresses t) ; unregister all these addresses
2377 ;; else, register normally and unregister what we need to
2378 (funcall remove-function unregister-list t)
2379 (dolist (article article-unregister-list)
2380 (spam-log-undo-registration
2381 (spam-fetch-field-message-id-fast article)
2382 'process
2383 declassification
2384 de-symbol))
2385 (funcall enter-function addresses nil))))
2387 (defun spam-blacklist-unregister-routine (articles)
2388 (spam-blacklist-register-routine articles t))
2390 (defun spam-blacklist-register-routine (articles &optional unregister)
2391 (spam-filelist-register-routine articles t unregister))
2393 (defun spam-whitelist-unregister-routine (articles)
2394 (spam-whitelist-register-routine articles t))
2396 (defun spam-whitelist-register-routine (articles &optional unregister)
2397 (spam-filelist-register-routine articles nil unregister))
2399 ;;}}}
2401 ;;{{{ Spam-report glue (gmane and resend reporting)
2402 (defun spam-report-gmane-register-routine (articles)
2403 (when articles
2404 (apply 'spam-report-gmane-spam articles)))
2406 (defun spam-report-gmane-unregister-routine (articles)
2407 (when articles
2408 (apply 'spam-report-gmane-ham articles)))
2410 (defun spam-report-resend-register-ham-routine (articles)
2411 (spam-report-resend-register-routine articles t))
2413 (defvar spam-report-resend-to)
2415 (defun spam-report-resend-register-routine (articles &optional ham)
2416 (require 'spam-report)
2417 (let* ((resend-to-gp
2418 (if ham
2419 (gnus-parameter-ham-resend-to gnus-newsgroup-name)
2420 (gnus-parameter-spam-resend-to gnus-newsgroup-name)))
2421 (spam-report-resend-to (or (car-safe resend-to-gp)
2422 spam-report-resend-to)))
2423 (spam-report-resend articles ham)))
2425 ;;}}}
2427 ;;{{{ Bogofilter
2428 (defun spam-check-bogofilter-headers (&optional score)
2429 (let ((header (message-fetch-field spam-bogofilter-header)))
2430 (when header ; return nil when no header
2431 (if score ; scoring mode
2432 (if (string-match "spamicity=\\([0-9.]+\\)" header)
2433 (match-string 1 header)
2434 "0")
2435 ;; spam detection mode
2436 (when (string-match spam-bogofilter-bogosity-positive-spam-header
2437 header)
2438 spam-split-group)))))
2440 ;; return something sensible if the score can't be determined
2441 (defun spam-bogofilter-score (&optional recheck)
2442 "Get the Bogofilter spamicity score."
2443 (interactive "P")
2444 (save-window-excursion
2445 (gnus-summary-show-article t)
2446 (set-buffer gnus-article-buffer)
2447 (let ((score (or (unless recheck
2448 (spam-check-bogofilter-headers t))
2449 (spam-check-bogofilter t))))
2450 (gnus-summary-show-article)
2451 (message "Spamicity score %s" score)
2452 (or score "0"))))
2454 (defun spam-verify-bogofilter ()
2455 "Verify the Bogofilter version is sufficient."
2456 (when (eq spam-bogofilter-valid 'unknown)
2457 (setq spam-bogofilter-valid
2458 (not (string-match "^bogofilter version 0\\.\\([0-9]\\|1[01]\\)\\."
2459 (shell-command-to-string
2460 (format "%s -V" spam-bogofilter-program))))))
2461 spam-bogofilter-valid)
2463 (defun spam-check-bogofilter (&optional score)
2464 "Check the Bogofilter backend for the classification of this message."
2465 (if (spam-verify-bogofilter)
2466 (let ((article-buffer-name (buffer-name))
2467 (db spam-bogofilter-database-directory)
2468 return)
2469 (with-temp-buffer
2470 (let ((temp-buffer-name (buffer-name)))
2471 (with-current-buffer article-buffer-name
2472 (apply 'call-process-region
2473 (point-min) (point-max)
2474 spam-bogofilter-program
2475 nil temp-buffer-name nil
2476 (if db `("-d" ,db "-v") `("-v"))))
2477 (setq return (spam-check-bogofilter-headers score))))
2478 return)
2479 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2481 (defun spam-bogofilter-register-with-bogofilter (articles
2482 spam
2483 &optional unregister)
2484 "Register an article, given as a string, as spam or non-spam."
2485 (if (spam-verify-bogofilter)
2486 (dolist (article articles)
2487 (let ((article-string (spam-get-article-as-string article))
2488 (db spam-bogofilter-database-directory)
2489 (switch (if unregister
2490 (if spam
2491 spam-bogofilter-spam-strong-switch
2492 spam-bogofilter-ham-strong-switch)
2493 (if spam
2494 spam-bogofilter-spam-switch
2495 spam-bogofilter-ham-switch))))
2496 (when (stringp article-string)
2497 (with-temp-buffer
2498 (insert article-string)
2500 (apply 'call-process-region
2501 (point-min) (point-max)
2502 spam-bogofilter-program
2503 nil nil nil switch
2504 (if db `("-d" ,db "-v") `("-v")))))))
2505 (gnus-error 5 "`spam.el' doesn't support obsolete bogofilter versions")))
2507 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
2508 (spam-bogofilter-register-with-bogofilter articles t unregister))
2510 (defun spam-bogofilter-unregister-spam-routine (articles)
2511 (spam-bogofilter-register-spam-routine articles t))
2513 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
2514 (spam-bogofilter-register-with-bogofilter articles nil unregister))
2516 (defun spam-bogofilter-unregister-ham-routine (articles)
2517 (spam-bogofilter-register-ham-routine articles t))
2520 ;;}}}
2522 ;;{{{ spamoracle
2523 (defun spam-check-spamoracle ()
2524 "Run spamoracle on an article to determine whether it's spam."
2525 (let ((article-buffer-name (buffer-name)))
2526 (with-temp-buffer
2527 (let ((temp-buffer-name (buffer-name)))
2528 (with-current-buffer article-buffer-name
2529 (let ((status
2530 (apply 'call-process-region
2531 (point-min) (point-max)
2532 spam-spamoracle-binary
2533 nil temp-buffer-name nil
2534 (if spam-spamoracle-database
2535 `("-f" ,spam-spamoracle-database "mark")
2536 '("mark")))))
2537 (if (eq 0 status)
2538 (progn
2539 (set-buffer temp-buffer-name)
2540 (goto-char (point-min))
2541 (when (re-search-forward "^X-Spam: yes;" nil t)
2542 spam-split-group))
2543 (error "Error running spamoracle: %s" status))))))))
2545 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
2546 "Run spamoracle in training mode."
2547 (with-temp-buffer
2548 (let ((temp-buffer-name (buffer-name)))
2549 (save-excursion
2550 (goto-char (point-min))
2551 (dolist (article articles)
2552 (insert (spam-get-article-as-string article)))
2553 (let* ((arg (if (spam-xor unregister article-is-spam-p)
2554 "-spam"
2555 "-good"))
2556 (status
2557 (apply 'call-process-region
2558 (point-min) (point-max)
2559 spam-spamoracle-binary
2560 nil temp-buffer-name nil
2561 (if spam-spamoracle-database
2562 `("-f" ,spam-spamoracle-database
2563 "add" ,arg)
2564 `("add" ,arg)))))
2565 (unless (eq 0 status)
2566 (error "Error running spamoracle: %s" status)))))))
2568 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2569 (spam-spamoracle-learn articles nil unregister))
2571 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2572 (spam-spamoracle-learn-ham articles t))
2574 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2575 (spam-spamoracle-learn articles t unregister))
2577 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2578 (spam-spamoracle-learn-spam articles t))
2580 ;;}}}
2582 ;;{{{ SpamAssassin
2583 ;;; based mostly on the bogofilter code
2584 (defun spam-check-spamassassin-headers (&optional score)
2585 "Check the SpamAssassin headers for the classification of this message."
2586 (if score ; scoring mode
2587 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2588 (when header
2589 (if (string-match spam-spamassassin-score-regexp header)
2590 (match-string 1 header)
2591 "0")))
2592 ;; spam detection mode
2593 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header)))
2594 (when header ; return nil when no header
2595 (when (string-match spam-spamassassin-positive-spam-flag-header
2596 header)
2597 spam-split-group)))))
2599 (defun spam-check-spamassassin (&optional score)
2600 "Check the SpamAssassin backend for the classification of this message."
2601 (let ((article-buffer-name (buffer-name)))
2602 (with-temp-buffer
2603 (let ((temp-buffer-name (buffer-name)))
2604 (with-current-buffer article-buffer-name
2605 (apply 'call-process-region
2606 (point-min) (point-max) spam-assassin-program
2607 nil temp-buffer-name nil spam-spamassassin-arguments))
2608 ;; check the return now (we're back in the temp buffer)
2609 (goto-char (point-min))
2610 (spam-check-spamassassin-headers score)))))
2612 ;; return something sensible if the score can't be determined
2613 (defun spam-spamassassin-score (&optional recheck)
2614 "Get the SpamAssassin score"
2615 (interactive "P")
2616 (save-window-excursion
2617 (gnus-summary-show-article t)
2618 (set-buffer gnus-article-buffer)
2619 (let ((score (or (unless recheck
2620 (spam-check-spamassassin-headers t))
2621 (spam-check-spamassassin t))))
2622 (gnus-summary-show-article)
2623 (message "SpamAssassin score %s" score)
2624 (or score "0"))))
2626 (defun spam-spamassassin-register-with-sa-learn (articles spam
2627 &optional unregister)
2628 "Register articles with spamassassin's sa-learn as spam or non-spam."
2629 (if articles
2630 (let ((action (if unregister spam-sa-learn-unregister-switch
2631 (if spam spam-sa-learn-spam-switch
2632 spam-sa-learn-ham-switch)))
2633 (summary-buffer-name (buffer-name)))
2634 (with-temp-buffer
2635 ;; group the articles into mbox format
2636 (dolist (article articles)
2637 (let (article-string)
2638 (with-current-buffer summary-buffer-name
2639 (setq article-string (spam-get-article-as-string article)))
2640 (when (stringp article-string)
2641 ;; mbox separator
2642 (insert (concat "From nobody " (current-time-string) "\n"))
2643 (insert article-string)
2644 (insert "\n"))))
2645 ;; call sa-learn on all messages at the same time
2646 (apply 'call-process-region
2647 (point-min) (point-max)
2648 spam-sa-learn-program
2649 nil nil nil "--mbox"
2650 (if spam-sa-learn-rebuild
2651 (list action)
2652 `("--no-rebuild" ,action)))))))
2654 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2655 (spam-spamassassin-register-with-sa-learn articles t unregister))
2657 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2658 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2660 (defun spam-spamassassin-unregister-spam-routine (articles)
2661 (spam-spamassassin-register-with-sa-learn articles t t))
2663 (defun spam-spamassassin-unregister-ham-routine (articles)
2664 (spam-spamassassin-register-with-sa-learn articles nil t))
2666 ;;}}}
2668 ;;{{{ Bsfilter
2669 ;;; based mostly on the bogofilter code
2670 (defun spam-check-bsfilter-headers (&optional score)
2671 (if score
2672 (or (nnmail-fetch-field spam-bsfilter-probability-header)
2673 "0")
2674 (let ((header (nnmail-fetch-field spam-bsfilter-header)))
2675 (when header ; return nil when no header
2676 (when (string-match "YES" header)
2677 spam-split-group)))))
2679 ;; return something sensible if the score can't be determined
2680 (defun spam-bsfilter-score (&optional recheck)
2681 "Get the Bsfilter spamicity score."
2682 (interactive "P")
2683 (save-window-excursion
2684 (gnus-summary-show-article t)
2685 (set-buffer gnus-article-buffer)
2686 (let ((score (or (unless recheck
2687 (spam-check-bsfilter-headers t))
2688 (spam-check-bsfilter t))))
2689 (gnus-summary-show-article)
2690 (message "Spamicity score %s" score)
2691 (or score "0"))))
2693 (defun spam-check-bsfilter (&optional score)
2694 "Check the Bsfilter backend for the classification of this message."
2695 (let ((article-buffer-name (buffer-name))
2696 (dir spam-bsfilter-database-directory)
2697 return)
2698 (with-temp-buffer
2699 (let ((temp-buffer-name (buffer-name)))
2700 (with-current-buffer article-buffer-name
2701 (apply 'call-process-region
2702 (point-min) (point-max)
2703 spam-bsfilter-program
2704 nil temp-buffer-name nil
2705 "--pipe"
2706 "--insert-flag"
2707 "--insert-probability"
2708 (when dir
2709 (list "--homedir" dir))))
2710 (setq return (spam-check-bsfilter-headers score))))
2711 return))
2713 (defun spam-bsfilter-register-with-bsfilter (articles
2714 spam
2715 &optional unregister)
2716 "Register an article, given as a string, as spam or non-spam."
2717 (dolist (article articles)
2718 (let ((article-string (spam-get-article-as-string article))
2719 (switch (if unregister
2720 (if spam
2721 spam-bsfilter-spam-strong-switch
2722 spam-bsfilter-ham-strong-switch)
2723 (if spam
2724 spam-bsfilter-spam-switch
2725 spam-bsfilter-ham-switch))))
2726 (when (stringp article-string)
2727 (with-temp-buffer
2728 (insert article-string)
2729 (apply 'call-process-region
2730 (point-min) (point-max)
2731 spam-bsfilter-program
2732 nil nil nil switch
2733 "--update"
2734 (when spam-bsfilter-database-directory
2735 (list "--homedir"
2736 spam-bsfilter-database-directory))))))))
2738 (defun spam-bsfilter-register-spam-routine (articles &optional unregister)
2739 (spam-bsfilter-register-with-bsfilter articles t unregister))
2741 (defun spam-bsfilter-unregister-spam-routine (articles)
2742 (spam-bsfilter-register-spam-routine articles t))
2744 (defun spam-bsfilter-register-ham-routine (articles &optional unregister)
2745 (spam-bsfilter-register-with-bsfilter articles nil unregister))
2747 (defun spam-bsfilter-unregister-ham-routine (articles)
2748 (spam-bsfilter-register-ham-routine articles t))
2750 ;;}}}
2752 ;;{{{ CRM114 Mailfilter
2753 (defun spam-check-crm114-headers (&optional score)
2754 (let ((header (message-fetch-field spam-crm114-header)))
2755 (when header ; return nil when no header
2756 (if score ; scoring mode
2757 (if (string-match "( pR: \\([0-9.-]+\\)" header)
2758 (match-string 1 header)
2759 "0")
2760 ;; spam detection mode
2761 (when (string-match spam-crm114-positive-spam-header
2762 header)
2763 spam-split-group)))))
2765 ;; return something sensible if the score can't be determined
2766 (defun spam-crm114-score ()
2767 "Get the CRM114 Mailfilter pR."
2768 (interactive)
2769 (save-window-excursion
2770 (gnus-summary-show-article t)
2771 (set-buffer gnus-article-buffer)
2772 (let ((score (or (spam-check-crm114-headers t)
2773 (spam-check-crm114 t))))
2774 (gnus-summary-show-article)
2775 (message "pR: %s" score)
2776 (or score "0"))))
2778 (defun spam-check-crm114 (&optional score)
2779 "Check the CRM114 Mailfilter backend for the classification of this message."
2780 (let ((article-buffer-name (buffer-name))
2781 (db spam-crm114-database-directory)
2782 return)
2783 (with-temp-buffer
2784 (let ((temp-buffer-name (buffer-name)))
2785 (with-current-buffer article-buffer-name
2786 (apply 'call-process-region
2787 (point-min) (point-max)
2788 spam-crm114-program
2789 nil temp-buffer-name nil
2790 (when db (list (concat "--fileprefix=" db)))))
2791 (setq return (spam-check-crm114-headers score))))
2792 return))
2794 (defun spam-crm114-register-with-crm114 (articles
2795 spam
2796 &optional unregister)
2797 "Register an article, given as a string, as spam or non-spam."
2798 (dolist (article articles)
2799 (let ((article-string (spam-get-article-as-string article))
2800 (db spam-crm114-database-directory)
2801 (switch (if unregister
2802 (if spam
2803 spam-crm114-spam-strong-switch
2804 spam-crm114-ham-strong-switch)
2805 (if spam
2806 spam-crm114-spam-switch
2807 spam-crm114-ham-switch))))
2808 (when (stringp article-string)
2809 (with-temp-buffer
2810 (insert article-string)
2812 (apply 'call-process-region
2813 (point-min) (point-max)
2814 spam-crm114-program
2815 nil nil nil
2816 (when db (list switch (concat "--fileprefix=" db)))))))))
2818 (defun spam-crm114-register-spam-routine (articles &optional unregister)
2819 (spam-crm114-register-with-crm114 articles t unregister))
2821 (defun spam-crm114-unregister-spam-routine (articles)
2822 (spam-crm114-register-spam-routine articles t))
2824 (defun spam-crm114-register-ham-routine (articles &optional unregister)
2825 (spam-crm114-register-with-crm114 articles nil unregister))
2827 (defun spam-crm114-unregister-ham-routine (articles)
2828 (spam-crm114-register-ham-routine articles t))
2830 ;;}}}
2832 ;;}}}
2834 ;;{{{ Hooks
2836 ;;;###autoload
2837 (defun spam-initialize (&rest symbols)
2838 "Install the spam.el hooks and do other initialization.
2839 When SYMBOLS is given, set those variables to t. This is so you
2840 can call `spam-initialize' before you set spam-use-* variables on
2841 explicitly, and matters only if you need the extra headers
2842 installed through `spam-necessary-extra-headers'."
2843 (interactive)
2845 (when spam-install-hooks
2846 (dolist (var symbols)
2847 (set var t))
2849 (dolist (header (spam-necessary-extra-headers))
2850 (add-to-list 'nnmail-extra-headers header)
2851 (add-to-list 'gnus-extra-headers header))
2853 ;; TODO: How do we redo this every time the `spam' face is customized?
2854 (push '((eq mark gnus-spam-mark) . spam)
2855 gnus-summary-highlight)
2856 ;; Add hooks for loading and saving the spam stats
2857 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2858 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2859 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2860 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2861 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2862 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2863 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam)
2864 ;; Don't install things more than once.
2865 (setq spam-install-hooks nil)))
2867 (defun spam-unload-hook ()
2868 "Uninstall the spam.el hooks."
2869 (interactive)
2870 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2871 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2872 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2873 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2874 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2875 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2876 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2878 (add-hook 'spam-unload-hook 'spam-unload-hook)
2880 ;;}}}
2882 (provide 'spam)
2884 ;;; spam.el ends here