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-list nil
50 "*List of folder name which is contained undecided domain.
51 If an element is symbol, use symbol-value instead."
52 :type '(repeat (choice (string :tag "Folder name")
53 (variable :tag "Variable")))
56 (defcustom wl-spam-undecided-folder-regexp-list '("inbox")
57 "*List of folder regexp which is contained undecided domain."
58 :type '(repeat (regexp :tag "Folder Regexp"))
61 (defcustom wl-spam-ignored-folder-list '(wl-draft-folder
64 "*List of folder name which is contained ignored domain.
65 If an element is symbol, use symbol-value instead."
66 :type '(repeat (choice (string :tag "Folder name")
67 (variable :tag "Variable")))
70 (defcustom wl-spam-ignored-folder-regexp-list nil
71 "*List of folder regexp which is contained ignored domain."
72 :type '(repeat (regexp :tag "Folder Regexp"))
75 (defcustom wl-spam-auto-check-folder-regexp-list nil
76 "*List of Folder regexp which check spam automatically."
77 :type '(repeat (regexp :tag "Folder Regexp"))
80 (defcustom wl-spam-auto-check-marks
81 (list wl-summary-new-uncached-mark
82 wl-summary-new-cached-mark)
83 "Persistent marks to check spam automatically."
84 :type '(choice (const :tag "All marks" all)
85 (repeat (string :tag "Mark")))
88 (wl-defface wl-highlight-summary-spam-face
93 (:foreground "LightSlateGray")))
94 "Face used for displaying messages mark as spam."
95 :group 'wl-summary-faces
98 (defcustom wl-spam-mark-action-list
102 wl-summary-register-temp-mark
103 wl-summary-exec-action-spam
104 wl-highlight-summary-spam-face
105 "Mark messages as spam."))
106 "A variable to define Mark & Action for spam.
107 Append this value to `wl-summary-mark-action-list' by `wl-spam-setup'.
109 See `wl-summary-mark-action-list' for the detail of element."
111 (string :tag "Temporary mark")
112 (symbol :tag "Action name")
113 (symbol :tag "Argument function")
114 (symbol :tag "Set mark function")
115 (symbol :tag "Exec function")
116 (symbol :tag "Face symbol")
117 (string :tag "Document string")))
120 (defsubst wl-spam-string-member-p (string list regexp-list)
121 (or (wl-string-member string list)
122 (wl-string-match-member string regexp-list)))
124 (defun wl-spam-domain (folder-name)
125 (cond ((string= folder-name wl-spam-folder)
127 ((wl-spam-string-member-p folder-name
128 wl-spam-undecided-folder-list
129 wl-spam-undecided-folder-regexp-list)
131 ((wl-spam-string-member-p folder-name
132 wl-spam-ignored-folder-list
133 wl-spam-ignored-folder-regexp-list)
138 (defun wl-spam-split-numbers (folder numbers)
140 (dolist (number numbers)
141 (let* ((domain (wl-spam-domain
142 (elmo-folder-name-internal
143 (elmo-message-folder folder number))))
144 (cell (assq domain alist)))
146 (setcdr cell (cons number (cdr cell)))
147 (setq alist (cons (list domain number) alist)))))
150 (defsubst wl-spam-auto-check-message-p (folder number)
151 (or (eq wl-spam-auto-check-marks 'all)
152 (member (wl-summary-message-mark folder number)
153 wl-spam-auto-check-marks)))
155 (defsubst wl-spam-map-spam-messages (folder numbers function &rest args)
156 (elmo-with-progress-display (elmo-spam-check-spam (length numbers))
158 (dolist (number (elmo-spam-list-spam-messages (elmo-spam-processor)
161 (apply function number args))))
163 (defun wl-spam-apply-partitions (folder partitions function msg)
166 (dolist (partition partitions)
167 (setq total (+ total (length (cdr partition)))))
168 (elmo-with-progress-display (elmo-spam-register total) msg
169 (dolist (partition partitions)
170 (funcall function folder (cdr partition) (car partition)))))))
172 (defun wl-spam-register-spam-messages (folder numbers)
173 (elmo-with-progress-display (elmo-spam-register (length numbers))
175 (elmo-spam-register-spam-messages (elmo-spam-processor)
179 (defun wl-spam-register-good-messages (folder numbers)
180 (elmo-with-progress-display (elmo-spam-register (length numbers))
182 (elmo-spam-register-good-messages (elmo-spam-processor)
186 (defun wl-spam-save-status (&optional force)
188 (let ((processor (elmo-spam-processor (not force))))
190 (and processor (elmo-spam-modified-p processor)))
191 (elmo-spam-save-status processor))))
193 ;; insinuate into summary mode
194 (defvar wl-summary-spam-map nil)
196 (unless wl-summary-spam-map
197 (let ((map (make-sparse-keymap)))
198 (define-key map "m" 'wl-summary-spam)
199 (define-key map "c" 'wl-summary-test-spam)
200 (define-key map "C" 'wl-summary-mark-spam)
201 (define-key map "s" 'wl-summary-register-as-spam)
202 (define-key map "S" 'wl-summary-register-as-spam-all)
203 (define-key map "n" 'wl-summary-register-as-good)
204 (define-key map "N" 'wl-summary-register-as-good-all)
205 (setq wl-summary-spam-map map)))
208 ;; Avoid compile warnings
209 (defalias-maybe 'wl-summary-spam 'ignore)
210 (defalias-maybe 'wl-summary-unmark-spam 'ignore))
212 (defun wl-summary-test-spam (&optional folder number)
214 (let ((folder (or folder wl-summary-buffer-elmo-folder))
215 (number (or number (wl-summary-message-number)))
217 (message "Checking spam...")
218 (if (setq spam (elmo-spam-message-spam-p (elmo-spam-processor)
220 (wl-summary-spam number)
221 (wl-summary-unmark-spam number))
222 (message "Checking spam...done")
223 (when (interactive-p)
224 (message "No: %d is %sa spam message." number (if spam "" "not ")))))
226 (defun wl-summary-test-spam-messages (folder numbers &rest args)
227 (elmo-with-progress-display (elmo-spam-check-spam (length numbers))
229 (let* ((spams (elmo-spam-list-spam-messages (elmo-spam-processor)
232 (goods (car (elmo-list-diff numbers spams))))
233 (dolist (number spams)
234 (wl-summary-spam number args))
235 (dolist (number goods)
236 (wl-summary-unmark-spam number)))))
238 (defun wl-summary-test-spam-region (beg end)
240 (let ((numbers (wl-summary-collect-numbers-region beg end)))
242 (wl-summary-test-spam-messages wl-summary-buffer-elmo-folder
245 (message "No message to test.")))))
247 (defun wl-thread-test-spam (&optional arg)
249 (wl-thread-call-region-func 'wl-summary-test-spam-region arg))
251 (defun wl-summary-mark-spam (&optional all)
252 "Set spam mark to messages which is spam classification."
256 (setq numbers wl-summary-buffer-number-list)
257 (dolist (number wl-summary-buffer-number-list)
258 (when (wl-spam-auto-check-message-p wl-summary-buffer-elmo-folder
260 (setq numbers (cons number numbers)))))
262 (wl-spam-map-spam-messages wl-summary-buffer-elmo-folder
266 (message "No message to test.")))))
268 (defun wl-summary-register-as-spam ()
270 (let ((number (wl-summary-message-number)))
272 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
275 (defun wl-summary-register-as-spam-all ()
277 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
278 wl-summary-buffer-number-list))
280 (defun wl-summary-target-mark-register-as-spam ()
283 (goto-char (point-min))
284 (let ((inhibit-read-only t)
285 (buffer-read-only nil)
286 wl-summary-buffer-disp-msg)
287 (wl-spam-register-spam-messages wl-summary-buffer-elmo-folder
288 wl-summary-buffer-target-mark-list)
289 (dolist (number wl-summary-buffer-target-mark-list)
290 (wl-summary-unset-mark number)))))
292 (defun wl-summary-register-as-good ()
294 (let ((number (wl-summary-message-number)))
296 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
299 (defun wl-summary-register-as-good-all ()
301 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
302 wl-summary-buffer-number-list))
304 (defun wl-summary-target-mark-register-as-good ()
307 (goto-char (point-min))
308 (let ((inhibit-read-only t)
309 (buffer-read-only nil)
310 wl-summary-buffer-disp-msg)
311 (wl-spam-register-good-messages wl-summary-buffer-elmo-folder
312 wl-summary-buffer-target-mark-list)
313 (dolist (number wl-summary-buffer-target-mark-list)
314 (wl-summary-unset-mark number)))))
316 ;; hook functions and other
317 (defun wl-summary-auto-check-spam ()
318 (when (elmo-string-match-member (wl-summary-buffer-folder-name)
319 wl-spam-auto-check-folder-regexp-list)
320 (wl-summary-mark-spam)))
322 (defun wl-summary-exec-action-spam (mark-list)
323 (let ((folder wl-summary-buffer-elmo-folder))
324 (wl-folder-confirm-existence (wl-folder-get-elmo-folder wl-spam-folder))
325 (wl-spam-apply-partitions
327 (wl-filter-associations
329 (wl-spam-split-numbers folder (mapcar #'car mark-list)))
330 (lambda (folder numbers domain)
331 (elmo-spam-register-spam-messages (elmo-spam-processor)
335 (wl-summary-move-mark-list-messages mark-list
339 (defun wl-summary-exec-action-refile-with-register (mark-list)
340 (let ((folder wl-summary-buffer-elmo-folder)
342 (dolist (info mark-list)
343 (case (wl-spam-domain (nth 2 info))
345 (setq spam-list (cons (car info) spam-list)))
347 (setq good-list (cons (car info) good-list)))))
348 (wl-spam-apply-partitions
350 (wl-filter-associations '(undecided good)
351 (wl-spam-split-numbers folder spam-list))
352 (lambda (folder numbers domain)
353 (elmo-spam-register-spam-messages (elmo-spam-processor)
357 (wl-spam-apply-partitions
359 (wl-filter-associations '(undecided spam)
360 (wl-spam-split-numbers folder good-list))
361 (lambda (folder numbers domain)
362 (elmo-spam-register-good-messages (elmo-spam-processor)
366 ;; execute refile messages
367 (wl-summary-exec-action-refile mark-list)))
369 (defun wl-message-check-spam ()
370 (let ((original (wl-message-get-original-buffer))
371 (number wl-message-buffer-cur-number)
373 (message "Checking spam...")
374 (when (setq spam (elmo-spam-buffer-spam-p (elmo-spam-processor) original))
375 (with-current-buffer wl-message-buffer-cur-summary-buffer
376 (wl-summary-spam number)))
377 (message "Checking spam...done")
378 (message "No: %d is %sa spam message." number (if spam "" "not "))))
380 (defun wl-refile-guess-by-spam (entity)
381 (when (elmo-spam-message-spam-p (elmo-spam-processor)
382 wl-summary-buffer-elmo-folder
383 (elmo-message-entity-number entity))
386 (defun wl-spam-setup ()
387 (add-hook 'wl-summary-sync-updated-hook #'wl-summary-auto-check-spam)
388 (let ((actions wl-summary-mark-action-list)
391 (setq action (car actions)
392 actions (cdr actions))
393 (when (eq (wl-summary-action-symbol action) 'refile)
394 (setcar (nthcdr 4 action) 'wl-summary-exec-action-refile-with-register)
395 (setq actions nil))))
396 (when wl-spam-mark-action-list
397 (setq wl-summary-mark-action-list (append
398 wl-summary-mark-action-list
399 wl-spam-mark-action-list))
400 (dolist (action wl-spam-mark-action-list)
401 (setq wl-summary-reserve-mark-list
402 (cons (wl-summary-action-mark action)
403 wl-summary-reserve-mark-list))
404 (setq wl-summary-skip-mark-list
405 (cons (wl-summary-action-mark action)
406 wl-summary-skip-mark-list))))
407 (define-key wl-summary-mode-map "k" wl-summary-spam-map)
409 wl-summary-mode-map "rkm" 'wl-summary-spam-region)
411 wl-summary-mode-map "rkc" 'wl-summary-test-spam-region)
413 wl-summary-mode-map "tkm" 'wl-thread-spam)
415 wl-summary-mode-map "tkc" 'wl-thread-test-spam)
417 wl-summary-mode-map "mk" 'wl-summary-target-mark-spam)
419 wl-summary-mode-map "ms" 'wl-summary-target-mark-register-as-spam)
421 wl-summary-mode-map "mn" 'wl-summary-target-mark-register-as-good))
424 (product-provide (provide 'wl-spam) (require 'wl-version))
426 (unless noninteractive
429 ;;; wl-spam.el ends here