commit
f9e7c6cf6a
|
@ -71,10 +71,11 @@ RUN groupadd -g 5000 vmail \
|
||||||
libwww-perl \
|
libwww-perl \
|
||||||
mysql-client \
|
mysql-client \
|
||||||
procps \
|
procps \
|
||||||
python-html2text \
|
python3 \
|
||||||
python-jinja2 \
|
python3-html2text \
|
||||||
python-mysql.connector \
|
python3-jinja2 \
|
||||||
python-redis \
|
python3-mysql.connector \
|
||||||
|
python3-redis \
|
||||||
redis-server \
|
redis-server \
|
||||||
supervisor \
|
supervisor \
|
||||||
syslog-ng \
|
syslog-ng \
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import smtplib
|
import smtplib
|
||||||
import os
|
import os
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
from email.MIMEMultipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.MIMEText import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.Utils import COMMASPACE, formatdate
|
from email.utils import COMMASPACE, formatdate
|
||||||
import cgi
|
import cgi
|
||||||
import jinja2
|
import jinja2
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
|
@ -20,7 +20,7 @@ while True:
|
||||||
r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0)
|
r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0)
|
||||||
r.ping()
|
r.ping()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print '%s - trying again...' % (ex)
|
print('%s - trying again...' % (ex))
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
@ -32,7 +32,7 @@ def query_mysql(query, headers = True, update = False):
|
||||||
try:
|
try:
|
||||||
cnx = mysql.connector.connect(unix_socket = '/var/run/mysqld/mysqld.sock', user='__DBUSER__', passwd='__DBPASS__', database='__DBNAME__', charset="utf8")
|
cnx = mysql.connector.connect(unix_socket = '/var/run/mysqld/mysqld.sock', user='__DBUSER__', passwd='__DBPASS__', database='__DBNAME__', charset="utf8")
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print '%s - trying again...' % (ex)
|
print('%s - trying again...' % (ex))
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
@ -40,10 +40,10 @@ def query_mysql(query, headers = True, update = False):
|
||||||
cur.execute(query)
|
cur.execute(query)
|
||||||
if not update:
|
if not update:
|
||||||
result = []
|
result = []
|
||||||
columns = tuple( [d[0].decode('utf8') for d in cur.description] )
|
columns = tuple( [d[0] for d in cur.description] )
|
||||||
for row in cur:
|
for row in cur:
|
||||||
if headers:
|
if headers:
|
||||||
result.append(dict(zip(columns, row)))
|
result.append(dict(list(zip(columns, row))))
|
||||||
else:
|
else:
|
||||||
result.append(row)
|
result.append(row)
|
||||||
cur.close()
|
cur.close()
|
||||||
|
@ -60,7 +60,7 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl):
|
||||||
try:
|
try:
|
||||||
template = Template(r.get('Q_HTML'))
|
template = Template(r.get('Q_HTML'))
|
||||||
except:
|
except:
|
||||||
print "Error: Cannot parse quarantine template, falling back to default template."
|
print("Error: Cannot parse quarantine template, falling back to default template.")
|
||||||
with open('/templates/quarantine.tpl') as file_:
|
with open('/templates/quarantine.tpl') as file_:
|
||||||
template = Template(file_.read())
|
template = Template(file_.read())
|
||||||
else:
|
else:
|
||||||
|
@ -74,7 +74,9 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl):
|
||||||
server = smtplib.SMTP('postfix', 590, 'quarantine')
|
server = smtplib.SMTP('postfix', 590, 'quarantine')
|
||||||
server.ehlo()
|
server.ehlo()
|
||||||
msg = MIMEMultipart('alternative')
|
msg = MIMEMultipart('alternative')
|
||||||
msg['From'] = r.get('Q_SENDER') or "quarantine@localhost"
|
msg_from = r.get('Q_SENDER') or "quarantine@localhost"
|
||||||
|
# Remove non-ascii chars from field
|
||||||
|
msg['From'] = ''.join([i if ord(i) < 128 else '' for i in msg_from])
|
||||||
msg['Subject'] = r.get('Q_SUBJ') or "Spam Quarantine Notification"
|
msg['Subject'] = r.get('Q_SUBJ') or "Spam Quarantine Notification"
|
||||||
msg['Date'] = formatdate(localtime = True)
|
msg['Date'] = formatdate(localtime = True)
|
||||||
text_part = MIMEText(text, 'plain', 'utf-8')
|
text_part = MIMEText(text, 'plain', 'utf-8')
|
||||||
|
@ -83,7 +85,7 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl):
|
||||||
msg.attach(html_part)
|
msg.attach(html_part)
|
||||||
msg['To'] = str(rcpt)
|
msg['To'] = str(rcpt)
|
||||||
text = msg.as_string()
|
text = msg.as_string()
|
||||||
server.sendmail(msg['From'].encode("ascii", errors="ignore"), msg['To'], text)
|
server.sendmail(msg['From'], msg['To'], text)
|
||||||
server.quit()
|
server.quit()
|
||||||
for res in meta_query:
|
for res in meta_query:
|
||||||
query_mysql('UPDATE quarantine SET notified = 1 WHERE id = "%d"' % (res['id']), update = True)
|
query_mysql('UPDATE quarantine SET notified = 1 WHERE id = "%d"' % (res['id']), update = True)
|
||||||
|
@ -91,7 +93,7 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl):
|
||||||
break
|
break
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
server.quit()
|
server.quit()
|
||||||
print '%s' % (ex)
|
print('%s' % (ex))
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
|
||||||
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')
|
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')
|
||||||
|
@ -102,25 +104,25 @@ for record in records:
|
||||||
try:
|
try:
|
||||||
last_notification = int(r.hget('Q_LAST_NOTIFIED', record['rcpt']))
|
last_notification = int(r.hget('Q_LAST_NOTIFIED', record['rcpt']))
|
||||||
if last_notification > time_now:
|
if last_notification > time_now:
|
||||||
print 'Last notification is > time now, assuming never'
|
print('Last notification is > time now, assuming never')
|
||||||
last_notification = 0
|
last_notification = 0
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print 'Could not determine last notification for %s, assuming never' % (record['rcpt'])
|
print('Could not determine last notification for %s, assuming never' % (record['rcpt']))
|
||||||
last_notification = 0
|
last_notification = 0
|
||||||
attrs_json = query_mysql('SELECT attributes FROM mailbox WHERE username = "%s"' % (record['rcpt']))
|
attrs_json = query_mysql('SELECT attributes FROM mailbox WHERE username = "%s"' % (record['rcpt']))
|
||||||
attrs = json.loads(str(attrs_json[0]['attributes']))
|
attrs = json.loads(str(attrs_json[0]['attributes'].decode('utf-8')))
|
||||||
if attrs['quarantine_notification'] not in ('hourly', 'daily', 'weekly', 'never'):
|
if attrs['quarantine_notification'] not in ('hourly', 'daily', 'weekly', 'never'):
|
||||||
print 'Abnormal quarantine_notification value'
|
print('Abnormal quarantine_notification value')
|
||||||
continue
|
continue
|
||||||
if attrs['quarantine_notification'] == 'hourly':
|
if attrs['quarantine_notification'] == 'hourly':
|
||||||
if last_notification == 0 or (last_notification + 3600) < time_now:
|
if last_notification == 0 or (last_notification + 3600) < time_now:
|
||||||
print "Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter'])
|
print("Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter']))
|
||||||
notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
|
notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
|
||||||
elif attrs['quarantine_notification'] == 'daily':
|
elif attrs['quarantine_notification'] == 'daily':
|
||||||
if last_notification == 0 or (last_notification + 86400) < time_now:
|
if last_notification == 0 or (last_notification + 86400) < time_now:
|
||||||
print "Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter'])
|
print("Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter']))
|
||||||
notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
|
notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
|
||||||
elif attrs['quarantine_notification'] == 'weekly':
|
elif attrs['quarantine_notification'] == 'weekly':
|
||||||
if last_notification == 0 or (last_notification + 604800) < time_now:
|
if last_notification == 0 or (last_notification + 604800) < time_now:
|
||||||
print "Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter'])
|
print("Notifying %s about %d new items in quarantine" % (record['rcpt'], record['counter']))
|
||||||
notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
|
notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl'])
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import smtplib
|
import smtplib
|
||||||
import os
|
import os
|
||||||
from email.MIMEMultipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.MIMEText import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.Utils import COMMASPACE, formatdate
|
from email.utils import COMMASPACE, formatdate
|
||||||
import jinja2
|
import jinja2
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
import redis
|
import redis
|
||||||
|
@ -17,7 +17,7 @@ if len(sys.argv) > 2:
|
||||||
percent = int(sys.argv[1])
|
percent = int(sys.argv[1])
|
||||||
username = str(sys.argv[2])
|
username = str(sys.argv[2])
|
||||||
else:
|
else:
|
||||||
print "Args missing"
|
print("Args missing")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
@ -25,7 +25,7 @@ while True:
|
||||||
r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0)
|
r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0)
|
||||||
r.ping()
|
r.ping()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print '%s - trying again...' % (ex)
|
print('%s - trying again...' % (ex))
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
@ -34,7 +34,7 @@ if r.get('QW_HTML'):
|
||||||
try:
|
try:
|
||||||
template = Template(r.get('QW_HTML'))
|
template = Template(r.get('QW_HTML'))
|
||||||
except:
|
except:
|
||||||
print "Error: Cannot parse quarantine template, falling back to default template."
|
print("Error: Cannot parse quarantine template, falling back to default template.")
|
||||||
with open('/templates/quota.tpl') as file_:
|
with open('/templates/quota.tpl') as file_:
|
||||||
template = Template(file_.read())
|
template = Template(file_.read())
|
||||||
else:
|
else:
|
||||||
|
@ -55,10 +55,10 @@ try:
|
||||||
msg.attach(html_part)
|
msg.attach(html_part)
|
||||||
msg['To'] = username
|
msg['To'] = username
|
||||||
p = Popen(['/usr/lib/dovecot/dovecot-lda', '-d', username, '-o', '"plugin/quota=maildir:User quota:noenforcing"'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
|
p = Popen(['/usr/lib/dovecot/dovecot-lda', '-d', username, '-o', '"plugin/quota=maildir:User quota:noenforcing"'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
|
||||||
p.communicate(input=msg.as_string())
|
p.communicate(input=bytes(msg.as_string(), 'utf-8'))
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print 'Failed to send quota notification: %s' % (ex)
|
print('Failed to send quota notification: %s' % (ex))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in New Issue