2004-11-23 Daishi Kato <daishi@axlight.com>
[elisp/wanderlust.git] / elmo / elsp-spamoracle.el
1 ;;; elsp-spamoracle.el --- SpamOracle support for elmo-spam.
2
3 ;; Copyright (C) 2004 Daishi Kato <daishi@axlight.com>
4
5 ;; Author: Daishi Kato <daishi@axlight.com>
6 ;; Keywords: mail, net news, spam
7
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14 ;;
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24 ;;
25
26 ;;; Commentary:
27 ;;
28
29 ;;; Code:
30 ;;
31 (require 'elmo-spam)
32
33 (defgroup elmo-spam-spamoracle nil
34   "Spam spamoracle configuration."
35   :group 'elmo-spam)
36
37 (defcustom elmo-spam-spamoracle-program "spamoracle"
38   "Program name of the SpamOracle."
39   :type '(string :tag "Program name of the SpamOracle")
40   :group 'elmo-spam-spamoracle)
41
42 (defcustom elmo-spam-spamoracle-config-filename nil
43   "Filename of the SpamOracle config."
44   :type '(file :tag "Filename of the SpamOracle config")
45   :group 'elmo-spam-spamoracle)
46
47 (defcustom elmo-spam-spamoracle-database-filename
48   (expand-file-name ".spamoracle.db" elmo-msgdb-directory)
49   "Filename of the SpamOracle database."
50   :type '(file :tag "Filename of the SpamOracle database")
51   :group 'elmo-spam-spamoracle)
52
53 (eval-and-compile
54   (luna-define-class elsp-spamoracle (elsp-generic)))
55
56 (defsubst elmo-spam-spamoracle-call (type)
57   (let ((args (cond
58                ((eq type 'check)
59                 (list "mark"))
60                ((eq type 'add-spam)
61                 (list "add" "-v" "-spam"))
62                ((eq type 'add-good)
63                 (list "add" "-v" "-good"))))
64         (output-buffer (get-buffer-create "*Output ELMO SpamOracle*")))
65     (with-current-buffer output-buffer
66       (erase-buffer))
67     (apply #'call-process-region
68            (point-min) (point-max)
69            elmo-spam-spamoracle-program
70            nil output-buffer
71            nil (delq nil
72                      (append (if elmo-spam-spamoracle-config-filename
73                                  (list "-config"
74                                        elmo-spam-spamoracle-config-filename))
75                              (if elmo-spam-spamoracle-database-filename
76                                  (list "-f"
77                                        elmo-spam-spamoracle-database-filename))
78                              args)))
79     (if (eq type 'check)
80         (with-current-buffer output-buffer
81           (goto-char (point-min))
82           (let ((body-point (re-search-forward "^$" nil t)))
83             (goto-char (point-min))
84             (re-search-forward "^X-Spam: yes;" body-point t)))
85       t)))
86
87 (luna-define-method elmo-spam-buffer-spam-p ((processor elsp-spamoracle)
88                                              buffer &optional register)
89   (let ((result (with-current-buffer buffer
90                   (elmo-spam-spamoracle-call 'check))))
91     (when register
92       (if result
93           (elmo-spam-register-spam-buffer processor buffer)
94         (elmo-spam-register-good-buffer processor buffer)))
95     result))
96
97 (luna-define-method elmo-spam-register-spam-buffer ((processor elsp-spamoracle)
98                                                     buffer &optional restore)
99   (with-current-buffer buffer
100     (elmo-spam-spamoracle-call 'add-spam)))
101
102 (luna-define-method elmo-spam-register-good-buffer ((processor elsp-spamoracle)
103                                                     buffer &optional restore)
104   (with-current-buffer buffer
105     (elmo-spam-spamoracle-call 'add-good)))
106
107 (require 'product)
108 (product-provide (provide 'elsp-spamoracle) (require 'elmo-version))
109
110 ;;; elsp-spamoracle.el ends here