#!/usr/bin/env python2
"""
    Makes sure that the spamassassin localrules contain the rules below

    Copyright  Afrihost.com (PTY) Ltd 2004
    Original file by: Ed Pascoe <ed@pascoe.co.za>
    $Id:  $
"""
__author__  = "Ed Pascoe <ed@pascoe.co.za>"
__date__    = "$Date: $ "
__version__ = "$Revision: $"
__credits__ = "Afrihost.com -- The best webhosting. Ever."

rules="""
#--------------------------------------------------------------------
#Email white lists, because the spam assassin white list doesnt work
#--------------------------------------------------------------------
header WHITE_BDFM        From:addr =~ /\@bdfm.co.za/i
describe WHITE_BDFM      White list Financial Mail stuff
score WHITE_BDFM          -100

header WHITE_ISVAX        From:addr =~ /\@.*vax.co.za/i
describe WHITE_ISVAX      White list IS fax solutions
score WHITE_ISVAX          -100

header WHITE_AFRIHOST     From:addr =~ /\@afrihost/i
describe WHITE_AFRIHOST   White list all afrihost stuff
score WHITE_AFRIHOST      -100

#ALL_TRUSTED seems broken
score ALL_TRUSTED      0

#-----------------------------------------------
#Tweaked scores
score URIBL_AB_SURBL 3.0
score URIBL_OB_SURBL 3.0
score URIBL_PH_SURBL 3.0
score URIBL_SC_SURBL 3.0
score URIBL_WS_SURBL 3.0
score URIBL_SBL 3.0
#-----------------------------------------------
score RCVD_IN_DSBL         0
score RCVD_NUMERIC_HELO    1.68
score FORGED_YAHOO_RCVD    2.2
score MIME_HTML_ONLY_MULTI 2.0
#-----------------------------------------------


"""
rulesfile="/etc/mail/spamassassin/local.cf"
searchstring="#*!*!*!*!*!AFRIHOST AUTO INSERT!*!*!*!*!*"

import os.path, sys
import os
import fcntl
import md5

#fcntl.flock(self.srchandle,fcntl.LOCK_EX) #Get exclusive lock
#fcntl.flock(self.srchandle,fcntl.LOCK_SH)
#self.srchandle=open(self.filename,"r+")
#
lines=[]
try: sf = open(rulesfile,"r+")
except IOError:
    print "%s does not exist. Skipping spamassasin upgrade" % (rulesfile)
    sys.exit(0)
fcntl.flock(sf,fcntl.LOCK_SH) #Get a lock on the file, Remember we are going to wipe it

inautoinsert=0
oldfile=md5.new() #We use this to find out if the file has changed
for l in sf:
    oldfile.update(l)
    if inautoinsert: #No point in keeping stuff we are about to remove
        if l.find(searchstring)>=0: inautoinsert=0
        continue
    else:
        if l.find(searchstring) >=0: 
            inautoinsert=1
            continue
        lines.append(l)
oldfile=oldfile.hexdigest()        
#Upgrade to Exclusive lock and truncate file

outtext=""
outtext=outtext+searchstring+"\n"
outtext=outtext+"""# All rules inside this INSERT section are added automatically,
# they will be overwritten by /usr/local/afribin/spamassassin_fix

"""
outtext=outtext+rules+"\n"
outtext=outtext+searchstring+"\n"
outtext=outtext+"".join(lines)
newfile=md5.new(outtext).hexdigest()
if (newfile != oldfile): 
    print "Spamassassin config changed, Old:",oldfile ,"New:", newfile
    fcntl.flock(sf,fcntl.LOCK_EX) 
    sf.seek(0)
    sf.truncate(0)
    sf.write(outtext)
    sf.close()
    sys.exit(1)

sf.close()
sys.exit(0)

