quota_notify.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python3
  2. import smtplib
  3. import os
  4. from email.mime.multipart import MIMEMultipart
  5. from email.mime.text import MIMEText
  6. from email.utils import COMMASPACE, formatdate
  7. import jinja2
  8. from jinja2 import Template
  9. import redis
  10. import time
  11. import json
  12. import sys
  13. import html2text
  14. from subprocess import Popen, PIPE, STDOUT
  15. if len(sys.argv) > 2:
  16. percent = int(sys.argv[1])
  17. username = str(sys.argv[2])
  18. else:
  19. print("Args missing")
  20. sys.exit(1)
  21. while True:
  22. try:
  23. r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0)
  24. r.ping()
  25. except Exception as ex:
  26. print('%s - trying again...' % (ex))
  27. time.sleep(3)
  28. else:
  29. break
  30. if r.get('QW_HTML'):
  31. try:
  32. template = Template(r.get('QW_HTML'))
  33. except:
  34. print("Error: Cannot parse quarantine template, falling back to default template.")
  35. with open('/templates/quota.tpl') as file_:
  36. template = Template(file_.read())
  37. else:
  38. with open('/templates/quota.tpl') as file_:
  39. template = Template(file_.read())
  40. html = template.render(username=username, percent=percent)
  41. text = html2text.html2text(html)
  42. try:
  43. msg = MIMEMultipart('alternative')
  44. msg['From'] = r.get('QW_SENDER') or "quota-warning@localhost"
  45. msg['Subject'] = r.get('QW_SUBJ') or "Quota warning"
  46. msg['Date'] = formatdate(localtime = True)
  47. text_part = MIMEText(text, 'plain', 'utf-8')
  48. html_part = MIMEText(html, 'html', 'utf-8')
  49. msg.attach(text_part)
  50. msg.attach(html_part)
  51. msg['To'] = username
  52. p = Popen(['/usr/libexec/dovecot/dovecot-lda', '-d', username, '-o', '"plugin/quota=maildir:User quota:noenforcing"'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
  53. p.communicate(input=bytes(msg.as_string(), 'utf-8'))
  54. domain = username.split("@")[-1]
  55. if domain and r.hget('QW_BCC', domain):
  56. bcc_data = json.loads(r.hget('QW_BCC', domain))
  57. bcc_rcpts = bcc_data['bcc_rcpts']
  58. if bcc_data['active'] == 1:
  59. for rcpt in bcc_rcpts:
  60. msg = MIMEMultipart('alternative')
  61. msg['From'] = username
  62. subject = r.get('QW_SUBJ') or "Quota warning"
  63. msg['Subject'] = subject + ' (' + username + ')'
  64. msg['Date'] = formatdate(localtime = True)
  65. text_part = MIMEText(text, 'plain', 'utf-8')
  66. html_part = MIMEText(html, 'html', 'utf-8')
  67. msg.attach(text_part)
  68. msg.attach(html_part)
  69. msg['To'] = rcpt
  70. server = smtplib.SMTP('postfix', 588, 'quotanotification')
  71. server.ehlo()
  72. server.sendmail(msg['From'], str(rcpt), msg.as_string())
  73. server.quit()
  74. except Exception as ex:
  75. print('Failed to send quota notification: %s' % (ex))
  76. sys.exit(1)
  77. try:
  78. sys.stdout.close()
  79. except:
  80. pass
  81. try:
  82. sys.stderr.close()
  83. except:
  84. pass