File: spambayes.el

package info (click to toggle)
spambayes 1.1b1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,828 kB
  • ctags: 7,683
  • sloc: python: 48,783; ansic: 535; sh: 89; lisp: 83; makefile: 49
file content (188 lines) | stat: -rw-r--r-- 6,049 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;; spambayes.el -- integrate spambayes into Gnus and VM
;; Copyright (C) 2003 Neale Pickett <neale@woozle.org>
;; Time-stamp: <2003-11-17 11:49:29 neale>

;; This is free software; you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free
;; Software Foundation; either version 2, or (at your option) any later
;; version.

;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;; Purpose:
;;
;; Functions to put spambayes into Gnus and VM.

;; GNUS
;; ----
;; To install, just drop this file in your load path, and insert the
;; following lines in ~/.gnus:
;;
;; (setq gnus-summary-mode-hook
;;   (lambda ()
;;     (require 'spambayes)
;;     (define-key gnus-summary-mode-map [(B) (s)] 'spambayes-gnus-refile-as-spam)
;;     (define-key gnus-summary-mode-map [(B) (h)] 'spambayes-gnus-refile-as-ham)))
;;
;; In summary mode, "B h" will train a message as ham and refile, and "B
;; s", appropriately enough, will train a message as spam and refile.
;; If you misfile something, simply locate it again and refile
;; it--sb_filter will know that you're retraining the message.
;;
;;
;; You can also put the following in ~/.gnus to run messages through the
;; filter as Gnus reads them in:
;;
;;  (setq nnmail-prepare-incoming-message-hook 'spambayes-filter-buffer)
;;
;; You can then use Gnus message splitting (looking at the
;; X-Spambayes-Classification header) to file messages based on the
;; spambayes score.
;;
;; Some folks may prefer using procmail to score messages when they
;; arrive.  See README.txt in the distribution for more information on
;; how to do this.




;; VM (Courtesy of Prabhu Ramachandran <prabhu@aero.iitm.ernet.in>)
;; ----
;; Put the following in ~/.vm:
;;
;; (load-library "spambayes")
;;
;; This binds "l h" to retrain processable messages as ham and "l s"
;; to retrain them as spam.
;; (define-key vm-mode-map "ls" 'spambayes-vm-retrain-as-spam)
;; (define-key vm-summary-mode-map "ls" 'spambayes-vm-retrain-as-spam)
;; (define-key vm-mode-map "lh" 'spambayes-vm-retrain-as-ham)
;; (define-key vm-summary-mode-map "lh" 'spambayes-vm-retrain-as-ham)
;;
;; (setq vm-auto-folder-alist
;;       '(("X-Spambayes-Classification:" ("spam" . "~/vmmail/SPAM"))
;;         ("X-Spambayes-Classification:" ("unsure" . "~/vmmail/UNSURE"))
;;         )
;; )
;;
;; Hitting the 'A' key will refile messages to the SPAM and UNSURE folders.
;;
;; The following visible header list might also be useful:
;; (setq vm-visible-headers
;;    '("Resent-"
;;      "From:" "Sender:" "Reply-To:"
;;      "To:" "Apparently-To:" "Cc:"
;;      "Subject:"
;;      "Date:"
;;      "X-Spambayes-Classification:"))


(defvar spambayes-filter-program "/usr/bin/sb_filter"
  "Path to the sb_filter program.")


;; Gnus

(defun spambayes-filter-buffer (&optional buffer)
  "Filter a buffer through Spambayes.

This pipes the a buffer through Spambayes, which adds its headers.  The
output of Spambayes replaces the contents of the buffer.  If no buffer
is specified, the current buffer is used.
"
  (shell-command-on-region
   (point-min)
   (point-max)
   (concat
    spambayes-filter-program
    " -f")
   (or buffer (current-buffer))
   t))

(defun spambayes-gnus-retrain (is-spam)
  "Retrain on all processable articles, or the one under the cursor.

This will replace the buffer contents with command output.  You can then
respool the article.

is-spam is a boolean--true if you want to retrain the message as spam,
false if you want to retrain as ham.
"
  (labels ((do-exec (n group is-spam)
		    (message "Retraining...")
		    (with-temp-buffer
		      (gnus-request-article-this-buffer n group)
		      (cond
		       ((zerop (shell-command-on-region
			       (point-min)
			       (point-max)
			       (concat
				spambayes-filter-program
				(if is-spam " -s" " -g")
				" -f")
			       (current-buffer)
			       t))
			(gnus-request-replace-article n group (current-buffer))
			(message "Retrained article."))
		       (t
			(message "Unable to parse article--leaving it alone."))))))
    (let ((group gnus-newsgroup-name)
	  (list gnus-newsgroup-processable))
      (if (>= (length list) 1)
	  (while list
	    (let ((n (car list)))
	      (do-exec n group is-spam))
	    (setq list (cdr list)))
	(let ((n (gnus-summary-article-number)))
	  (do-exec n group is-spam))))))

(defun spambayes-gnus-refile-as-spam ()
  "Retrain and refilter all process-marked messages as spam, then respool them"
  (interactive)
  (spambayes-gnus-retrain 't)
  (gnus-summary-respool-article nil (gnus-group-method gnus-newsgroup-name)))

(defun spambayes-gnus-refile-as-ham ()
  "Retrain and refilter all process-marked messages as ham, then respool them"
  (interactive)
  (spambayes-gnus-retrain nil)
  (gnus-summary-respool-article nil (gnus-group-method gnus-newsgroup-name)))



;;; VM

(defun spambayes-vm-retrain (is-spam)
  "Retrain on all processable articles, or the one under the cursor.

is-spam is a boolean--true if you want to retrain the message as spam,
false if you want to retrain as ham.
"
  (interactive)
  (message (concat "Retraining" (if is-spam " as SPAM" " as HAM") " ..."))
  (vm-pipe-message-to-command
   (concat spambayes-filter-program (if is-spam " -s" " -g") " -f") nil)
  (message (concat "Done retraining messages"
                   (if is-spam " as SPAM" " as HAM") ".") )
)

(defun spambayes-vm-retrain-as-spam ()
  "Retrain and refilter messages as spam"
  (interactive)
  (spambayes-vm-retrain t)
)

(defun spambayes-vm-retrain-as-ham ()
  "Retrain and refilter messages as ham"
  (interactive)
  (spambayes-vm-retrain nil)
)

(provide 'spambayes)