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 "+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)
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 (not force))))
157 (and processor (elmo-spam-modified-p processor)))
158 (elmo-spam-save-status processor))))
160 ;; insinuate into summary mode
161 (defvar wl-summary-spam-map nil)
163 (unless wl-summary-spam-map
164 (let ((map (make-sparse-keymap)))
165 (define-key map "m" 'wl-summary-spam)
166 (define-key map "c" 'wl-summary-test-spam)
167 (define-key map "C" 'wl-summary-mark-spam)
168 (define-key map "s" 'wl-summary-register-as-spam)
169 (define-key map "S" 'wl-summary-register-as-spam-all)
170 (define-key map "n" 'wl-summary-register-as-good)
171 (define-key map "N" 'wl-summary-register-as-good-all)
172 (setq wl-summary-spam-map map)))
175 ;; Avoid compile warnings
176 (defalias-maybe 'wl-summary-spam 'ignore))
178 (defun wl-summary-test-spam (&optional folder number)
180 (let ((folder (or folder wl-summary-buffer-elmo-folder))
181 (number (or number (wl-summary-message-number)))
183 (message "Cheking spam...")
184 (when (setq spam (elmo-spam-message-spam-p (elmo-spam-processor)
186 (wl-summary-spam number))
187 (message "Cheking spam...done")
188 (when (interactive-p)
189 (message "No: %d is %sa spam message." number (if spam "" "not ")))))
191 (defun wl-summary-mark-spam (&optional all)
192 "Set spam mark to messages which is spam classification."
196 (setq numbers wl-summary-buffer-number-list)
197 (dolist (number wl-summary-buffer-number-list)
198 (when (wl-spam-auto-check-message-p wl-summary-buffer-elmo-folder
200 (setq numbers (cons number numbers)))))
202 (wl-spam-map-spam-messages wl-summary-buffer-elmo-folder
206 (message "No message to test.")))))
208 (defun wl-summary-register-as-spam ()
210 (let ((number (wl-summary-message-number)))
212 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
215 (defun wl-summary-register-as-spam-all ()
217 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
218 wl-summary-buffer-number-list))
220 (defun wl-summary-target-mark-register-as-spam ()
223 (goto-char (point-min))
224 (let ((inhibit-read-only t)
225 (buffer-read-only nil)
226 wl-summary-buffer-disp-msg)
227 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
228 wl-summary-buffer-target-mark-list)
229 (dolist (number wl-summary-buffer-target-mark-list)
230 (wl-summary-unset-mark number)))))
232 (defun wl-summary-register-as-good ()
234 (let ((number (wl-summary-message-number)))
236 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
239 (defun wl-summary-register-as-good-all ()
241 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
242 wl-summary-buffer-number-list))
244 (defun wl-summary-target-mark-register-as-good ()
247 (goto-char (point-min))
248 (let ((inhibit-read-only t)
249 (buffer-read-only nil)
250 wl-summary-buffer-disp-msg)
251 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
252 wl-summary-buffer-target-mark-list)
253 (dolist (number wl-summary-buffer-target-mark-list)
254 (wl-summary-unset-mark number)))))
256 ;; hook functions and other
257 (defun wl-summary-auto-check-spam ()
258 (when (elmo-string-match-member (wl-summary-buffer-folder-name)
259 wl-spam-auto-check-folder-regexp-list)
260 (wl-summary-mark-spam)))
262 (defun wl-summary-exec-action-spam (mark-list)
263 (let ((domain (wl-spam-domain (elmo-folder-name-internal
264 wl-summary-buffer-elmo-folder)))
265 (total (length mark-list)))
266 (wl-folder-confirm-existence (elmo-make-folder wl-spam-folder))
267 (when (memq domain '(undecided good))
268 (message "Registering spam...")
269 (elmo-with-progress-display (> total elmo-display-progress-threshold)
270 (elmo-spam-register total "Registering spam...")
271 (elmo-spam-register-spam-messages (elmo-spam-processor)
272 wl-summary-buffer-elmo-folder
273 (mapcar #'car mark-list)
275 (message "Registering spam...done"))
276 (wl-summary-move-mark-list-messages mark-list
278 "Refiling spam...")))
280 (defun wl-summary-exec-action-refile-with-register (mark-list)
281 (let* ((processor (elmo-spam-processor))
282 (folder wl-summary-buffer-elmo-folder)
283 (domain (wl-spam-domain (elmo-folder-name-internal folder)))
284 spam-list good-list total)
285 (unless (eq domain 'ignore)
286 (dolist (info mark-list)
287 (case (wl-spam-domain (nth 2 info))
289 (setq spam-list (cons (car info) spam-list)))
291 (setq good-list (cons (car info) good-list)))))
293 (spam (setq spam-list nil))
294 (good (setq good-list nil)))
295 (when (or spam-list good-list)
297 (setq total (length spam-list))
298 (message "Registering spam...")
299 (elmo-with-progress-display (> total elmo-display-progress-threshold)
300 (elmo-spam-register total "Registering spam...")
301 (elmo-spam-register-spam-messages processor folder spam-list
303 (message "Registering spam...done"))
305 (setq total (length good-list))
306 (message "Registering good...")
307 (elmo-with-progress-display (> total elmo-display-progress-threshold)
308 (elmo-spam-register total "Registering good...")
309 (elmo-spam-register-good-messages processor folder good-list
311 (message "Registering good...done"))))
312 ;; execute refile messages
313 (wl-summary-exec-action-refile mark-list)))
315 (defun wl-message-check-spam ()
316 (let ((original (wl-message-get-original-buffer))
317 (number wl-message-buffer-cur-number)
319 (message "Cheking spam...")
320 (when (setq spam (elmo-spam-buffer-spam-p (elmo-spam-processor) original))
321 (with-current-buffer wl-message-buffer-cur-summary-buffer
322 (wl-summary-spam number)))
323 (message "Cheking spam...done")
324 (message "No: %d is %sa spam message." number (if spam "" "not "))))
326 (defun wl-refile-guess-by-spam (entity)
327 (when (elmo-spam-message-spam-p (elmo-spam-processor)
328 wl-summary-buffer-elmo-folder
329 (elmo-message-entity-number entity))
332 (defun wl-spam-setup ()
333 (add-hook 'wl-summary-prepared-hook #'wl-summary-auto-check-spam)
334 (let ((actions wl-summary-mark-action-list)
337 (setq action (car actions)
338 actions (cdr actions))
339 (when (eq (wl-summary-action-symbol action) 'refile)
340 (setcar (nthcdr 4 action) 'wl-summary-exec-action-refile-with-register)
341 (setq actions nil))))
342 (when wl-spam-mark-action-list
343 (setq wl-summary-mark-action-list (append
344 wl-summary-mark-action-list
345 wl-spam-mark-action-list))
346 (dolist (action wl-spam-mark-action-list)
347 (setq wl-summary-reserve-mark-list
348 (cons (wl-summary-action-mark action)
349 wl-summary-reserve-mark-list))
350 (setq wl-summary-skip-mark-list
351 (cons (wl-summary-action-mark action)
352 wl-summary-skip-mark-list))))
353 (define-key wl-summary-mode-map "k" wl-summary-spam-map)
355 wl-summary-mode-map "mk" 'wl-summary-target-mark-spam)
357 wl-summary-mode-map "ms" 'wl-summary-target-mark-register-as-spam)
359 wl-summary-mode-map "mn" 'wl-summary-target-mark-register-as-good))
362 (product-provide (provide 'wl-spam) (require 'wl-version))
364 (unless noninteractive
367 ;;; wl-spam.el ends here