quarantine_notify.py 4.9 KB

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