From 10cfaab6953e7878875664704be9691bda956c3a Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 13 2017 15:17:28 +0000 Subject: [PATCH 1/2] Sometime due to pgp signature or alike part of emails are multiparts In these case we need a recursive loop to extract the body of the email. Fixes https://pagure.io/pagure/issue/2616 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure-milters/comment_email_milter.py b/pagure-milters/comment_email_milter.py index 8f27fcb..a60abf4 100644 --- a/pagure-milters/comment_email_milter.py +++ b/pagure-milters/comment_email_milter.py @@ -36,14 +36,25 @@ import pagure def get_email_body(emailobj): ''' Return the body of the email, preferably in text. ''' - body = None - if emailobj.is_multipart(): - for payload in emailobj.get_payload(): - body = payload.get_payload() - if payload.get_content_type() == 'text/plain': - break - else: - body = emailobj.get_payload() + def _get_body(emailobj): + """ Return the first text/plain body found if the email is multipart + or just the regular payload otherwise. + """ + if emailobj.is_multipart(): + for payload in emailobj.get_payload(): + # If the message comes with a signature it can be that this + # payload itself has multiple parts, so just return the + # first one + if payload.is_multipart(): + return _get_body(payload) + + body = payload.get_payload() + if payload.get_content_type() == 'text/plain': + return body + else: + return emailobj.get_payload() + + body = _get_body(emailobj) enc = emailobj['Content-Transfer-Encoding'] if enc == 'base64': From a22ec59a320fc5ecf3250b33c83bc5e8e6e9e5a7 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Nov 13 2017 15:17:28 +0000 Subject: [PATCH 2/2] Flake8 fixes to the milter Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure-milters/comment_email_milter.py b/pagure-milters/comment_email_milter.py index a60abf4..0be8a8d 100644 --- a/pagure-milters/comment_email_milter.py +++ b/pagure-milters/comment_email_milter.py @@ -10,11 +10,9 @@ import base64 import email import hashlib import os -import urlparse import StringIO import sys import time -from socket import AF_INET, AF_INET6 from multiprocessing import Process as Thread, Queue import Milter @@ -30,7 +28,7 @@ if 'PAGURE_CONFIG' not in os.environ \ os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg' -import pagure +import pagure # noqa def get_email_body(emailobj): @@ -144,7 +142,7 @@ class PagureMilter(Milter.Base): email_address = msg['to'] if 'reply+' in msg.get('cc', ''): email_address = msg['cc'] - if not 'reply+' in email_address: + if 'reply+' not in email_address: self.log( 'No valid recipient email found in To/Cc: %s' % email_address) @@ -159,7 +157,7 @@ class PagureMilter(Milter.Base): except: self.log( "Could not find an user in the DB associated with %s" % - from_email) + from_email) return Milter.CONTINUE hashes = [] @@ -190,10 +188,9 @@ class PagureMilter(Milter.Base): self.log('Not a pagure ticket or pull-request email, let it go') return Milter.CONTINUE - def handle_ticket_email(self, emailobj, msg_id): ''' Add the email as a comment on a ticket. ''' - uid = msg_id.split('-ticket-')[-1].split('@')[0] + uid = msg_id.split('-ticket-')[-1].split('@')[0] parent_id = None if '-' in uid: uid, parent_id = uid.rsplit('-', 1) @@ -224,7 +221,7 @@ class PagureMilter(Milter.Base): def handle_request_email(self, emailobj, msg_id): ''' Add the email as a comment on a request. ''' - uid = msg_id.split('-pull-request-')[-1].split('@')[0] + uid = msg_id.split('-pull-request-')[-1].split('@')[0] parent_id = None if '-' in uid: uid, parent_id = uid.rsplit('-', 1) @@ -257,11 +254,14 @@ class PagureMilter(Milter.Base): def background(): while True: t = logq.get() - if not t: break - msg,id,ts = t - print("%s [%d]" % (time.strftime('%Y%b%d %H:%M:%S',time.localtime(ts)),id),) + if not t: + break + msg, id, ts = t + print("%s [%d]" % (time.strftime( + '%Y%b%d %H:%M:%S', time.localtime(ts)), id)) # 2005Oct13 02:34:11 [1] msg1 msg2 msg3 ... - for i in msg: print(i,) + for i in msg: + print(i,) print