1 ;;; elmo-spam.el --- Spam filtering interface to processor.
3 ;; Copyright (C) 2003 Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
4 ;; Copyright (C) 2003 Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Author: Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news, spam
9 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
33 (eval-when-compile (require 'cl
))
39 (defgroup elmo-spam nil
40 "Spam configuration for wanderlust."
43 (defcustom elmo-spam-scheme nil
44 "*Scheme of spam processor implementation. "
45 :type
'(choice (const :tag
"none" nil
)
46 (const :tag
"Bogofilter" bogofilter
)
47 (const :tag
"Spamfilter" spamfilter
)
48 (const :tag
"SpamAssassin" sa
)
49 (const :tag
"Header" header
)
50 (const :tag
"Bsfilter" bsfilter
))
54 (luna-define-class elsp-generic
()))
57 (luna-define-generic elmo-spam-buffer-spam-p
(processor buffer
59 "Return non-nil if contents of BUFFER is spam.
60 PROCESSOR is spam processor structure.
61 If optional augument REGISTER is non-nil,
62 register according to the classification.")
64 (luna-define-generic elmo-spam-register-spam-buffer
(processor
67 "Register contents of BUFFER as spam.
68 PROCESSOR is spam processor structure.
69 If optional argument RESTORE is non-nil, unregister from non-spam list.")
71 (luna-define-generic elmo-spam-register-good-buffer
(processor
74 "Register contents of BUFFER as non-spam.
75 PROCESSOR is spam processor structure.
76 If optional argument RESTORE is non-nil, unregister from spam list.")
79 (luna-define-generic elmo-spam-modified-p
(processor)
80 "Return non-nil if status of PROCESSOR is modified.")
82 (luna-define-generic elmo-spam-save-status
(processor)
83 "Save status of the PROCESSOR.")
85 (luna-define-generic elmo-spam-message-spam-p
(processor folder number
87 "Return non-nil if the message in the FOLDER with NUMBER is spam.
88 PROCESSOR is spam processor structure.
89 If optional augument REGISTER is non-nil,
90 register according to the classification.")
92 (luna-define-generic elmo-spam-list-spam-messages
(processor
93 folder
&optional numbers
)
94 "Return a list of message numbers which is classified as spam.
95 PROCESSOR is spam processor structure.
96 FOLDER is the ELMO folder structure.
97 If optional argument NUMBERS is specified and is a list of message numbers,
98 messages are searched from the list.")
100 (luna-define-generic elmo-spam-register-spam-messages
(processor
104 "Register contents of messages as spam.
105 PROCESSOR is spam processor structure.
106 FOLDER is the ELMO folder structure.
107 If optional argument NUMBERS is specified and is a list of message numbers,
108 messages are searched from the list.
109 If optional argument RESTORE is non-nil, unregister from non-spam list.")
111 (luna-define-generic elmo-spam-register-good-messages
(processor
115 "Register contents of messages as non spam.
116 PROCESSOR is spam processor structure.
117 FOLDER is the ELMO folder structure.
118 If optional argument NUMBERS is specified and is a list of message numbers,
119 messages are searched from the list.
120 If optional argument RESTORE is non-nil, unregister from spam list.")
123 (defun elmo-spam-message-fetch (folder number
)
124 (let (elmo-message-fetch-threshold)
125 (when enable-multibyte-characters
126 (set-buffer-multibyte nil
))
129 (elmo-find-fetch-strategy folder number nil
'entire
)
132 (defun elmo-spam-process-messages-as-mbox (folder numbers number-per-process
137 (while (and numbers
(< count number-per-process
))
138 (insert "From MAILER-DAEMON@example.com\n")
139 (let ((begin (point)))
142 (elmo-spam-message-fetch folder
(car numbers
))
145 (while (re-search-forward "^>*From " nil t
)
146 (goto-char (match-beginning 0))
149 (goto-char (point-max))
151 (setq count
(1+ count
)
152 numbers
(cdr numbers
)))
153 (apply function count args
)
157 (luna-define-method elmo-spam-message-spam-p
((processor elsp-generic
)
158 folder number
&optional register
)
160 (elmo-spam-message-fetch folder number
)
161 (elmo-spam-buffer-spam-p processor
(current-buffer) register
)))
163 (luna-define-method elmo-spam-list-spam-messages
((processor elsp-generic
)
164 folder
&optional numbers
)
165 (let ((numbers (or numbers
(elmo-folder-list-messages folder t t
)))
167 (dolist (number numbers
)
168 (when (elmo-spam-message-spam-p processor folder number
)
169 (setq spam-list
(cons number spam-list
)))
170 (elmo-progress-notify 'elmo-spam-check-spam
))
171 (nreverse spam-list
)))
173 (luna-define-method elmo-spam-register-spam-messages
((processor elsp-generic
)
177 (let ((numbers (or numbers
(elmo-folder-list-messages folder t t
))))
179 (dolist (number numbers
)
181 (elmo-spam-message-fetch folder number
)
182 (elmo-spam-register-spam-buffer processor
(current-buffer) restore
)
183 (elmo-progress-notify 'elmo-spam-register
)))))
185 (luna-define-method elmo-spam-register-good-messages
((processor elsp-generic
)
189 (let ((numbers (or numbers
(elmo-folder-list-messages folder t t
))))
191 (dolist (number numbers
)
193 (elmo-spam-message-fetch folder number
)
194 (elmo-spam-register-good-buffer processor
(current-buffer) restore
)
195 (elmo-progress-notify 'elmo-spam-register
)))))
197 (provide 'elsp-generic
)
199 (defvar elmo-spam-processor-internal nil
)
201 (defun elmo-spam-processor (&optional if-exists
)
202 (or elmo-spam-processor-internal
204 (let* ((scheme (or elmo-spam-scheme
'generic
))
205 (class (intern (format "elsp-%s" scheme
))))
207 (setq elmo-spam-processor-internal
208 (luna-make-entity class
))))))
210 ;; Backend for header match
212 (luna-define-class elsp-header
(elsp-generic)))
214 (defgroup elmo-spam-header nil
215 "Spam header configuration."
218 (defcustom elmo-spam-header-good-alist
'(("X-Spam-Flag" .
"No"))
219 "*Regular expression for positive header good matches."
220 :type
'(repeat (cons (string :tag
"Header name")
221 (regexp :tag
"Regular expression to match good header")))
222 :group
'elmo-spam-header
)
224 (defcustom elmo-spam-header-spam-alist
'(("X-Spam-Flag" .
"Yes"))
225 "*Regular expression for positive header spam matches."
226 :type
'(repeat (cons (string :tag
"Header name")
227 (regexp :tag
"Regular expression to match spam header")))
228 :group
'elmo-spam-header
)
230 (defun elmo-spam-header-check-headers (fetch-field-function)
232 (dolist (pair elmo-spam-header-good-alist
)
233 (let ((field-body (funcall fetch-field-function
(car pair
))))
234 (when (and field-body
(string-match (cdr pair
) field-body
))
236 (dolist (pair elmo-spam-header-spam-alist
)
237 (let ((field-body (funcall fetch-field-function
(car pair
))))
238 (when (and field-body
(string-match (cdr pair
) field-body
))
241 (luna-define-method elmo-spam-buffer-spam-p
((processor elsp-header
)
242 buffer
&optional register
)
243 (with-current-buffer buffer
245 (std11-narrow-to-header)
246 (elmo-spam-header-check-headers #'elmo-decoded-field-body
))))
248 (luna-define-method elmo-spam-message-spam-p
((processor elsp-header
)
249 folder number
&optional register
)
250 (let ((entity (elmo-message-entity folder number
))
254 (elmo-spam-header-check-headers
256 (or (elmo-message-entity-field entity
257 (intern (downcase field-name
))
261 (setq buffer
(get-buffer-create
262 (generate-new-buffer-name
263 " *elmo-spam-work*")))
265 (elmo-spam-message-fetch folder number
)
266 (std11-narrow-to-header))
267 (elmo-decoded-field-body field-name
))))))
268 (and buffer
(kill-buffer buffer
)))))
270 (luna-define-method elmo-spam-register-spam-messages
((processor elsp-header
)
274 (elmo-progress-notify 'elmo-spam-register
(length numbers
)))
276 (luna-define-method elmo-spam-register-good-messages
((processor elsp-header
)
280 (elmo-progress-notify 'elmo-spam-register
(length numbers
)))
282 (provide 'elsp-header
)
286 (product-provide (provide 'elmo-spam
) (require 'elmo-version
))
288 ;;; elmo-spam.el ends here