You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.7 KiB

  1. # Copyright 2017-18 Eficent Business and IT Consulting Services S.L.
  2. # (www.eficent.com)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import email
  5. import xmlrpc.client as xmlrpclib
  6. import logging
  7. from odoo import api, models
  8. from odoo.tools import pycompat
  9. _logger = logging.getLogger(__name__)
  10. class MailThread(models.AbstractModel):
  11. _inherit = 'mail.thread'
  12. @api.model
  13. def message_process(self, model, message, custom_values=None,
  14. save_original=False, strip_attachments=False,
  15. thread_id=None):
  16. if isinstance(message, xmlrpclib.Binary):
  17. message = bytes(message.data)
  18. # message_from_string parses from a *native string*, except
  19. # apparently sometimes message is ISO-8859-1 binary data or some
  20. # shit and the straightforward version (pycompat.to_native) won't
  21. # work right -> always encode message to bytes then use the
  22. # relevant method depending on ~python version
  23. if isinstance(message, pycompat.text_type):
  24. message = message.encode('utf-8')
  25. extract = getattr(email, 'message_from_bytes',
  26. email.message_from_string)
  27. msg_txt = extract(message)
  28. msg = self.message_parse(msg_txt)
  29. _logger.info(
  30. 'Fetched mail from %s to %s with Message-Id %s',
  31. msg.get('from'), msg.get('to'), msg.get('message_id'))
  32. return super(MailThread, self).message_process(
  33. model, message, custom_values=custom_values,
  34. save_original=save_original,
  35. strip_attachments=strip_attachments, thread_id=thread_id)