From cf2e8d654581f8f833b6b2854cbc0e662c32ac34 Mon Sep 17 00:00:00 2001 From: farnbacher Date: Thu, 15 Aug 2019 20:18:01 +0200 Subject: [PATCH 1/4] Update for usage with Python 3 - print with parantheses and .format - change import of email modules - change shebang --- data/Dockerfiles/dovecot/quota_notify.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/data/Dockerfiles/dovecot/quota_notify.py b/data/Dockerfiles/dovecot/quota_notify.py index 669adec2c..064f3e789 100755 --- a/data/Dockerfiles/dovecot/quota_notify.py +++ b/data/Dockerfiles/dovecot/quota_notify.py @@ -1,10 +1,10 @@ -#!/usr/bin/python +#!/usr/bin/python3 import smtplib import os -from email.MIMEMultipart import MIMEMultipart -from email.MIMEText import MIMEText -from email.Utils import COMMASPACE, formatdate +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.utils import COMMASPACE, formatdate import jinja2 from jinja2 import Template import redis @@ -17,7 +17,7 @@ if len(sys.argv) > 2: percent = int(sys.argv[1]) username = str(sys.argv[2]) else: - print "Args missing" + print("Args missing") sys.exit(1) while True: @@ -25,7 +25,7 @@ while True: r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0) r.ping() except Exception as ex: - print '%s - trying again...' % (ex) + print("{0} - trying again...".format(ex)) time.sleep(3) else: break @@ -34,7 +34,7 @@ if r.get('QW_HTML'): try: template = Template(r.get('QW_HTML')) 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_: template = Template(file_.read()) else: @@ -58,7 +58,7 @@ try: p.communicate(input=msg.as_string()) except Exception as ex: - print 'Failed to send quota notification: %s' % (ex) + print('Failed to send quota notification: {0}'.format(ex)) sys.exit(1) try: From c579a2c01e854ac6f93d25b1133c6cb00c3011b3 Mon Sep 17 00:00:00 2001 From: farnbacher Date: Thu, 15 Aug 2019 20:32:20 +0200 Subject: [PATCH 2/4] Update for usage with Python 3 - print with parantheses and .format - change shebang - change import of email modules --- data/Dockerfiles/dovecot/quarantine_notify.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/data/Dockerfiles/dovecot/quarantine_notify.py b/data/Dockerfiles/dovecot/quarantine_notify.py index b1af332a9..75b3c7105 100755 --- a/data/Dockerfiles/dovecot/quarantine_notify.py +++ b/data/Dockerfiles/dovecot/quarantine_notify.py @@ -1,11 +1,11 @@ -#!/usr/bin/python +#!/usr/bin/python3 import smtplib import os import mysql.connector -from email.MIMEMultipart import MIMEMultipart -from email.MIMEText import MIMEText -from email.Utils import COMMASPACE, formatdate +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.utils import COMMASPACE, formatdate import cgi import jinja2 from jinja2 import Template @@ -20,7 +20,7 @@ while True: r = redis.StrictRedis(host='redis', decode_responses=True, port=6379, db=0) r.ping() except Exception as ex: - print '%s - trying again...' % (ex) + print("{0} - trying again...".format(ex)) time.sleep(3) else: break @@ -32,7 +32,7 @@ def query_mysql(query, headers = True, update = False): try: cnx = mysql.connector.connect(unix_socket = '/var/run/mysqld/mysqld.sock', user='__DBUSER__', passwd='__DBPASS__', database='__DBNAME__', charset="utf8") except Exception as ex: - print '%s - trying again...' % (ex) + print("{0} - trying again...".format(ex)) time.sleep(3) else: break @@ -60,7 +60,7 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl): try: template = Template(r.get('Q_HTML')) 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_: template = Template(file_.read()) else: @@ -91,7 +91,7 @@ def notify_rcpt(rcpt, msg_count, quarantine_acl): break except Exception as ex: server.quit() - print '%s' % (ex) + print('{0}'.format(ex)) 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') @@ -102,25 +102,25 @@ for record in records: try: last_notification = int(r.hget('Q_LAST_NOTIFIED', record['rcpt'])) if last_notification > time_now: - print 'Last notification is > time now, assuming never' + print('Last notification is > time now, assuming never') last_notification = 0 except Exception as ex: - print 'Could not determine last notification for %s, assuming never' % (record['rcpt']) + print('Could not determine last notification for {0}, assuming never'.format(record['rcpt'])) last_notification = 0 attrs_json = query_mysql('SELECT attributes FROM mailbox WHERE username = "%s"' % (record['rcpt'])) attrs = json.loads(str(attrs_json[0]['attributes'])) if attrs['quarantine_notification'] not in ('hourly', 'daily', 'weekly', 'never'): - print 'Abnormal quarantine_notification value' + print('Abnormal quarantine_notification value') continue if attrs['quarantine_notification'] == 'hourly': 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 {0} about {1} new items in quarantine".format(record['rcpt'], record['counter'])) notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl']) elif attrs['quarantine_notification'] == 'daily': 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 {0} about {1} new items in quarantine".format(record['rcpt'], record['counter'])) notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl']) elif attrs['quarantine_notification'] == 'weekly': 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 {0} about {1} new items in quarantine".format(record['rcpt'], record['counter'])) notify_rcpt(record['rcpt'], record['counter'], record['quarantine_acl']) From a11946721878816907a95ca9ade6eb2bb6270077 Mon Sep 17 00:00:00 2001 From: farnbacher Date: Thu, 15 Aug 2019 20:36:32 +0200 Subject: [PATCH 3/4] Install Python 3 packages for ported quota_notify.py & quarantine_notify.py --- data/Dockerfiles/dovecot/Dockerfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data/Dockerfiles/dovecot/Dockerfile b/data/Dockerfiles/dovecot/Dockerfile index 8bc60d9f0..13f6f6f3e 100644 --- a/data/Dockerfiles/dovecot/Dockerfile +++ b/data/Dockerfiles/dovecot/Dockerfile @@ -71,10 +71,10 @@ RUN groupadd -g 5000 vmail \ libwww-perl \ mysql-client \ procps \ - python-html2text \ - python-jinja2 \ - python-mysql.connector \ - python-redis \ + python3-html2text \ + python3-jinja2 \ + python3-mysql-connector \ + python3-redis \ redis-server \ supervisor \ syslog-ng \ From 50c272dbf6f3601cf84ed39886353ec802f1641c Mon Sep 17 00:00:00 2001 From: farnbacher Date: Thu, 15 Aug 2019 20:58:23 +0200 Subject: [PATCH 4/4] fix wrong package name --- data/Dockerfiles/dovecot/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/Dockerfiles/dovecot/Dockerfile b/data/Dockerfiles/dovecot/Dockerfile index 13f6f6f3e..e0ed3491a 100644 --- a/data/Dockerfiles/dovecot/Dockerfile +++ b/data/Dockerfiles/dovecot/Dockerfile @@ -73,7 +73,7 @@ RUN groupadd -g 5000 vmail \ procps \ python3-html2text \ python3-jinja2 \ - python3-mysql-connector \ + python3-mysql.connector \ python3-redis \ redis-server \ supervisor \