]> git.donarmstrong.com Git - bin.git/blob - learn_from_spam
add mutt alias which executes neomutt if that exists
[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 -s $((100 * 1024 * 1024))"
10 HAM_REPORT="spamc --learntype=ham -s $((100 * 1024 * 1024))"
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         else
22             spamasssin --local --report < "$file" >/dev/null 2>&1;
23         fi
24         formail -c < "$file" | grep -e '^From ' -e 'From: ' | spamassassin --add-to-blacklist >/dev/null 2>&1;
25         formail -c < "$file" | grep -e '^From ' -e 'From: ' | spamassassin --remove-from-whitelist >/dev/null 2>&1;
26         # check to see if it's still ham
27         if $SPAM_CHECK < "$file" >/dev/null 2>&1; then
28             mkdir -p "${NEEDS_SPAM_RULES}/cur";
29             TMP="$(mktemp -d)"
30             trap "rm -f '$TMP'/file; rmdir '$TMP'" EXIT
31             # this message is still not spam; may need custom rules
32             spamc -s $((100 * 1024 * 1024)) < "$file" > "$TMP/file" 2>/dev/null;
33             mv "$TMP/file" "${NEEDS_SPAM_RULES}/cur/$(basename "$file")";
34             trap - EXIT;
35             rmdir "$TMP";
36         elif [ "$(pwd)" = "${NEEDS_SPAM_RULES}" ]; then
37             rm -f "$file";
38         fi;
39     done;
40 elif [ "$(basename $0)" = "learn_from_ham" ]; then
41     for file in "$@"; do
42         $HAM_REPORT < "$file" >/dev/null 2>&1;
43         formail -c < "$file" |grep -e '^From ' -e 'From: ' -e 'To: ' -e 'Cc: '|spamassassin --add-to-whitelist >/dev/null 2>&1;
44         if ! $SPAM_CHECK < "$file" >/dev/null 2>&1; then
45             mkdir -p "${NEEDS_HAM_RULES}/cur";
46             TMP="$(mktemp -d)"
47             trap "rm -f '$TMP'/file; rmdir '$TMP'" EXIT
48             spamc -s $((100 * 1024 * 1024)) < "$file" > "$TMP/file" 2>/dev/null;
49             mv "$TMP/file" "${NEEDS_HAM_RULES}/cur/$(basename "$file")";
50             trap - EXIT;
51             rmdir "$TMP";
52         elif [ "$(pwd)" = "${NEEDS_HAM_RULES}" ]; then
53             rm -f "$file";
54         fi;
55     done;
56 else
57     "Called in a way this script cannot recognize";
58 fi;