blob: 99be7cc68e64bb69e8a36da8aaee1421c1adb9a5 [file] [log] [blame]
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +01001#!/usr/bin/python3
2
3import smtplib
4import os
5from email.mime.multipart import MIMEMultipart
6from email.mime.text import MIMEText
7from email.utils import COMMASPACE, formatdate
8import jinja2
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +02009from jinja2.sandbox import SandboxedEnvironment
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010010import redis
11import time
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020012import json
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010013import sys
14import html2text
15from subprocess import Popen, PIPE, STDOUT
16
17if len(sys.argv) > 2:
18 percent = int(sys.argv[1])
19 username = str(sys.argv[2])
20else:
21 print("Args missing")
22 sys.exit(1)
23
24while True:
25 try:
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020026 r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0, username='quota_notify', password='')
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010027 r.ping()
28 except Exception as ex:
29 print('%s - trying again...' % (ex))
30 time.sleep(3)
31 else:
32 break
33
34if r.get('QW_HTML'):
35 try:
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020036 env = SandboxedEnvironment()
37 template = env.from_string(r.get('QW_HTML'))
38 except Exception:
39 print("Error: Cannot parse quota template, falling back to default template.")
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010040 with open('/templates/quota.tpl') as file_:
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020041 env = SandboxedEnvironment()
42 template = env.from_string(file_.read())
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010043else:
44 with open('/templates/quota.tpl') as file_:
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020045 env = SandboxedEnvironment()
46 template = env.from_string(file_.read())
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010047
Matthias Andreas Benkard97f12022026-10-23 08:46:40 +020048try:
49 html = template.render(username=username, percent=percent)
50except (jinja2.exceptions.SecurityError, jinja2.TemplateError) as ex:
51 print(f"SecurityError or TemplateError in template rendering: {ex}")
52 sys.exit(1)
53
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010054text = html2text.html2text(html)
55
56try:
57 msg = MIMEMultipart('alternative')
58 msg['From'] = r.get('QW_SENDER') or "quota-warning@localhost"
59 msg['Subject'] = r.get('QW_SUBJ') or "Quota warning"
60 msg['Date'] = formatdate(localtime = True)
61 text_part = MIMEText(text, 'plain', 'utf-8')
62 html_part = MIMEText(html, 'html', 'utf-8')
63 msg.attach(text_part)
64 msg.attach(html_part)
65 msg['To'] = username
Matthias Andreas Benkard9dabdab2024-03-06 13:12:09 +010066 p = Popen(['/usr/libexec/dovecot/dovecot-lda', '-d', username, '-o', '"plugin/quota=maildir:User quota:noenforcing"'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010067 p.communicate(input=bytes(msg.as_string(), 'utf-8'))
68
Matthias Andreas Benkard8f915942021-08-16 10:57:25 +020069 domain = username.split("@")[-1]
70 if domain and r.hget('QW_BCC', domain):
71 bcc_data = json.loads(r.hget('QW_BCC', domain))
72 bcc_rcpts = bcc_data['bcc_rcpts']
73 if bcc_data['active'] == 1:
74 for rcpt in bcc_rcpts:
75 msg = MIMEMultipart('alternative')
76 msg['From'] = username
77 subject = r.get('QW_SUBJ') or "Quota warning"
78 msg['Subject'] = subject + ' (' + username + ')'
79 msg['Date'] = formatdate(localtime = True)
80 text_part = MIMEText(text, 'plain', 'utf-8')
81 html_part = MIMEText(html, 'html', 'utf-8')
82 msg.attach(text_part)
83 msg.attach(html_part)
84 msg['To'] = rcpt
85 server = smtplib.SMTP('postfix', 588, 'quotanotification')
86 server.ehlo()
87 server.sendmail(msg['From'], str(rcpt), msg.as_string())
88 server.quit()
89
Matthias Andreas Benkard4edeb9f2021-01-02 15:32:21 +010090except Exception as ex:
91 print('Failed to send quota notification: %s' % (ex))
92 sys.exit(1)
93
94try:
95 sys.stdout.close()
96except:
97 pass
98
99try:
100 sys.stderr.close()
101except:
102 pass