* elmo-spam.el (elmo-spam-processor): Added 3rd argument
[elisp/wanderlust.git] / elmo / elmo-spam.el
1 ;;; elmo-spam.el --- Spam filtering interface to processor.
2
3 ;; Copyright (C) 2003 Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
4 ;; Copyright (C) 2003 Yuuichi Teranishi <teranisi@gohome.org>
5
6 ;; Author: Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
7 ;; Keywords: mail, net news, spam
8
9 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
10
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)
14 ;; any later version.
15 ;;
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.
20 ;;
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.
25 ;;
26
27 ;;; Commentary:
28 ;;
29
30 ;;; Code:
31 ;;
32
33 (eval-when-compile (require 'cl))
34
35 (require 'luna)
36 (require 'elmo-util)
37 (require 'elmo)
38
39 (defgroup elmo-spam nil
40   "Spam configuration for wanderlust."
41   :group 'elmo)
42
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   :group 'elmo-spam)
49
50 (eval-and-compile
51   (luna-define-class elsp-generic ()))
52
53 ;; required method
54 (luna-define-generic elmo-spam-buffer-spam-p (processor buffer
55                                                         &optional register)
56   "Return non-nil if contents of BUFFER is spam.
57 PROCESSOR is spam processor structure.
58 If optional augument REGISTER is non-nil,
59 register according to the classification.")
60
61 (luna-define-generic elmo-spam-register-spam-buffer (processor
62                                                      buffer
63                                                      &optional restore)
64   "Register contents of BUFFER as spam.
65 PROCESSOR is spam processor structure.
66 If optional argument RESTORE is non-nil, unregister from non-spam list.")
67
68 (luna-define-generic elmo-spam-register-good-buffer (processor
69                                                      buffer
70                                                      &optional restore)
71   "Register contents of BUFFER as non-spam.
72 PROCESSOR is spam processor structure.
73 If optional argument RESTORE is non-nil, unregister from spam list.")
74
75 ;; optional method
76 (luna-define-generic elmo-spam-modified-p (processor)
77   "Return non-nil if status of PROCESSOR is modified.")
78
79 (luna-define-generic elmo-spam-save-status (processor)
80   "Save status of the PROCESSOR.")
81
82 (luna-define-generic elmo-spam-message-spam-p (processor folder number
83                                                          &optional register)
84   "Return non-nil if the message in the FOLDER with NUMBER is spam.
85 PROCESSOR is spam processor structure.
86 If optional augument REGISTER is non-nil,
87 register according to the classification.")
88
89 (luna-define-generic elmo-spam-list-spam-messages (processor
90                                                    folder &optional numbers)
91   "Return a list of message numbers which is gussed spam.
92 PROCESSOR is spam processor structure.
93 FOLDER is the ELMO folder structure.
94 If optional argument NUMBERS is specified and is a list of message numbers,
95 messages are searched from the list.")
96
97 (luna-define-generic elmo-spam-register-spam-messages (processor
98                                                        folder
99                                                        &optional
100                                                        numbers restore)
101   "Register contents of messages as spam.
102 PROCESSOR is spam processor structure.
103 FOLDER is the ELMO folder structure.
104 If optional argument NUMBERS is specified and is a list of message numbers,
105 messages are searched from the list.
106 If optional argument RESTORE is non-nil, unregister from non-spam list.")
107
108 (luna-define-generic elmo-spam-register-good-messages (processor
109                                                        folder
110                                                        &optional
111                                                        numbers restore)
112   "Register contents of messages as non spam.
113 PROCESSOR is spam processor structure.
114 FOLDER is the ELMO folder structure.
115 If optional argument NUMBERS is specified and is a list of message numbers,
116 messages are searched from the list.
117 If optional argument RESTORE is non-nil, unregister from spam list.")
118
119 ;; for internal use
120 (defun elmo-spam-message-fetch (folder number)
121   (let (elmo-message-fetch-threshold)
122     (elmo-message-fetch
123      folder number
124      (elmo-find-fetch-strategy folder
125                                (elmo-message-entity folder number))
126      nil (current-buffer) 'unread)))
127
128 ;; generic implement
129 (luna-define-method elmo-spam-message-spam-p ((processor elsp-generic)
130                                               folder number &optional register)
131   (with-temp-buffer
132     (elmo-spam-message-fetch folder number)
133     (elmo-spam-buffer-spam-p processor (current-buffer) register)))
134
135 (luna-define-method elmo-spam-list-spam-messages ((processor elsp-generic)
136                                                   folder &optional numbers)
137   (let ((numbers (or numbers (elmo-folder-list-messages folder t t)))
138         spam-list)
139     (dolist (number numbers)
140       (when (elmo-spam-message-spam-p processor folder number)
141         (setq spam-list (cons number spam-list)))
142       (elmo-progress-notify 'elmo-spam-check-spam))
143     (nreverse spam-list)))
144
145 (luna-define-method elmo-spam-register-spam-messages ((processor elsp-generic)
146                                                       folder
147                                                       &optional
148                                                       numbers restore)
149   (let ((numbers (or numbers (elmo-folder-list-messages folder t t))))
150     (with-temp-buffer
151       (buffer-disable-undo (current-buffer))
152       (dolist (number numbers)
153         (erase-buffer)
154         (elmo-spam-message-fetch folder number)
155         (elmo-spam-register-spam-buffer processor (current-buffer) restore)
156         (elmo-progress-notify 'elmo-spam-register)))))
157
158 (luna-define-method elmo-spam-register-good-messages ((processor elsp-generic)
159                                                       folder
160                                                       &optional
161                                                       numbers restore)
162   (let ((numbers (or numbers (elmo-folder-list-messages folder t t))))
163     (with-temp-buffer
164       (buffer-disable-undo (current-buffer))
165       (dolist (number numbers)
166         (erase-buffer)
167         (elmo-spam-message-fetch folder number)
168         (elmo-spam-register-good-buffer processor (current-buffer) restore)
169         (elmo-progress-notify 'elmo-spam-register)))))
170
171 (provide 'elsp-generic)
172
173 (defvar elmo-spam-processor-internal nil)
174
175 (defun elmo-spam-processor (&optional if-exists)
176   (or elmo-spam-processor-internal
177       (unless if-exists
178         (let* ((scheme (or elmo-spam-scheme 'generic))
179                (class (intern (format "elsp-%s" scheme))))
180           (require class)
181           (setq elmo-spam-processor-internal
182                 (luna-make-entity class))))))
183
184 (require 'product)
185 (product-provide (provide 'elmo-spam) (require 'elmo-version))
186
187 ;;; elmo-spam.el ends here