rmail-spam-filter.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. ;;; rmail-spam-filter.el --- spam filter for Rmail, the Emacs mail reader
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Keywords: email, spam, filter, rmail
  4. ;; Author: Eli Tziperman <eli AT deas.harvard.edu>
  5. ;; Package: rmail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; -----------
  19. ;;; Automatically recognize and delete junk email before it is
  20. ;;; displayed in rmail/rmail-summary. Spam emails are defined by
  21. ;;; specifying one or more of the sender, subject and contents.
  22. ;;; URL: http://www.weizmann.ac.il/~eli/Downloads/rmail-spam-filter/
  23. ;;; Usage:
  24. ;;; ------
  25. ;;; put in your .emacs:
  26. ;;; (require 'rmail-spam-filter)
  27. ;;; and use customize (in rmail-spam-filter group) to:
  28. ;;; (*) turn on the variable rmail-use-spam-filter,
  29. ;;; (*) specify in variable rsf-definitions-alist what sender,
  30. ;;; subject and contents make an email be considered spam.
  31. ;;; in addition, you may:
  32. ;;; (*) Block future mail with the subject or sender of a message
  33. ;;; while reading it in RMAIL: just click on the "Spam" item on the
  34. ;;; menubar, and add the subject or sender to the list of spam
  35. ;;; definitions using the mouse and the appropriate menu item. You
  36. ;;; need to later also save the list of spam definitions using the
  37. ;;; same menu item, or alternatively, see variable
  38. ;;; `rsf-autosave-newly-added-definitions'.
  39. ;;; (*) specify if blind-cc'ed mail (no "To:" header field) is to be
  40. ;;; treated as spam (variable rsf-no-blind-cc; Thanks to Ethan
  41. ;;; Brown <ethan@gso.saic.com> for this).
  42. ;;; (*) specify if rmail-spam-filter should ignore case of spam
  43. ;;; definitions (variable rsf-ignore-case; Thanks to
  44. ;;; Ethan Brown <ethan@gso.saic.com> for the suggestion).
  45. ;;; (*) Specify a "white-list" of trusted senders. If any
  46. ;;; rsf-white-list string matches a substring of the "From"
  47. ;;; header, the message is flagged as a valid, non-spam message (Ethan
  48. ;;; Brown <ethan@gso.saic.com>).
  49. ;;; (*) rmail-spam-filter is best used with a general purpose spam
  50. ;;; filter such as the procmail-based http://www.spambouncer.org/.
  51. ;;; Spambouncer is set to only mark messages as spam/blocked/bulk/OK
  52. ;;; via special headers, and these headers may then be defined in
  53. ;;; rmail-spam-filter such that the spam is rejected by
  54. ;;; rmail-spam-filter itself.
  55. (require 'rmail)
  56. (require 'rmailsum)
  57. (defgroup rmail-spam-filter nil
  58. "Spam filter for Rmail, the Emacs mail reader."
  59. :group 'rmail)
  60. (defcustom rmail-use-spam-filter nil
  61. "Non-nil to activate the Rmail spam filter.
  62. Set `rsf-definitions-alist' to define what you consider spam emails."
  63. :type 'boolean
  64. :group 'rmail-spam-filter)
  65. (defcustom rsf-file "~/XRMAIL-SPAM"
  66. "Name of Rmail file for optionally saving some of the spam.
  67. You can either just delete spam, or save it in this file for
  68. later review. Which action to take for each spam definition is
  69. specified by the \"action\" element of the definition."
  70. :type 'string
  71. :group 'rmail-spam-filter)
  72. (defcustom rsf-no-blind-cc nil
  73. "Non-nil means mail with no explicit To: or Cc: is spam."
  74. :type 'boolean
  75. :group 'rmail-spam-filter)
  76. (defcustom rsf-ignore-case nil
  77. "Non-nil means to ignore case in `rsf-definitions-alist'."
  78. :type 'boolean
  79. :group 'rmail-spam-filter)
  80. (defcustom rsf-beep nil
  81. "Non-nil means to beep if spam is found."
  82. :type 'boolean
  83. :group 'rmail-spam-filter)
  84. (defcustom rsf-sleep-after-message 2.0
  85. "Seconds to wait after displaying a message that spam was found."
  86. :type 'number
  87. :group 'rmail-spam-filter)
  88. (defcustom rsf-min-region-to-spam-list 7
  89. "Minimum size of region that you can add to the spam list.
  90. The aim is to avoid adding too short a region, which could result
  91. in false positive identification of a valid message as spam."
  92. :type 'integer
  93. :group 'rmail-spam-filter)
  94. (defcustom rsf-autosave-newly-added-definitions nil
  95. "Non-nil to auto-save new spam entries.
  96. Any time you add an entry via the \"Spam\" menu, immediately saves
  97. the custom file."
  98. :type 'boolean
  99. :group 'rmail-spam-filter)
  100. (defcustom rsf-white-list nil
  101. "List of regexps to identify valid senders.
  102. If any element matches the \"From\" header, the message is
  103. flagged as a valid, non-spam message. E.g., if your domain is
  104. \"emacs.com\" then including \"emacs\\\\.com\" in this list would
  105. flag all mail (purporting to be) from your colleagues as valid."
  106. :type '(repeat string)
  107. :group 'rmail-spam-filter)
  108. (defcustom rsf-definitions-alist nil
  109. "A list of rules (definitions) matching spam messages.
  110. Each rule is an alist, with elements of the form (FIELD . REGEXP).
  111. The recognized FIELDS are: from, to, subject, content-type,
  112. x-spam-status, and contents. The \"contents\" element refers to
  113. the entire text of the message; all the other elements refer to
  114. message headers of the same name.
  115. Using an empty-string for REGEXP is the same as omitting that
  116. element altogether.
  117. Each rule should contain one \"action\" element, saying what to do
  118. if the rule is matched. This has the form (action . CHOICE), where
  119. CHOICE may be either `output-and-delete' (save to `rsf-file', then delete),
  120. or `delete-spam' (just delete).
  121. A rule matches only if all the specified elements match."
  122. :type '(repeat
  123. (list :format "%v"
  124. (cons :format "%v" :value (from . "")
  125. (const :format "" from)
  126. (string :tag "From" ""))
  127. (cons :format "%v" :value (to . "")
  128. (const :format "" to)
  129. (string :tag "To" ""))
  130. (cons :format "%v" :value (subject . "")
  131. (const :format "" subject)
  132. (string :tag "Subject" ""))
  133. (cons :format "%v" :value (content-type . "")
  134. (const :format "" content-type)
  135. (string :tag "Content-Type" ""))
  136. (cons :format "%v" :value (contents . "")
  137. (const :format "" contents)
  138. (string :tag "Contents" ""))
  139. (cons :format "%v" :value (x-spam-status . "")
  140. (const :format "" x-spam-status)
  141. (string :tag "X-Spam-Status" ""))
  142. (cons :format "%v" :value (action . output-and-delete)
  143. (const :format "" action)
  144. (choice :tag "Action selection"
  145. (const :tag "Output and delete" output-and-delete)
  146. (const :tag "Delete" delete-spam)
  147. ))))
  148. :group 'rmail-spam-filter)
  149. ;; FIXME nothing uses this, and it could just be let-bound.
  150. (defvar rsf-scanning-messages-now nil
  151. "Non-nil when `rmail-spam-filter' scans messages.")
  152. ;; the advantage over the automatic filter definitions is the AND conjunction
  153. ;; of in-one-definition-elements
  154. (defun rsf-check-field (field-symbol message-data definition result)
  155. "Check if a message appears to be spam.
  156. FIELD-SYMBOL is one of the possible keys of a `rsf-definitions-alist'
  157. rule; e.g. from, to. MESSAGE-DATA is a string giving the value of
  158. FIELD-SYMBOL in the current message. DEFINITION is the element of
  159. `rsf-definitions-alist' currently being checked.
  160. RESULT is a cons of the form (MAYBE-SPAM . IS-SPAM). If the car
  161. is nil, or if the entry for FIELD-SYMBOL in this DEFINITION is
  162. absent or the empty string, this function does nothing.
  163. Otherwise, if MESSAGE-DATA is non-nil and the entry matches it,
  164. the cdr is set to t. Else, the car is set to nil."
  165. (let ((definition-field (cdr (assoc field-symbol definition))))
  166. ;; Only in this case can maybe-spam change from t to nil.
  167. (if (and (car result) (> (length definition-field) 0))
  168. ;; If FIELD-SYMBOL field appears in the message, and also in
  169. ;; spam definition list, this is potentially a spam.
  170. (if (and message-data
  171. (string-match definition-field message-data))
  172. ;; If we do not get a contradiction from another field, this is spam
  173. (setcdr result t)
  174. ;; The message data contradicts the specification, this is not spam.
  175. ;; Note that the total absence of a header specified in the
  176. ;; rule means this cannot be spam.
  177. (setcar result nil)))))
  178. (defun rmail-spam-filter (msg)
  179. "Return nil if message number MSG is spam based on `rsf-definitions-alist'.
  180. If spam, optionally output message to a file `rsf-file' and delete
  181. it from rmail file. Called for each new message retrieved by
  182. `rmail-get-new-mail'."
  183. (let ((return-value)
  184. ;; maybe-spam is in the car, this-is-a-spam-email in cdr.
  185. (maybe-spam '(nil . nil))
  186. message-sender message-to message-cc message-recipients
  187. message-subject message-content-type message-spam-status
  188. (num-spam-definition-elements (safe-length rsf-definitions-alist))
  189. (num-element 0)
  190. (exit-while-loop nil)
  191. ;; Do we want to ignore case in spam definitions.
  192. (case-fold-search rsf-ignore-case)
  193. ;; make sure bbdb does not create entries for messages while spam
  194. ;; filter is scanning the rmail file:
  195. (bbdb/mail_auto_create_p nil)
  196. ;; Other things may wish to know if we are running (nothing
  197. ;; uses this at present).
  198. (rsf-scanning-messages-now t))
  199. (save-excursion
  200. ;; Narrow buffer to header of message and get Sender and
  201. ;; Subject fields to be used below:
  202. (save-restriction
  203. (goto-char (rmail-msgbeg msg))
  204. (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
  205. (setq message-sender (mail-fetch-field "From"))
  206. (setq message-to (mail-fetch-field "To")
  207. message-cc (mail-fetch-field "Cc")
  208. message-recipients (or (and message-to message-cc
  209. (concat message-to ", " message-cc))
  210. message-to
  211. message-cc))
  212. (setq message-subject (mail-fetch-field "Subject"))
  213. (setq message-content-type (mail-fetch-field "Content-Type"))
  214. (setq message-spam-status (mail-fetch-field "X-Spam-Status")))
  215. ;; Check for blind CC condition. Set vars such that while
  216. ;; loop will be bypassed and spam condition will trigger.
  217. (and rsf-no-blind-cc
  218. (null message-recipients)
  219. (setq exit-while-loop t
  220. maybe-spam '(t . t)))
  221. ;; Check white list, and likewise cause while loop bypass.
  222. (and message-sender
  223. (let ((white-list rsf-white-list)
  224. (found nil))
  225. (while (and (not found) white-list)
  226. (if (string-match (car white-list) message-sender)
  227. (setq found t)
  228. (setq white-list (cdr white-list))))
  229. found)
  230. (setq exit-while-loop t
  231. maybe-spam '(nil . nil)))
  232. ;; Scan all elements of the list rsf-definitions-alist.
  233. (while (and (< num-element num-spam-definition-elements)
  234. (not exit-while-loop))
  235. (let ((definition (nth num-element rsf-definitions-alist)))
  236. ;; Initialize car, which is set to t in one of two cases:
  237. ;; (1) unspecified definition-elements are found in
  238. ;; rsf-definitions-alist, (2) empty field is found in the
  239. ;; message being scanned (e.g. empty subject, sender,
  240. ;; recipients, etc). It is set to nil if a non-empty field
  241. ;; of the scanned message does not match a specified field
  242. ;; in rsf-definitions-alist.
  243. ;; FIXME the car is never set to t?!
  244. ;; Initialize cdr to nil. This is set to t if one of the
  245. ;; spam definitions matches a field in the scanned message.
  246. (setq maybe-spam (cons t nil))
  247. ;; Maybe the different fields should also be done in a
  248. ;; loop to make the whole thing more flexible.
  249. ;; If sender field is not specified in message being
  250. ;; scanned, AND if "from" field does not appear in spam
  251. ;; definitions for this element, this may still be spam due
  252. ;; to another element...
  253. (rsf-check-field 'from message-sender definition maybe-spam)
  254. ;; Next, if spam was not ruled out already, check recipients:
  255. (rsf-check-field 'to message-recipients definition maybe-spam)
  256. ;; Next, if spam was not ruled out already, check subject:
  257. (rsf-check-field 'subject message-subject definition maybe-spam)
  258. ;; Next, if spam was not ruled out already, check content-type:
  259. (rsf-check-field 'content-type message-content-type
  260. definition maybe-spam)
  261. ;; Next, if spam was not ruled out already, check contents:
  262. ;; If contents field is not specified, this may still be
  263. ;; spam due to another element...
  264. (rsf-check-field 'contents
  265. (buffer-substring-no-properties
  266. (rmail-msgbeg msg) (rmail-msgend msg))
  267. definition maybe-spam)
  268. ;; Finally, check the X-Spam-Status header. You will typically
  269. ;; look for the "Yes" string in this header field.
  270. (rsf-check-field 'x-spam-status message-spam-status
  271. definition maybe-spam)
  272. ;; If the search in rsf-definitions-alist found
  273. ;; that this email is spam, output the email to the spam
  274. ;; rmail file, mark the email for deletion, leave the
  275. ;; while loop and return nil so that an rmail summary line
  276. ;; won't be displayed for this message: (FIXME ?)
  277. (if (and (car maybe-spam) (cdr maybe-spam))
  278. (setq exit-while-loop t)
  279. ;; Else, spam was not yet found, proceed to next element
  280. ;; in rsf-definitions-alist:
  281. (setq num-element (1+ num-element)))))
  282. (if (and (car maybe-spam) (cdr maybe-spam))
  283. ;; Temporarily set rmail-current-message in order to output
  284. ;; and delete the spam msg if needed:
  285. (let ((rmail-current-message msg) ; FIXME does this do anything?
  286. (action (cdr (assq 'action
  287. (nth num-element rsf-definitions-alist))))
  288. (newfile (not (file-exists-p rsf-file))))
  289. ;; Check action item in rsf-definitions-alist and do it.
  290. (cond
  291. ((eq action 'output-and-delete)
  292. ;; Else the prompt to write a new file leaves the raw
  293. ;; mbox buffer visible.
  294. (and newfile
  295. (rmail-show-message (rmail-first-unseen-message) t))
  296. (rmail-output rsf-file)
  297. ;; Swap back, else rmail-get-new-mail-1 gets confused.
  298. (when newfile
  299. (rmail-swap-buffers-maybe)
  300. (widen))
  301. ;; Don't delete if automatic deletion after output is on.
  302. (or rmail-delete-after-output (rmail-delete-message)))
  303. ((eq action 'delete-spam)
  304. (rmail-delete-message)))
  305. (setq return-value nil))
  306. (setq return-value t)))
  307. return-value))
  308. (defun rmail-get-new-mail-filter-spam (nnew)
  309. "Check the most NNEW recent messages for spam.
  310. This is called at the end of `rmail-get-new-mail-1' if there is new mail."
  311. (let* ((nold (- rmail-total-messages nnew))
  312. (nspam 0)
  313. (nscan (1+ nold))
  314. ;; Save the original deleted state of all the messages.
  315. (rdv-old rmail-deleted-vector)
  316. errflag)
  317. ;; Set all messages undeleted so that the expunge only affects spam.
  318. (setq rmail-deleted-vector (make-string (1+ rmail-total-messages) ?\s))
  319. (while (and (not errflag) (<= nscan rmail-total-messages))
  320. (condition-case nil
  321. (or (rmail-spam-filter nscan)
  322. (setq nspam (1+ nspam)))
  323. (error (setq errflag nscan)))
  324. (setq nscan (1+ nscan)))
  325. (unwind-protect
  326. (if errflag
  327. (progn
  328. (setq rmail-use-spam-filter nil)
  329. (if rsf-beep (ding t))
  330. (message "Spam filter error for new message %d, disabled" errflag)
  331. (sleep-for rsf-sleep-after-message))
  332. (when (> nspam 0)
  333. ;; Otherwise sleep or expunge prompt leaves raw mbox buffer showing.
  334. (rmail-show-message (or (rmail-first-unseen-message) 1) t)
  335. (unwind-protect
  336. (progn
  337. (if rsf-beep (ding t))
  338. (message "Rmail spam-filter detected and deleted %d spam \
  339. message%s"
  340. nspam (if (= 1 nspam) "" "s"))
  341. (sleep-for rsf-sleep-after-message)
  342. (if (rmail-expunge-confirmed) (rmail-only-expunge t)))
  343. ;; Swap back, else get-new-mail-1 gets confused.
  344. (rmail-swap-buffers-maybe)
  345. (widen))))
  346. ;; Restore the original deleted state. Character N refers to message N.
  347. (setq rmail-deleted-vector
  348. (concat (substring rdv-old 0 (1+ nold))
  349. ;; This still works if we deleted all the new mail.
  350. (substring rmail-deleted-vector (1+ nold)))))
  351. ;; Return a message based on the number of spam messages found.
  352. (cond
  353. (errflag ", error in spam filter")
  354. ((zerop nspam) "")
  355. ((= 1 nnew) ", and it appears to be spam")
  356. ((= nspam nnew) ", and all appear to be spam")
  357. (t (format ", and %d appear%s to be spam" nspam
  358. (if (= 1 nspam) "s" ""))))))
  359. ;; define functions for interactively adding sender/subject of a
  360. ;; specific message to the spam definitions while reading it, using
  361. ;; the menubar:
  362. (defun rsf-add-subject-to-spam-list ()
  363. "Add the \"Subject\" header to the spam list."
  364. (interactive)
  365. (let ((message-subject (regexp-quote (rmail-get-header "Subject"))))
  366. ;; Note the use of a backquote and comma on the subject line here,
  367. ;; to make sure message-subject is actually evaluated and its value
  368. ;; substituted.
  369. (add-to-list 'rsf-definitions-alist
  370. ;; Note that an empty element is treated the same as
  371. ;; an absent one, so why does it bother to add them?
  372. (list '(from . "")
  373. '(to . "")
  374. `(subject . ,message-subject)
  375. '(content-type . "")
  376. '(contents . "")
  377. '(action . output-and-delete))
  378. t)
  379. (customize-mark-to-save 'rsf-definitions-alist)
  380. (if rsf-autosave-newly-added-definitions
  381. (progn
  382. (custom-save-all)
  383. (message "Added subject `%s' to spam list, and saved it"
  384. message-subject))
  385. (message "Added subject `%s' to spam list (remember to save it)"
  386. message-subject))))
  387. (defun rsf-add-sender-to-spam-list ()
  388. "Add the \"From\" address to the spam list."
  389. (interactive)
  390. (let ((message-sender (regexp-quote (rmail-get-header "From"))))
  391. (add-to-list 'rsf-definitions-alist
  392. (list `(from . ,message-sender)
  393. '(to . "")
  394. '(subject . "")
  395. '(content-type . "")
  396. '(contents . "")
  397. '(action . output-and-delete))
  398. t)
  399. (customize-mark-to-save 'rsf-definitions-alist)
  400. (if rsf-autosave-newly-added-definitions
  401. (progn
  402. (custom-save-all)
  403. (message "Added sender `%s' to spam list, and saved it"
  404. message-sender))
  405. (message "Added sender `%s' to spam list (remember to save it)"
  406. message-sender))))
  407. (defun rsf-add-region-to-spam-list ()
  408. "Add the marked region in the Rmail buffer to the spam list.
  409. Adds to spam definitions as a \"contents\" field."
  410. (interactive)
  411. (set-buffer rmail-buffer)
  412. ;; Check if region is inactive or has zero size.
  413. (if (not (and mark-active (not (= (region-beginning) (region-end)))))
  414. ;; If inactive, print error message.
  415. (message "You must highlight some text in the Rmail buffer")
  416. (if (< (- (region-end) (region-beginning)) rsf-min-region-to-spam-list)
  417. (message "Region is too small (minimum %d characters)"
  418. rsf-min-region-to-spam-list)
  419. ;; If region active and long enough, add to list of spam definitions.
  420. (let ((region-to-spam-list (regexp-quote
  421. (buffer-substring-no-properties
  422. (region-beginning) (region-end)))))
  423. (add-to-list 'rsf-definitions-alist
  424. (list '(from . "")
  425. '(to . "")
  426. '(subject . "")
  427. '(content-type . "")
  428. `(contents . ,region-to-spam-list)
  429. '(action . output-and-delete))
  430. t)
  431. (customize-mark-to-save 'rsf-definitions-alist)
  432. (if rsf-autosave-newly-added-definitions
  433. (progn
  434. (custom-save-all)
  435. (message "Added highlighted text:\n%s\n\
  436. to the spam list, and saved it" region-to-spam-list))
  437. (message "Added highlighted text:\n%s\n\
  438. to the spam list (remember to save it)" region-to-spam-list))))))
  439. (defun rsf-customize-spam-definitions ()
  440. "Customize `rsf-definitions-alist'."
  441. (interactive)
  442. (customize-variable 'rsf-definitions-alist))
  443. (defun rsf-customize-group ()
  444. "Customize the rmail-spam-filter group."
  445. (interactive)
  446. (customize-group 'rmail-spam-filter))
  447. (defun rsf-custom-save-all ()
  448. "Interactive version of `custom-save-all'."
  449. (interactive)
  450. (custom-save-all))
  451. ;; Add menu items (and keyboard shortcuts) to both rmail and rmail-summary.
  452. (dolist (map (list rmail-summary-mode-map rmail-mode-map))
  453. (easy-menu-define nil map nil
  454. '("Spam"
  455. ["Add subject to spam list" rsf-add-subject-to-spam-list]
  456. ["Add sender to spam list" rsf-add-sender-to-spam-list]
  457. ["Add region to spam list" rsf-add-region-to-spam-list]
  458. ["Save spam definitions" rsf-custom-save-all]
  459. "--"
  460. ["Customize spam definitions" rsf-customize-spam-definitions]
  461. ["Browse spam customizations" rsf-customize-group]
  462. ))
  463. (define-key map "\C-cSt" 'rsf-add-subject-to-spam-list)
  464. (define-key map "\C-cSr" 'rsf-add-sender-to-spam-list)
  465. (define-key map "\C-cSn" 'rsf-add-region-to-spam-list)
  466. (define-key map "\C-cSa" 'rsf-custom-save-all)
  467. (define-key map "\C-cSd" 'rsf-customize-spam-definitions)
  468. (define-key map "\C-cSg" 'rsf-customize-group))
  469. (defun rsf-add-content-type-field ()
  470. "Maintain backward compatibility for `rmail-spam-filter'.
  471. The most recent version of `rmail-spam-filter' checks the content-type
  472. field of the incoming mail to see if it is spam. The format of
  473. `rsf-definitions-alist' has therefore changed. This function
  474. checks to see if the old format is used, and updates it if necessary."
  475. (interactive)
  476. (if (and rsf-definitions-alist
  477. (not (assoc 'content-type (car rsf-definitions-alist))))
  478. (let ((result nil)
  479. (current nil)
  480. (definitions rsf-definitions-alist))
  481. (while definitions
  482. (setq current (car definitions))
  483. (setq definitions (cdr definitions))
  484. (setq result
  485. (append result
  486. (list
  487. (list (assoc 'from current)
  488. (assoc 'to current)
  489. (assoc 'subject current)
  490. (cons 'content-type "")
  491. (assoc 'contents current)
  492. (assoc 'action current))))))
  493. (setq rsf-definitions-alist result)
  494. (customize-mark-to-save 'rsf-definitions-alist)
  495. (if rsf-autosave-newly-added-definitions
  496. (progn
  497. (custom-save-all)
  498. (message "Spam definitions converted to new format, and saved"))
  499. (message "Spam definitions converted to new format (remember to save)")))))
  500. (provide 'rmail-spam-filter)
  501. ;;; rmail-spam-filter ends here