quarantine_notify.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/python
  2. import smtplib
  3. import os
  4. import mysql.connector
  5. from email.MIMEMultipart import MIMEMultipart
  6. from email.MIMEText import MIMEText
  7. from email.Utils import COMMASPACE, formatdate
  8. import cgi
  9. import jinja2
  10. from jinja2 import Template
  11. import json
  12. import redis
  13. import time
  14. import html2text
  15. while True:
  16. try:
  17. r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0)
  18. r.ping()
  19. except Exception as ex:
  20. print '%s - trying again...' % (ex)
  21. time.sleep(3)
  22. else:
  23. break
  24. time_now = int(time.time())
  25. def query_mysql(query, headers = True, update = False):
  26. while True:
  27. try:
  28. cnx = mysql.connector.connect(unix_socket = '/var/run/mysqld/mysqld.sock', user='__DBUSER__', passwd='__DBPASS__', database='__DBNAME__', charset="utf8")
  29. except Exception as ex:
  30. print '%s - trying again...' % (ex)
  31. time.sleep(3)
  32. else:
  33. break
  34. cur = cnx.cursor()
  35. cur.execute(query)
  36. if not update:
  37. result = []
  38. columns = tuple( [d[0].decode('utf8') for d in cur.description] )
  39. for row in cur:
  40. if headers:
  41. result.append(dict(zip(columns, row)))
  42. else:
  43. result.append(row)
  44. cur.close()
  45. cnx.close()
  46. return result
  47. else:
  48. cnx.commit()
  49. cur.close()
  50. cnx.close()
  51. def notify_rcpt(rcpt, msg_count):
  52. meta_query = query_mysql('SELECT id, subject, sender, created FROM quarantine WHERE notified = 0 AND rcpt = "%s"' % (rcpt))
  53. if r.get('Q_HTML'):
  54. try:
  55. template = Template(r.get('Q_HTML'))
  56. except:
  57. print "Error: Cannot parse quarantine template, falling back to default template."
  58. with open('/templates/quarantine.tpl') as file_:
  59. template = Template(file_.read())
  60. else:
  61. with open('/templates/quarantine.tpl') as file_:
  62. template = Template(file_.read())
  63. html = template.render(meta=meta_query, counter=msg_count)
  64. text = html2text.html2text(html)
  65. count = 0
  66. while count < 15:
  67. try:
  68. server = smtplib.SMTP('postfix', 590, 'quarantine')
  69. server.ehlo()
  70. msg = MIMEMultipart('alternative')
  71. msg['From'] = r.get('Q_SENDER') or "quarantine@localhost"
  72. msg['Subject'] = r.get('Q_SUBJ') or "Spam Quarantine Notification"
  73. msg['Date'] = formatdate(localtime = True)
  74. text_part = MIMEText(text, 'plain', 'utf-8')
  75. html_part = MIMEText(html, 'html', 'utf-8')
  76. msg.attach(text_part)
  77. msg.attach(html_part)
  78. msg['To'] = str(rcpt)
  79. text = msg.as_string()
  80. server.sendmail(msg['From'], msg['To'], text)
  81. server.quit()
  82. for res in meta_query:
  83. query_mysql('UPDATE quarantine SET notified = 1 WHERE id = "%d"' % (res['id']), update = True)
  84. r.hset('Q_LAST_NOTIFIED', record['rcpt'], time_now)
  85. break
  86. except Exception as ex:
  87. print '%s' % (ex)
  88. time.sleep(3)
  89. records = query_mysql('SELECT count(id) AS counter, rcpt FROM quarantine WHERE notified = 0 GROUP BY rcpt')
  90. for record in records:
  91. attrs = ''
  92. attrs_json = ''
  93. try:
  94. last_notification = int(r.hget('Q_LAST_NOTIFIED', record['rcpt']))
  95. if last_notification > time_now:
  96. print 'Last notification is > time now, assuming never'
  97. last_notification = 0
  98. except Exception as ex:
  99. print 'Could not determine last notification for %s, assuming never' % (record['rcpt'])
  100. last_notification = 0
  101. attrs_json = query_mysql('SELECT attributes FROM mailbox WHERE username = "%s"' % (record['rcpt']))
  102. attrs = json.loads(str(attrs_json[0]['attributes']))
  103. if attrs['quarantine_notification'] not in ('hourly', 'daily', 'weekly', 'never'):
  104. print 'Abnormal quarantine_notification value'
  105. continue
  106. if attrs['quarantine_notification'] == 'hourly':
  107. if last_notification == 0 or (last_notification + 3600) < time_now:
  108. print "Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter'])
  109. notify_rcpt(record['rcpt'], record['counter'])
  110. elif attrs['quarantine_notification'] == 'daily':
  111. if last_notification == 0 or (last_notification + 86400) < time_now:
  112. print "Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter'])
  113. notify_rcpt(record['rcpt'], record['counter'])
  114. elif attrs['quarantine_notification'] == 'weekly':
  115. if last_notification == 0 or (last_notification + 604800) < time_now:
  116. print "Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter'])
  117. notify_rcpt(record['rcpt'], record['counter'])
  118. else:
  119. break