So, my goal is to create some web pages where users of a mail server can check whether any mail sent to them got filtered before reaching their inboxes. Eventually it will give details like why it got filtered and give the opportunity to download the message. Right now it just creates two basic html files that list all senders who got filtered for each recipient, and a list of all recipients for each sender.
Here's a rough outline on how it works:
I use awk to search the log file for filtered mails and return a string with the from and to addresses for each (I plan to extract more info in the future, like quarantine file, date, mail ID etc)
then I split the string in the getaddrs function which strips off any junk from the log file and returns a nested list of from and to addresses.
next I convert these into two dictionaries using either the sender or recipient address as the key.
finally these are passed to the buildHTML**** functions which create the two static HTML pages.
Besides the extra functionality I mentioned I'm also thinking of a complete rewrite to use OO code rather than funtions (hey I learned to program before OO came along and it's still what comes most naturally to me on small projects ) and perhaps using a DB rather than static HTML pages.
Any suggestions for improving what I've done, I'm sure there must be better ways of doing some (probably most) of this.
Here's the code:
Code: Select all
import os
def getaddrs(data):
#split the from and to addresses
addresses = data.split(" ")
faddr = str(addresses[0])
taddr = str(addresses[1])
#4 types of junk to remove: from=<>, to=<>, <> (both from and to)
if faddr.startswith("from=<",0):
faddr = faddr.lstrip("from=<").rstrip(">")
if faddr.startswith("<",0):
faddr = faddr.lstrip("<").rstrip(">")
if taddr.startswith("to=<",0):
taddr = taddr.lstrip("to=<").rstrip(">\n")
if taddr.startswith("<",0):
taddr = taddr.lstrip("<").rstrip(">,\n")
#split taddr into a list of to addresses (since a single mail can have
#multiple recipents)
taddrs = taddr.split(",")
#remove the junk from these to addresses and create a list for all recipients
to = []
for i in taddrs:
j = i.lstrip("<").rstrip(">")
to.append(j)
return [faddr, to]
def buildHTMLrecipients(recipients):
f = open("recipients.html", "w")
f.write("<table border=1><th>Recipients</th><th>Sender</th>\n")
for r, s in recipients.iteritems():
line = "<tr><td>"+ r + "</td><td>"+ str(s) + "</td></tr>\n"
f.write(line)
f.write("</table>")
f.close()
return 0
def buildHTMLsenders(senders):
f = open("senders.html", "w")
f.write("<table border=1><th>Sender</th><th>Recipients</th>\n")
for s, r in senders.iteritems():
recipientString = str(r)
line = "<tr><td>"+ s + "</td><td>"+ recipientString + "</td></tr>\n"
f.write(line)
f.write("</table>")
f.close()
return 0
#postfix log file
logfile = "mail.log"
#commands must return a string in the format:
#FROM_ADDRESS TO_ADDRESSES
cmd1 = "awk '/spamcop/ {print $25, $26}' "+logfile
cmd2 = "awk '/Blocked SPAM/ {print $11, $13}' "+logfile
#Run the commands and build a list of all mails
mails = []
loglines=os.popen(cmd1)
for line in loglines:
mails.append(getaddrs(str(line)))
loglines=os.popen(cmd2)
for line in loglines:
mails.append(getaddrs(str(line)))
#build sender dictionary of recipients (IE a list of all recipients for a given sender)
senders = {}
for mail in mails:
#if the sender is already in the dictionary then add this mail's recipients
#to the senders existing list
fromAddr = str(mail[0])
# we're iterating through mail[1] since it's a list
for toAddresses in mail[1]:
if fromAddr in senders:
senders[fromAddr].append(toAddresses)
else:
senders[fromAddr] = [toAddresses]
#build recipient dictionary of senders (IE a list of all senders for a given recipient)
recipients = {}
for mail in mails:
#if the recipient is already in the dictonary then add this mail's sender
#to the recipient's existing list
#NB we must iterate through mail[1] since it is a list
for toAddresses in mail[1]:
toAddr = str(toAddresses)
if toAddr in recipients:
recipients[toAddr].append(mail[0])
else:
recipients[toAddr] = [mail[0]]
buildHTMLrecipients(recipients)
buildHTMLsenders(senders)