#!/bin/sh # Post.Office Spam Sorting Solution # Copyright by Joe Savelberg -- jmsc@euregio.net # version 0.1 -- 2004-01-28 @ 02:00 GMT # Use at own risk. Moved messages are deleted from SpamFolder # # Install this script in your root home directory # chmod 755 to make it executable # chown root:wheel to allow it to read/write files # install a cron job to run this script every minute # example for /etc/crontab: # 0 * * * * root ~root/spamsort.sh > /dev/null 2>&1 # Show your support by donating something through PayPal # https://www.paypal.com/xclick/business=jmsc%40euregio.net&item_name=Post.Office+Tools&cn=Comments+or+Suggestions&tax=0¤cy_code=USD # General Options # sortspam=[yes|no] -- enable or disable the script # usespamassassin=[yes|no] -- instead of rewriting just the subject, you can process the message by SpamAssassin again to rewrite headers # deletefiles=[yes|no] -- delete original files in spamchecker directory after copying them to the user directory # debugon=[yes|no] -- writes more information to the log file to see what's going on. sortspam="yes" usespamassassin="no" deletefiles="no" debugon="yes" # Location and name of this script spamscript="/var/root/spamsort.sh" # Setting up the correct paths to logs and temporary files # Normally, you don't have to change these spamsortlog="/var/log/spamsort.log" tmpmessage="/tmp/satmp.txt" # Location of your Post.Office program directory folder (auto-detect): # programdir="/usr/local/post.office" programdir=`grep "ProgramDir" /etc/post.office.conf | cut -d "=" -f 2` # Filtering Options # spamaddress is the e-mail address of the local mailbox where you receive spam messages from SpamAssassin # if you don't copy spam messages to a local mailbox then this script won't work. spamaddress="spamchecker@domain.com" # List users that you'd like to include or exclude in the filtering. # You can use extended regular expressions. includeusers="user2@domain\.com|testuser" excludeusers="somebody" # Set the folder name where you'd like to sort the files into. # If your users access their mail by POP3 then you should set this to "/in" # If your users access their mail by IMAP4 then you can set this to anything # you'd like -- e.g. to automatically put the spam into a Junk/Spam folder # then you should set it to "/junk" or "/spam" junkfolder="/in" # Location of SpamAssassin -- only necessary when usespammassin="yes" spamassassin="/usr/bin/spamassassin" # ################################################################################ # # The real script starts here. Normally there is nothing to change below this line # # ################################################################################ echo "`date` [Start] spamsort" >> $spamsortlog # Setting start time of the execution startdate=`date "+%m%d%H%M.00"` if [ "$sortspam" != "yes" ] ; then echo "`date` [End] spamsort - set spamsort=yes to run the script" >> $spamsortlog exit fi spamcheck=`${programdir}/cmdutils/getuid ${spamaddress}` spamcheck=`${programdir}/cmdutils/getpopmbox ${spamcheck}` spamcheck="${spamcheck}/in/" #echo $spamcheck # Checking if the spamsort script is already running to prevent too many simultaneous executions grepproc=$(ps -ax | grep "sh $spamscript" | grep -v "grep sh $spamscript" | wc -l) if [ $grepproc -gt 2 ] then echo $grepproc echo "`date` [End] too many spamsort processes: ${grepproc}" >> $spamsortlog exit fi # Now getting all local mailboxes #only users with a valid local mailbox are added accountinfo=`${programdir}/cmdutils/listacct -s ":" -i POP-Address,SMTP-Address,Local-Delivery | grep ":Mailbox"` # Applying the include and exclude filters to the list # of available local accounts (exclude users egrep -i -v) if [ -n "${includeusers}" ] then accountinfo=`echo "${accountinfo}" | tr " " "\n" | egrep -i "${includeusers}"` if [ "$debugon" = "yes" ] ; then echo "`date` [Debug] Step 1 including accounts ${includeusers}" >> $spamsortlog fi fi if [ -n "${excludeusers}" ] then accountinfo=`echo "${accountinfo}" | tr " " "\n" | egrep -i -v "${excludeusers}"` if [ "$debugon" = "yes" ] ; then echo "`date` [Debug] Step 2 excluding accounts ${excludeusers}" >> $spamsortlog fi fi if [ "$debugon" = "yes" ] ; then echo "`date` [Debug] List of Accounts that will be used:" echo "`date` [Debug] ${accountinfo}" echo "`date` [Debug] List of Accounts that will be used:" >> $spamsortlog echo "`date` [Debug] ${accountinfo}" >> $spamsortlog fi deletelist="" for i in ${accountinfo} do uid=`echo ${i} | cut -d ":" -f 1` uiddir=`${programdir}/cmdutils/getuid ${uid}` uiddir=`${programdir}/cmdutils/getpopmbox ${uiddir}` userjunkfolder="${uiddir}${junkfolder}" if [ ! -d ${userjunkfolder} ] then mkdir -p ${userjunkfolder} chown "mta:mail" ${userjunkfolder} chmod 770 ${userjunkfolder} fi if [ "$debugon" = "yes" ] ; then echo "" echo "`date` [Debug] Processing: ${uiddir}" echo "`date` [Debug] Processing: ${uiddir}" >> $spamsortlog fi smtpid=`echo ${i} | cut -d ":" -f 2| tr "," "|"` messages=`egrep -l -i -r "for <${smtpid}>; " ${spamcheck}` for x in ${messages} do if [ "$debugon" = "yes" ] ; then echo "" echo "`date` [Debug] Working on: ${x}" echo "`date` [Debug] Working on: ${x}" >> $spamsortlog fi if [ "$usespamassassin" != "yes" ] ; then tmp=`grep "Subject: \[*** Spam ***\] " ${x}` if [ -z "${tmp}" ] then mytmp=`sed 's/Subject: /Subject: [*** Spam ***] /g' ${x}` echo -n "${mytmp}" > ${x} if [ "$debugon" = "yes" ] ; then echo "`date` [Debug] Copying ${x} to ${userjunkfolder}/" echo "`date` [Debug] Copying ${x} to ${userjunkfolder}/" >> $spamsortlog fi cp -p ${x} ${userjunkfolder}/ fi else if [ "$debugon" = "yes" ] ; then echo "`date` [Debug] Starting SpamAssassin for ${x}" echo "`date` [Debug] Starting SpamAssassin for ${x}" >> $spamsortlog fi ${spamassassin} -t < ${x} > ${tmpmessage} cat ${tmpmessage} > ${x} if [ "$debugon" = "yes" ] ; then echo "`date` [Debug] Copying ${x} to ${userjunkfolder}/" echo "`date` [Debug] Copying ${x} to ${userjunkfolder}/" >> $spamsortlog fi cp -p ${x} ${userjunkfolder}/ rm ${tmpmessage} fi done deletelist="${deletelist} ${messages}" done if [ "$deletefiles" = "yes" ] ; then if [ "$debugon" = "yes" ] ; then echo "`date` [Debug] Deleting message files from $spamcheck" echo "`date` [Debug] Deleting message files from $spamcheck" >> $spamsortlog fi for q in ${deletelist} do if [ -f "${q}" ] then echo "Deleting ${q}" rm ${q} fi done fi echo "`date` [End] spamsort - started at $startdate" >> $spamsortlog #end