88155
|
1 ;;; mh-junk.el --- Interface to anti-spam measures
|
|
2
|
|
3 ;; Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Satyaki Das <satyaki@theforce.stanford.edu>,
|
|
6 ;; Bill Wohler <wohler@newt.com>
|
|
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
|
|
8 ;; Keywords: mail, spam
|
|
9
|
|
10 ;; This file is part of GNU Emacs.
|
|
11
|
|
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
13 ;; it under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
20 ;; GNU General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
25 ;; Boston, MA 02110-1301, USA.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; Spam handling in MH-E.
|
|
30
|
|
31 ;;; Change Log:
|
|
32
|
|
33 ;;; Code:
|
|
34
|
|
35 ;;(message "< mh-junk")
|
|
36 (eval-when-compile (require 'mh-acros))
|
|
37 (mh-require-cl)
|
|
38 (require 'mh-buffers)
|
|
39 (require 'mh-e)
|
|
40 ;;(message "> mh-junk")
|
|
41
|
|
42 ;; Interactive functions callable from the folder buffer
|
|
43 ;;;###mh-autoload
|
|
44 (defun mh-junk-blacklist (range)
|
|
45 "Blacklist RANGE as spam.
|
|
46
|
|
47 This command trains the spam program in use (see the option
|
|
48 `mh-junk-program') with the content of RANGE and then handles the
|
|
49 message(s) as specified by the option `mh-junk-disposition'.
|
|
50
|
|
51 Check the documentation of `mh-interactive-range' to see how RANGE is
|
|
52 read in interactive use.
|
|
53
|
|
54 For more information about using your particular spam fighting
|
|
55 program, see:
|
|
56
|
|
57 - `mh-spamassassin-blacklist'
|
|
58 - `mh-bogofilter-blacklist'
|
|
59 - `mh-spamprobe-blacklist'"
|
|
60 (interactive (list (mh-interactive-range "Blacklist")))
|
|
61 (let ((blacklist-func (nth 1 (assoc mh-junk-choice mh-junk-function-alist))))
|
|
62 (unless blacklist-func
|
|
63 (error "Customize `mh-junk-program' appropriately"))
|
|
64 (let ((dest (cond ((null mh-junk-disposition) nil)
|
|
65 ((equal mh-junk-disposition "") "+")
|
|
66 ((eq (aref mh-junk-disposition 0) ?+)
|
|
67 mh-junk-disposition)
|
|
68 ((eq (aref mh-junk-disposition 0) ?@)
|
|
69 (concat mh-current-folder "/"
|
|
70 (substring mh-junk-disposition 1)))
|
|
71 (t (concat "+" mh-junk-disposition)))))
|
|
72 (mh-iterate-on-range msg range
|
|
73 (message "Blacklisting message %d..." msg)
|
|
74 (funcall (symbol-function blacklist-func) msg)
|
|
75 (message "Blacklisting message %d...done" msg)
|
|
76 (if (not (memq msg mh-seen-list))
|
|
77 (setq mh-seen-list (cons msg mh-seen-list)))
|
|
78 (if dest
|
|
79 (mh-refile-a-msg nil (intern dest))
|
|
80 (mh-delete-a-msg nil)))
|
|
81 (mh-next-msg))))
|
|
82
|
|
83 ;;;###mh-autoload
|
|
84 (defun mh-junk-whitelist (range)
|
|
85 "Whitelist RANGE as ham.
|
|
86
|
|
87 This command reclassifies the RANGE as ham if it were incorrectly
|
|
88 classified as spam (see the option `mh-junk-program'). It then
|
|
89 refiles the message into the \"+inbox\" folder.
|
|
90
|
|
91 Check the documentation of `mh-interactive-range' to see how
|
|
92 RANGE is read in interactive use."
|
|
93 (interactive (list (mh-interactive-range "Whitelist")))
|
|
94 (let ((whitelist-func (nth 2 (assoc mh-junk-choice mh-junk-function-alist))))
|
|
95 (unless whitelist-func
|
|
96 (error "Customize `mh-junk-program' appropriately"))
|
|
97 (mh-iterate-on-range msg range
|
|
98 (message "Whitelisting message %d..." msg)
|
|
99 (funcall (symbol-function whitelist-func) msg)
|
|
100 (message "Whitelisting message %d...done" msg)
|
|
101 (mh-refile-a-msg nil (intern mh-inbox)))
|
|
102 (mh-next-msg)))
|
|
103
|
|
104
|
|
105
|
|
106 ;; Spamassassin Interface
|
|
107
|
|
108 (defvar mh-spamassassin-executable (executable-find "spamassassin"))
|
|
109 (defvar mh-sa-learn-executable (executable-find "sa-learn"))
|
|
110
|
|
111 (defun mh-spamassassin-blacklist (msg)
|
|
112 "Blacklist MSG with SpamAssassin.
|
|
113
|
|
114 SpamAssassin is one of the more popular spam filtering programs.
|
|
115 Get it from your local distribution or from
|
|
116 http://spamassassin.org/.
|
|
117
|
|
118 To use SpamAssassin, add the following recipes to
|
|
119 \".procmailrc\":
|
|
120
|
|
121 MAILDIR=$HOME/`mhparam Path`
|
|
122
|
|
123 # Fight spam with SpamAssassin.
|
|
124 :0fw
|
|
125 | spamc
|
|
126
|
|
127 # Anything with a spam level of 10 or more is junked immediately.
|
|
128 :0:
|
|
129 * ^X-Spam-Level: ..........
|
|
130 /dev/null
|
|
131
|
|
132 :0:
|
|
133 * ^X-Spam-Status: Yes
|
|
134 spam/.
|
|
135
|
|
136 If you don't use \"spamc\", use \"spamassassin -P -a\".
|
|
137
|
|
138 Note that one of the recipes above throws away messages with a
|
|
139 score greater than or equal to 10. Here's how you can determine a
|
|
140 value that works best for you.
|
|
141
|
|
142 First, run \"spamassassin -t\" on every mail message in your
|
|
143 archive and use Gnumeric to verify that the average plus the
|
|
144 standard deviation of good mail is under 5, the SpamAssassin
|
|
145 default for \"spam\".
|
|
146
|
|
147 Using Gnumeric, sort the messages by score and view the messages
|
|
148 with the highest score. Determine the score which encompasses all
|
|
149 of your interesting messages and add a couple of points to be
|
|
150 conservative. Add that many dots to the \"X-Spam-Level:\" header
|
|
151 field above to send messages with that score down the drain.
|
|
152
|
|
153 In the example above, messages with a score of 5-9 are set aside
|
|
154 in the \"+spam\" folder for later review. The major weakness of
|
|
155 rules-based filters is a plethora of false positives so it is
|
|
156 worthwhile to check.
|
|
157
|
|
158 If SpamAssassin classifies a message incorrectly, or is unsure,
|
|
159 you can use the MH-E commands \\[mh-junk-blacklist] and
|
|
160 \\[mh-junk-whitelist].
|
|
161
|
|
162 The command \\[mh-junk-blacklist] adds a \"blacklist_from\" entry
|
|
163 to \"~/spamassassin/user_prefs\", deletes the message, and sends
|
|
164 the message to the Razor, so that others might not see this spam.
|
|
165 If the \"sa-learn\" command is available, the message is also
|
|
166 recategorized as spam.
|
|
167
|
|
168 The command \\[mh-junk-whitelist] adds a \"whitelist_from\" rule
|
|
169 to the \"~/.spamassassin/user_prefs\" file. If the \"sa-learn\"
|
|
170 command is available, the message is also recategorized as ham.
|
|
171
|
|
172 Over time, you'll observe that the same host or domain occurs
|
|
173 repeatedly in the \"blacklist_from\" entries, so you might think
|
|
174 that you could avoid future spam by blacklisting all mail from a
|
|
175 particular domain. The utility function
|
|
176 `mh-spamassassin-identify-spammers' helps you do precisely that.
|
|
177 This function displays a frequency count of the hosts and domains
|
|
178 in the \"blacklist_from\" entries from the last blank line in
|
|
179 \"~/.spamassassin/user_prefs\" to the end of the file. This
|
|
180 information can be used so that you can replace multiple
|
|
181 \"blacklist_from\" entries with a single wildcard entry such as:
|
|
182
|
|
183 blacklist_from *@*amazingoffersdirect2u.com
|
|
184
|
|
185 In versions of SpamAssassin (2.50 and on) that support a Bayesian
|
|
186 classifier, \\[mh-junk-blacklist] uses the program \"sa-learn\"
|
|
187 to recategorize the message as spam. Neither MH-E, nor
|
|
188 SpamAssassin, rebuilds the database after adding words, so you
|
|
189 will need to run \"sa-learn --rebuild\" periodically. This can be
|
|
190 done by adding the following to your crontab:
|
|
191
|
|
192 0 * * * * sa-learn --rebuild > /dev/null 2>&1"
|
|
193 (unless mh-spamassassin-executable
|
|
194 (error "Unable to find the spamassassin executable"))
|
|
195 (let ((current-folder mh-current-folder)
|
|
196 (msg-file (mh-msg-filename msg mh-current-folder))
|
|
197 (sender))
|
|
198 (save-excursion
|
|
199 (message "Reporting message %d..." msg)
|
|
200 (mh-truncate-log-buffer)
|
|
201 (call-process mh-spamassassin-executable msg-file mh-log-buffer nil
|
|
202 ;;"--report" "--remove-from-whitelist"
|
|
203 "-r" "-R") ; spamassassin V2.20
|
|
204 (when mh-sa-learn-executable
|
|
205 (message "Recategorizing this message as spam...")
|
|
206 (call-process mh-sa-learn-executable msg-file mh-log-buffer nil
|
|
207 "--single" "--spam" "--local" "--no-rebuild"))
|
|
208 (message "Blacklisting message %d..." msg)
|
|
209 (set-buffer (get-buffer-create mh-temp-buffer))
|
|
210 (erase-buffer)
|
|
211 (call-process (expand-file-name mh-scan-prog mh-progs)
|
|
212 nil mh-junk-background nil
|
|
213 (format "%s" msg) current-folder
|
|
214 "-format" "%<(mymbox{from})%|%(addr{from})%>")
|
|
215 (goto-char (point-min))
|
|
216 (if (search-forward-regexp "^\\(.+\\)$" nil t)
|
|
217 (progn
|
|
218 (setq sender (match-string 0))
|
|
219 (mh-spamassassin-add-rule "blacklist_from" sender)
|
|
220 (message "Blacklisting message %d...done" msg))
|
|
221 (message "Blacklisting message %d...not done (from my address)" msg)))))
|
|
222
|
|
223 (defun mh-spamassassin-whitelist (msg)
|
|
224 "Whitelist MSG with SpamAssassin.
|
|
225
|
|
226 The \\[mh-junk-whitelist] command adds a \"whitelist_from\" rule to
|
|
227 the \"~/.spamassassin/user_prefs\" file. If the \"sa-learn\" command
|
|
228 is available, the message is also recategorized as ham.
|
|
229
|
|
230 See `mh-spamassassin-blacklist' for more information."
|
|
231 (unless mh-spamassassin-executable
|
|
232 (error "Unable to find the spamassassin executable"))
|
|
233 (let ((msg-file (mh-msg-filename msg mh-current-folder))
|
|
234 (show-buffer (get-buffer mh-show-buffer))
|
|
235 from)
|
|
236 (save-excursion
|
|
237 (set-buffer (get-buffer-create mh-temp-buffer))
|
|
238 (erase-buffer)
|
|
239 (message "Removing spamassassin markup from message...")
|
|
240 (call-process mh-spamassassin-executable msg-file mh-temp-buffer nil
|
|
241 ;; "--remove-markup"
|
|
242 "-d") ; spamassassin V2.20
|
|
243 (if show-buffer
|
|
244 (kill-buffer show-buffer))
|
|
245 (write-file msg-file)
|
|
246 (when mh-sa-learn-executable
|
|
247 (message "Recategorizing this message as ham...")
|
|
248 (call-process mh-sa-learn-executable msg-file mh-temp-buffer nil
|
|
249 "--single" "--ham" "--local --no-rebuild"))
|
|
250 (message "Whitelisting message %d..." msg)
|
|
251 (setq from
|
|
252 (car (mh-funcall-if-exists
|
|
253 ietf-drums-parse-address (mh-get-header-field "From:"))))
|
|
254 (kill-buffer nil)
|
|
255 (unless (or (null from) (equal from ""))
|
|
256 (mh-spamassassin-add-rule "whitelist_from" from))
|
|
257 (message "Whitelisting message %d...done" msg))))
|
|
258
|
|
259 (defun mh-spamassassin-add-rule (rule body)
|
|
260 "Add a new rule to \"~/.spamassassin/user_prefs\".
|
|
261 The name of the rule is RULE and its body is BODY."
|
|
262 (save-window-excursion
|
|
263 (let* ((line (format "%s\t%s\n" rule body))
|
|
264 (case-fold-search t)
|
|
265 (file (expand-file-name "~/.spamassassin/user_prefs"))
|
|
266 (buffer-exists (find-buffer-visiting file)))
|
|
267 (find-file file)
|
|
268 (if (not (search-forward (format "\n%s" line) nil t))
|
|
269 (progn
|
|
270 (goto-char (point-max))
|
|
271 (insert (if (bolp) "" "\n") line)
|
|
272 (save-buffer)))
|
|
273 (if (not buffer-exists)
|
|
274 (kill-buffer nil)))))
|
|
275
|
|
276 (defun mh-spamassassin-identify-spammers ()
|
|
277 "Identify spammers who are repeat offenders.
|
|
278
|
|
279 This function displays a frequency count of the hosts and domains
|
|
280 in the \"blacklist_from\" entries from the last blank line in
|
|
281 \"~/.spamassassin/user_prefs\" to the end of the file. This
|
|
282 information can be used so that you can replace multiple
|
|
283 \"blacklist_from\" entries with a single wildcard entry such as:
|
|
284
|
|
285 blacklist_from *@*amazingoffersdirect2u.com"
|
|
286 (interactive)
|
|
287 (let* ((file (expand-file-name "~/.spamassassin/user_prefs"))
|
|
288 (domains (make-hash-table :test 'equal)))
|
|
289 (find-file file)
|
|
290 ;; Only consider entries between last blank line and end of file.
|
|
291 (goto-char (1- (point-max)))
|
|
292 (search-backward-regexp "^$")
|
|
293 ;; Perform frequency count.
|
|
294 (save-excursion
|
|
295 (while (search-forward-regexp "^blacklist_from\\s-*\\(.*\\)@\\(.*\\)$"
|
|
296 nil t)
|
|
297 (let ((host (match-string 2))
|
|
298 value)
|
|
299 ;; Remove top-level-domain from hostname.
|
|
300 (setq host (cdr (reverse (split-string host "\\."))))
|
|
301 ;; Add counts for each host and domain part.
|
|
302 (while host
|
|
303 (setq value (gethash (car host) domains))
|
|
304 (setf (gethash (car host) domains) (1+ (if (not value) 0 value)))
|
|
305 (setq host (cdr host))))))
|
|
306
|
|
307 ;; Output
|
|
308 (delete-other-windows)
|
|
309 (pop-to-buffer (get-buffer-create "*MH-E Spammer Frequencies*"))
|
|
310 (erase-buffer)
|
|
311 (maphash '(lambda (key value) ""
|
|
312 (if (> value 2)
|
|
313 (insert (format "%s %s\n" key value))))
|
|
314 domains)
|
|
315 (sort-numeric-fields 2 (point-min) (point-max))
|
|
316 (reverse-region (point-min) (point-max))
|
|
317 (goto-char (point-min))))
|
|
318
|
|
319
|
|
320
|
|
321 ;; Bogofilter Interface
|
|
322
|
|
323 (defvar mh-bogofilter-executable (executable-find "bogofilter"))
|
|
324
|
|
325 (defun mh-bogofilter-blacklist (msg)
|
|
326 "Blacklist MSG with bogofilter.
|
|
327
|
|
328 Bogofilter is a Bayesian spam filtering program. Get it from your
|
|
329 local distribution or from http://bogofilter.sourceforge.net/.
|
|
330
|
|
331 Bogofilter is taught by running:
|
|
332
|
|
333 bogofilter -n < good-message
|
|
334
|
|
335 on every good message, and
|
|
336
|
|
337 bogofilter -s < spam-message
|
|
338
|
|
339 on every spam message. This is called a full training; three other
|
|
340 training methods are described in the FAQ that is distributed with
|
|
341 bogofilter. Note that most Bayesian filters need 1000 to 5000 of each
|
|
342 type of message to start doing a good job.
|
|
343
|
|
344 To use bogofilter, add the following recipes to \".procmailrc\":
|
|
345
|
|
346 MAILDIR=$HOME/`mhparam Path`
|
|
347
|
|
348 # Fight spam with bogofilter.
|
|
349 :0fw
|
|
350 | bogofilter -3 -e -p
|
|
351
|
|
352 :0:
|
|
353 * ^X-Bogosity: Yes, tests=bogofilter
|
|
354 spam/.
|
|
355
|
|
356 :0:
|
|
357 * ^X-Bogosity: Unsure, tests=bogofilter
|
|
358 spam/unsure/.
|
|
359
|
|
360 If bogofilter classifies a message incorrectly, or is unsure, you can
|
|
361 use the MH-E commands \\[mh-junk-blacklist] and \\[mh-junk-whitelist]
|
|
362 to update bogofilter's training.
|
|
363
|
|
364 The \"Bogofilter FAQ\" suggests that you run the following
|
|
365 occasionally to shrink the database:
|
|
366
|
|
367 bogoutil -d wordlist.db | bogoutil -l wordlist.db.new
|
|
368 mv wordlist.db wordlist.db.prv
|
|
369 mv wordlist.db.new wordlist.db
|
|
370
|
|
371 The \"Bogofilter tuning HOWTO\" describes how you can fine-tune Bogofilter."
|
|
372 (unless mh-bogofilter-executable
|
|
373 (error "Unable to find the bogofilter executable"))
|
|
374 (let ((msg-file (mh-msg-filename msg mh-current-folder)))
|
|
375 (call-process mh-bogofilter-executable msg-file mh-junk-background
|
|
376 nil "-s")))
|
|
377
|
|
378 (defun mh-bogofilter-whitelist (msg)
|
|
379 "Whitelist MSG with bogofilter.
|
|
380
|
|
381 See `mh-bogofilter-blacklist' for more information."
|
|
382 (unless mh-bogofilter-executable
|
|
383 (error "Unable to find the bogofilter executable"))
|
|
384 (let ((msg-file (mh-msg-filename msg mh-current-folder)))
|
|
385 (call-process mh-bogofilter-executable msg-file mh-junk-background
|
|
386 nil "-n")))
|
|
387
|
|
388
|
|
389
|
|
390 ;; Spamprobe Interface
|
|
391
|
|
392 (defvar mh-spamprobe-executable (executable-find "spamprobe"))
|
|
393
|
|
394 (defun mh-spamprobe-blacklist (msg)
|
|
395 "Blacklist MSG with SpamProbe.
|
|
396
|
|
397 SpamProbe is a Bayesian spam filtering program. Get it from your local
|
|
398 distribution or from http://spamprobe.sourceforge.net.
|
|
399
|
|
400 To use SpamProbe, add the following recipes to \".procmailrc\":
|
|
401
|
|
402 MAILDIR=$HOME/`mhparam Path`
|
|
403
|
|
404 # Fight spam with SpamProbe.
|
|
405 :0
|
|
406 SCORE=| spamprobe receive
|
|
407
|
|
408 :0 wf
|
|
409 | formail -I \"X-SpamProbe: $SCORE\"
|
|
410
|
|
411 :0:
|
|
412 *^X-SpamProbe: SPAM
|
|
413 spam/.
|
|
414
|
|
415 If SpamProbe classifies a message incorrectly, you can use the
|
|
416 MH-E commands \\[mh-junk-blacklist] and \\[mh-junk-whitelist] to
|
|
417 update SpamProbe's training."
|
|
418 (unless mh-spamprobe-executable
|
|
419 (error "Unable to find the spamprobe executable"))
|
|
420 (let ((msg-file (mh-msg-filename msg mh-current-folder)))
|
|
421 (call-process mh-spamprobe-executable msg-file mh-junk-background
|
|
422 nil "spam")))
|
|
423
|
|
424 (defun mh-spamprobe-whitelist (msg)
|
|
425 "Whitelist MSG with SpamProbe.
|
|
426
|
|
427 See `mh-spamprobe-blacklist' for more information."
|
|
428 (unless mh-spamprobe-executable
|
|
429 (error "Unable to find the spamprobe executable"))
|
|
430 (let ((msg-file (mh-msg-filename msg mh-current-folder)))
|
|
431 (call-process mh-spamprobe-executable msg-file mh-junk-background
|
|
432 nil "good")))
|
|
433
|
|
434 (provide 'mh-junk)
|
|
435
|
|
436 ;; Local Variables:
|
|
437 ;; indent-tabs-mode: nil
|
|
438 ;; sentence-end-double-space: nil
|
|
439 ;; End:
|
|
440
|
|
441 ;; arch-tag: 603335f1-77ff-4306-8828-5d3dad51abe1
|
|
442 ;;; mh-junk.el ends here
|