63bd6f4aaf0452f51a69344f2c8d153e62b47137
[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                  (const :tag "SpamAssassin" sa)
49                  (const :tag "Bsfilter" bsfilter))
50   :group 'elmo-spam)
51
52 (eval-and-compile
53   (luna-define-class elsp-generic ()))
54
55 ;; required method
56 (luna-define-generic elmo-spam-buffer-spam-p (processor buffer
57                                                         &optional register)
58   "Return non-nil if contents of BUFFER is spam.
59 PROCESSOR is spam processor structure.
60 If optional augument REGISTER is non-nil,
61 register according to the classification.")
62
63 (luna-define-generic elmo-spam-register-spam-buffer (processor
64                                                      buffer
65                                                      &optional restore)
66   "Register contents of BUFFER as spam.
67 PROCESSOR is spam processor structure.
68 If optional argument RESTORE is non-nil, unregister from non-spam list.")
69
70 (luna-define-generic elmo-spam-register-good-buffer (processor
71                                                      buffer
72                                                      &optional restore)
73   "Register contents of BUFFER as non-spam.
74 PROCESSOR is spam processor structure.
75 If optional argument RESTORE is non-nil, unregister from spam list.")
76
77 ;; optional method
78 (luna-define-generic elmo-spam-modified-p (processor)
79   "Return non-nil if status of PROCESSOR is modified.")
80
81 (luna-define-generic elmo-spam-save-status (processor)
82   "Save status of the PROCESSOR.")
83
84 (luna-define-generic elmo-spam-message-spam-p (processor folder number
85                                                          &optional register)
86   "Return non-nil if the message in the FOLDER with NUMBER is spam.
87 PROCESSOR is spam processor structure.
88 If optional augument REGISTER is non-nil,
89 register according to the classification.")
90
91 (luna-define-generic elmo-spam-list-spam-messages (processor
92                                                    folder &optional numbers)
93   "Return a list of message numbers which is gussed spam.
94 PROCESSOR is spam processor structure.
95 FOLDER is the ELMO folder structure.
96 If optional argument NUMBERS is specified and is a list of message numbers,
97 messages are searched from the list.")
98
99 (luna-define-generic elmo-spam-register-spam-messages (processor
100                                                        folder
101                                                        &optional
102                                                        numbers restore)
103   "Register contents of messages as spam.
104 PROCESSOR is spam processor structure.
105 FOLDER is the ELMO folder structure.
106 If optional argument NUMBERS is specified and is a list of message numbers,
107 messages are searched from the list.
108 If optional argument RESTORE is non-nil, unregister from non-spam list.")
109
110 (luna-define-generic elmo-spam-register-good-messages (processor
111                                                        folder
112                                                        &optional
113                                                        numbers restore)
114   "Register contents of messages as non spam.
115 PROCESSOR is spam processor structure.
116 FOLDER is the ELMO folder structure.
117 If optional argument NUMBERS is specified and is a list of message numbers,
118 messages are searched from the list.
119 If optional argument RESTORE is non-nil, unregister from spam list.")
120
121 ;; for internal use
122 (defun elmo-spam-message-fetch (folder number)
123   (let (elmo-message-fetch-threshold)
124     (elmo-message-fetch
125      folder number
126      (elmo-find-fetch-strategy folder
127                                (elmo-message-entity folder number))
128      nil (current-buffer) 'unread)))
129
130 ;; generic implement
131 (luna-define-method elmo-spam-message-spam-p ((processor elsp-generic)
132                                               folder number &optional register)
133   (with-temp-buffer
134     (elmo-spam-message-fetch folder number)
135     (elmo-spam-buffer-spam-p processor (current-buffer) register)))
136
137 (luna-define-method elmo-spam-list-spam-messages ((processor elsp-generic)
138                                                   folder &optional numbers)
139   (let ((numbers (or numbers (elmo-folder-list-messages folder t t)))
140         spam-list)
141     (dolist (number numbers)
142       (when (elmo-spam-message-spam-p processor folder number)
143         (setq spam-list (cons number spam-list)))
144       (elmo-progress-notify 'elmo-spam-check-spam))
145     (nreverse spam-list)))
146
147 (luna-define-method elmo-spam-register-spam-messages ((processor elsp-generic)
148                                                       folder
149                                                       &optional
150                                                       numbers restore)
151   (let ((numbers (or numbers (elmo-folder-list-messages folder t t))))
152     (with-temp-buffer
153       (buffer-disable-undo (current-buffer))
154       (dolist (number numbers)
155         (erase-buffer)
156         (elmo-spam-message-fetch folder number)
157         (elmo-spam-register-spam-buffer processor (current-buffer) restore)
158         (elmo-progress-notify 'elmo-spam-register)))))
159
160 (luna-define-method elmo-spam-register-good-messages ((processor elsp-generic)
161                                                       folder
162                                                       &optional
163                                                       numbers restore)
164   (let ((numbers (or numbers (elmo-folder-list-messages folder t t))))
165     (with-temp-buffer
166       (buffer-disable-undo (current-buffer))
167       (dolist (number numbers)
168         (erase-buffer)
169         (elmo-spam-message-fetch folder number)
170         (elmo-spam-register-good-buffer processor (current-buffer) restore)
171         (elmo-progress-notify 'elmo-spam-register)))))
172
173 (provide 'elsp-generic)
174
175 (defvar elmo-spam-processor-internal nil)
176
177 (defun elmo-spam-processor (&optional if-exists)
178   (or elmo-spam-processor-internal
179       (unless if-exists
180         (let* ((scheme (or elmo-spam-scheme 'generic))
181                (class (intern (format "elsp-%s" scheme))))
182           (require class)
183           (setq elmo-spam-processor-internal
184                 (luna-make-entity class))))))
185
186 (require 'product)
187 (product-provide (provide 'elmo-spam) (require 'elmo-version))
188
189 ;;; elmo-spam.el ends here