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