1 ;;; wl-spam.el --- Spam filtering interface for Wanderlust.
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))
38 (require 'wl-highlight)
41 "Spam configuration for wanderlust."
44 (defcustom wl-spam-folder-name "+spam"
49 (defcustom wl-spam-undecided-folder-regexp-list '("inbox")
50 "*List of folder regexp which is contained undecided domain."
51 :type '(repeat (regexp :tag "Folder Regexp"))
54 (defcustom wl-spam-ignored-folder-regexp-list
55 (list (regexp-opt (list wl-draft-folder
58 "*List of folder regexp which is contained ignored domain."
59 :type '(repeat (regexp :tag "Folder Regexp"))
62 (defcustom wl-spam-auto-check-folder-regexp-list nil
63 "*List of Folder regexp which check spam automatically."
64 :type '(repeat (regexp :tag "Folder Regexp"))
67 (defcustom wl-spam-auto-check-marks
68 (list wl-summary-new-uncached-mark
69 wl-summary-new-cached-mark)
70 "Persistent marks to check spam automatically."
71 :type '(choice (const :tag "All marks" all)
72 (repeat (string :tag "Mark")))
75 (wl-defface wl-highlight-summary-spam-face
80 (:foreground "LightSlateGray")))
81 "Face used for displaying messages mark as spam."
82 :group 'wl-summary-faces
85 (defcustom wl-spam-mark-action-list
89 wl-summary-register-temp-mark
90 wl-summary-exec-action-spam
91 wl-highlight-summary-spam-face
92 "Mark messages as spam."))
93 "A variable to define Mark & Action for spam.
94 Append this value to `wl-summary-mark-action-list' by `wl-spam-setup'.
96 See `wl-summary-mark-action-list' for the detail of element."
97 :type '(repeat (string :tag "Temporary mark")
98 (symbol :tag "Set mark function")
99 (symbol :tag "Unset mark function")
100 (symbol :tag "Exec function")
101 (symbol :tag "Face symbol")
102 (string :tag "Document string"))
105 (defun wl-spam-domain (folder-name)
106 (cond ((string= folder-name wl-spam-folder-name)
108 ((wl-string-match-member folder-name
109 wl-spam-undecided-folder-regexp-list)
111 ((wl-string-match-member folder-name
112 wl-spam-ignored-folder-regexp-list)
117 (defsubst wl-spam-auto-check-message-p (folder number)
118 (or (eq wl-spam-auto-check-marks 'all)
119 (member (wl-summary-message-mark folder number)
120 wl-spam-auto-check-marks)))
122 (defsubst wl-spam-map-spam-messages (folder numbers function &rest args)
123 (let ((total (length numbers)))
124 (message "Checking spam...")
125 (elmo-with-progress-display (> total elmo-display-progress-threshold)
126 (elmo-spam-check-spam total "Checking spam...")
127 (dolist (number (elmo-spam-list-spam-messages (elmo-spam-processor)
130 (apply function number args)))
131 (message "Checking spam...done")))
133 (defun wl-spam-register-spam-messages (folder numbers)
134 (let ((total (length numbers)))
135 (message "Registering spam...")
136 (elmo-with-progress-display (> total elmo-display-progress-threshold)
137 (elmo-spam-register total "Registering spam...")
138 (elmo-spam-register-spam-messages (elmo-spam-processor)
139 wl-summary-buffer-elmo-folder
141 (message "Registering spam...done")))
143 (defun wl-spam-register-good-messages (folder numbers)
144 (let ((total (length numbers)))
145 (message "Registering good...")
146 (elmo-with-progress-display (> total elmo-display-progress-threshold)
147 (elmo-spam-register total "Registering good...")
148 (elmo-spam-register-good-messages (elmo-spam-processor)
149 wl-summary-buffer-elmo-folder
151 (message "Registering good...done")))
153 (defun wl-spam-save-status (&optional force)
155 (let ((processor (elmo-spam-processor)))
156 (when (or force (elmo-spam-modified-p processor))
157 (elmo-spam-save-status processor))))
159 ;; insinuate into summary mode
160 (defvar wl-summary-spam-map nil)
162 (unless wl-summary-spam-map
163 (let ((map (make-sparse-keymap)))
164 (define-key map "m" 'wl-summary-spam)
165 (define-key map "c" 'wl-summary-test-spam)
166 (define-key map "C" 'wl-summary-mark-spam)
167 (define-key map "s" 'wl-summary-register-as-spam)
168 (define-key map "S" 'wl-summary-register-as-spam-all)
169 (define-key map "n" 'wl-summary-register-as-good)
170 (define-key map "N" 'wl-summary-register-as-good-all)
171 (setq wl-summary-spam-map map)))
174 ;; Avoid compile warnings
175 (defalias-maybe 'wl-summary-spam 'ignore))
177 (defun wl-summary-test-spam (&optional folder number)
179 (let ((folder (or folder wl-summary-buffer-elmo-folder))
180 (number (or number (wl-summary-message-number)))
182 (message "Cheking spam...")
183 (when (setq spam (elmo-spam-message-spam-p (elmo-spam-processor)
185 (wl-summary-spam number))
186 (message "Cheking spam...done")
187 (when (interactive-p)
188 (message "No: %d is %sa spam message." number (if spam "" "not ")))))
190 (defun wl-summary-mark-spam (&optional all)
191 "Set spam mark to messages which is spam classification."
195 (setq numbers wl-summary-buffer-number-list)
196 (dolist (number wl-summary-buffer-number-list)
197 (when (wl-spam-auto-check-message-p wl-summary-buffer-elmo-folder
199 (setq numbers (cons number numbers)))))
201 (wl-spam-map-spam-messages wl-summary-buffer-elmo-folder
205 (message "No message to test.")))))
207 (defun wl-summary-register-as-spam ()
209 (let ((number (wl-summary-message-number)))
211 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
214 (defun wl-summary-register-as-spam-all ()
216 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
217 wl-summary-buffer-number-list))
219 (defun wl-summary-target-mark-register-as-spam ()
222 (goto-char (point-min))
223 (let ((inhibit-read-only t)
224 (buffer-read-only nil)
225 wl-summary-buffer-disp-msg)
226 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
227 wl-summary-buffer-target-mark-list)
228 (dolist (number wl-summary-buffer-target-mark-list)
229 (wl-summary-unset-mark number)))))
231 (defun wl-summary-register-as-good ()
233 (let ((number (wl-summary-message-number)))
235 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
238 (defun wl-summary-register-as-good-all ()
240 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
241 wl-summary-buffer-number-list))
243 (defun wl-summary-target-mark-register-as-good ()
246 (goto-char (point-min))
247 (let ((inhibit-read-only t)
248 (buffer-read-only nil)
249 wl-summary-buffer-disp-msg)
250 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
251 wl-summary-buffer-target-mark-list)
252 (dolist (number wl-summary-buffer-target-mark-list)
253 (wl-summary-unset-mark number)))))
255 ;; hook functions and other
256 (defun wl-summary-auto-check-spam ()
257 (when (elmo-string-match-member (wl-summary-buffer-folder-name)
258 wl-spam-auto-check-folder-regexp-list)
259 (wl-summary-mark-spam)))
261 (defun wl-summary-exec-action-spam (mark-list)
262 (let ((domain (wl-spam-domain (elmo-folder-name-internal
263 wl-summary-buffer-elmo-folder)))
264 (total (length mark-list)))
265 (when (memq domain '(undecided good))
266 (message "Registering spam...")
267 (elmo-with-progress-display (> total elmo-display-progress-threshold)
268 (elmo-spam-register total "Registering spam...")
269 (elmo-spam-register-spam-messages (elmo-spam-processor)
270 wl-summary-buffer-elmo-folder
271 (mapcar #'car mark-list)
273 (message "Registering spam...done"))
274 (wl-summary-move-mark-list-messages mark-list
276 "Refiling spam...")))
278 (defun wl-summary-exec-action-refile-with-register (mark-list)
279 (let* ((processor (elmo-spam-processor))
280 (folder wl-summary-buffer-elmo-folder)
281 (domain (wl-spam-domain (elmo-folder-name-internal folder)))
282 spam-list good-list total)
283 (unless (eq domain 'ignore)
284 (dolist (info mark-list)
285 (case (wl-spam-domain (nth 2 info))
287 (setq spam-list (cons (car info) spam-list)))
289 (setq good-list (cons (car info) good-list)))))
291 (spam (setq spam-list nil))
292 (good (setq good-list nil)))
293 (when (or spam-list good-list)
294 (message "Registering spam...")
295 (setq total (+ (length spam-list) (length good-list)))
296 (elmo-with-progress-display (> total elmo-display-progress-threshold)
297 (elmo-spam-register total "Registering spam...")
299 (elmo-spam-register-spam-messages processor folder spam-list
302 (elmo-spam-register-good-messages processor folder good-list
304 (message "Registering spam...done")))
305 ;; execute refile messages
306 (wl-summary-exec-action-refile mark-list)))
308 (defun wl-message-check-spam ()
309 (let ((original (wl-message-get-original-buffer))
310 (number wl-message-buffer-cur-number)
312 (message "Cheking spam...")
313 (when (elmo-spam-buffer-spam-p (elmo-spam-processor) original)
314 (with-current-buffer wl-message-buffer-cur-summary-buffer
315 (wl-summary-spam number)))
316 (message "Cheking spam...done")
317 (when (interactive-p)
318 (message "No: %d is %sa spam message." number (if spam "" "not ")))))
320 (defun wl-refile-guess-by-spam (entity)
321 (when (elmo-spam-message-spam-p (elmo-spam-processor)
322 wl-summary-buffer-elmo-folder
323 (elmo-message-entity-number entity))
324 wl-spam-folder-name))
326 (defun wl-spam-setup ()
327 (add-hook 'wl-summary-prepared-hook #'wl-summary-auto-check-spam)
328 (let ((actions wl-summary-mark-action-list)
331 (setq action (car actions)
332 actions (cdr actions))
333 (when (eq (wl-summary-action-symbol action) 'refile)
334 (setcar (nthcdr 4 action) 'wl-summary-exec-action-refile-with-register)
335 (setq actions nil))))
336 (when wl-spam-mark-action-list
337 (setq wl-summary-mark-action-list (append
338 wl-summary-mark-action-list
339 wl-spam-mark-action-list))
340 (dolist (action wl-spam-mark-action-list)
341 (setq wl-summary-reserve-mark-list
342 (cons (wl-summary-action-mark action)
343 wl-summary-reserve-mark-list))
344 (setq wl-summary-skip-mark-list
345 (cons (wl-summary-action-mark action)
346 wl-summary-skip-mark-list))))
347 (define-key wl-summary-mode-map "k" wl-summary-spam-map)
349 wl-summary-mode-map "ms" 'wl-summary-target-mark-register-as-spam)
351 wl-summary-mode-map "mn" 'wl-summary-target-mark-register-as-good))
354 (product-provide (provide 'wl-spam) (require 'wl-version))
356 (unless noninteractive
359 ;;; wl-spam.el ends here