2010-07-11 David Maus <dmaus@ictsoc.de>
[elisp/wanderlust.git] / elmo / elsp-spamfilter.el
1 ;;; elsp-spamfilter.el --- Spamfilter 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
34 (require 'luna)
35 (require 'mime-view)
36 (require 'spamfilter)
37
38 (defgroup elmo-spam-spamfilter nil
39   "Spam spamfilter configuration."
40   :group 'elmo-spam)
41
42 (defcustom elmo-spam-spamfilter-corpus-filename
43   (expand-file-name ".spamfilter" elmo-msgdb-directory)
44   "Filename of the Spamfilter corpus."
45   :type '(file :tag "Filename of the Spamfilter corpus")
46   :group 'elmo-spam-spamfilter)
47
48 (eval-and-compile
49   (luna-define-class elsp-spamfilter (elsp-generic)
50                      (good-corpus bad-corpus modified))
51   (luna-define-internal-accessors 'elsp-spamfilter))
52
53 (luna-define-method initialize-instance :around ((processor elsp-spamfilter)
54                                                  &rest init-args)
55   (luna-call-next-method)
56   (let ((spamf-good-corpus (make-spamf-corpus
57                             :name "spamf-good-corpus"
58                             :table (make-hash-table :test #'eq)
59                             :message-count 0))
60         (spamf-bad-corpus  (make-spamf-corpus
61                             :name "spamf-bad-corpus"
62                             :table (make-hash-table :test #'eq)
63                             :message-count 0)))
64     (spamf-load-corpus-from-file elmo-spam-spamfilter-corpus-filename)
65     (elsp-spamfilter-set-good-corpus-internal processor spamf-good-corpus)
66     (elsp-spamfilter-set-bad-corpus-internal  processor spamf-bad-corpus)
67     processor))
68
69 (luna-define-method elmo-spam-modified-p ((processor elsp-spamfilter))
70   (elsp-spamfilter-modified-internal processor))
71
72 (luna-define-method elmo-spam-save-status ((processor elsp-spamfilter))
73   (spamf-save-corpus-to-file
74    elmo-spam-spamfilter-corpus-filename
75    (elsp-spamfilter-good-corpus-internal processor)
76    (elsp-spamfilter-bad-corpus-internal  processor))
77   (elsp-spamfilter-set-modified-internal processor nil))
78
79 (defun elsp-spamfilter-decode-buffer (buffer)
80   (mime-display-message
81    (mime-open-entity 'elmo-buffer buffer)
82    (current-buffer)))
83
84 (defsubst elsp-spamfilter-register-buffer-internal (processor buffer spam)
85   (spamf-register-words-buffer
86    (if spam
87        (elsp-spamfilter-bad-corpus-internal processor)
88      (elsp-spamfilter-good-corpus-internal processor))
89    buffer)
90   (elsp-spamfilter-set-modified-internal processor t))
91
92 (luna-define-method elmo-spam-buffer-spam-p ((processor elsp-spamfilter)
93                                              buffer &optional register)
94   (with-temp-buffer
95     (elsp-spamfilter-decode-buffer buffer)
96     (let ((spam (spamf-spam-buffer-p
97                  (current-buffer)
98                  (elsp-spamfilter-good-corpus-internal processor)
99                  (elsp-spamfilter-bad-corpus-internal  processor))))
100       (when register
101         (elsp-spamfilter-register-buffer-internal
102          processor (current-buffer) spam))
103       spam)))
104
105 (luna-define-method elmo-spam-register-spam-buffer ((processor elsp-spamfilter)
106                                                     buffer &optional restore)
107   (with-temp-buffer
108     (elsp-spamfilter-decode-buffer buffer)
109     (elsp-spamfilter-register-buffer-internal processor (current-buffer) t)))
110
111 (luna-define-method elmo-spam-register-good-buffer ((processor elsp-spamfilter)
112                                                     buffer &optional restore)
113   (with-temp-buffer
114     (elsp-spamfilter-decode-buffer buffer)
115     (elsp-spamfilter-register-buffer-internal processor (current-buffer) nil)))
116
117 (require 'product)
118 (product-provide (provide 'elsp-spamfilter) (require 'elmo-version))
119
120 ;;; elsp-spamfilter.el ends here