From 5bb82865466f650500920a477dd0aed30e80744f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: May 04 2020 18:32:24 +0000 Subject: Fix getting the milter running with python3 Turns out the file descriptor we're recieving is in bytes while email_from_file expects a unicode stream in python3. So if we run the code using python3, use email_from_binary_file and it's is python2, keep using the email_from_file. We also need to encode all strings to bytes before it's passed on to hashlib. Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure-milters/comment_email_milter.py b/pagure-milters/comment_email_milter.py index 54a2b6f..4085bd2 100644 --- a/pagure-milters/comment_email_milter.py +++ b/pagure-milters/comment_email_milter.py @@ -18,6 +18,7 @@ from multiprocessing import Process as Thread, Queue import Milter import requests +import six from Milter.utils import parse_addr @@ -130,7 +131,10 @@ class PagureMilter(Milter.Base): def eom(self): """ End of Message """ self.fp.seek(0) - msg = email.message_from_file(self.fp) + if six.PY3: + msg = email.message_from_binary_file(self.fp) + else: + msg = email.message_from_file(self.fp) msg_id = msg.get("In-Reply-To", None) if msg_id is None: @@ -172,7 +176,14 @@ class PagureMilter(Milter.Base): hashes = [] for email_obj in user.emails: - m = hashlib.sha512("%s%s%s" % (msg_id, salt, email_obj.email)) + m = hashlib.sha512( + b"%s%s%s" + % ( + msg_id.encode("utf-8"), + salt.encode("utf-8"), + email_obj.email.encode("utf-8"), + ) + ) hashes.append(m.hexdigest()) tohash = email_address.split("@")[0].split("+")[-1]