#!/usr/bin/python -u
"""List email addresses in the database."""
import cgi
import cgitb
import email.Utils
import MySQLdb
import sys
import urllib
cgitb.enable(display=0, logdir="/tmp")
__author__= 'Faried Nawaz'
__version__ = '20060615'
# tables that are used internally, and not for storing addresses
removeTables = [ 'maillog', 'unsubscribed' ]
def getTables():
"""List tables in the mails database."""
try:
cnx = MySQLdb.connect(user='mailer', passwd='XXX', db='mails')
cur = cnx.cursor()
except:
return None
cur.execute("show tables")
t = [ row[0] for row in cur.fetchall() ]
cnx.commit()
cur.close()
cnx.close()
tables = []
for table in t:
if table not in removeTables:
tables.append(table)
return tables
def displayAddressInfo(table='addresses'):
"""Display addresses and logs."""
tables = getTables()
print '''Content-type: text/html
axfooblug.com: Addresses
axfooblug subscribers
Tables: '''
alist = []
for t in tables:
alist.append('%s' % (t, t, t))
print ' '.join(alist)
print '''
Current subscribers in table %s:
''' % (table)
cnx = MySQLdb.connect(user='mailer', passwd='XXX', db='mails')
cur = cnx.cursor()
cur.execute("""select * from %s order by id""" % (table))
rows = cur.fetchall()
for row in rows:
infolist = []
infolist.append('subscribed on %s' % row[4])
if row[2] == 0:
infolist.append('(unsubscribed)')
infolist.append('subscribe' % (table, urllib.quote(row[1])))
if row[3] == 1:
infolist.append('(address bouncing)')
infolist.append('delete' % (table, urllib.quote(row[1])))
print '%s %s
' % (row[1], ';'.join(infolist))
print '''
Unsubscribe log:
'''
cur.execute("""select * from unsubscribed where `table` = %s order by id""", (table,))
rows = cur.fetchall()
for row in rows:
print '%s unsubscribed on %s ' % (row[1], row[2])
print '''
'''
cnx.commit()
cur.close()
cnx.close()
def main():
"""Main branching logic."""
form = cgi.FieldStorage()
if form.has_key('table'):
displayAddressInfo(table=form['table'].value)
else:
displayAddressInfo()
sys.exit(0)
if __name__ == '__main__': main()
# eof