public
nobgit
read
NobMail
Based on mailcow: dockerized
Languages
Repository composition by tracked source files.
PHP
49%
JavaScript
35%
HTML
9%
CSS
4%
Shell
2%
Python
1%
Lua
0%
Perl
0%
Ruby
0%
SCSS
0%
Create file
Wiki Documentation
Clone
https://nobgit.com/orgs/nobgit/nobmail.git
ssh://[email protected]:2222/orgs/nobgit/nobmail.git
Trace
data/Dockerfiles/dovecot/quota_notify.py
Trace helps you understand code history line by line. See who changed each line, when it changed, and which commit introduced it.
Author
Date
Commit
Line
Code
1
#!/usr/bin/python3
3
import smtplib
4
import os
5
from email.mime.multipart import MIMEMultipart
6
from email.mime.text import MIMEText
7
from email.utils import COMMASPACE, formatdate
8
import jinja2
9
from jinja2.sandbox import SandboxedEnvironment
10
import redis
11
import time
12
import json
13
import sys
14
import html2text
15
from subprocess import Popen, PIPE, STDOUT
17
if len(sys.argv) > 2:
18
percent = int(sys.argv[1])
19
username = str(sys.argv[2])
20
else:
21
print("Args missing")
22
sys.exit(1)
24
while True:
25
try:
26
r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0, username='quota_notify', password='')
27
r.ping()
28
except Exception as ex:
29
print('%s - trying again...' % (ex))
30
time.sleep(3)
31
else:
32
break
34
if r.get('QW_HTML'):
35
try:
36
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.")
40
with open('/templates/quota.tpl') as file_:
41
env = SandboxedEnvironment()
42
template = env.from_string(file_.read())
43
else:
44
with open('/templates/quota.tpl') as file_:
45
env = SandboxedEnvironment()
46
template = env.from_string(file_.read())
48
try:
49
html = template.render(username=username, percent=percent)
50
except (jinja2.exceptions.SecurityError, jinja2.TemplateError) as ex:
51
print(f"SecurityError or TemplateError in template rendering: {ex}")
52
sys.exit(1)
54
text = html2text.html2text(html)
56
try:
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
66
p = Popen(['/usr/libexec/dovecot/dovecot-lda', '-d', username, '-o', '"plugin/quota=maildir:User quota:noenforcing"'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
67
p.communicate(input=bytes(msg.as_string(), 'utf-8'))
69
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()
90
except Exception as ex:
91
print('Failed to send quota notification: %s' % (ex))
92
sys.exit(1)
94
try:
95
sys.stdout.close()
96
except:
97
pass
99
try:
100
sys.stderr.close()
101
except:
102
pass