(elmo-spam-bogofilter-spam-switch): Abolish.
[elisp/wanderlust.git] / elmo / elsp-bogofilter.el
1 ;;; elsp-bogofilter.el --- Bogofilter support for elmo-spam.
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 (require 'elmo-spam)
33 (require 'luna)
34
35 (defgroup elmo-spam-bogofilter nil
36   "Spam bogofilter configuration."
37   :group 'elmo-spam)
38
39 (defcustom elmo-spam-bogofilter-program "bogofilter"
40   "*Program name of the Bogofilter."
41   :type '(string :tag "Program name of the bogofilter")
42   :group 'elmo-spam-bogofilter)
43
44 (defcustom elmo-spam-bogofilter-args nil
45   "*Argument list for bogofilter."
46   :type '(repeat string)
47   :group 'elmo-spam-bogofilter)
48
49 (defcustom elmo-spam-bogofilter-database-directory nil
50   "*Directory path of the Bogofilter databases."
51   :type '(choice (directory :tag "Location of the Bogofilter database directory")
52                  (const :tag "Use the default"))
53   :group 'elmo-spam-bogofilter)
54
55 (defcustom elmo-spam-bogofilter-arguments-alist
56   '((classify . ("-v" "-2"
57                  (if register "-u")
58                  (if elmo-spam-bogofilter-database-directory
59                      (list "-d" elmo-spam-bogofilter-database-directory))))
60     (register . ("-v"
61                  (if spam "-s" "-n")
62                  (if restore (if spam "-N" "-S"))
63                  (if elmo-spam-bogofilter-database-directory
64                      (list "-d" elmo-spam-bogofilter-database-directory)))))
65   "*An alist of options that are used with call bogofilter process.
66 Each element is a list of following:
67 \(TYPE . LIST-EXP)
68 TYPE is a symbol from `classify' or `register'.
69 LIST-EXP is an expression to get list of options."
70   :type '(repeat (cons (choice (const :tag "Classify" classify)
71                                (const :tag "Register" register))
72                        (repeat sexp)))
73   :group 'elmo-spam-bogofilter)
74
75 (defcustom elmo-spam-bogofilter-debug nil
76   "Non-nil to debug elmo bogofilter spam backend."
77   :type 'boolean
78   :group 'elmo-spam-bogofilter)
79
80
81 (eval-and-compile
82   (luna-define-class elsp-bogofilter (elsp-generic)))
83
84 (defsubst elmo-spam-bogofilter-call (&rest args)
85   (apply #'call-process-region
86          (point-min) (point-max)
87          elmo-spam-bogofilter-program
88          nil (if elmo-spam-bogofilter-debug
89                  (get-buffer-create "*Debug ELMO SPAM Bogofilter*"))
90          nil
91          (delq nil (append elmo-spam-bogofilter-args
92                            (elmo-flatten args)))))
93
94 (defmacro elmo-spam-bogofilter-arguments (type)
95   `(mapcar #'eval
96            (cdr (assq ,type elmo-spam-bogofilter-arguments-alist))))
97
98 (luna-define-method elmo-spam-buffer-spam-p ((processor elsp-bogofilter)
99                                              buffer &optional register)
100   (with-current-buffer buffer
101     (= 0 (elmo-spam-bogofilter-call
102           (elmo-spam-bogofilter-arguments 'classify)))))
103
104 (defsubst elsp-bogofilter-register-buffer (buffer spam restore)
105   (with-current-buffer buffer
106     (elmo-spam-bogofilter-call
107      (elmo-spam-bogofilter-arguments 'register))))
108
109 (luna-define-method elmo-spam-register-spam-buffer ((processor elsp-bogofilter)
110                                                     buffer &optional restore)
111   (elsp-bogofilter-register-buffer buffer t restore))
112
113 (luna-define-method elmo-spam-register-good-buffer ((processor elsp-bogofilter)
114                                                     buffer &optional restore)
115   (elsp-bogofilter-register-buffer buffer nil restore))
116
117 (require 'product)
118 (product-provide (provide 'elsp-bogofilter) (require 'elmo-version))
119
120 ;;; elsp-bogofilter.el ends here