From ff0c0dc1c552b89ff473b6cb5b90229ffa271455 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Mon, 30 May 2011 19:47:05 +0200
Subject: [PATCH] ability to save xhtml messages. Fixes #5736

---
 src/chat_control.py      | 12 +++++++-----
 src/common/config.py     |  1 +
 src/common/connection.py | 15 +++++++++------
 src/history_window.py    | 11 ++++++++---
 src/session.py           |  6 +++++-
 5 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/chat_control.py b/src/chat_control.py
index ead468ba90..201b71389d 100644
--- a/src/chat_control.py
+++ b/src/chat_control.py
@@ -2921,11 +2921,13 @@ class ChatControl(ChatControlBase):
                 small_attr = ['small']
             else:
                 small_attr = []
-            ChatControlBase.print_conversation_line(self, row[2], kind, name, tim,
-                                                    small_attr,
-                                                    small_attr + ['restored_message'],
-                                                    small_attr + ['restored_message'],
-                                                    False, old_kind=local_old_kind)
+            xhtml = None
+            if row[2].startswith('<body '):
+                xhtml = row[2]
+            ChatControlBase.print_conversation_line(self, row[2], kind, name,
+                tim, small_attr, small_attr + ['restored_message'],
+                small_attr + ['restored_message'], False,
+                old_kind=local_old_kind, xhtml=xhtml)
             if row[2].startswith('/me ') or row[2].startswith('/me\n'):
                 local_old_kind = None
             else:
diff --git a/src/common/config.py b/src/common/config.py
index 0e706b820d..959f40e884 100644
--- a/src/common/config.py
+++ b/src/common/config.py
@@ -234,6 +234,7 @@ class Config:
             'print_status_in_chats': [opt_bool, True, _('If False, Gajim will no longer print status line in chats when a contact changes his or her status and/or his or her status message.')],
             'print_status_in_muc': [opt_str, 'in_and_out', _('can be "none", "all" or "in_and_out". If "none", Gajim will no longer print status line in groupchats when a member changes his or her status and/or his or her status message. If "all" Gajim will print all status messages. If "in_and_out", Gajim will only print FOO enters/leaves group chat.')],
             'log_contact_status_changes': [opt_bool, False],
+            'log_xhtml_messages': [opt_bool, False, _('Log XHTML messages instead of plain text messages.')],
             'just_connected_bg_color': [opt_str, '#adc3c6', _('Background color of contacts when they just signed in.')],
             'just_disconnected_bg_color': [opt_str, '#ab6161', _('Background color of contacts when they just signed out.')],
             'restored_messages_color': [opt_color, '#555753'],
diff --git a/src/common/connection.py b/src/common/connection.py
index ffe694dd01..06b1a15074 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -432,10 +432,10 @@ class CommonConnection:
 
         if callback:
             callback(jid, msg, keyID, forward_from, session, original_message,
-                    subject, type_, msg_iq)
+                    subject, type_, msg_iq, xhtml)
 
     def log_message(self, jid, msg, forward_from, session, original_message,
-    subject, type_):
+    subject, type_, xhtml=None):
         if not forward_from and session and session.is_loggable():
             ji = gajim.get_jid_without_resource(jid)
             if gajim.config.should_log(self.name, ji):
@@ -451,6 +451,9 @@ class CommonConnection:
                     else:
                         kind = 'single_msg_sent'
                     try:
+                        if xhtml and gajim.config.get('log_xhtml_messages'):
+                            log_msg = '<body xmlns="%s">%s</body>' % (
+                                common.xmpp.NS_XHTML, xhtml)
                         gajim.logger.write(kind, jid, log_msg)
                     except exceptions.PysqliteOperationalError, e:
                         self.dispatch('DB_ERROR', (_('Disk Write Error'),
@@ -1751,7 +1754,7 @@ class Connection(CommonConnection, ConnectionHandlers):
     callback_args=[], now=False):
 
         def cb(jid, msg, keyID, forward_from, session, original_message,
-        subject, type_, msg_iq):
+        subject, type_, msg_iq, xhtml):
             msg_id = self.connection.send(msg_iq, now=now)
             jid = helpers.parse_jid(jid)
             gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self,
@@ -1760,7 +1763,7 @@ class Connection(CommonConnection, ConnectionHandlers):
                 callback(msg_id, *callback_args)
 
             self.log_message(jid, msg, forward_from, session, original_message,
-                    subject, type_)
+                    subject, type_, xhtml)
 
         self._prepare_message(jid, msg, keyID, type_=type_, subject=subject,
             chatstate=chatstate, msg_id=msg_id, composing_xep=composing_xep,
@@ -1773,7 +1776,7 @@ class Connection(CommonConnection, ConnectionHandlers):
             return
 
         def cb(jid, msg, keyID, forward_from, session, original_message,
-        subject, type_, msg_iq):
+        subject, type_, msg_iq, xhtml):
             msg_id = self.connection.send(msg_iq, now=obj.now)
             jid = helpers.parse_jid(obj.jid)
             gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self,
@@ -1784,7 +1787,7 @@ class Connection(CommonConnection, ConnectionHandlers):
             if not obj.is_loggable:
                 return
             self.log_message(jid, msg, forward_from, session, original_message,
-                    subject, type_)
+                    subject, type_, xhtml)
 
         self._prepare_message(obj.jid, obj.message, obj.keyID, type_=obj.type_,
             subject=obj.subject, chatstate=obj.chatstate, msg_id=obj.msg_id,
diff --git a/src/history_window.py b/src/history_window.py
index 961b45f3be..2b03acd1ee 100644
--- a/src/history_window.py
+++ b/src/history_window.py
@@ -478,12 +478,17 @@ class HistoryWindow:
 
         if subject:
             message = _('Subject: %s\n') % subject + message
-        message += '\n'
+        xhtml = None
+        if message.startswith('<body '):
+            xhtml = message
+
         if tag_msg:
             self.history_textview.print_real_text(message, [tag_msg],
-                    name=contact_name)
+                    name=contact_name, xhtml=xhtml)
         else:
-            self.history_textview.print_real_text(message, name=contact_name)
+            self.history_textview.print_real_text(message, name=contact_name,
+                xhtml=xhtml)
+        self.history_textview.print_real_text('\n')
 
     def on_query_entry_activate(self, widget):
         text = self.query_entry.get_text()
diff --git a/src/session.py b/src/session.py
index 7e30e4c409..9c04c38d00 100644
--- a/src/session.py
+++ b/src/session.py
@@ -85,8 +85,12 @@ class ChatControlSession(stanza_session.EncryptedStanzaSession):
 
         if self.is_loggable() and obj.msgtxt:
             try:
+                if obj.xhtml and gajim.config.get('log_xhtml_messages'):
+                    msg_to_log = obj.xhtml
+                else:
+                    msg_to_log = obj.msgtxt
                 msg_id = gajim.logger.write(log_type, obj.fjid,
-                    obj.msgtxt, tim=obj.timestamp, subject=obj.subject)
+                    msg_to_log, tim=obj.timestamp, subject=obj.subject)
             except exceptions.PysqliteOperationalError, e:
                 self.conn.dispatch('ERROR', (_('Disk WriteError'), str(e)))
             except exceptions.DatabaseMalformed:
-- 
GitLab