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
This commit is contained in:
Philipp Hörist 2017-04-08 20:10:57 +02:00
parent 8a789763a2
commit 54eb1cbbb8
2 changed files with 39 additions and 45 deletions

View file

@ -507,34 +507,39 @@ class CommonConnection:
gajim.nec.push_incoming_event( gajim.nec.push_incoming_event(
StanzaMessageOutgoingEvent(None, **vars(obj))) StanzaMessageOutgoingEvent(None, **vars(obj)))
def log_message(self, jid, msg, forward_from, session, original_message, def log_message(self, obj):
subject, type_, xhtml=None, additional_data=None): if not obj.is_loggable:
if additional_data is None: return
additional_data = {}
if not forward_from and session and session.is_loggable(): if obj.forward_from or not obj.session or not obj.session.is_loggable():
ji = gajim.get_jid_without_resource(jid) return
if gajim.config.should_log(self.name, ji):
log_msg = msg if not gajim.config.should_log(self.name, obj.jid):
if original_message is not None: return
log_msg = original_message
if log_msg: if obj.xhtml and gajim.config.get('log_xhtml_messages'):
if type_ == 'chat': 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' kind = 'chat_msg_sent'
else: else:
kind = 'single_msg_sent' kind = 'single_msg_sent'
try: try:
if xhtml and gajim.config.get('log_xhtml_messages'): gajim.logger.write(
log_msg = '<body xmlns="%s">%s</body>' % ( kind, obj.jid, message, subject=obj.subject,
nbxmpp.NS_XHTML, xhtml) additional_data=obj.additional_data)
gajim.logger.write(kind, jid, log_msg, subject=subject, additional_data=additional_data) except exceptions.PysqliteOperationalError as error:
except exceptions.PysqliteOperationalError as e: self.dispatch('DB_ERROR', (_('Disk Write Error'), str(error)))
self.dispatch('DB_ERROR', (_('Disk Write Error'),
str(e)))
except exceptions.DatabaseMalformed: except exceptions.DatabaseMalformed:
pritext = _('Database Error') pritext = _('Database Error')
sectext = _('The database file (%s) cannot be read. Try' sectext = _('The database file (%s) cannot be read. Try'
' to repair it (see ' ' to repair it (see '
'http://trac.gajim.org/wiki/DatabaseBackup)' 'https://dev.gajim.org/gajim/gajim/wikis/help/DatabaseBackup)'
' or remove it (all history will be lost).') % \ ' or remove it (all history will be lost).') % \
common.logger.LOG_DB_PATH common.logger.LOG_DB_PATH
self.dispatch('DB_ERROR', (pritext, sectext)) self.dispatch('DB_ERROR', (pritext, sectext))
@ -2136,21 +2141,13 @@ class Connection(CommonConnection, ConnectionHandlers):
if obj.callback: if obj.callback:
obj.callback(obj, obj.msg_iq, *obj.callback_args) obj.callback(obj, obj.msg_iq, *obj.callback_args)
if not obj.is_loggable:
return
if isinstance(obj.jid, list): if isinstance(obj.jid, list):
for j in obj.jid: for j in obj.jid:
if obj.session is None: if obj.session is None:
obj.session = self.get_or_create_session(j, '') obj.session = self.get_or_create_session(j, '')
self.log_message( self.log_message(obj)
j, obj.message, obj.forward_from, obj.session,
obj.original_message, obj.subject, obj.type_, obj.xhtml,
obj.additional_data)
else: else:
self.log_message( self.log_message(obj)
obj.jid, obj.message, obj.forward_from, obj.session,
obj.original_message, obj.subject, obj.type_, obj.xhtml,
obj.additional_data)
def send_contacts(self, contacts, fjid, type_='message'): def send_contacts(self, contacts, fjid, type_='message'):
""" """

View file

@ -342,10 +342,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
if obj.callback: if obj.callback:
obj.callback(obj.msg_iq, *obj.callback_args) obj.callback(obj.msg_iq, *obj.callback_args)
if not obj.is_loggable: self.log_message(obj)
return
self.log_message(obj.jid, obj.message, obj.forward_from,
obj.session, obj.original_message, obj.subject, obj.type_)
def on_send_not_ok(reason): def on_send_not_ok(reason):
reason += ' ' + _('Your message could not be sent.') reason += ' ' + _('Your message could not be sent.')