· 9 years ago · May 11, 2017, 03:44 AM
1#!/usr/bin/sh
2#mailstat.sh
3
4#a couple of temp files
5TEMPFILE="/home/peloruso/${RANDOM}${RANDOM}${RANDOM}"
6XSRTEMP="/home/peloruso/${RANDOM}${RANDOM}${RANDOM}"
7
8#set these to your correct information
9SQL_USER=mysqluser
10SQL_PW=password
11SQL_DB=mysqldatabase
12SQL_TABLE=mysqltable
13
14#procmail sends the current message to the program in STDIN, so use tee to put the message in
15#a file so we can use it over and over
16tee ${TEMPFILE}
17
18
19#requires the X-SHA1SUM header to exist.
20# :0w:${PMDIR}/sha1sum.lock
21# SHA1SUM=|/usr/bin/sha1sum
22#
23# :0fw:${PMDIR}/formail.lock
24# | /usr/bin/formail -A "X-SHA1SUM: ${SHA1SUM}"
25SHA1SUM=`/usr/bin/formail -x "X-SHA1SUM:"<${TEMPFILE}`
26SHA1SUM=`echo ${SHA1SUM}|sed 's/^ *//;s/ -$//'`
27
28
29#requires the X-PM-User header to exist.
30# :0fw:${PMDIR}/formail.lock
31# | /usr/bin/formail -A "X-PM-User: ${USER}"
32PMUSER=`/usr/bin/formail -x "X-PM-User:"<${TEMPFILE}`
33PMUSER=`echo ${PMUSER}|sed 's/^ *//'`
34
35
36#in .spamassassin/local.cf, add the following line to create the necessary header
37#add_header all Rules _TESTS(,)_
38XSR=`/usr/bin/formail -x "X-Spam-Rules:"<${TEMPFILE}`
39
40
41#in .spamassassin/local.cf, add the following line to create the necessary header
42#add_header all Score _SCORE_
43SCORE=`/usr/bin/formail -x "X-Spam-Score:"<${TEMPFILE}`
44SCORE=`echo ${SCORE}|sed 's/^ *//'`
45
46
47#requires the X-Orig-Class header to exist.
48# :0fw:${PMDIR}/formail.lock
49# * X-Spam-Level: RRR
50# | /usr/bin/formail -A "X-Orig-Class: SPAM"
51#
52# :0Efw:${PMDIR}/formail.lock
53# | /usr/bin/formail -A "X-Orig-Class: HAM"
54ORIG_CLASS=`/usr/bin/formail -x "X-Orig-Class:"<${TEMPFILE}`
55ORIG_CLASS=`echo ${ORIG_CLASS}|sed 's/^ *//'`
56
57
58#requires the X-Sender-in-whitelist header to exist
59# :0fw:${PMDIR}/formail.lock
60# * ^X-Spam-Report:.*USER_IN_WHITELIST
61# | /usr/bin/formail -A "X-Sender-in-whitelist: Yes"
62#
63# :0Efw:${PMDIR}/formail.lock
64# | /usr/bin/formail -A "X-Sender-in-whitelist: No"
65SENDER_IN_WHITELIST=`/usr/bin/formail -x "X-Sender-in-whitelist:"<${TEMPFILE}`
66SENDER_IN_WHITELIST=`echo ${SENDER_IN_WHITELIST}|sed 's/^ *//'`
67
68
69#requires the X-Sender-in-blacklist header to exist
70# :0fw:${PMDIR}/formail.lock
71# * ^X-Spam-Report:.*USER_IN_BLACKLIST
72# | /usr/bin/formail -A "X-Sender-in-blacklist: Yes"
73#
74# :0Efw:${PMDIR}/formail.lock
75# | /usr/bin/formail -A "X-Sender-in-blacklist: No"
76SENDER_IN_BLACKLIST=`/usr/bin/formail -x "X-Sender-in-blacklist:"<${TEMPFILE}`
77SENDER_IN_BLACKLIST=`echo ${SENDER_IN_BLACKLIST}|sed 's/^ *//'`
78
79
80#pipe the X-Spam-Rules header through 'tr' and 'sed' to format correctly.
81#specifically, change all of the commas to newlines using tr (put one rule on each line)
82#then make sure there are no blank lines using sed
83/usr/bin/formail -x "X-Spam-Rules" <${TEMPFILE}| tr ',' '\n' |sed '/^$/d'>${XSRTEMP}
84
85
86#used to determine if the mail is one of my approved "commercial" emails, or for a mailing list
87#for example,
88# :0fw:${PMDIR}/formail.lock
89# | /usr/bin/formail -A "X-folder-delivered-to: ${DEFAULT}Lists/cur"
90DEST=`/usr/bin/formail -x "X-folder-delivered-to:"<${TEMPFILE}`
91DEST=`echo ${DEST}|sed 's/^ *//'`
92DEST=`echo ${DEST}|sed 's/\/home\/peloruso\/mail\/pelorus.org\/skip\/\.//'`
93DEST=`echo ${DEST}|sed 's/\/home\/peloruso\/mail\/pelorus.org\/suzanne\/\.//'`
94DEST=`echo ${DEST}|sed 's/\/cur.*//'`
95
96#count the number of rules that were hit
97RULECOUNT=`wc -l ${XSRTEMP}|grep [0-9]* -o`
98
99
100#add the columns to the database. I "try" to add a column for every rule that was hit. Of course,
101#if the column already exists, it will not recreate the column, so it generates an error in the
102#procmail log. Also build "Q2", which is similar to the ALTER statement, except that it will be
103#used in the INSERT statement. Note the XSRTEMP redirection after the 'done' statement on the
104#last line. So, each time through the do loop, this will try to add a new column with the name
105#of the rule. It will also build Q2 which is used in the QUERY statement a little later.
106#Q2 will look something like this (starting with the first comma):
107# , 'BAYES_99', 'AWL', 'HTML_MESSAGE'
108while read rules
109do
110 mysql -u ${SQL_USER} --password=${SQL_PW} -e"ALTER TABLE \`$SQL_DB\`.\`$SQL_TABLE\` ADD $rules BOOL NOT NULL DEFAULT 0"
111 Q2="$Q2, \`$rules\`"
112done <${XSRTEMP}
113
114
115#Now build a string that will basically have a ,'1' for each rule that was hit. The logic here
116#is that if the rule was listed here, it was hit, so the value will be '1'. Since I don't
117#update the values of *ALL* columns--just the ones that were actually hit, they all get a '1'
118#So, like Q2 above, this will build a string for the QUERY statement below that will look like
119#this (starting with the first comma)
120# , '1', '1'
121for (( i=0; i<$RULECOUNT; i++))
122do
123 RULESTRING="${RULESTRING}, '1'"
124done
125
126#Calculate the values of WHITELISTED and BLACKLISTED
127if [ "$SENDER_IN_WHITELIST" = Yes ]
128then
129 WHITELISTED=1
130else
131 WHITELISTED=0
132fi
133
134if [ "$SENDER_IN_BLACKLIST" = Yes ]
135then
136 BLACKLISTED=1
137else
138 BLACKLISTED=0
139fi
140
141
142#determine if the message was an approved commercial email, or from a mailing list
143#If there are more than two, a case statement would be more appropriate
144if [ "$DEST" = Lists ]
145then
146 MAILING_LIST=1
147else
148 MAILING_LIST=0
149fi
150
151if [ "$DEST" = Commercial ]
152then
153 COMMERCIAL=1
154else
155 COMMERCIAL=0
156fi
157
158
159#Now I have all of the variables. I can build my QUERY.
160#A typical query will look like this:
161# INSERT INTO `peloruso_mailstats`.`messages` (`sha1sum`, `username`, `orig_class`, `score`,
162# `mail_list`, `commercial`, `whitelist`, `blacklist`, `AWL`, `BAYES_20`, `NORMAL_HTTP_TO_IP`,
163# `ONLINE_PHARMACY`, `SPF_PASS`, `TVD_VISIT_PHARMA`, `URIBL_JP_SURBL`, `URIBL_OB_SURBL`, `URICOUNTRY_DE`)
164# VALUES ('f3e4406dab1313c9586bcfcf01ec8332582a071a', 'skip', 'SPAM', '10.0', '0', '0', '0' , '0' ,
165# '1', '1', '1', '1', '1', '1', '1', '1', '1')
166QUERY="INSERT INTO \`${SQL_DB}\`.\`${SQL_TABLE}\` (\`sha1sum\`, \`username\`, \`orig_class\`, \`score\`, \`mail_list\`, \`commercial\`, \`whitelist\`, \`blacklist\`$Q2) \
167 VALUES ('${SHA1SUM}', '${PMUSER}', '${ORIG_CLASS}', '${SCORE}', '${MAILING_LIST}', '${COMMERCIAL}', '${WHITELISTED}' , '${BLACKLISTED}' ${RULESTRING})"
168
169#I do save a copy of the query for debugging
170echo "QUERY=${QUERY}">>/home/peloruso/mailstat.log
171
172#execute the query
173mysql -u ${SQL_USER} --password=${SQL_PW} -e "${QUERY}"
174
175#delete the temp files
176rm -f ${TEMPFILE} ${XSRTEMP}