-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoveQmailspam.sh
executable file
·125 lines (106 loc) · 3.06 KB
/
moveQmailspam.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# ------------------------------------------------------------------
# [José Gonçalves] <jose.goncalves@dlcproduction.ch>
# moveQmailSpam
# Scripts used to move all mail tagged as spam to the qmail
# spam folder.
# This scripts should not be used if you can change your MTA
# to Dovecot. With Dovecot you can work with Sieve to do this
# job.
# ------------------------------------------------------------------
VERSION=0.0.2
UID=$(uuidgen)
USAGE="Usage: command -dhv args"
MAIL_ADDR="root@localhost"
LOGFILE="logs.txt"
# --- History ------------------------------------------------------
# - [09.07.2017] Add - uidgenerator (lock section)
# - [08.07.2017] Add - Fix regex pattern.
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
echo $USAGE
exit 1;
fi
while getopts ":d:vh" optname; do
case "$optname" in
"v")
echo "Version $VERSION"
exit 0;
;;
"d")
echo "-d argument: $OPTARG"
domain_name=$2
;;
"h")
echo $USAGE
exit 0;
;;
"?")
echo "Unknown option $OPTARG"
exit 0;
;;
":")
echo "No argument value for option $OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param=$1
# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/$UID.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# --- Syslog ------------------------------------------------------
readonly SCRIPT_NAME=$(basename $0)
# log function send your message to stdout
log() {
echo "$@"
logger -p user.notice -t $SCRIPT_NAME "$@"
}
# err function send your message to stderr
err() {
echo "$@" >&2
logger -p user.error -t $SCRIPT_NAME "$@"
}
# Local file logs / debug
if [ -e $LOGFILE ]; then
rm -f $LOGFILE
else
touch $LOGFILE
fi
# --- Body --------------------------------------------------------
mailboxes=`ls /var/qmail/mailnames/$domain_name | awk '{print $1}'`
for box in $mailboxes; do
mailbox_folder="/var/qmail/mailnames/${domain_name}/${box}/Maildir/cur/"
spam_folder="/var/qmail/mailnames/${domain_name}/${box}/Maildir/.Spam/cur/"
spam_list=`find ${mailbox_folder} -type f | xargs grep -E '^Subject: [* []+SPAM[] *]+' | awk -F ":" '{print $1":"$2}'`
for line in $spam_list; do
log "Moving $line to $spam_folder"
#echo "Moving $line to $spam_folder" >> $LOGFILE
mv -v $line $spam_folder >> $LOGFILE
done
done
# Resume job, and send mail
cat $LOGFILE | mail -s "moveQmailSpam script --> Moving Spam [$domain_name]" ${MAIL_ADDR}
# Cleaning
unset $VERSION
unset $UID
unset $USAGE
unset $LOGFILE
unset $MAIL_ADDR
unset $domain_name
unset $spam_list
unset $spam_folder
unset $mailbox_folder
# Job done
exit 0;
# -----------------------------------------------------------------