7000307207a64c091922d97d22107b2cdcf82c8d
[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 . ((if register "-u")
57                  (if elmo-spam-bogofilter-database-directory
58                      (list "-d" elmo-spam-bogofilter-database-directory))))
59     (register . ((if spam "-s" "-n")
60                  (if restore (if spam "-N" "-S"))
61                  (if elmo-spam-bogofilter-database-directory
62                      (list "-d" elmo-spam-bogofilter-database-directory)))))
63   "*An alist of options that are used with call bogofilter process.
64 Each element is a list of following:
65 \(TYPE . LIST-EXP)
66 TYPE is a symbol from `classify' or `register'.
67 LIST-EXP is an expression to get list of options."
68   :type '(repeat (cons (choice (const :tag "Classify" classify)
69                                (const :tag "Register" register))
70                        (repeat sexp)))
71   :group 'elmo-spam-bogofilter)
72
73 (defcustom elmo-spam-bogofilter-debug nil
74   "Non-nil to debug elmo bogofilter spam backend."
75   :type 'boolean
76   :group 'elmo-spam-bogofilter)
77
78
79 (eval-and-compile
80   (luna-define-class elsp-bogofilter (elsp-generic)))
81
82 (defsubst elmo-spam-bogofilter-call (&rest args)
83   (apply #'call-process-region
84          (point-min) (point-max)
85          elmo-spam-bogofilter-program
86          nil (if elmo-spam-bogofilter-debug
87                  (get-buffer-create "*Debug ELMO SPAM Bogofilter*"))
88          nil
89          (delq nil (append elmo-spam-bogofilter-args
90                            (elmo-flatten args)))))
91
92 (defmacro elmo-spam-bogofilter-arguments (type)
93   `(mapcar #'eval
94            (cdr (assq ,type elmo-spam-bogofilter-arguments-alist))))
95
96 (luna-define-method elmo-spam-buffer-spam-p ((processor elsp-bogofilter)
97                                              buffer &optional register)
98   (with-current-buffer buffer
99     (= 0 (elmo-spam-bogofilter-call
100           (elmo-spam-bogofilter-arguments 'classify)))))
101
102 (defsubst elsp-bogofilter-register-buffer (buffer spam restore)
103   (with-current-buffer buffer
104     (elmo-spam-bogofilter-call
105      (elmo-spam-bogofilter-arguments 'register))))
106
107 (luna-define-method elmo-spam-register-spam-buffer ((processor elsp-bogofilter)
108                                                     buffer &optional restore)
109   (elsp-bogofilter-register-buffer buffer t restore))
110
111 (luna-define-method elmo-spam-register-good-buffer ((processor elsp-bogofilter)
112                                                     buffer &optional restore)
113   (elsp-bogofilter-register-buffer buffer nil restore))
114
115 (require 'product)
116 (product-provide (provide 'elsp-bogofilter) (require 'elmo-version))
117
118 ;;; elsp-bogofilter.el ends here