1 ;;; elsp-bsfilter.el --- Bsfilter support for elmo-spam.
3 ;; Copyright (C) 2004 Hiroya Murata <lapis-lazuli@pop06.odn.ne.jp>
4 ;; Copyright (C) 2004 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.
35 (defgroup elmo-spam-bsfilter nil
36 "Spam bsfilter configuration."
39 (defcustom elmo-spam-bsfilter-shell-program "ruby"
42 :group 'elmo-spam-bsfilter)
44 (defcustom elmo-spam-bsfilter-shell-switch nil
47 :group 'elmo-spam-bsfilter)
49 (defcustom elmo-spam-bsfilter-program (exec-installed-p "bsfilter")
50 "*Program name of the Bsfilter."
51 :type '(string :tag "Program name of the bsfilter")
52 :group 'elmo-spam-bsfilter)
54 (defcustom elmo-spam-bsfilter-args nil
55 "*Argument list for bsfilter."
56 :type '(repeat string)
57 :group 'elmo-spam-bsfilter)
59 (defcustom elmo-spam-bsfilter-update-switch "--synchronous-auto-update"
60 "*The switch that Bsfilter uses to update database with classify."
62 :group 'elmo-spam-bsfilter)
64 (defcustom elmo-spam-bsfilter-database-directory nil
65 "*Directory path of the Bsfilter databases."
66 :type '(choice (directory :tag "Location of the Bsfilter database directory")
67 (const :tag "Use the default"))
68 :group 'elmo-spam-bsfilter)
70 (defcustom elmo-spam-bsfilter-max-files-per-process 100
71 "Number of files processed at once."
73 :group 'elmo-spam-bsfilter)
75 (defcustom elmo-spam-bsfilter-max-messages-per-process 30
76 "Number of messages processed at once."
78 :group 'elmo-spam-bsfilter)
80 (defcustom elmo-spam-bsfilter-debug nil
81 "Non-nil to debug elmo bsfilter spam backend."
83 :group 'elmo-spam-bsfilter)
86 (luna-define-class elsp-bsfilter (elsp-generic)))
88 (defsubst elsp-bsfilter-call-bsfilter (&rest args)
89 (apply #'call-process-region
90 (point-min) (point-max)
91 elmo-spam-bsfilter-shell-program
92 nil (if elmo-spam-bsfilter-debug
93 (get-buffer-create "*Debug ELMO Bsfilter*"))
95 (append (if elmo-spam-bsfilter-shell-switch
96 (list elmo-spam-bsfilter-shell-switch))
97 (if elmo-spam-bsfilter-program
98 (list elmo-spam-bsfilter-program))
99 elmo-spam-bsfilter-args
100 (if elmo-spam-bsfilter-database-directory
101 (list "--homedir" elmo-spam-bsfilter-database-directory))
102 (elmo-flatten args))))
104 (luna-define-method elmo-spam-buffer-spam-p ((processor elsp-bsfilter)
105 buffer &optional register)
106 (with-current-buffer buffer
107 (= 0 (elsp-bsfilter-call-bsfilter
108 (if register elmo-spam-bsfilter-update-switch)))))
110 (defsubst elsp-bsfilter-list-spam-files (targets)
113 elmo-spam-bsfilter-shell-program
115 (list t (if elmo-spam-bsfilter-debug
116 (get-buffer-create "*Debug ELMO Bsfilter*")))
118 (append (if elmo-spam-bsfilter-shell-switch
119 (list elmo-spam-bsfilter-shell-switch))
120 (if elmo-spam-bsfilter-program
121 (list elmo-spam-bsfilter-program))
122 elmo-spam-bsfilter-args
124 (if elmo-spam-bsfilter-database-directory
125 (list "--homedir" elmo-spam-bsfilter-database-directory))
128 (luna-define-method elmo-spam-list-spam-messages :around
129 ((processor elsp-bsfilter) folder &optional numbers)
130 (if (not (elmo-folder-message-file-p folder))
131 (luna-call-next-method)
132 (let* ((nth-of-targets (1- (or elmo-spam-bsfilter-max-files-per-process
134 (numbers (or numbers (elmo-folder-list-messages folder t t)))
135 (hash (elmo-make-hash (length numbers)))
138 (let ((filename (elmo-message-file-name folder number)))
139 (elmo-set-hash-val filename number hash)
145 (let* ((last (nthcdr nth-of-targets targets))
149 (elsp-bsfilter-list-spam-files targets)
150 (elmo-progress-notify 'elmo-spam-check-spam (length targets))
151 (setq targets next)))
152 (goto-char (point-min))
154 (let* ((filename (buffer-substring (point) (save-excursion
157 (number (elmo-get-hash-val filename hash)))
159 (setq results (cons number results)))
164 (defsubst elsp-bsfilter-register-buffer (buffer spam restore &optional mbox)
165 (with-current-buffer buffer
166 (elsp-bsfilter-call-bsfilter
168 (if restore (if spam "--sub-clean" "--sub-spam"))
169 (if spam "--add-spam" "--add-clean")
170 (if mbox "--mbox"))))
172 (luna-define-method elmo-spam-register-spam-buffer ((processor elsp-bsfilter)
173 buffer &optional restore)
174 (elsp-bsfilter-register-buffer buffer t restore))
176 (luna-define-method elmo-spam-register-good-buffer ((processor elsp-bsfilter)
177 buffer &optional restore)
178 (elsp-bsfilter-register-buffer buffer nil restore))
180 (defsubst elmo-spam-bsfilter-register-messages (folder numbers spam restore)
181 (let ((numbers (or numbers (elmo-folder-list-messages folder t t))))
182 (if (and (> (length numbers) 1)
183 elmo-spam-bsfilter-max-messages-per-process
184 (> elmo-spam-bsfilter-max-messages-per-process 0))
185 (elmo-spam-process-messages-as-mbox
187 elmo-spam-bsfilter-max-messages-per-process
188 (lambda (count spam restore)
189 (elsp-bsfilter-register-buffer (current-buffer) spam restore 'mbox)
190 (elmo-progress-notify 'elmo-spam-register count))
192 (luna-call-next-method))))
194 (luna-define-method elmo-spam-register-spam-messages :around
195 ((processor elsp-bsfilter) folder &optional numbers restore)
196 (elmo-spam-bsfilter-register-messages folder numbers t restore))
198 (luna-define-method elmo-spam-register-good-messages :around
199 ((processor elsp-bsfilter) folder &optional numbers restore)
200 (elmo-spam-bsfilter-register-messages folder numbers nil restore))
203 (product-provide (provide 'elsp-bsfilter) (require 'elmo-version))
205 ;;; elsp-bsfilter.el ends here