-
Notifications
You must be signed in to change notification settings - Fork 0
/
asf_learn
executable file
·94 lines (80 loc) · 1.97 KB
/
asf_learn
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /bin/sh
# asf_learn (Bourne shell script) -- Advanced Spam Filtering's learning script
# Learns from a given user's "learn-spam" & "learn-notspam" folders
#
# Usage: asf_learn [-d] <username>
#
# Note: this script must be run as the user given as <username>
# The cron job that calls this script should redirect stdout to a WRITABLE log file
#
# Version: 1.1
self=${0##*/}
syslog_facility=mail
syslog_tag="$self[$$]"
dir_empty()
{
if [ "$(ls -A $1)" ]; then
return 1
else
return 0
fi
}
# *** MAINLINE ***
# check for -d (the debug switch)
if [ x"$1" = x-d ] ; then
shift
log()
{
log_level=$1
shift
echo "$self($log_level): $*"
}
else
log()
{
log_level=$1
shift
logger -t $syslog_tag -p $syslog_facility.$log_level "$*"
}
fi
if [ -n "$1" ] ; then
username=$1
else
echo "$self: no username supplied" >&2
exit 1
fi
user_homedir=$(getent passwd $username | cut -d: -f6)
if [ ! -d $user_homedir ] ; then
echo "$self: invalid user '$username'" >&2
exit 2
fi
if [ ! -d $user_homedir/Maildir/.learn-spam/cur ] ; then
echo "$self: ~$username/Maildir/.learn-spam doesn't exist" >&2
exit 3
fi
if [ ! -d $user_homedir/Maildir/.learn-notspam/cur ] ; then
echo "$self: ~$username/Maildir/.learn-notspam doesn't exist" >&2
exit 3
fi
echo "$(date --rfc-3339=seconds) $username"
if ! dir_empty $user_homedir/Maildir/.learn-spam/cur
then
touch $user_homedir/Maildir/.learn-spam/asf_learned
log debug "spam for $username... learning"
sa-learn --no-sync --spam $user_homedir/Maildir/.learn-spam/cur
sync=y
fi
if ! dir_empty $user_homedir/Maildir/.learn-notspam/cur
then
log debug "ham for $username... learning"
sa-learn --no-sync --ham $user_homedir/Maildir/.learn-notspam/cur
sync=y
log debug "ham for $username... moving to inbox"
mv -v $user_homedir/Maildir/.learn-notspam/cur/* $user_homedir/Maildir/cur
fi
if [ "$sync" = y ] ; then
log debug "syncing for $username"
sa-learn --sync
fi
echo
log info "done for $username"