[MERGE] merge with latest stable
[odoo/odoo.git] / addons / mail_gateway / mail_gateway.py
index 8c6e2a9..c4baca5 100644 (file)
@@ -25,10 +25,12 @@ import tools
 import binascii
 import email
 from email.header import decode_header
+from email.utils import parsedate
 import base64
 import re
 from tools.translate import _
 import logging
+import xmlrpclib
 
 _logger = logging.getLogger('mailgate')
 
@@ -40,24 +42,56 @@ class mailgate_thread(osv.osv):
     _description = 'Mailgateway Thread'
 
     _columns = {
-        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', domain=[('history', '=', True)]),
-        'log_ids': fields.one2many('mailgate.message', 'res_id', 'Logs', domain=[('history', '=', False)]),
+        'message_ids': fields.one2many('mailgate.message', 'res_id', 'Messages', readonly=True),
     }
 
+    def copy(self, cr, uid, id, default=None, context=None):
+        """
+        Overrides orm copy method.
+        @param self: the object pointer
+        @param cr: the current row, from the database cursor,
+        @param uid: the current user’s ID for security checks,
+        @param id: Id of mailgate thread
+        @param default: Dictionary of default values for copy.
+        @param context: A standard dictionary for contextual values
+        """
+        if default is None:
+            default = {}
+
+        default.update({
+            'message_ids': [],
+            'date_closed': False,
+            'date_open': False
+        })
+        return super(mailgate_thread, self).copy(cr, uid, id, default, context=context)
+
     def message_new(self, cr, uid, msg, context):
         raise Exception, _('Method is not implemented')
 
-    def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
+    def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context=None):
         raise Exception, _('Method is not implemented')
 
-    def emails_get(self, cr, uid, ids, context=None):
-        raise Exception, _('Method is not implemented')
+    def message_followers(self, cr, uid, ids, context=None):
+        """ Get a list of emails of the people following this thread
+        """
+        res = {}
+        if isinstance(ids, (str, int, long)):
+            ids = [long(ids)]
+        for thread in self.browse(cr, uid, ids, context=context):
+            l=[]
+            for message in thread.message_ids:
+                l.append((message.user_id and message.user_id.email) or '')
+                l.append(message.email_from or '')
+                l.append(message.email_cc or '')
+            res[thread.id] = l
+        return res
 
     def msg_send(self, cr, uid, id, *args, **argv):
         raise Exception, _('Method is not implemented')
 
-    def _history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
-                    email_from=False, message_id=False, references=None, attach=None, context=None):
+    def history(self, cr, uid, cases, keyword, history=False, subject=None, email=False, details=None, \
+                    email_from=False, message_id=False, references=None, attach=None, email_cc=None, \
+                    email_bcc=None, email_date=None, context=None):
         """
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
@@ -65,8 +99,12 @@ class mailgate_thread(osv.osv):
         @param cases: a browse record list
         @param keyword: Case action keyword e.g.: If case is closed "Close" keyword is used
         @param history: Value True/False, If True it makes entry in case History otherwise in Case Log
-        @param email: Email address if any
-        @param details: Details of case history if any 
+        @param email: Email-To / Recipient address
+        @param email_from: Email From / Sender address if any
+        @param email_cc: Comma-Separated list of Carbon Copy Emails To addresse if any
+        @param email_bcc: Comma-Separated list of Blind Carbon Copy Emails To addresses if any
+        @param email_date: Email Date string if different from now, in server Timezone
+        @param details: Description, Details of case history if any
         @param atach: Attachment sent in email
         @param context: A standard dictionary for contextual values"""
         if context is None:
@@ -74,6 +112,11 @@ class mailgate_thread(osv.osv):
         if attach is None:
             attach = []
 
+        if email_date:
+            edate = parsedate(email_date)
+            if edate is not None:
+                email_date = time.strftime('%Y-%m-%d %H:%M:%S', edate)
+
         # The mailgate sends the ids of the cases and not the object list
 
         if all(isinstance(case_id, (int, long)) for case_id in cases):
@@ -83,110 +126,212 @@ class mailgate_thread(osv.osv):
         obj = self.pool.get('mailgate.message')
 
         for case in cases:
+            attachments = []
+            for att in attach:
+                if isinstance(att,(int,long)):
+                    attachments.append(att)
+                elif isinstance(att,dict):
+                    domain = [
+                        ('name', '=', att[0]),
+                        ('res_id', '=', case.id),
+                        ('res_model', '=', case._name)
+                    ]
+                    att_ids = att_obj.search(cr, uid, domain, context=context)
+
+                    if att_ids:
+                        attachments.extend(att_ids)
+                    else:
+                        values = {
+                            'res_model' : case._name,
+                            'res_id' : case.id,
+                            'name' : att[0],
+                            'datas' : base64.encodestring(att[1])
+                        }
+                        attachment_id = att_obj.create(cr, uid, values, context=context)
+                        attachments.append(attachment_id)
+
+            partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
+            if not partner_id and case._name == 'res.partner':
+                partner_id = case.id
             data = {
-                'name': keyword, 
-                'user_id': uid, 
-                'model' : case._name, 
-                'res_id': case.id, 
-                'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
-                'message_id': message_id, 
+                'name': keyword,
+                'user_id': uid,
+                'model' : case._name,
+                'partner_id': partner_id,
+                'res_id': case.id,
+                'date': time.strftime('%Y-%m-%d %H:%M:%S'),
+                'message_id': message_id,
+                'description': details or (hasattr(case, 'description') and case.description or False),
+                'attachment_ids': [(6, 0, attachments)]
             }
-            attachments = []
-            if history:
-                for att in attach:
-                    attachments.append(att_obj.create(cr, uid, {'name': att[0], 'datas': base64.encodestring(att[1])}))
 
-                data = {
-                    'name': subject or 'History', 
-                    'history': True, 
-                    'user_id': uid, 
-                    'model' : case._name, 
-                    'res_id': case.id,
-                    'date': time.strftime('%Y-%m-%d %H:%M:%S'), 
-                    'description': details or (hasattr(case, 'description') and case.description or False), 
-                    'email_to': email or \
-                        (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
-                         case.user_id.address_id.email) or tools.config.get('email_from', False), 
+            if history:
+                for param in (email, email_cc, email_bcc):
+                    if isinstance(param, list):
+                        param = ", ".join(param)
+
+                data.update({
+                    'name': subject or _('History'),
+                    'history': True,
+                    'date': email_date or time.strftime('%Y-%m-%d %H:%M:%S'),
+                    'email_to': email,
                     'email_from': email_from or \
                         (hasattr(case, 'user_id') and case.user_id and case.user_id.address_id and \
-                         case.user_id.address_id.email) or tools.config.get('email_from', False), 
-                    'partner_id': hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False, 
-                    'references': references, 
-                    'message_id': message_id, 
-                    'attachment_ids': [(6, 0, attachments)]
-                }
-            res = obj.create(cr, uid, data, context)
+                         case.user_id.address_id.email),
+                    'email_cc': email_cc,
+                    'email_bcc': email_bcc,
+                    'references': references,
+                })
+            obj.create(cr, uid, data, context=context)
         return True
 mailgate_thread()
 
+def format_date_tz(date, tz=None):
+    if not date:
+        return 'n/a'
+    format = tools.DEFAULT_SERVER_DATETIME_FORMAT
+    return tools.server_to_local_timestamp(date, format, format, tz)
+
 class mailgate_message(osv.osv):
     '''
     Mailgateway Message
     '''
+    def open_document(self, cr, uid, ids, context=None):
+        """ To Open Document
+        @param self: The object pointer.
+        @param cr: A database cursor
+        @param uid: ID of the user currently logged in
+        @param ids: the ID of messages
+        @param context: A standard dictionary
+        """
+        action_data = False
+        if ids:
+            message_id = ids[0]
+            mailgate_data = self.browse(cr, uid, message_id, context=context)
+            model = mailgate_data.model
+            res_id = mailgate_data.res_id
+
+            action_pool = self.pool.get('ir.actions.act_window')
+            action_ids = action_pool.search(cr, uid, [('res_model', '=', model)])
+            if action_ids:
+                action_data = action_pool.read(cr, uid, action_ids[0], context=context)
+                action_data.update({
+                    'domain' : "[('id','=',%d)]"%(res_id),
+                    'nodestroy': True,
+                    'context': {}
+                    })
+        return action_data
+
+    def open_attachment(self, cr, uid, ids, context=None):
+        """ To Open attachments
+        @param self: The object pointer.
+        @param cr: A database cursor
+        @param uid: ID of the user currently logged in
+        @param ids: the ID of messages
+        @param context: A standard dictionary
+        """
+        action_data = False
+        action_pool = self.pool.get('ir.actions.act_window')
+        message_pool = self.browse(cr ,uid, ids, context=context)[0]
+        att_ids = [x.id for x in message_pool.attachment_ids] 
+        action_ids = action_pool.search(cr, uid, [('res_model', '=', 'ir.attachment')])
+        if action_ids:
+            action_data = action_pool.read(cr, uid, action_ids[0], context=context)
+            action_data.update({
+                'domain': [('id','in',att_ids)],
+                'nodestroy': True
+                })
+        return action_data
+
+    def truncate_data(self, cr, uid, data, context=None):
+        data_list = data and data.split('\n') or []
+        if len(data_list) > 3:
+            res = '\n\t'.join(data_list[:3]) + '...'
+        else:
+            res = '\n\t'.join(data_list)
+        return res
+
+    def _get_display_text(self, cr, uid, ids, name, arg, context=None):
+        if context is None:
+            context = {}
+        tz = context.get('tz')
+        result = {}
+        for message in self.browse(cr, uid, ids, context=context):
+            msg_txt = ''
+            msg_name = message.name
+            if message.history:
+                msg_txt += (message.email_from or '/') + _(' wrote on ') + format_date_tz(message.date, tz) + ':\n\t'
+                if message.description:
+                    msg_txt += self.truncate_data(cr, uid, message.description, context=context)
+            else:
+                msg_txt = (message.user_id.name or '/') + _(' on ') + format_date_tz(message.date, tz) + ':\n\t'
+                if msg_name == _('Opportunity'):
+                    msg_txt += _("Converted to Opportunity")
+                elif msg_name == _('Note'):
+                    msg_txt = (message.user_id.name or '/') + _(' added note on ') + format_date_tz(message.date, tz) + ':\n\t'
+                    msg_txt += self.truncate_data(cr, uid, message.description, context=context)
+                elif msg_name == _('Stage'):
+                    msg_txt += _("Changed Stage to: ") + message.description
+                elif msg_name:
+                    msg_txt += _("Changed Status to: ") + msg_name
+            result[message.id] = msg_txt
+        return result
+
     _name = 'mailgate.message'
     _description = 'Mailgateway Message'
-    _order = 'id desc'
+    _order = 'date desc'
     _columns = {
-        'name':fields.char('Subject', size=128), 
-        'model': fields.char('Object Name', size=128), 
-        'res_id': fields.integer('Resource ID'),
+        'name':fields.text('Subject', readonly=True),
+        'model': fields.char('Object Name', size=128, select=1, readonly=True),
+        'res_id': fields.integer('Resource ID', select=1, readonly=True),
         'ref_id': fields.char('Reference Id', size=256, readonly=True, help="Message Id in Email Server.", select=True),
-        'date': fields.datetime('Date'), 
-        'history': fields.boolean('Is History?'),
-        'user_id': fields.many2one('res.users', 'User Responsible', readonly=True), 
-        'message': fields.text('Description'), 
-        'email_from': fields.char('Email From', size=84), 
-        'email_to': fields.char('Email To', size=84), 
-        'email_cc': fields.char('Email CC', size=84), 
-        'email_bcc': fields.char('Email BCC', size=84), 
+        'date': fields.datetime('Date', readonly=True),
+        'history': fields.boolean('Is History?', readonly=True),
+        'user_id': fields.many2one('res.users', 'User Responsible', readonly=True),
+        'message': fields.text('Description', readonly=True),
+        'email_from': fields.char('From', size=128, help="Email From", readonly=True),
+        'email_to': fields.char('To', help="Email Recipients", size=256, readonly=True),
+        'email_cc': fields.char('Cc', help="Carbon Copy Email Recipients", size=256, readonly=True),
+        'email_bcc': fields.char('Bcc', help='Blind Carbon Copy Email Recipients', size=256, readonly=True),
         'message_id': fields.char('Message Id', size=1024, readonly=True, help="Message Id on Email.", select=True),
-        'references': fields.text('References', readonly=True, help="Referencess emails."),
-        'description': fields.text('Description'), 
-        'partner_id': fields.many2one('res.partner', 'Partner', required=False), 
-        'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments'), 
+        'references': fields.text('References', readonly=True, help="References emails."),
+        'description': fields.text('Description', readonly=True),
+        'partner_id': fields.many2one('res.partner', 'Partner', required=False),
+        'attachment_ids': fields.many2many('ir.attachment', 'message_attachment_rel', 'message_id', 'attachment_id', 'Attachments', readonly=True),
+        'display_text': fields.function(_get_display_text, method=True, type='text', size="512", string='Display Text'),
     }
 
+    def init(self, cr):
+        cr.execute("""SELECT indexname
+                      FROM pg_indexes
+                      WHERE indexname = 'mailgate_message_res_id_model_idx'""")
+        if not cr.fetchone():
+            cr.execute("""CREATE INDEX mailgate_message_res_id_model_idx
+                          ON mailgate_message (model, res_id)""")
+
 mailgate_message()
 
 class mailgate_tool(osv.osv_memory):
 
     _name = 'email.server.tools'
     _description = "Email Server Tools"
-    
-    def _to_decode(self, s, charsets):
-        if not s:
-            return s
-        for charset in charsets:
-            if charset:
-                try:
-                    return s.decode(charset)
-                except UnicodeError:
-                    pass
-        return s.decode('latin1')
 
     def _decode_header(self, text):
+        """Returns unicode() string conversion of the the given encoded smtp header"""
         if text:
-            text = decode_header(text.replace('\r', '')) 
-        return ''.join(map(lambda x:self._to_decode(x[0], [x[1]]), text or []))
-    def to_email(self, text):
-        _email = re.compile(r'.*<.*@.*\..*>', re.UNICODE)
-        def record(path):
-            eml = path.group()
-            index = eml.index('<')
-            eml = eml[index:-1].replace('<', '').replace('>', '')
-            return eml
-
-        bits = _email.sub(record, text)
-        return bits
-    
+            text = decode_header(text.replace('\r', ''))
+            return ''.join([tools.ustr(x[0], x[1]) for x in text])
+
+    def to_email(self,text):
+        return re.findall(r'([^ ,<@]+@[^> ,]+)',text)
+
     def history(self, cr, uid, model, res_ids, msg, attach, context=None):
         """This function creates history for mails fetched
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks,
         @param model: OpenObject Model
-        @param res_ids: Ids of the record of OpenObject model created 
+        @param res_ids: Ids of the record of OpenObject model created
         @param msg: Email details
         @param attach: Email attachments
         """
@@ -195,104 +340,92 @@ class mailgate_tool(osv.osv_memory):
 
         msg_pool = self.pool.get('mailgate.message')
         for res_id in res_ids:
+            case = self.pool.get(model).browse(cr, uid, res_id, context=context)
+            partner_id = hasattr(case, 'partner_id') and (case.partner_id and case.partner_id.id or False) or False
+            if not partner_id and model == 'res.partner':
+                partner_id = res_id
             msg_data = {
-                        'name': msg.get('subject', 'No subject'), 
-                        'date': msg.get('date') , 
-                        'description': msg.get('body', msg.get('from')), 
-                        'history': True,
-                        'res_model': model, 
-                        'email_cc': msg.get('cc'), 
-                        'email_from': msg.get('from'), 
-                        'email_to': msg.get('to'), 
-                        'message_id': msg.get('message-id'), 
-                        'references': msg.get('references'), 
-                        'res_id': res_id,
-                        'user_id': uid, 
-                        'attachment_ids': [(6, 0, attach)]
+                'name': msg.get('subject', 'No subject'),
+                'date': msg.get('date'),
+                'description': msg.get('body', msg.get('from')),
+                'history': True,
+                'partner_id': partner_id,
+                'model': model,
+                'email_cc': msg.get('cc'),
+                'email_from': msg.get('from'),
+                'email_to': msg.get('to'),
+                'message_id': msg.get('message-id'),
+                'references': msg.get('references') or msg.get('in-reply-to'),
+                'res_id': res_id,
+                'user_id': uid,
+                'attachment_ids': [(6, 0, attach)]
             }
-            msg_id = msg_pool.create(cr, uid, msg_data, context=context)
+            msg_pool.create(cr, uid, msg_data, context=context)
         return True
-    
-    def email_send(self, cr, uid, model, res_id, msg, from_email=False, email_default=False):
-        """This function Sends return email on submission of  Fetched email in OpenERP database
-        @param self: The object pointer
-        @param cr: the current row, from the database cursor,
-        @param uid: the current user’s ID for security checks,
-        @param model: OpenObject Model
-        @param res_id: Id of the record of OpenObject model created from the Email details 
-        @param msg: Email details
-        @param email_default: Default Email address in case of any Problem
+
+    def email_forward(self, cr, uid, model, res_ids, msg, email_error=False, context=None):
+        """Sends an email to all people following the thread
+        @param res_id: Id of the record of OpenObject model created from the email message
+        @param msg: email.message.Message to forward
+        @param email_error: Default Email address in case of any Problem
         """
-        history_pool = self.pool.get('mailgate.message')
-        model_pool = self.pool.get(model)
-        from_email = from_email or tools.config.get('email_from', None)
-        message = email.message_from_string(tools.ustr(msg).encode('utf-8'))
-        subject = "[%s] %s" %(res_id, message['Subject'])
-        #msg_mails = []
-        #mails = [self._decode_header(message['From']), self._decode_header(message['To'])]
-        #mails += self._decode_header(message.get('Cc', '')).split(',')
-
-        values = {}
-        if hasattr(model_pool, 'emails_get'):
-            values = model_pool.emails_get(cr, uid, [res_id])
-        emails = values.get(res_id, {})
-
-        priority = emails.get('priority', [3])[0]
-        em = emails['user_email'] + emails['email_from'] + emails['email_cc']
-        msg_mails = map(self.to_email, filter(None, em))
-
-        #mm = [self._decode_header(message['From']), self._decode_header(message['To'])]
-        #mm += self._decode_header(message.get('Cc', '')).split(',')
-
-        #msg_mails = map(self.to_email, filter(None, mm))        
-        
-        encoding = message.get_content_charset()
-        message['body'] = message.get_payload(decode=True)
-        if encoding:
-            message['body'] = self._to_decode(message['body'], [encoding])
-
-        from_mail = self._decode_header(message['From'])
-        body = _("""
-Hello %s,""" % (from_mail))
-        body += _("""
-
-    Your Request ID: %s""") % (res_id)
-        body += _("""
-        
-Thanks
-
--------- Original Message --------        
-%s
-""") % (self._to_decode(message['body'], [encoding]))
-        res = None
-        try:
-            res = tools.email_send(from_email, msg_mails, subject, body, openobject_id=res_id)
-        except Exception, e:
-            if email_default:
-                temp_msg = '[%s] %s'%(res_id, message['Subject'])
-                del message['Subject']
-                message['Subject'] = '[OpenERP-FetchError] %s' %(temp_msg)
-                tools.email_send(from_email, email_default, message.get('Subject'), message.get('body'), openobject_id=res_id)
-        return res
 
-    def process_email(self, cr, uid, model, message, attach=True, context=None):
-        """This function Processes email and create record for given OpenERP model 
+        model_pool = self.pool.get(model)
+        for res in model_pool.browse(cr, uid, res_ids, context=context):
+            message_followers = model_pool.message_followers(cr, uid, [res.id])[res.id]
+            message_followers_emails = self.to_email(','.join(filter(None, message_followers)))
+            message_recipients = self.to_email(','.join(filter(None,
+                                                         [self._decode_header(msg['from']),
+                                                         self._decode_header(msg['to']),
+                                                         self._decode_header(msg['cc'])])))
+            message_forward = [i for i in message_followers_emails if (i and (i not in message_recipients))]
+
+            if message_forward:
+                # TODO: we need an interface for this for all types of objects, not just leads
+                if hasattr(res, 'section_id'):
+                    del msg['reply-to']
+                    msg['reply-to'] = res.section_id.reply_to
+
+                smtp_from = self.to_email(msg['from'])
+                if not tools.misc._email_send(smtp_from, message_forward, msg, openobject_id=res.id) and email_error:
+                    subj = msg['subject']
+                    del msg['subject'], msg['to'], msg['cc'], msg['bcc']
+                    msg['subject'] = '[OpenERP-Forward-Failed] %s' % subj
+                    msg['to'] = email_error
+                    tools.misc._email_send(smtp_from, self.to_email(email_error), msg, openobject_id=res.id)
+
+    def process_email(self, cr, uid, model, message, custom_values=None, attach=True, context=None):
+        """This function Processes email and create record for given OpenERP model
         @param self: The object pointer
         @param cr: the current row, from the database cursor,
         @param uid: the current user’s ID for security checks,
         @param model: OpenObject Model
-        @param message: Email details
+        @param message: Email details, passed as a string or an xmlrpclib.Binary
         @param attach: Email attachments
         @param context: A standard dictionary for contextual values"""
 
-        model_pool = self.pool.get(model)
-        if not context:
+        # extract message bytes, we are forced to pass the message as binary because
+        # we don't know its encoding until we parse its headers and hence can't
+        # convert it to utf-8 for transport between the mailgate script and here.
+        if isinstance(message, xmlrpclib.Binary):
+            message = str(message.data)
+
+        if context is None:
             context = {}
+
+        if custom_values is None or not isinstance(custom_values, dict):
+            custom_values = {}
+
+        model_pool = self.pool.get(model)
         res_id = False
+
         # Create New Record into particular model
         def create_record(msg):
+            att_ids = []
             if hasattr(model_pool, 'message_new'):
-                res_id = model_pool.message_new(cr, uid, msg, context)
+                res_id,att_ids = model_pool.message_new(cr, uid, msg, context=context)
+                if custom_values:
+                    model_pool.write(cr, uid, [res_id], custom_values, context=context)
             else:
                 data = {
                     'name': msg.get('subject'),
@@ -305,7 +438,6 @@ Thanks
                 data.update(self.get_partner(cr, uid, msg.get('from'), context=context))
                 res_id = model_pool.create(cr, uid, data, context=context)
 
-                att_ids = []
                 if attach:
                     for attachment in msg.get('attachments', []):
                         data_attach = {
@@ -318,20 +450,20 @@ Thanks
                         }
                         att_ids.append(self.pool.get('ir.attachment').create(cr, uid, data_attach))
 
-            return res_id
-
-        history_pool = self.pool.get('mailgate.message')
+            return res_id, att_ids
 
         # Warning: message_from_string doesn't always work correctly on unicode,
         # we must use utf-8 strings here :-(
-        msg_txt = email.message_from_string(tools.ustr(message).encode('utf-8'))
-        message_id = msg_txt.get('Message-ID', False)
+        if isinstance(message, unicode):
+            message = message.encode('utf-8')
+        msg_txt = email.message_from_string(message)
+        message_id = msg_txt.get('message-id', False)
         msg = {}
 
         if not message_id:
             # Very unusual situation, be we should be fault-tolerant here
             message_id = time.time()
-            msg_txt['Message-ID'] = message_id
+            msg_txt['message-id'] = message_id
             _logger.info('Message without message-id, generating a random one: %s', message_id)
 
         fields = msg_txt.keys()
@@ -350,14 +482,14 @@ Thanks
         if 'Delivered-To' in fields:
             msg['to'] = self._decode_header(msg_txt.get('Delivered-To'))
 
-        if 'Cc' in fields:
-            msg['cc'] = self._decode_header(msg_txt.get('Cc'))
+        if 'CC' in fields:
+            msg['cc'] = self._decode_header(msg_txt.get('CC'))
 
-        if 'Reply-To' in fields:
+        if 'Reply-to' in fields:
             msg['reply'] = self._decode_header(msg_txt.get('Reply-To'))
 
         if 'Date' in fields:
-            msg['date'] = msg_txt.get('Date')
+            msg['date'] = self._decode_header(msg_txt.get('Date'))
 
         if 'Content-Transfer-Encoding' in fields:
             msg['encoding'] = msg_txt.get('Content-Transfer-Encoding')
@@ -365,58 +497,63 @@ Thanks
         if 'References' in fields:
             msg['references'] = msg_txt.get('References')
 
+        if 'In-Reply-To' in fields:
+            msg['in-reply-to'] = msg_txt.get('In-Reply-To')
+
         if 'X-Priority' in fields:
-            msg['priority'] = msg_txt.get('X-priority', '3 (Normal)').split(' ')[0]
+            msg['priority'] = msg_txt.get('X-Priority', '3 (Normal)').split(' ')[0]
 
-        if not msg_txt.is_multipart() or 'text/plain' in msg.get('content-type', ''):
+        if not msg_txt.is_multipart() or 'text/plain' in msg.get('Content-Type', ''):
             encoding = msg_txt.get_content_charset()
-            msg['body'] = msg_txt.get_payload(decode=True)
-            if encoding:
-                msg['body'] = tools.ustr(msg['body'])
+            body = msg_txt.get_payload(decode=True)
+            if 'text/html' in msg_txt.get('Content-Type', ''):
+                body = tools.html2plaintext(body)
+            msg['body'] = tools.ustr(body, encoding)
 
         attachments = {}
+        has_plain_text = False
         if msg_txt.is_multipart() or 'multipart/alternative' in msg.get('content-type', ''):
             body = ""
-            counter = 1
             for part in msg_txt.walk():
                 if part.get_content_maintype() == 'multipart':
                     continue
 
                 encoding = part.get_content_charset()
-
+                filename = part.get_filename()
                 if part.get_content_maintype()=='text':
                     content = part.get_payload(decode=True)
-                    filename = part.get_filename()
-                    if filename :
+                    if filename:
                         attachments[filename] = content
-                    else:
-                        if encoding:
-                            content = unicode(content, encoding)
+                    elif not has_plain_text:
+                        # main content parts should have 'text' maintype
+                        # and no filename. we ignore the html part if
+                        # there is already a plaintext part without filename,
+                        # because presumably these are alternatives.
+                        content = tools.ustr(content, encoding)
                         if part.get_content_subtype() == 'html':
-                            body = tools.html2plaintext(content)
+                            body = tools.ustr(tools.html2plaintext(content))
                         elif part.get_content_subtype() == 'plain':
                             body = content
-                elif part.get_content_maintype()=='application' or part.get_content_maintype()=='image' or part.get_content_maintype()=='text':
-                    filename = part.get_filename();
+                            has_plain_text = True
+                elif part.get_content_maintype() in ('application', 'image') \
+                        or part.get_content_subtype() in ('octet-stream'):
                     if filename :
                         attachments[filename] = part.get_payload(decode=True)
                     else:
                         res = part.get_payload(decode=True)
-                        if encoding:
-                            res = tools.ustr(res)
-
-                        body += res
+                        body += tools.ustr(res, encoding)
 
             msg['body'] = body
             msg['attachments'] = attachments
         res_ids = []
+        attachment_ids = []
         new_res_id = False
-        if msg.get('references'):
-            references = msg.get('references')
+        if msg.get('references') or msg.get('in-reply-to'):
+            references = msg.get('references') or msg.get('in-reply-to')
             if '\r\n' in references:
-                references = msg.get('references').split('\r\n')
+                references = references.split('\r\n')
             else:
-                references = msg.get('references').split(' ')
+                references = references.split(' ')
             for ref in references:
                 ref = ref.strip()
                 res_id = tools.misc.reference_re.search(ref)
@@ -428,30 +565,35 @@ Thanks
                         res_id = res_id.group(1)
                 if res_id:
                     res_id = int(res_id)
-                    res_ids.append(res_id)
                     model_pool = self.pool.get(model)
-
-                    vals = {}
-                    if hasattr(model_pool, 'message_update'):
-                        model_pool.message_update(cr, uid, [res_id], vals, msg, context=context)
+                    if model_pool.exists(cr, uid, res_id):
+                        res_ids.append(res_id)
+                        if hasattr(model_pool, 'message_update'):
+                            model_pool.message_update(cr, uid, [res_id], {}, msg, context=context)
+                        else:
+                            raise NotImplementedError('model %s does not support updating records, mailgate API method message_update() is missing'%model)
 
         if not len(res_ids):
-            new_res_id = create_record(msg)
+            new_res_id, attachment_ids = create_record(msg)
             res_ids = [new_res_id]
+
         # Store messages
         context.update({'model' : model})
-        if hasattr(model_pool, '_history'):
-            model_pool._history(cr, uid, res_ids, _('Receive'), history=True, 
-                            subject = msg.get('subject'), 
-                            email = msg.get('to'), 
-                            details = msg.get('body'), 
-                            email_from = msg.get('from'), 
-                            message_id = msg.get('message-id'), 
-                            references = msg.get('references', False),
-                            attach = msg.get('attachments', {}).items(), 
+        if hasattr(model_pool, 'history'):
+            model_pool.history(cr, uid, res_ids, _('receive'), history=True,
+                            subject = msg.get('subject'),
+                            email = msg.get('to'),
+                            details = msg.get('body'),
+                            email_from = msg.get('from'),
+                            email_cc = msg.get('cc'),
+                            message_id = msg.get('message-id'),
+                            references = msg.get('references', False) or msg.get('in-reply-to', False),
+                            attach = attachment_ids or attachments.items(),
+                            email_date = msg.get('date'),
                             context = context)
         else:
-            self.history(cr, uid, model, res_ids, msg, att_ids, context=context)
+            self.history(cr, uid, model, res_ids, msg, attachment_ids, context=context)
+        self.email_forward(cr, uid, model, res_ids, msg_txt)
         return new_res_id
 
     def get_partner(self, cr, uid, from_email, context=None):
@@ -466,8 +608,8 @@ Thanks
             'partner_address_id': False,
             'partner_id': False
         }
-        from_email = self.to_email(from_email)
-        address_ids = address_pool.search(cr, uid, [('email', '=', from_email)])
+        from_email = self.to_email(from_email)[0]
+        address_ids = address_pool.search(cr, uid, [('email', 'like', from_email)])
         if address_ids:
             address = address_pool.browse(cr, uid, address_ids[0])
             res['partner_address_id'] = address_ids[0]
@@ -477,4 +619,4 @@ Thanks
 
 mailgate_tool()
 
-
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: