Mercurial > hg > Blog
annotate content/Linux/bogofilter.md @ 110:be0331916375
README
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Fri, 18 Jun 2021 07:24:40 +0200 |
parents | ec4fc0b8cc08 |
children |
rev | line source |
---|---|
100
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
1 Title: Filtering spam mails with bogofilter |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
2 Date: 2020-03-22 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
3 Lang: en |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
4 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
5 I'm running a [Postfix](http://www.postfix.org/) server here at home to process incoming mail. To integrate spam filtering I'm delivering mails via [procmail](https://en.wikipedia.org/wiki/Procmail) - it's as simple as setting `mailbox_command = /usr/bin/procmail` in postfix' `main.cf`. |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
6 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
7 Spam filtering works by invoking the filter ([bogofilter](https://bogofilter.sourceforge.io/) in my case) from procmail like this: |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
8 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
9 :::shell |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
10 :0 fw |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
11 | /usr/bin/bogofilter -uep |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
12 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
13 This command will pipe the mail content through bogofilter which will add a `X-Bogosity` header denoting the spam status of the mail. |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
14 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
15 A second procmail rule moves all spam to a designated spam folder on my IMAP server: |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
16 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
17 :::shell |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
18 :0 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
19 * ^X-Bogosity: Spam.* |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
20 $HOME/.maildir/.INBOX.Spam/ |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
21 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
22 For training I use two designated folders on my IMAP server: *LearnSpam* and *LearnGood*. I'm simply moving mails that I want to train as spam to the *LearnSpam* folder. Once per hour I train spam/good mails using this script: |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
23 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
24 :::shell |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
25 #!/bin/bash |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
26 SPAM_FOLDER=$HOME/.maildir/.INBOX.Spam.LearnSpam |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
27 if [[ -d $SPAM_FOLDER ]]; then |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
28 SPAM_CUR=$SPAM_FOLDER/cur |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
29 # only invoke bogofilter if there are mails in the folder |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
30 if [[ "$(ls -A $SPAM_CUR)" ]]; then |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
31 /usr/bin/bogofilter -s -B $SPAM_FOLDER && rm $SPAM_CUR/* |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
32 fi |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
33 fi |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
34 HAM_FOLDER=$HOME/.maildir/.INBOX.Spam.LearnGood |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
35 if [[ -d $HAM_FOLDER ]]; then |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
36 HAM_CUR=$HAM_FOLDER/cur |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
37 # only invoke bogofilter if there are mails in the folder |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
38 if [[ "$(ls -A $HAM_CUR)" ]]; then |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
39 /usr/bin/bogofilter -n -B $HAM_FOLDER && rm $HAM_CUR/* |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
40 fi |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
41 fi |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
42 |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
43 The script is invoked via cron. |
ec4fc0b8cc08
add a blog entry for bogofilter
Dirk Olmes <dirk@xanthippe.ping.de>
parents:
diff
changeset
|
44 |