From 54eb1cbbb8a13e2bc7b74f56f99629ff6b38b67b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Philipp=20H=C3=B6rist?= <forenjunkie@chello.at>
Date: Sat, 8 Apr 2017 20:10:57 +0200
Subject: [PATCH] Simplify log_message()

- Pass EventObj instead of variables
- decrease indent level
- check all conditions inside log_message() not before calling
- fixed bug where gc private messages where not logged with the correct jid
---
 src/common/connection.py                   | 79 +++++++++++-----------
 src/common/zeroconf/connection_zeroconf.py |  5 +-
 2 files changed, 39 insertions(+), 45 deletions(-)

diff --git a/src/common/connection.py b/src/common/connection.py
index 271fd239e..d5c709350 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -507,37 +507,42 @@ class CommonConnection:
             gajim.nec.push_incoming_event(
                 StanzaMessageOutgoingEvent(None, **vars(obj)))
 
-    def log_message(self, jid, msg, forward_from, session, original_message,
-    subject, type_, xhtml=None, additional_data=None):
-        if additional_data is None:
-            additional_data = {}
-        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):
-                log_msg = msg
-                if original_message is not None:
-                    log_msg = original_message
-                if log_msg:
-                    if type_ == 'chat':
-                        kind = 'chat_msg_sent'
-                    else:
-                        kind = 'single_msg_sent'
-                    try:
-                        if xhtml and gajim.config.get('log_xhtml_messages'):
-                            log_msg = '<body xmlns="%s">%s</body>' % (
-                                nbxmpp.NS_XHTML, xhtml)
-                        gajim.logger.write(kind, jid, log_msg, subject=subject, additional_data=additional_data)
-                    except exceptions.PysqliteOperationalError as e:
-                        self.dispatch('DB_ERROR', (_('Disk Write Error'),
-                            str(e)))
-                    except exceptions.DatabaseMalformed:
-                        pritext = _('Database Error')
-                        sectext = _('The database file (%s) cannot be read. Try'
-                            ' to repair it (see '
-                            'http://trac.gajim.org/wiki/DatabaseBackup)'
-                            ' or remove it (all history will be lost).') % \
-                            common.logger.LOG_DB_PATH
-                        self.dispatch('DB_ERROR', (pritext, sectext))
+    def log_message(self, obj):
+        if not obj.is_loggable:
+            return
+
+        if obj.forward_from or not obj.session or not obj.session.is_loggable():
+            return
+
+        if not gajim.config.should_log(self.name, obj.jid):
+            return
+
+        if obj.xhtml and gajim.config.get('log_xhtml_messages'):
+            message = '<body xmlns="%s">%s</body>' % (nbxmpp.NS_XHTML,
+                                                      obj.xhtml)
+        else:
+            message = obj.original_message or obj.message
+        if not message:
+            return
+
+        if obj.type_ == 'chat':
+            kind = 'chat_msg_sent'
+        else:
+            kind = 'single_msg_sent'
+        try:
+            gajim.logger.write(
+                kind, obj.jid, message, subject=obj.subject,
+                additional_data=obj.additional_data)
+        except exceptions.PysqliteOperationalError as error:
+            self.dispatch('DB_ERROR', (_('Disk Write Error'), str(error)))
+        except exceptions.DatabaseMalformed:
+            pritext = _('Database Error')
+            sectext = _('The database file (%s) cannot be read. Try'
+                ' to repair it (see '
+                'https://dev.gajim.org/gajim/gajim/wikis/help/DatabaseBackup)'
+                ' or remove it (all history will be lost).') % \
+                common.logger.LOG_DB_PATH
+            self.dispatch('DB_ERROR', (pritext, sectext))
 
     def ack_subscribed(self, jid):
         """
@@ -2136,21 +2141,13 @@ class Connection(CommonConnection, ConnectionHandlers):
         if obj.callback:
             obj.callback(obj, obj.msg_iq, *obj.callback_args)
 
-        if not obj.is_loggable:
-            return
         if isinstance(obj.jid, list):
             for j in obj.jid:
                 if obj.session is None:
                     obj.session = self.get_or_create_session(j, '')
-                self.log_message(
-                    j, obj.message, obj.forward_from, obj.session,
-                    obj.original_message, obj.subject, obj.type_, obj.xhtml,
-                    obj.additional_data)
+                self.log_message(obj)
         else:
-            self.log_message(
-                obj.jid, obj.message, obj.forward_from, obj.session,
-                obj.original_message, obj.subject, obj.type_, obj.xhtml,
-                obj.additional_data)
+            self.log_message(obj)
 
     def send_contacts(self, contacts, fjid, type_='message'):
         """
diff --git a/src/common/zeroconf/connection_zeroconf.py b/src/common/zeroconf/connection_zeroconf.py
index 6a513d5fc..9db42774d 100644
--- a/src/common/zeroconf/connection_zeroconf.py
+++ b/src/common/zeroconf/connection_zeroconf.py
@@ -342,10 +342,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
             if obj.callback:
                 obj.callback(obj.msg_iq, *obj.callback_args)
 
-            if not obj.is_loggable:
-                return
-            self.log_message(obj.jid, obj.message, obj.forward_from,
-                obj.session, obj.original_message, obj.subject, obj.type_)
+            self.log_message(obj)
 
         def on_send_not_ok(reason):
             reason += ' ' + _('Your message could not be sent.')