aboutsummaryrefslogtreecommitdiff
path: root/philter.sh
blob: f260a96983585f0200ca9dbcd2728a2c0aae1f51 (plain)
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
#!/bin/bash
#
# Maildir simple filter
# Feedback: rhatto at riseup.net | gpl
#

PREFILTER="$HOME/apps/scripts/philter.py"
BASE="$HOME/mail/"
MAILBOXES="$BASE/Sync/"
INBOXES=""
#TRASHCAN="$BASE/INBOX.Trash/cur"
TRASHCAN="$BASE/INBOX.Trash/new"
SUBJECT="yes" # wheter to filter subject
DEL="no"      # delete the message 
BOGOFILTER="yes"
FILTER="***SPAM***"

# Load configuration
if [ -e "$HOME/.config/scripts/philter" ]; then
  source $HOME/.config/scripts/philter
fi

for account in $INBOXES; do

	NEWBOX="$MAILBOXES/$account/INBOX/new"

	if [ -x $PREFILTER ]; then
		$PREFILTER
	fi

	cont="0"
	cd $NEWBOX

	for file in `ls -1`; do
		if grep -m 1 -e "X-Bogosity" "$file" | grep -q "Spam"; then
			mv "$file" "$TRASHCAN"
			if [[ "$DEL" == "yes" ]]; then
				rm "$TRASHCAN/$file"
			fi
			((cont++))
		elif [[ "$BOGOFILTER" == "yes" ]]; then
			# bogofilter
			if cat $file | bogofilter -u -e -p | grep -q -e "^X-Bogosity: Spam, tests=bogofilter"; then
				mv "$file" "$TRASHCAN"
				if [[ "$DEL" == "yes" ]]; then
					rm "$TRASHCAN/$file"
				fi
				((cont++))
			fi
		fi
	done

	if [ ! -z "$SUBJECT" ]; then
		for file in `ls -1`; do
			if grep -m 1 "$FILTER" "$file" | grep -q "Subject"; then
				mv "$file" "$TRASHCAN"
				if [[ "$DEL" == "yes" ]]; then
					rm "$TRASHCAN/$file"
				fi
				((cont++))
			fi
		done
	fi

	echo "Total: $cont filtered messages for account $account."

done