]> git.donarmstrong.com Git - bin.git/blob - learn_from_spam
use the new format for capture in remember-mail
[bin.git] / learn_from_spam
1 #!/bin/bash
2
3 # these are for non-spamc setups
4 SPAM_REPORT="spamassassin --report"
5 HAM_REPORT="spamassassin --revoke"
6 SPAM_CHECK="spamassassin -e"
7
8 # these are for spamc
9 SPAM_REPORT="spamc --learntype=spam"
10 HAM_REPORT="spamc --learntype=ham"
11 SPAM_CHECK="spamc -s $((100 * 1024 * 1024)) -c"
12
13 # needs rules directories
14 NEEDS_SPAM_RULES="$(echo ~/Maildir/spam/needs_rules)"
15 NEEDS_HAM_RULES="$(echo ~/Maildir/spam/needs_ham_rules)"
16
17 if [ "$(basename $0)" = "learn_from_spam" ]; then
18     for file in "$@"; do
19         if ! formail -c < "$file"|grep -q '^List-Id'; then
20             $SPAM_REPORT < "$file" >/dev/null 2>&1;
21         fi;
22         formail -c < "$file" | grep -e '^From ' -e 'From: ' | spamassassin --add-to-blacklist >/dev/null 2>&1;
23         formail -c < "$file" | grep -e '^From ' -e 'From: ' | spamassassin --remove-from-whitelist >/dev/null 2>&1;
24         # check to see if it's still ham
25         if $SPAM_CHECK < "$file" >/dev/null 2>&1; then
26             mkdir -p "${NEEDS_SPAM_RULES}/cur";
27             TMP="$(mktemp -d)"
28             trap "rm -f '$TMP'/file; rmdir '$TMP'" EXIT
29             # this message is still not spam; may need custom rules
30             spamc < "$file" > "$TMP/file" 2>/dev/null;
31             mv "$TMP/file" "${NEEDS_SPAM_RULES}/cur/$(basename "$file")";
32             trap - EXIT;
33             rmdir "$TMP";
34         elif [ "$(pwd)" = "${NEEDS_SPAM_RULES}" ]; then
35             rm -f "$file";
36         fi;
37     done;
38 elif [ "$(basename $0)" = "learn_from_ham" ]; then
39     for file in "$@"; do
40         $HAM_REPORT < "$file" >/dev/null 2>&1;
41         formail -c < "$file" |grep -e '^From ' -e 'From: ' -e 'To: ' -e 'Cc: '|spamassassin --add-to-whitelist >/dev/null 2>&1;
42         if ! $SPAM_CHECK < "$file" >/dev/null 2>&1; then
43             mkdir -p "${NEEDS_HAM_RULES}/cur";
44             TMP="$(mktemp -d)"
45             trap "rm -f '$TMP'/file; rmdir '$TMP'" EXIT
46             spamc < "$file" > "$TMP/file" 2>/dev/null;
47             mv "$TMP/file" "${NEEDS_HAM_RULES}/cur/$(basename "$file")";
48             trap - EXIT;
49             rmdir "$TMP";
50         elif [ "$(pwd)" = "${NEEDS_HAM_RULES}" ]; then
51             rm -f "$file";
52         fi;
53     done;
54 else
55     "Called in a way this script cannot recognize";
56 fi;