comment_email_milter.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Milter calls methods of your class at milter events.
  4. # Return REJECT,TEMPFAIL,ACCEPT to short circuit processing for a message.
  5. # You can also add/del recipients, replacebody, add/del headers, etc.
  6. from __future__ import print_function, unicode_literals, absolute_import
  7. import base64
  8. import email
  9. import hashlib
  10. import os
  11. import sys
  12. import time
  13. from io import BytesIO
  14. from multiprocessing import Process as Thread, Queue
  15. import Milter
  16. import requests
  17. from Milter.utils import parse_addr
  18. import pagure.config
  19. import pagure.lib.model_base
  20. import pagure.lib.query
  21. if 'PAGURE_CONFIG' not in os.environ \
  22. and os.path.exists('/etc/pagure/pagure.cfg'):
  23. os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
  24. logq = Queue(maxsize=4)
  25. _config = pagure.config.reload_config()
  26. def get_email_body(emailobj):
  27. ''' Return the body of the email, preferably in text.
  28. '''
  29. def _get_body(emailobj):
  30. """ Return the first text/plain body found if the email is multipart
  31. or just the regular payload otherwise.
  32. """
  33. if emailobj.is_multipart():
  34. for payload in emailobj.get_payload():
  35. # If the message comes with a signature it can be that this
  36. # payload itself has multiple parts, so just return the
  37. # first one
  38. if payload.is_multipart():
  39. return _get_body(payload)
  40. body = payload.get_payload()
  41. if payload.get_content_type() == 'text/plain':
  42. return body
  43. else:
  44. return emailobj.get_payload()
  45. body = _get_body(emailobj)
  46. enc = emailobj['Content-Transfer-Encoding']
  47. if enc == 'base64':
  48. body = base64.decodestring(body)
  49. return body
  50. def clean_item(item):
  51. ''' For an item provided as <item> return the content, if there are no
  52. <> then return the string.
  53. '''
  54. if '<' in item:
  55. item = item.split('<')[1]
  56. if '>' in item:
  57. item = item.split('>')[0]
  58. return item
  59. class PagureMilter(Milter.Base):
  60. def __init__(self): # A new instance with each new connection.
  61. self.id = Milter.uniqueID() # Integer incremented with each call.
  62. self.fp = None
  63. def log(self, message):
  64. print(message)
  65. sys.stdout.flush()
  66. def envfrom(self, mailfrom, *str):
  67. self.log("mail from: %s - %s" % (mailfrom, str))
  68. self.fromparms = Milter.dictfromlist(str)
  69. # NOTE: self.fp is only an *internal* copy of message data. You
  70. # must use addheader, chgheader, replacebody to change the message
  71. # on the MTA.
  72. self.fp = BytesIO()
  73. self.canon_from = '@'.join(parse_addr(mailfrom))
  74. from_txt = 'From %s %s\n' % (self.canon_from, time.ctime())
  75. self.fp.write(from_txt.encode('utf-8'))
  76. return Milter.CONTINUE
  77. @Milter.noreply
  78. def header(self, name, hval):
  79. ''' Headers '''
  80. # add header to buffer
  81. header_txt = "%s: %s\n" % (name, hval)
  82. self.fp.write(header_txt.encode('utf-8'))
  83. return Milter.CONTINUE
  84. @Milter.noreply
  85. def eoh(self):
  86. ''' End of Headers '''
  87. self.fp.write(b"\n")
  88. return Milter.CONTINUE
  89. @Milter.noreply
  90. def body(self, chunk):
  91. ''' Body '''
  92. self.fp.write(chunk)
  93. return Milter.CONTINUE
  94. @Milter.noreply
  95. def envrcpt(self, to, *str):
  96. rcptinfo = to, Milter.dictfromlist(str)
  97. print(rcptinfo)
  98. return Milter.CONTINUE
  99. def eom(self):
  100. ''' End of Message '''
  101. self.fp.seek(0)
  102. msg = email.message_from_file(self.fp)
  103. msg_id = msg.get('In-Reply-To', None)
  104. if msg_id is None:
  105. self.log('No In-Reply-To, keep going')
  106. return Milter.CONTINUE
  107. # Ensure we don't get extra lines in the message-id
  108. msg_id = msg_id.split('\n')[0].strip()
  109. self.log('msg-ig %s' % msg_id)
  110. self.log('To %s' % msg['to'])
  111. self.log('Cc %s' % msg.get('cc'))
  112. self.log('From %s' % msg['From'])
  113. # Check the email was sent to the right address
  114. email_address = msg['to']
  115. if 'reply+' in msg.get('cc', ''):
  116. email_address = msg['cc']
  117. if 'reply+' not in email_address:
  118. self.log(
  119. 'No valid recipient email found in To/Cc: %s'
  120. % email_address)
  121. return Milter.CONTINUE
  122. # Ensure the user replied to his/her own notification, not that
  123. # they are trying to forge their ID into someone else's
  124. salt = _config.get('SALT_EMAIL')
  125. from_email = clean_item(msg['From'])
  126. session = pagure.lib.model_base.create_session(_config['DB_URL'])
  127. try:
  128. user = pagure.lib.query.get_user(session, from_email)
  129. except:
  130. self.log(
  131. "Could not find an user in the DB associated with %s" %
  132. from_email)
  133. session.remove()
  134. return Milter.CONTINUE
  135. hashes = []
  136. for email_obj in user.emails:
  137. m = hashlib.sha512('%s%s%s' % (msg_id, salt, email_obj.email))
  138. hashes.append(m.hexdigest())
  139. tohash = email_address.split('@')[0].split('+')[-1]
  140. if tohash not in hashes:
  141. self.log('hash list: %s' % hashes)
  142. self.log('tohash: %s' % tohash)
  143. self.log('Hash does not correspond to the destination')
  144. session.remove()
  145. return Milter.CONTINUE
  146. if msg['From'] and msg['From'] == _config.get('FROM_EMAIL'):
  147. self.log("Let's not process the email we send")
  148. session.remove()
  149. return Milter.CONTINUE
  150. msg_id = clean_item(msg_id)
  151. if msg_id and '-ticket-' in msg_id:
  152. self.log('Processing issue')
  153. session.remove()
  154. return self.handle_ticket_email(msg, msg_id)
  155. elif msg_id and '-pull-request-' in msg_id:
  156. self.log('Processing pull-request')
  157. session.remove()
  158. return self.handle_request_email(msg, msg_id)
  159. else:
  160. self.log('Not a pagure ticket or pull-request email, let it go')
  161. session.remove()
  162. return Milter.CONTINUE
  163. def handle_ticket_email(self, emailobj, msg_id):
  164. ''' Add the email as a comment on a ticket. '''
  165. uid = msg_id.split('-ticket-')[-1].split('@')[0]
  166. parent_id = None
  167. if '-' in uid:
  168. uid, parent_id = uid.rsplit('-', 1)
  169. if '/' in uid:
  170. uid = uid.split('/')[0]
  171. self.log('uid %s' % uid)
  172. self.log('parent_id %s' % parent_id)
  173. data = {
  174. 'objid': uid,
  175. 'comment': get_email_body(emailobj),
  176. 'useremail': clean_item(emailobj['From']),
  177. }
  178. url = _config.get('APP_URL')
  179. if url.endswith('/'):
  180. url = url[:-1]
  181. url = '%s/pv/ticket/comment/' % url
  182. self.log('Calling URL: %s' % url)
  183. req = requests.put(url, data=data)
  184. if req.status_code == 200:
  185. self.log('Comment added')
  186. return Milter.ACCEPT
  187. self.log('Could not add the comment to ticket to pagure')
  188. self.log(req.text)
  189. return Milter.CONTINUE
  190. def handle_request_email(self, emailobj, msg_id):
  191. ''' Add the email as a comment on a request. '''
  192. uid = msg_id.split('-pull-request-')[-1].split('@')[0]
  193. parent_id = None
  194. if '-' in uid:
  195. uid, parent_id = uid.rsplit('-', 1)
  196. if '/' in uid:
  197. uid = uid.split('/')[0]
  198. self.log('uid %s' % uid)
  199. self.log('parent_id %s' % parent_id)
  200. data = {
  201. 'objid': uid,
  202. 'comment': get_email_body(emailobj),
  203. 'useremail': clean_item(emailobj['From']),
  204. }
  205. url = _config.get('APP_URL')
  206. if url.endswith('/'):
  207. url = url[:-1]
  208. url = '%s/pv/pull-request/comment/' % url
  209. self.log('Calling URL: %s' % url)
  210. req = requests.put(url, data=data)
  211. if req.status_code == 200:
  212. self.log('Comment added on PR')
  213. return Milter.ACCEPT
  214. self.log('Could not add the comment to PR to pagure')
  215. self.log(req.text)
  216. return Milter.CONTINUE
  217. def background():
  218. while True:
  219. t = logq.get()
  220. if not t:
  221. break
  222. msg, id, ts = t
  223. print("%s [%d]" % (time.strftime(
  224. '%Y%b%d %H:%M:%S', time.localtime(ts)), id))
  225. # 2005Oct13 02:34:11 [1] msg1 msg2 msg3 ...
  226. for i in msg:
  227. print(i,)
  228. print
  229. def main():
  230. bt = Thread(target=background)
  231. bt.start()
  232. socketname = "/var/run/pagure/paguresock"
  233. timeout = 600
  234. # Register to have the Milter factory create instances of your class:
  235. Milter.factory = PagureMilter
  236. print("%s pagure milter startup" % time.strftime('%Y%b%d %H:%M:%S'))
  237. sys.stdout.flush()
  238. Milter.runmilter("paguremilter", socketname, timeout)
  239. logq.put(None)
  240. bt.join()
  241. print("%s pagure milter shutdown" % time.strftime('%Y%b%d %H:%M:%S'))
  242. if __name__ == "__main__":
  243. main()