Sync to HEAD
[bpt/emacs.git] / lisp / mail / rmail-spam-filter.el
index 0ae87fe..43177b7 100644 (file)
@@ -3,7 +3,7 @@
 ;; Copyright (C) 2002 
 ;;             Free Software Foundation, Inc.
 ;; Keywords: email, spam, filter, rmail
-;; Author: Eli Tziperman <eli@beach.weizmann.ac.il>
+;; Author: Eli Tziperman <eli AT deas.harvard.edu>
 
 ;; This file is part of GNU Emacs.
 
@@ -41,7 +41,7 @@
 
 ;;; (*) turn on the variable rmail-use-spam-filter,
 
-;;; (*) specify in variable rmail-spam-definitions-alist what sender,
+;;; (*) specify in variable rsf-definitions-alist what sender,
 ;;; subject and contents make an email be considered spam.
 
 ;;; in addition, you may:
 ;;; (*) Block future mail with the subject or sender of a message
 ;;; while reading it in RMAIL: just click on the "Spam" item on the
 ;;; menubar, and add the subject or sender to the list of spam
-;;; definitions using the mouse and the appropriate menu item. Â  You
+;;; definitions using the mouse and the appropriate menu item. You
 ;;; need to later also save the list of spam definitions using the
 ;;; same menu item, or alternatively, see variable
-;;; `rmail-spam-filter-autosave-newly-added-spam-definitions'.
+;;; `rsf-autosave-newly-added-definitions'.
 
 ;;; (*) specify if blind-cc'ed mail (no "To:" header field) is to be
-;;; treated as spam (variable rmail-spam-no-blind-cc; Thanks to Ethan
+;;; treated as spam (variable rsf-no-blind-cc; Thanks to Ethan
 ;;; Brown <ethan@gso.saic.com> for this).
 
 ;;; (*) specify if rmail-spam-filter should ignore case of spam
-;;; definitions (variable rmail-spam-filter-ignore-case; Thanks to
+;;; definitions (variable rsf-ignore-case; Thanks to
 ;;; Ethan Brown <ethan@gso.saic.com> for the suggestion).
 
 ;;; (*) Specify a "white-list" of trusted senders. If any
-;;; rmail-spam-white-list string matches a substring of the "From"
+;;; rsf-white-list string matches a substring of the "From"
 ;;; header, the message is flagged as a valid, non-spam message (Ethan
 ;;; Brown <ethan@gso.saic.com>).
 
+;;; (*) rmail-spam-filter is best used with a general purpose spam
+;;; filter such as the procmail-based http://www.spambouncer.org/.
+;;; Spambouncer is set to only mark messages as spam/blocked/bulk/OK
+;;; via special headers, and these headers may then be defined in
+;;; rmail-spam-filter such that the spam is rejected by
+;;; rmail-spam-filter itself.
+
 ;;; (*) rmail spam filter also works with bbdb to prevent spam senders
 ;;; from entering into the .bbdb file.  See variable
-;;; "rmail-spam-filter-auto-delete-spam-bbdb-entries".  This is done
+;;; "rsf-auto-delete-spam-bbdb-entries".  This is done
 ;;; in two ways: (a) bbdb is made not to auto-create entries for
 ;;; messages that are deleted by the rmail-spam-filter, (b) when a
 ;;; message is deleted in rmail, the user is offered to delete the
 ;;; sender's bbdb entry as well _if_ it was created at the same day.
 
 (require 'rmail)
+(if (> emacs-major-version 20)
+    (require 'rmailsum)
+  (if (not (fboundp 'rmail-make-summary-line)) (load-library "rmailsum")))
 
-;; For find-if and other cool common lisp functions we may want to use. (EDB)
-(require 'cl)                          
+;; For find-if and other cool common lisp functions we may want to use.
+(eval-when-compile
+  (require 'cl))
 
 (defgroup rmail-spam-filter nil
   "Spam filter for RMAIL, the mail reader for Emacs."
 
 (defcustom rmail-use-spam-filter nil
   "*Non-nil to activate the rmail spam filter.
-Specify `rmail-spam-definitions-alist' to define what you consider spam
+Specify `rsf-definitions-alist' to define what you consider spam
 emails."
   :type 'boolean
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-file "~/XRMAIL-SPAM"
+(defcustom rsf-file "~/XRMAIL-SPAM"
   "*Name of rmail file for optionally saving some of the spam.
 Spam may be either just deleted, or saved in a separate spam file to
 be looked at at a later time.  Whether the spam is just deleted or
 also saved in a separete spam file is specified for each definition of
-spam, as one of the fields of `rmail-spam-definitions-alist'"
+spam, as one of the fields of `rsf-definitions-alist'"
   :type 'string
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-no-blind-cc nil
+(defcustom rsf-no-blind-cc nil
   "*Non-nil to treat blind CC (no To: header) as spam."
   :type 'boolean
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-filter-ignore-case nil
-  "*Non-nil to ignore case in `rmail-spam-definitions-alist'."
+(defcustom rsf-ignore-case nil
+  "*Non-nil to ignore case in `rsf-definitions-alist'."
   :type 'boolean
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-filter-beep nil
+(defcustom rsf-beep nil
   "*Non-nil to beep if spam is found."
   :type 'boolean
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-sleep-after-message 2.0
+(defcustom rsf-sleep-after-message 2.0
   "*Seconds to wait after display of message that spam was found."
   :type 'number
   :group 'rmail-spam-filter )
-  
-(defcustom rmail-spam-filter-auto-delete-spam-bbdb-entries nil
+
+(defcustom rsf-min-region-to-spam-list 7
+  "*User may highlight a region in an incomming message and use
+  the menubar to add this region to the spam definitions.  This
+  variable specifies the minimum size of region that may be added
+  to spam list, to avoid accidentally adding a too short region
+  which would result in false positive identification of spam
+  messages."
+  :type 'integer
+  :group 'rmail-spam-filter )
+
+(defcustom rsf-auto-delete-spam-bbdb-entries nil
   "*Non-nil to make sure no entries are made in bbdb for spam emails.
 This is done in two ways: (1) bbdb is made not to auto-create entries
 for messages that are deleted by the `rmail-spam-filter', (2) when a
@@ -131,7 +152,7 @@ take an effect."
   :type 'boolean
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-filter-autosave-newly-added-spam-definitions nil
+(defcustom rsf-autosave-newly-added-definitions nil
   "*Non-nil to auto save new spam entries.
 New entries entered via the spam menu bar item are then saved to
 customization file immediately after being added via the menu bar, and
@@ -140,17 +161,17 @@ entries."
   :type 'boolean
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-white-list nil
+(defcustom rsf-white-list nil
   "*List of strings to identify valid senders.
-If any rmail-spam-white-list string matches a substring of the 'From'
+If any rsf-white-list string matches a substring of the 'From'
 header, the message is flagged as a valid, non-spam message.  Example:
 If your domain is emacs.com then including 'emacs.com' in your
-rmail-spam-white-list would flag all mail from your colleagues as
+rsf-white-list would flag all mail from your colleagues as
 valid."
   :type '(repeat string)
   :group 'rmail-spam-filter )
 
-(defcustom rmail-spam-definitions-alist nil
+(defcustom rsf-definitions-alist nil
   "*Alist matching strings defining what messages are considered spam.
 Each definition may contain specifications of one or more of the
 elements {subject, sender, recipients or contents}, as well as a
@@ -159,7 +180,9 @@ is defined as one that fits all of the specified elements of any one
 of the spam definitions.  The strings that specify spam subject,
 sender, etc, may be regexp.  For example, to specify that the subject
 may be either 'this is spam' or 'another spam', use the regexp: 'this
-is spam\|another spam' (without the single quotes)."
+is spam\\|another spam' (without the single quotes).  To specify that
+if the contents contain both this and that the message is spam,
+specify 'this\\&that' in the appropriate spam definition field."
   :type '(repeat 
           (list :format "%v"
           (cons :format "%v" :value (from . "")
@@ -171,6 +194,9 @@ is spam\|another spam' (without the single quotes)."
           (cons :format "%v" :value (subject . "")
                 (const :format ""  subject)
                 (string :tag "Subject"  ""))
+          (cons :format "%v" :value (content-type . "")
+                (const :format ""  content-type)
+                (string :tag "Content-Type"  ""))
           (cons :format "%v" :value (contents . "")
                 (const :format ""  contents)
                 (string :tag "Contents"  ""))
@@ -183,13 +209,38 @@ is spam\|another spam' (without the single quotes)."
    ))
   :group 'rmail-spam-filter)
 
-(defvar rmail-spam-filter-scanning-messages-now nil
+(defvar rsf-scanning-messages-now nil
   "Non nil when rmail-spam-filter scans messages,
-for interaction with `rmail-bbdb-auto-delete-spam-entries'")
+for interaction with `rsf-bbdb-auto-delete-spam-entries'")
+
+;; the advantage over the automatic filter definitions is the AND conjunction
+;; of in-one-definition-elements
+(defun check-field (field-symbol message-data definition result)
+  "Check if field-symbol is in `rsf-definitions-alist'.
+Capture maybe-spam and this-is-a-spam-email in a cons in result,
+where maybe-spam is in first and this-is-a-spam-email is in rest. 
+The values are returned by destructively changing result.
+If FIELD-SYMBOL field does not exist AND is not specified,
+this may still be spam due to another element...
+if (first result) is nil, we already have a contradiction in another
+field"
+  (let ((definition-field (cdr (assoc field-symbol definition))))
+    (if (and (first result) (> (length definition-field) 0))
+        ;; only in this case can maybe-spam change from t to nil
+        ;; ... else, if FIELD-SYMBOL field does appear in the message,
+        ;; and it also appears in spam definition list, this
+        ;; is potentially a spam:
+        (if (and message-data
+                 (string-match definition-field message-data))
+            ;; if we do not get a contradiction from another field, this is
+            ;; spam
+            (setf (rest result) t)
+          ;; the message data contradicts the specification, this is no spam
+          (setf (first result) nil)))))
 
 (defun rmail-spam-filter (msg)
-  "Return nil if msg is spam based on rmail-spam-definitions-alist.
-If spam, optionally output msg to a file `rmail-spam-file' and delete
+  "Return nil if msg is spam based on rsf-definitions-alist.
+If spam, optionally output msg to a file `rsf-file' and delete
 it from rmail file.  Called for each new message retrieved by
 `rmail-get-new-mail'."
 
@@ -200,22 +251,23 @@ it from rmail file.  Called for each new message retrieved by
        (message-sender)
        (message-recipients)
        (message-subject)
+       (message-content-type)
        (num-spam-definition-elements)
        (num-element 0)
        (exit-while-loop nil)
        (saved-case-fold-search case-fold-search)
        (save-current-msg)
-       (rmail-spam-filter-saved-bbdb/mail_auto_create_p nil)
+       (rsf-saved-bbdb/mail_auto_create_p nil)
        )
     
     ;; make sure bbdb does not create entries for messages while spam
     ;; filter is scanning the rmail file:
-    (setq rmail-spam-filter-saved-bbdb/mail_auto_create_p 'bbdb/mail_auto_create_p)
+    (setq rsf-saved-bbdb/mail_auto_create_p 'bbdb/mail_auto_create_p)
     (setq bbdb/mail_auto_create_p nil)
-    ;; let `rmail-bbdb-auto-delete-spam-entries' know that rmail spam
+    ;; let `rsf-bbdb-auto-delete-spam-entries' know that rmail spam
     ;; filter is running, so that deletion of rmail messages should be
     ;; ignored for now:
-    (setq rmail-spam-filter-scanning-messages-now t)
+    (setq rsf-scanning-messages-now t)
     (save-excursion
       (save-restriction
        (setq this-is-a-spam-email nil)
@@ -225,166 +277,111 @@ it from rmail file.  Called for each new message retrieved by
          (goto-char (rmail-msgbeg msg))
          (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
          (setq message-sender (mail-fetch-field "From"))
-         (setq message-recipients (mail-fetch-field "To"))
+         (setq message-recipients
+               (concat (mail-fetch-field "To")
+                       (if (mail-fetch-field "Cc")
+                           (concat ", " (mail-fetch-field "Cc")))))
          (setq message-subject (mail-fetch-field "Subject"))
+         (setq message-content-type (mail-fetch-field "Content-Type"))
          )
        ;; Find number of spam-definition elements in the list
-       ;; rmail-spam-definitions-alist specified by user:
+       ;; rsf-definitions-alist specified by user:
        (setq num-spam-definition-elements (safe-length
-                                           rmail-spam-definitions-alist))
+                                           rsf-definitions-alist))
 
        ;;; do we want to ignore case in spam definitions:
-         (setq case-fold-search rmail-spam-filter-ignore-case)
+         (setq case-fold-search rsf-ignore-case)
        
        ;; Check for blind CC condition.  Set vars such that while
-       ;; loop will be bypassed and spam condition will trigger (EDB)
-       (if (and rmail-spam-no-blind-cc
+       ;; loop will be bypassed and spam condition will trigger
+       (if (and rsf-no-blind-cc
                 (null message-recipients))
-           (progn
-             (setq exit-while-loop t)
-             (setq maybe-spam t)
-             (setq this-is-a-spam-email t)))
-       
-         ;; Check white list, and likewise cause while loop
-         ;;  bypass. (EDB)
-         (if (find-if '(lambda (white-str)
-                         (string-match white-str message-sender))
-                      rmail-spam-white-list)
-             (progn
-               (setq exit-while-loop t)
-               (setq maybe-spam nil)
-               (setq this-is-a-spam-email nil)))
-           
-       ;; scan all elements of the list rmail-spam-definitions-alist
+           (setq exit-while-loop t
+                  maybe-spam t
+                  this-is-a-spam-email t))
+
+        ;; Check white list, and likewise cause while loop
+        ;;  bypass.
+        (if (let ((white-list rsf-white-list)
+                  (found nil))
+              (while (and (not found) white-list)
+                (if (string-match (car white-list) message-sender)
+                    (setq found t)
+                  (setq white-list (cdr white-list))))
+              found)
+            (setq exit-while-loop t
+                  maybe-spam nil
+                  this-is-a-spam-email nil))
+
+        ;; maybe-spam is in first, this-is-a-spam-email in rest, this
+        ;; simplifies the call to check-field
+        (setq maybe-spam (cons maybe-spam this-is-a-spam-email))
+
+       ;; scan all elements of the list rsf-definitions-alist
        (while (and
                (< num-element num-spam-definition-elements)
                (not exit-while-loop))
-         (progn
+          (let ((definition (nth num-element rsf-definitions-alist)))
            ;; Initialize maybe-spam which is set to t in one of two
            ;; cases: (1) unspecified definition-elements are found in
-           ;; rmail-spam-definitions-alist, (2) empty field is found
+           ;; rsf-definitions-alist, (2) empty field is found
            ;; in the message being scanned (e.g. empty subject,
            ;; sender, recipients, etc).  The variable is set to nil
            ;; if a non empty field of the scanned message does not
            ;; match a specified field in
-           ;; rmail-spam-definitions-alist.
-           (setq maybe-spam t)
+           ;; rsf-definitions-alist.
+
            ;; initialize this-is-a-spam-email to nil.  This variable
            ;; is set to t if one of the spam definitions matches a
            ;; field in the scanned message.
-           (setq this-is-a-spam-email nil)
+            (setq maybe-spam (cons t nil))
 
            ;; start scanning incoming message:
            ;;---------------------------------
            
-           ;; if sender field is not specified in message being
+            ;; Maybe the different fields should also be done in a
+            ;; loop to make the whole thing more flexible
+           ;; if sender field is not specified in message being
            ;; scanned, AND if "from" field does not appear in spam
            ;; definitions for this element, this may still be spam
            ;; due to another element...
-           (if (and (not message-sender)
-                    (string-match
-                     (cdr (assoc 'from (nth num-element
-                                            rmail-spam-definitions-alist))) ""))
-               (setq maybe-spam t)
-             ;; ... else, if message-sender does appear in the
-             ;; message, and it also appears in the spam definition
-             ;; list, it is potentially spam:
-             (if (and message-sender
-                      (string-match
-                       (cdr (assoc 'from (nth num-element
-                                              rmail-spam-definitions-alist)))
-                       message-sender)
-                      )
-                 (setq this-is-a-spam-email t)
-               (setq maybe-spam nil)
-               )
-             )
-           ;; next, if spam was not ruled out already, check recipients:
-           (if maybe-spam
-               ;; if To field does not exist AND is not specified,
-               ;; this may still be spam due to another element...
-               (if (and (not message-recipients)
-                        (string-match
-                         (cdr (assoc 'to
-                                     (nth num-element
-                                          rmail-spam-definitions-alist))) ""))
-                   (setq maybe-spam t)
-                 ;; ... else, if To field does appear in the message,
-                 ;; and it also appears in spam definition list, this
-                 ;; is potentially a spam:
-                 (if (and message-recipients
-                          (string-match
-                           (cdr (assoc 'to (nth num-element
-                                                rmail-spam-definitions-alist)))
-                           message-recipients)
-                          )
-                     (setq this-is-a-spam-email t)
-                   (setq maybe-spam nil)
-                   )
-                 )
-             )
-           ;; next, if spam was not ruled out already, check subject:
-           (if maybe-spam
-               ;; if subject field does not exist AND is not
-               ;; specified, this may still be spam due to another
-               ;; element...
-               (if (and (not message-subject)
-                       (string-match
-                        (cdr (assoc 'subject
-                                    (nth num-element
-                                         rmail-spam-definitions-alist)))
-                        ""))
-                   (setq maybe-spam t)
-                 ;; ... else, if subject field does appear in the
-                 ;; message, and it also appears in the spam
-                 ;; definition list, this is potentially a spam:
-                 (if (and message-subject
-                          (string-match
-                           (cdr (assoc 'subject (nth num-element
-                                                     rmail-spam-definitions-alist)))
-                           message-subject)
-                          )
-                     (setq this-is-a-spam-email t)
-                   (setq maybe-spam nil)
-                   )
-                 )
-             )
+            (check-field 'from message-sender definition maybe-spam)
+           ;; next, if spam was not ruled out already, check recipients:
+            (check-field 'to message-recipients definition maybe-spam)
+           ;; next, if spam was not ruled out already, check subject:
+            (check-field 'subject message-subject definition maybe-spam)
+           ;; next, if spam was not ruled out already, check content-type:
+            (check-field 'content-type message-content-type 
+                         definition maybe-spam)
            ;; next, if spam was not ruled out already, check
            ;; contents: if contents field is not specified, this may
            ;; still be spam due to another element...
-           (if maybe-spam
-               (if (string-match
-                    (cdr (assoc 'contents
-                                (nth num-element
-                                     rmail-spam-definitions-alist))) "")
-                   (setq maybe-spam t)
-                 ;; ... else, check to see if it appears in spam
-                 ;; definition:
-                 (if (string-match
-                      (cdr (assoc 'contents
-                                  (nth num-element
-                                       rmail-spam-definitions-alist)))
-                      (buffer-substring
-                       (rmail-msgbeg msg) (rmail-msgend msg)))
-                     (setq this-is-a-spam-email t)
-                   (setq maybe-spam nil)))
-             )
-           ;; if the search in rmail-spam-definitions-alist found
+            (check-field 'contents 
+                         (buffer-substring
+                          (rmail-msgbeg msg) (rmail-msgend msg))
+                         definition maybe-spam)
+            
+           ;; if the search in rsf-definitions-alist found
            ;; that this email is spam, output the email to the spam
            ;; rmail file, mark the email for deletion, leave the
            ;; while loop and return nil so that an rmail summary line
            ;; wont be displayed for this message:
-           (if (and this-is-a-spam-email maybe-spam)
+           (if (and (first maybe-spam) (rest maybe-spam))
                ;; found that this is spam, no need to look at the
-               ;; rest of the rmail-spam-definitions-alist, exit
+               ;; rest of the rsf-definitions-alist, exit
                ;; loop:
                (setq exit-while-loop t)
              ;; else, spam was not yet found, increment number of
-             ;; element in rmail-spam-definitions-alist and proceed
+             ;; element in rsf-definitions-alist and proceed
              ;; to next element:
              (setq num-element (+ num-element 1)))
            )
-         )
+          )
+        
+        ;; (BK) re-set originally used variables
+        (setq this-is-a-spam-email (rest maybe-spam)
+              maybe-spam (first maybe-spam))
+
        (if (and this-is-a-spam-email maybe-spam)
            (progn
              ;;(message "Found spam!")
@@ -394,39 +391,42 @@ it from rmail file.  Called for each new message retrieved by
              ;; output and delete the spam msg if needed:
              (setq save-current-msg rmail-current-message)
              (setq rmail-current-message msg)
-             ;; check action item and rmail-spam-definitions-alist
+             ;; check action item and rsf-definitions-alist
              ;; and do it:
              (cond
               ((equal (cdr (assoc 'action
-                                  (nth num-element rmail-spam-definitions-alist)))
+                                  (nth num-element rsf-definitions-alist)))
                       'output-and-delete)
                (progn
-                 (rmail-output-to-rmail-file rmail-spam-file)
-                 (rmail-delete-message)
+                 (rmail-output-to-rmail-file rsf-file 1 t)
+                  ;; Don't delete if automatic deletion after output
+                  ;; is turned on
+                 (unless rmail-delete-after-output (rmail-delete-message))
                  ))
               ((equal (cdr (assoc 'action
-                                  (nth num-element rmail-spam-definitions-alist)))
+                                  (nth num-element rsf-definitions-alist)))
                       'delete-spam)
                (progn
                  (rmail-delete-message)
                  ))
               )
               (setq rmail-current-message save-current-msg)
-              (setq bbdb/mail_auto_create_p 'rmail-spam-filter-saved-bbdb/mail_auto_create_p)
+              (setq bbdb/mail_auto_create_p
+              'rsf-saved-bbdb/mail_auto_create_p)
              ;; set return value.  These lines must be last in the
              ;; function, so that they will determine the value
              ;; returned by rmail-spam-filter:
              (setq return-value nil))
            (setq return-value t))))
     (setq case-fold-search saved-case-fold-search)
-    (setq rmail-spam-filter-scanning-messages-now nil)
+    (setq rsf-scanning-messages-now nil)
     return-value))
 
 
 ;; define functions for interactively adding sender/subject of a
 ;; specific message to the spam definitions while reading it, using
 ;; the menubar:
-(defun rmail-spam-filter-add-subject-to-spam-list ()
+(defun rsf-add-subject-to-spam-list ()
   (interactive)
   (set-buffer rmail-buffer)
   (let ((message-subject))
@@ -434,15 +434,16 @@ it from rmail file.  Called for each new message retrieved by
     ;; note the use of a backquote and comma on the subject line here,
     ;; to make sure message-subject is actually evaluated and its value
     ;; substituted:
-    (add-to-list 'rmail-spam-definitions-alist
+    (add-to-list 'rsf-definitions-alist
                 (list '(from . "")
                       '(to . "")
                       `(subject . ,message-subject)
+                      '(content-type . "")
                       '(contents . "")
                       '(action . output-and-delete))
                 t)
-    (customize-mark-to-save 'rmail-spam-definitions-alist)
-    (if rmail-spam-filter-autosave-newly-added-spam-definitions
+    (customize-mark-to-save 'rsf-definitions-alist)
+    (if rsf-autosave-newly-added-definitions
        (progn
          (custom-save-all)
          (message (concat "added subject \n <<< \n" message-subject
@@ -450,10 +451,11 @@ it from rmail file.  Called for each new message retrieved by
                           "and saved the spam definitions to file.")))
       (message (concat "added subject \n <<< \n" message-subject
                       " \n >>> \n to list of spam definitions. \n"
-                      "Don't forget to save the spam definitions to file using the spam menu"))
+                      "Don't forget to save the spam definitions to file using the spam
+                      menu"))
       )))
 
-(defun rmail-spam-filter-add-sender-to-spam-list ()
+(defun rsf-add-sender-to-spam-list ()
   (interactive)
   (set-buffer rmail-buffer)
   (let ((message-sender))
@@ -461,15 +463,16 @@ it from rmail file.  Called for each new message retrieved by
     ;; note the use of a backquote and comma on the "from" line here,
     ;; to make sure message-sender is actually evaluated and its value
     ;; substituted:
-    (add-to-list 'rmail-spam-definitions-alist
+    (add-to-list 'rsf-definitions-alist
                 (list `(from . ,message-sender)
                       '(to . "")
                       '(subject . "")
+                      '(content-type . "")
                       '(contents . "")
                       '(action . output-and-delete))
                 t)
-    (customize-mark-to-save 'rmail-spam-definitions-alist)
-    (if rmail-spam-filter-autosave-newly-added-spam-definitions
+    (customize-mark-to-save 'rsf-definitions-alist)
+    (if rsf-autosave-newly-added-definitions
        (progn
          (custom-save-all)
          (message (concat "added sender \n <<< \n" message-sender
@@ -477,13 +480,14 @@ it from rmail file.  Called for each new message retrieved by
                           "and saved the spam definitions to file.")))
       (message (concat "added sender \n <<< \n " message-sender
                       " \n >>> \n to list of spam definitions."
-                      "Don't forget to save the spam definitions to file using the spam menu"))
+                      "Don't forget to save the spam definitions to file using the spam
+                      menu"))
       )))
 
 
-(defun rmail-spam-filter-add-region-to-spam-list ()
-  "Add the region makred by user in the rmail buffer to the list of
-  spam definitions as a contents field."
+(defun rsf-add-region-to-spam-list ()
+  "Add the region makred by user in the rmail buffer to spam list.
+Added to spam definitions as a contents field."
   (interactive)
   (set-buffer rmail-buffer)
   (let ((region-to-spam-list))
@@ -491,41 +495,48 @@ it from rmail file.  Called for each new message retrieved by
     (if (not (and mark-active (not (= (region-beginning) (region-end)))))
        ;; if inactive, print error message:
        (message "you need to first highlight some text in the rmail buffer")
-      ;; if active, add to list of spam definisions:
-      (progn
-       (setq region-to-spam-list (buffer-substring (region-beginning) (region-end)))
-       ;; note the use of a backquote and comma on the "from" line here,
-       ;; to make sure message-sender is actually evaluated and its value
-       ;; substituted:
-       (add-to-list 'rmail-spam-definitions-alist
-                    (list '(from . "")
-                          '(to . "")
-                          '(subject . "")
-                          `(contents . ,region-to-spam-list)
-                          '(action . output-and-delete))
-                    t)
-       (customize-mark-to-save 'rmail-spam-definitions-alist)
-       (if rmail-spam-filter-autosave-newly-added-spam-definitions
-           (progn
-             (custom-save-all)
-             (message (concat "added highlighted text \n <<< \n" region-to-spam-list
-                              " \n >>> \n to list of spam definitions. \n"
-                              "and saved the spam definitions to file.")))
-         (message (concat "added highlighted text \n <<< \n " region-to-spam-list
-                          " \n >>> \n to list of spam definitions."
-                          "Don't forget to save the spam definitions to file using the spam menu"))
-         )))))
-
-
-(defun rmail-spam-filter-customize-spam-definitions ()
+      (if (< (- (region-end) (region-beginning)) rsf-min-region-to-spam-list)
+         (message
+          (concat "highlighted region is too small; min length set by variable \n"
+                  "rsf-min-region-to-spam-list"
+                  " is " (number-to-string rsf-min-region-to-spam-list)))
+       ;; if region active and long enough, add to list of spam definisions:
+       (progn
+         (setq region-to-spam-list (buffer-substring (region-beginning) (region-end)))
+         ;; note the use of a backquote and comma on the "from" line here,
+         ;; to make sure message-sender is actually evaluated and its value
+         ;; substituted:
+         (add-to-list 'rsf-definitions-alist
+                      (list '(from . "")
+                            '(to . "")
+                            '(subject . "")
+                            '(content-type . "")
+                            `(contents . ,region-to-spam-list)
+                            '(action . output-and-delete))
+                      t)
+         (customize-mark-to-save 'rsf-definitions-alist)
+         (if rsf-autosave-newly-added-definitions
+             (progn
+               (custom-save-all)
+               (message (concat "added highlighted text \n <<< \n" region-to-spam-list
+                                " \n >>> \n to list of spam definitions. \n"
+                                "and saved the spam definitions to file.")))
+           (message (concat "added highlighted text \n <<< \n " region-to-spam-list
+                            " \n >>> \n to list of spam definitions."
+                            "Don't forget to save the spam definitions to file using the
+                            spam menu"))
+           ))))))
+
+
+(defun rsf-customize-spam-definitions ()
   (interactive)
-  (customize-variable (quote rmail-spam-definitions-alist)))
+  (customize-variable (quote rsf-definitions-alist)))
 
-(defun rmail-spam-filter-customize-group ()
+(defun rsf-customize-group ()
   (interactive)
   (customize-group (quote rmail-spam-filter)))
 
-(defun rmail-spam-custom-save-all ()
+(defun rsf-custom-save-all ()
   (interactive)
   (custom-save-all))
 
@@ -537,96 +548,89 @@ it from rmail file.  Called for each new message retrieved by
   (cons "Spam" (make-sparse-keymap "Spam")))
 
 (define-key rmail-summary-mode-map [menu-bar spam customize-group]
-  '("Browse customizations of rmail spam filter" . rmail-spam-filter-customize-group))
+  '("Browse customizations of rmail spam filter" . rsf-customize-group))
 (define-key rmail-mode-map [menu-bar spam customize-group]
-  '("Browse customizations of rmail spam filter" . rmail-spam-filter-customize-group))
-(define-key rmail-summary-mode-map "\C-cSg" 'rmail-spam-filter-customize-group)
-(define-key rmail-mode-map "\C-cSg" 'rmail-spam-filter-customize-group)
+  '("Browse customizations of rmail spam filter" . rsf-customize-group))
+(define-key rmail-summary-mode-map "\C-cSg" 'rsf-customize-group)
+(define-key rmail-mode-map "\C-cSg" 'rsf-customize-group)
 
 (define-key rmail-summary-mode-map [menu-bar spam customize-spam-list]
-  '("Customize list of spam definitions" . rmail-spam-filter-customize-spam-definitions))
+  '("Customize list of spam definitions" . rsf-customize-spam-definitions))
 (define-key rmail-mode-map [menu-bar spam customize-spam-list]
-  '("Customize list of spam definitions" . rmail-spam-filter-customize-spam-definitions))
-(define-key rmail-summary-mode-map "\C-cSd" 'rmail-spam-filter-customize-spam-definitions)
-(define-key rmail-mode-map "\C-cSd" 'rmail-spam-filter-customize-spam-definitions)
+  '("Customize list of spam definitions" . rsf-customize-spam-definitions))
+(define-key rmail-summary-mode-map "\C-cSd" 'rsf-customize-spam-definitions)
+(define-key rmail-mode-map "\C-cSd" 'rsf-customize-spam-definitions)
 
 (define-key rmail-summary-mode-map [menu-bar spam lambda] '("----"))
 (define-key rmail-mode-map [menu-bar spam lambda] '("----"))
 
 (define-key rmail-summary-mode-map [menu-bar spam my-custom-save-all]
-  '("save newly added spam definitions to customization file" . rmail-spam-custom-save-all))
+  '("save newly added spam definitions to customization file" . rsf-custom-save-all))
 (define-key rmail-mode-map [menu-bar spam my-custom-save-all]
-  '("save newly added spam definitions to customization file" . rmail-spam-custom-save-all))
-(define-key rmail-summary-mode-map "\C-cSa" 'rmail-spam-custom-save-all)
-(define-key rmail-mode-map "\C-cSa" 'rmail-spam-custom-save-all)
+  '("save newly added spam definitions to customization file" . rsf-custom-save-all))
+(define-key rmail-summary-mode-map "\C-cSa" 'rsf-custom-save-all)
+(define-key rmail-mode-map "\C-cSa" 'rsf-custom-save-all)
 
 (define-key rmail-summary-mode-map [menu-bar spam add-region-to-spam-list]
-  '("add region to spam list" . rmail-spam-filter-add-region-to-spam-list))
+  '("add region to spam list" . rsf-add-region-to-spam-list))
 (define-key rmail-mode-map [menu-bar spam add-region-to-spam-list]
-  '("add region to spam list" . rmail-spam-filter-add-region-to-spam-list))
-(define-key rmail-summary-mode-map "\C-cSn" 'rmail-spam-filter-add-region-to-spam-list)
-(define-key rmail-mode-map "\C-cSn" 'rmail-spam-filter-add-region-to-spam-list)
+  '("add region to spam list" . rsf-add-region-to-spam-list))
+(define-key rmail-summary-mode-map "\C-cSn" 'rsf-add-region-to-spam-list)
+(define-key rmail-mode-map "\C-cSn" 'rsf-add-region-to-spam-list)
 
 (define-key rmail-summary-mode-map [menu-bar spam add-sender-to-spam-list]
-  '("add sender to spam list" . rmail-spam-filter-add-sender-to-spam-list))
+  '("add sender to spam list" . rsf-add-sender-to-spam-list))
 (define-key rmail-mode-map [menu-bar spam add-sender-to-spam-list]
-  '("add sender to spam list" . rmail-spam-filter-add-sender-to-spam-list))
-(define-key rmail-summary-mode-map "\C-cSr" 'rmail-spam-filter-add-sender-to-spam-list)
-(define-key rmail-mode-map "\C-cSr" 'rmail-spam-filter-add-sender-to-spam-list)
+  '("add sender to spam list" . rsf-add-sender-to-spam-list))
+(define-key rmail-summary-mode-map "\C-cSr" 'rsf-add-sender-to-spam-list)
+(define-key rmail-mode-map "\C-cSr" 'rsf-add-sender-to-spam-list)
 
 (define-key rmail-summary-mode-map [menu-bar spam add-subject-to-spam-list]
-  '("add subject to spam list" . rmail-spam-filter-add-subject-to-spam-list))
+  '("add subject to spam list" . rsf-add-subject-to-spam-list))
 (define-key rmail-mode-map [menu-bar spam add-subject-to-spam-list]
-  '("add subject to spam list" . rmail-spam-filter-add-subject-to-spam-list))
-(define-key rmail-summary-mode-map "\C-cSt" 'rmail-spam-filter-add-subject-to-spam-list)
-(define-key rmail-mode-map "\C-cSt" 'rmail-spam-filter-add-subject-to-spam-list)
-
-
-(defun rmail-bbdb-auto-delete-spam-entries ()
-  "When deleting a message in RMAIL, check to see if the bbdb entry
-was created today, and if it was, prompt to delete it too.  This function 
-needs to be called via the `rmail-delete-message-hook' like this:
-\(add-hook 'rmail-delete-message-hook 'rmail-bbdb-auto-delete-spam-entries)"
-  (interactive)
-  (require 'bbdb-hooks)
-  (if (not rmail-spam-filter-scanning-messages-now)
-      (if (get-buffer "*BBDB*")
-         (save-excursion
-           (set-buffer (get-buffer "*BBDB*"))
-           (if (bbdb-current-record)
-               (if (equal
-                    (format-time-string bbdb-time-internal-format (current-time))
-                    (bbdb-record-getprop (bbdb-current-record) 'creation-date))
-                   (bbdb-delete-current-record (bbdb-current-record))))))))
-
-(defun rmail-spam-filter-bbdb-dont-create-entries-for-spam ()
-  "Make sure senderes of rmail messages marked as deleted are not added to bbdb.
-Need to add this as a hook like this:
-\(setq bbdb/mail-auto-create-p 'rmail-spam-filter-bbdb-dont-create-entries-for-spam)
-and this is also used in conjunction with rmail-bbdb-auto-delete-spam-entries. 
-More doc: rmail-bbdb-auto-delete-spam-entries will delete newly created bbdb 
-entries of mail that is deleted.  However, if one scrolls back to the deleted 
-messages, then the sender is again added to the bbdb.  This function 
-prevents this.  Also, don't create entries for messages in the `rmail-spam-file'."
+  '("add subject to spam list" . rsf-add-subject-to-spam-list))
+(define-key rmail-summary-mode-map "\C-cSt" 'rsf-add-subject-to-spam-list)
+(define-key rmail-mode-map "\C-cSt" 'rsf-add-subject-to-spam-list)
+
+(defun rsf-add-content-type-field ()
+  "Maintain backward compatibility with previous versions of rmail-spam-filter.
+The most recent version of rmai-spam-filter checks the contents
+field of the incoming mail to see if it spam.  The format of
+`rsf-definitions-alist' has therefore changed.  This function
+checks to see if old format is used, and if it is, it converts
+`rsf-definitions-alist' to the new format.  Invoked
+automatically, no user input is required."
   (interactive)
-  (not
-   ;; don't create a bbdb entry if one of the following conditions is satisfied: 
-   (or
-    ;; 1) looking at a deleted message:
-    (rmail-message-deleted-p rmail-current-message)
-    ;; 2) looking at messages in rmail-spam-file:
-    (string-match
-     (expand-file-name rmail-spam-file)
-     (expand-file-name (buffer-file-name rmail-buffer)))
-    )))
-
-;; activate bbdb-anti-spam measures:
-(if rmail-spam-filter-auto-delete-spam-bbdb-entries
-    (progn
-      (add-hook 'rmail-delete-message-hook 'rmail-bbdb-auto-delete-spam-entries)
-      (setq bbdb/mail-auto-create-p 'rmail-spam-filter-bbdb-dont-create-entries-for-spam)
-      ))
+  (if (and rsf-definitions-alist
+           (not (assoc 'content-type (car rsf-definitions-alist))))
+      (let ((result nil)
+            (current nil)
+            (definitions rsf-definitions-alist))
+        (while definitions
+          (setq current (car definitions))
+          (setq definitions (cdr definitions))
+          (setq result 
+                (append result
+                        (list 
+                         (list (assoc 'from current)
+                               (assoc 'to current)
+                               (assoc 'subject current)
+                               (cons 'content-type "")
+                               (assoc 'contents current)
+                               (assoc 'action current))))))
+        (setq rsf-definitions-alist result)
+        (customize-mark-to-save 'rsf-definitions-alist)
+        (if rsf-autosave-newly-added-definitions
+            (progn
+              (custom-save-all)
+              (message (concat "converted spam definitions to new format\n"
+                               "and saved the spam definitions to file.")))
+          (message (concat "converted spam definitions to new format\n"
+                           "Don't forget to save the spam definitions to file using the
+                           spam menu"))
+          ))))
 
 (provide 'rmail-spam-filter)
 
-;;; rmail-spam-filter ends here
+;;; arch-tag: 03e1d45d-b72f-4dd7-8f04-e7fd78249746
+;;; rmail-spam-fitler ends here