From d344c24a2a5cd4d9a302e51460291e5daba9befa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 4 Apr 2017 18:55:19 +0200 Subject: [PATCH 1/5] Pass EventObject around instead of many vars This makes it much easier to read and modify --- src/common/connection.py | 214 +++++++++------------ src/common/connection_handlers_events.py | 7 + src/common/zeroconf/connection_zeroconf.py | 7 +- 3 files changed, 96 insertions(+), 132 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index f6a3d47d3..6ea33f5a3 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -263,60 +263,46 @@ class CommonConnection: """ raise NotImplementedError - def _prepare_message(self, jid, msg, keyID, type_='chat', subject='', - chatstate=None, msg_id=None, resource=None, user_nick=None, xhtml=None, - session=None, forward_from=None, form_node=None, label=None, - original_message=None, delayed=None, attention=False, correction_msg=None, - callback=None): + def _prepare_message(self, obj, callback): + if not self.connection or self.connected < 2: return 1 - if isinstance(jid, list): - new_list = [] - for j in jid: + if isinstance(obj.jid, list): + for jid in obj.jid: try: - new_list.append(self.check_jid(j)) + self.check_jid(jid) except helpers.InvalidFormat: gajim.nec.push_incoming_event(InformationEvent(None, conn=self, level='error', pri_txt=_('Invalid JID'), sec_txt=_('It is not possible to send a message ' - 'to %s, this JID is not valid.') % j)) + 'to %s, this JID is not valid.') % jid)) return - fjid = new_list else: try: - jid = self.check_jid(jid) + self.check_jid(obj.jid) except helpers.InvalidFormat: gajim.nec.push_incoming_event(InformationEvent(None, conn=self, level='error', pri_txt=_('Invalid JID'), sec_txt=_( 'It is not possible to send a message to %s, this JID is not ' - 'valid.') % jid)) + 'valid.') % obj.jid)) return - fjid = jid - if resource: - fjid += '/' + resource - if session: - fjid = session.get_to() - - if msg and not xhtml and gajim.config.get( + if obj.message and not obj.xhtml and gajim.config.get( 'rst_formatting_outgoing_messages'): from common.rst_xhtml_generator import create_xhtml - xhtml = create_xhtml(msg) - if not msg and chatstate is None and form_node is None: + obj.xhtml = create_xhtml(obj.message) + if not obj.message and obj.chatstate is None and obj.form_node is None: return - msgtxt = msg - msgenc = '' - - if keyID and self.USE_GPG: - xhtml = None - if keyID == 'UNKNOWN': + if obj.keyID and self.USE_GPG: + obj.xhtml = None + if obj.keyID == 'UNKNOWN': error = _('Neither the remote presence is signed, nor a key was ' 'assigned.') - elif keyID.endswith('MISMATCH'): + elif obj.keyID.endswith('MISMATCH'): error = _('The contact\'s key (%s) does not match the key assigned ' - 'in Gajim.' % keyID[:8]) + 'in Gajim.' % obj.keyID[:8]) else: myKeyID = gajim.config.get_per('accounts', self.name, 'keyid') def encrypt_thread(msg, keyID, always_trust=False): @@ -325,47 +311,28 @@ class CommonConnection: always_trust) def _on_encrypted(output): msgenc, error = output - if error.startswith( 'NOT_TRUSTED'): + if error.startswith('NOT_TRUSTED'): def _on_always_trust(answer): if answer: - gajim.thread_interface(encrypt_thread, [msg, keyID, + gajim.thread_interface(encrypt_thread, [obj.message, obj.keyID, True], _on_encrypted, []) else: - self._message_encrypted_cb(output, type_, msg, - msgtxt, original_message, fjid, resource, - jid, xhtml, subject, chatstate, msg_id, - label, forward_from, delayed, session, - form_node, user_nick, keyID, attention, - correction_msg, callback) + self._message_encrypted_cb(output, obj, callback) gajim.nec.push_incoming_event(GPGTrustKeyEvent(None, conn=self, keyID=error.split(' ')[-1], callback=_on_always_trust)) else: - self._message_encrypted_cb(output, type_, msg, msgtxt, - original_message, fjid, resource, jid, xhtml, - subject, chatstate, msg_id, label, forward_from, - delayed, session, form_node, user_nick, keyID, - attention, correction_msg, callback) - gajim.thread_interface(encrypt_thread, [msg, keyID, False], + self._message_encrypted_cb(output, obj, callback) + gajim.thread_interface(encrypt_thread, [obj.message, obj.keyID, False], _on_encrypted, []) return - self._message_encrypted_cb(('', error), type_, msg, msgtxt, - original_message, fjid, resource, jid, xhtml, subject, - chatstate, msg_id, label, forward_from, delayed, session, - form_node, user_nick, keyID, attention, correction_msg, - callback) + self._message_encrypted_cb((None, error), obj, callback) return - self._on_continue_message(type_, msg, msgtxt, original_message, fjid, - resource, jid, xhtml, subject, msgenc, keyID, chatstate, msg_id, - label, forward_from, delayed, session, form_node, user_nick, - attention, correction_msg, callback) + self._on_continue_message(obj, callback) - def _message_encrypted_cb(self, output, type_, msg, msgtxt, - original_message, fjid, resource, jid, xhtml, subject, chatstate, msg_id, - label, forward_from, delayed, session, form_node, user_nick, keyID, - attention, correction_msg, callback): + def _message_encrypted_cb(self, output, obj, callback): msgenc, error = output if msgenc and not error: @@ -375,120 +342,119 @@ class CommonConnection: # we're not english: one in locale and one en msgtxt = _('[This message is *encrypted* (See :XEP:`27`]') + \ ' (' + msgtxt + ')' - self._on_continue_message(type_, msg, msgtxt, original_message, - fjid, resource, jid, xhtml, subject, msgenc, keyID, - chatstate, msg_id, label, forward_from, delayed, session, - form_node, user_nick, attention, correction_msg, callback) + self._on_continue_message(obj, callback, msgtxt=msgtxt, msgenc=msgenc) return # Encryption failed, do not send message tim = time.localtime() gajim.nec.push_incoming_event(MessageNotSentEvent(None, conn=self, - jid=jid, message=msgtxt, error=error, time_=tim, session=session)) + jid=obj.jid, message=msgtxt, error=error, time_=tim, session=obj.session)) - def _on_continue_message(self, type_, msg, msgtxt, original_message, fjid, - resource, jid, xhtml, subject, msgenc, keyID, chatstate, msg_id, - label, forward_from, delayed, session, form_node, user_nick, attention, - correction_msg, callback): + def _on_continue_message(self, obj, callback, msgtxt=None, msgenc=None): - if correction_msg: - id_ = correction_msg.getID() - if correction_msg.getTag('replace'): - correction_msg.delChild('replace') - correction_msg.setTag('replace', attrs={'id': id_}, + if not msgtxt: + msgtxt = obj.message + fjid = obj.get_full_jid() + + if obj.correction_msg: + id_ = obj.correction_msg.getID() + if obj.correction_msg.getTag('replace'): + obj.correction_msg.delChild('replace') + obj.correction_msg.setTag('replace', attrs={'id': id_}, namespace=nbxmpp.NS_CORRECT) id2 = self.connection.getAnID() - correction_msg.setID(id2) - correction_msg.setBody(msgtxt) - if xhtml: - correction_msg.setXHTML(xhtml) + obj.correction_msg.setID(id2) + obj.correction_msg.setBody(msgtxt) + if obj.xhtml: + obj.correction_msg.setXHTML(obj.xhtml) - if session: - session.last_send = time.time() + if obj.session: + obj.session.last_send = time.time() # XEP-0200 - if session.enable_encryption: - correction_msg = session.encrypt_stanza(correction_msg) + if obj.session.enable_encryption: + obj.correction_msg = obj.session.encrypt_stanza(obj.correction_msg) if callback: - callback(jid, msg, keyID, forward_from, session, original_message, - subject, type_, correction_msg, xhtml) + callback( + obj.jid, obj.message, obj.keyID, obj.forward_from, + obj.session, obj.original_message, obj.subject, obj.type_, + obj.correction_msg, obj.xhtml) return - - if type_ == 'chat': - msg_iq = nbxmpp.Message(body=msgtxt, typ=type_, - xhtml=xhtml) + if obj.type_ == 'chat': + msg_iq = nbxmpp.Message(body=msgtxt, typ=obj.type_, + xhtml=obj.xhtml) else: - if subject: + if obj.subject: msg_iq = nbxmpp.Message(body=msgtxt, typ='normal', - subject=subject, xhtml=xhtml) + subject=obj.subject, xhtml=obj.xhtml) else: msg_iq = nbxmpp.Message(body=msgtxt, typ='normal', - xhtml=xhtml) + xhtml=obj.xhtml) - if msg_id: - msg_iq.setID(msg_id) + if obj.msg_id: + msg_iq.setID(obj.msg_id) if msgenc: msg_iq.setTag(nbxmpp.NS_ENCRYPTED + ' x').setData(msgenc) - if form_node: - msg_iq.addChild(node=form_node) - if label: - msg_iq.addChild(node=label) + if obj.form_node: + msg_iq.addChild(node=obj.form_node) + if obj.label: + msg_iq.addChild(node=obj.label) # XEP-0172: user_nickname - if user_nick: - msg_iq.setTag('nick', namespace = nbxmpp.NS_NICK).setData( - user_nick) + if obj.user_nick: + msg_iq.setTag('nick', namespace=nbxmpp.NS_NICK).setData( + obj.user_nick) # XEP-0203 - if delayed: + if obj.delayed: our_jid = gajim.get_jid_from_account(self.name) + '/' + \ self.server_resource - timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(delayed)) + timestamp = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime(obj.delayed)) msg_iq.addChild('delay', namespace=nbxmpp.NS_DELAY2, attrs={'from': our_jid, 'stamp': timestamp}) # XEP-0224 - if attention: + if obj.attention: msg_iq.setTag('attention', namespace=nbxmpp.NS_ATTENTION) - if isinstance(jid, list): + if isinstance(obj.jid, list): if self.addressing_supported: msg_iq.setTo(gajim.config.get_per('accounts', self.name, 'hostname')) addresses = msg_iq.addChild('addresses', namespace=nbxmpp.NS_ADDRESS) - for j in jid: + for j in obj.jid: addresses.addChild('address', attrs = {'type': 'to', 'jid': j}) else: iqs = [] - for j in jid: + for j in obj.jid: iq = nbxmpp.Message(node=msg_iq) iq.setTo(j) iqs.append(iq) msg_iq = iqs else: msg_iq.setTo(fjid) - r_ = resource - if not r_ and jid != fjid: # Only if we're not in a pm + r_ = obj.resource + if not r_ and obj.jid != fjid: # Only if we're not in a pm r_ = gajim.get_resource_from_jid(fjid) if r_: - contact = gajim.contacts.get_contact(self.name, jid, r_) + contact = gajim.contacts.get_contact(self.name, obj.jid, r_) else: contact = gajim.contacts.get_contact_with_highest_priority( - self.name, jid) + self.name, obj.jid) # chatstates - if peer supports xep85, send chatstates # please note that the only valid tag inside a message containing a # tag is the active event - if chatstate and contact and contact.supports(nbxmpp.NS_CHATSTATES): - msg_iq.setTag(chatstate, namespace=nbxmpp.NS_CHATSTATES) + if obj.chatstate and contact and contact.supports(nbxmpp.NS_CHATSTATES): + msg_iq.setTag(obj.chatstate, namespace=nbxmpp.NS_CHATSTATES) only_chatste = False if not msgtxt: only_chatste = True - if only_chatste and not session.enable_encryption: + if only_chatste and not obj.session.enable_encryption: msg_iq.setTag('no-store', namespace=nbxmpp.NS_MSG_HINTS) @@ -498,20 +464,20 @@ class CommonConnection: nbxmpp.NS_RECEIPTS): msg_iq.setTag('request', namespace=nbxmpp.NS_RECEIPTS) - if forward_from: + if obj.forward_from: addresses = msg_iq.addChild('addresses', namespace=nbxmpp.NS_ADDRESS) addresses.addChild('address', attrs = {'type': 'ofrom', - 'jid': forward_from}) + 'jid': obj.forward_from}) - if session: + if obj.session: # XEP-0201 - session.last_send = time.time() - msg_iq.setThread(session.thread_id) + obj.session.last_send = time.time() + msg_iq.setThread(obj.session.thread_id) # XEP-0200 - if session.enable_encryption: - msg_iq = session.encrypt_stanza(msg_iq) + if obj.session.enable_encryption: + msg_iq = obj.session.encrypt_stanza(msg_iq) if self.carbons_enabled: msg_iq.addChild(name='private', namespace=nbxmpp.NS_CARBONS) @@ -524,8 +490,9 @@ class CommonConnection: namespace=nbxmpp.NS_MSG_HINTS) if callback: - callback(jid, msg, keyID, forward_from, session, original_message, - subject, type_, msg_iq, xhtml) + callback(obj.jid, obj.message, obj.keyID, obj.forward_from, + obj.session, obj.original_message, obj.subject, obj.type_, + msg_iq, obj.xhtml) def log_message(self, jid, msg, forward_from, session, original_message, subject, type_, xhtml=None, additional_data=None): @@ -2159,13 +2126,8 @@ class Connection(CommonConnection, ConnectionHandlers): "session":session, "original_message":original_message, "subject":subject, "type_":type_, "msg_iq":msg_iq, "xhtml":xhtml, "obj":obj})) - self._prepare_message(obj.jid, obj.message, obj.keyID, type_=obj.type_, - subject=obj.subject, chatstate=obj.chatstate, msg_id=obj.msg_id, - resource=obj.resource, user_nick=obj.user_nick, xhtml=obj.xhtml, - label=obj.label, session=obj.session, forward_from=obj.forward_from, - form_node=obj.form_node, original_message=obj.original_message, - delayed=obj.delayed, attention=obj.attention, - correction_msg=obj.correction_msg, callback=cb) + + self._prepare_message(obj, cb) def _nec_stanza_message_outgoing(self, obj): if obj.conn.name != self.name: diff --git a/src/common/connection_handlers_events.py b/src/common/connection_handlers_events.py index 18e71b393..82f28e665 100644 --- a/src/common/connection_handlers_events.py +++ b/src/common/connection_handlers_events.py @@ -2749,6 +2749,13 @@ class MessageOutgoingEvent(nec.NetworkOutgoingEvent): self.correction_msg = None self.automatic_message = True + def get_full_jid(self): + if self.resource: + return self.jid + '/' + self.resource + if self.session: + return self.session.get_to() + return self.jid + def generate(self): return True diff --git a/src/common/zeroconf/connection_zeroconf.py b/src/common/zeroconf/connection_zeroconf.py index 0c55146de..3c56bb1e2 100644 --- a/src/common/zeroconf/connection_zeroconf.py +++ b/src/common/zeroconf/connection_zeroconf.py @@ -367,12 +367,7 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf): 'Contact is offline. Your message could not be sent.'), msg=None, time_=None, session=session)) - self._prepare_message(obj.jid, obj.message, obj.keyID, type_=obj.type_, - subject=obj.subject, chatstate=obj.chatstate, msg_id=obj.msg_id, - resource=obj.resource, user_nick=obj.user_nick, xhtml=obj.xhtml, - label=obj.label, session=obj.session, forward_from=obj.forward_from, - form_node=obj.form_node, original_message=obj.original_message, - delayed=obj.delayed, attention=obj.attention, callback=cb) + self._prepare_message(obj, cb) def send_stanza(self, stanza): # send a stanza untouched From 28a3a58ea9a98ef11351d3eb9f87f25e7e63e602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 4 Apr 2017 21:28:47 +0200 Subject: [PATCH 2/5] Refactor encrypting PGP Message - Add a _encrypt_message() and _finished_encrypt() method - Handle Fallback body text in _on_continue_message() where we build the Stanza --- src/common/connection.py | 92 ++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 45 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index 6ea33f5a3..497bcbce0 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -296,63 +296,65 @@ class CommonConnection: return if obj.keyID and self.USE_GPG: - obj.xhtml = None - if obj.keyID == 'UNKNOWN': - error = _('Neither the remote presence is signed, nor a key was ' - 'assigned.') - elif obj.keyID.endswith('MISMATCH'): - error = _('The contact\'s key (%s) does not match the key assigned ' - 'in Gajim.' % obj.keyID[:8]) - else: - myKeyID = gajim.config.get_per('accounts', self.name, 'keyid') - def encrypt_thread(msg, keyID, always_trust=False): - # encrypt message. This function returns (msgenc, error) - return self.gpg.encrypt(msg, [keyID, myKeyID], - always_trust) - def _on_encrypted(output): - msgenc, error = output - if error.startswith('NOT_TRUSTED'): - def _on_always_trust(answer): - if answer: - gajim.thread_interface(encrypt_thread, [obj.message, obj.keyID, - True], _on_encrypted, []) - else: - self._message_encrypted_cb(output, obj, callback) - gajim.nec.push_incoming_event(GPGTrustKeyEvent(None, - conn=self, keyID=error.split(' ')[-1], - callback=_on_always_trust)) - else: - self._message_encrypted_cb(output, obj, callback) - gajim.thread_interface(encrypt_thread, [obj.message, obj.keyID, False], - _on_encrypted, []) - return - - self._message_encrypted_cb((None, error), obj, callback) + self._encrypt_message(obj, callback) return self._on_continue_message(obj, callback) - def _message_encrypted_cb(self, output, obj, callback): - msgenc, error = output + def _encrypt_message(self, obj, callback): + obj.xhtml = None + if obj.keyID == 'UNKNOWN': + error = _('Neither the remote presence is signed, nor a key was ' + 'assigned.') + elif obj.keyID.endswith('MISMATCH'): + error = _('The contact\'s key (%s) does not match the key assigned ' + 'in Gajim.' % obj.keyID[:8]) + else: + myKeyID = gajim.config.get_per('accounts', self.name, 'keyid') + key_list = [obj.keyID, myKeyID] + def _on_encrypted(output): + msgenc, error = output + if error.startswith('NOT_TRUSTED'): + def _on_always_trust(answer): + if answer: + gajim.thread_interface( + self.gpg.encrypt, [obj.message, key_list, True], + _on_encrypted, []) + else: + self._finished_encrypt( + obj, callback, msgenc=msgenc, error=error) + gajim.nec.push_incoming_event(GPGTrustKeyEvent(None, + conn=self, keyID=error.split(' ')[-1], + callback=_on_always_trust)) + else: + self._finished_encrypt( + obj, callback, msgenc=msgenc, error=error) + gajim.thread_interface( + self.gpg.encrypt, [obj.message, key_list, False], + _on_encrypted, []) + return + self._finished_encrypt(obj, callback, error=error) - if msgenc and not error: + def _finished_encrypt(self, obj, callback, msgenc=None, error=None): + if error: + gajim.nec.push_incoming_event( + MessageNotSentEvent( + None, conn=self, jid=obj.jid, message=obj.message, + error=error, time_=time.time(), session=obj.session)) + return + self._on_continue_message(obj, callback, msgenc) + + def _on_continue_message(self, obj, callback, msgenc=None): + if msgenc: msgtxt = '[This message is *encrypted* (See :XEP:`27`]' lang = os.getenv('LANG') if lang is not None and not lang.startswith('en'): # we're not english: one in locale and one en msgtxt = _('[This message is *encrypted* (See :XEP:`27`]') + \ ' (' + msgtxt + ')' - self._on_continue_message(obj, callback, msgtxt=msgtxt, msgenc=msgenc) - return - # Encryption failed, do not send message - tim = time.localtime() - gajim.nec.push_incoming_event(MessageNotSentEvent(None, conn=self, - jid=obj.jid, message=msgtxt, error=error, time_=tim, session=obj.session)) - - def _on_continue_message(self, obj, callback, msgtxt=None, msgenc=None): - - if not msgtxt: + else: msgtxt = obj.message + fjid = obj.get_full_jid() if obj.correction_msg: From caba5e17234d52bd7e36a867782d950a0f62ffa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 4 Apr 2017 21:55:16 +0200 Subject: [PATCH 3/5] Rename _on_continue_message() _build_message_stanza is a better fitting name --- src/common/connection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index 497bcbce0..81d50ddde 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -299,7 +299,7 @@ class CommonConnection: self._encrypt_message(obj, callback) return - self._on_continue_message(obj, callback) + self._build_message_stanza(obj, callback) def _encrypt_message(self, obj, callback): obj.xhtml = None @@ -342,9 +342,9 @@ class CommonConnection: None, conn=self, jid=obj.jid, message=obj.message, error=error, time_=time.time(), session=obj.session)) return - self._on_continue_message(obj, callback, msgenc) + self._build_message_stanza(obj, callback, msgenc) - def _on_continue_message(self, obj, callback, msgenc=None): + def _build_message_stanza(self, obj, callback, msgenc=None): if msgenc: msgtxt = '[This message is *encrypted* (See :XEP:`27`]' lang = os.getenv('LANG') From 298ba4d9399b437f749b391371497f717d6d6d69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 4 Apr 2017 23:21:11 +0200 Subject: [PATCH 4/5] Fix Last Message Correction for PGP messages --- src/common/connection.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/common/connection.py b/src/common/connection.py index 81d50ddde..882a42579 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -369,6 +369,13 @@ class CommonConnection: if obj.xhtml: obj.correction_msg.setXHTML(obj.xhtml) + if msgenc: + encrypted_tag = obj.correction_msg.getTag( + 'x', namespace=nbxmpp.NS_ENCRYPTED) + obj.correction_msg.delChild(encrypted_tag) + obj.correction_msg.setTag( + 'x', namespace=nbxmpp.NS_ENCRYPTED).setData(msgenc) + if obj.session: obj.session.last_send = time.time() @@ -398,7 +405,7 @@ class CommonConnection: msg_iq.setID(obj.msg_id) if msgenc: - msg_iq.setTag(nbxmpp.NS_ENCRYPTED + ' x').setData(msgenc) + msg_iq.setTag('x', namespace=nbxmpp.NS_ENCRYPTED).setData(msgenc) if obj.form_node: msg_iq.addChild(node=obj.form_node) From bbb5c0f50d719c447e8b35900e8a535b3ce0b682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Thu, 6 Apr 2017 23:21:19 +0200 Subject: [PATCH 5/5] Simplify sending message - Dont use callbacks, they have no gain in that situation and make the code hard to read - pass the whole object to a new Event instead of many vars --- src/common/connection.py | 108 +++++++++------------ src/common/zeroconf/connection_zeroconf.py | 61 ++++++------ 2 files changed, 75 insertions(+), 94 deletions(-) diff --git a/src/common/connection.py b/src/common/connection.py index 882a42579..271fd239e 100644 --- a/src/common/connection.py +++ b/src/common/connection.py @@ -263,7 +263,7 @@ class CommonConnection: """ raise NotImplementedError - def _prepare_message(self, obj, callback): + def _prepare_message(self, obj): if not self.connection or self.connected < 2: return 1 @@ -296,12 +296,12 @@ class CommonConnection: return if obj.keyID and self.USE_GPG: - self._encrypt_message(obj, callback) + self._encrypt_message(obj) return - self._build_message_stanza(obj, callback) + self._build_message_stanza(obj) - def _encrypt_message(self, obj, callback): + def _encrypt_message(self, obj): obj.xhtml = None if obj.keyID == 'UNKNOWN': error = _('Neither the remote presence is signed, nor a key was ' @@ -321,30 +321,29 @@ class CommonConnection: self.gpg.encrypt, [obj.message, key_list, True], _on_encrypted, []) else: - self._finished_encrypt( - obj, callback, msgenc=msgenc, error=error) + self._finished_encrypt(obj, msgenc=msgenc, + error=error) gajim.nec.push_incoming_event(GPGTrustKeyEvent(None, conn=self, keyID=error.split(' ')[-1], callback=_on_always_trust)) else: - self._finished_encrypt( - obj, callback, msgenc=msgenc, error=error) + self._finished_encrypt(obj, msgenc=msgenc, error=error) gajim.thread_interface( self.gpg.encrypt, [obj.message, key_list, False], _on_encrypted, []) return - self._finished_encrypt(obj, callback, error=error) + self._finished_encrypt(obj, error=error) - def _finished_encrypt(self, obj, callback, msgenc=None, error=None): + def _finished_encrypt(self, obj, msgenc=None, error=None): if error: gajim.nec.push_incoming_event( MessageNotSentEvent( None, conn=self, jid=obj.jid, message=obj.message, error=error, time_=time.time(), session=obj.session)) return - self._build_message_stanza(obj, callback, msgenc) + self._build_message_stanza(obj, msgenc) - def _build_message_stanza(self, obj, callback, msgenc=None): + def _build_message_stanza(self, obj, msgenc=None): if msgenc: msgtxt = '[This message is *encrypted* (See :XEP:`27`]' lang = os.getenv('LANG') @@ -383,11 +382,7 @@ class CommonConnection: if obj.session.enable_encryption: obj.correction_msg = obj.session.encrypt_stanza(obj.correction_msg) - if callback: - callback( - obj.jid, obj.message, obj.keyID, obj.forward_from, - obj.session, obj.original_message, obj.subject, obj.type_, - obj.correction_msg, obj.xhtml) + self._push_stanza_message_outgoing(obj, obj.correction_msg) return if obj.type_ == 'chat': @@ -498,10 +493,19 @@ class CommonConnection: msg_iq.addChild(name='no-store', namespace=nbxmpp.NS_MSG_HINTS) - if callback: - callback(obj.jid, obj.message, obj.keyID, obj.forward_from, - obj.session, obj.original_message, obj.subject, obj.type_, - msg_iq, obj.xhtml) + self._push_stanza_message_outgoing(obj, msg_iq) + + def _push_stanza_message_outgoing(self, obj, msg_iq): + obj.conn = self + if isinstance(msg_iq, list): + for iq in msg_iq: + obj.msg_iq = iq + gajim.nec.push_incoming_event( + StanzaMessageOutgoingEvent(None, **vars(obj))) + else: + obj.msg_iq = msg_iq + 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): @@ -2118,53 +2122,35 @@ class Connection(CommonConnection, ConnectionHandlers): if obj.account != self.name: return - # parameters of this function are packet into _cb_parameters for later usage in _nec_stanza_message_outgoing's cb() - def cb(jid, msg, keyID, forward_from, session, original_message, - subject, type_, msg_iq, xhtml): - if isinstance(msg_iq, list): - for iq in msg_iq: - gajim.nec.push_incoming_event(StanzaMessageOutgoingEvent( - None, conn=self, msg_iq=iq, now=obj.now, automatic_message=obj.automatic_message, additional_data=obj.additional_data, - _cb_parameters={"jid":jid, "msg":msg, "keyID":keyID, "forward_from":forward_from, - "session":session, "original_message":original_message, "subject":subject, "type_":type_, - "msg_iq":msg_iq, "xhtml":xhtml, "obj":obj})) - else: - gajim.nec.push_incoming_event(StanzaMessageOutgoingEvent(None, - conn=self, msg_iq=msg_iq, now=obj.now, automatic_message=obj.automatic_message, additional_data=obj.additional_data, - _cb_parameters={"jid":jid, "msg":msg, "keyID":keyID, "forward_from":forward_from, - "session":session, "original_message":original_message, "subject":subject, "type_":type_, - "msg_iq":msg_iq, "xhtml":xhtml, "obj":obj})) - - - self._prepare_message(obj, cb) + self._prepare_message(obj) def _nec_stanza_message_outgoing(self, obj): if obj.conn.name != self.name: return obj.msg_id = self.connection.send(obj.msg_iq, now=obj.now) - # obj in this function is the obj as seen in _nec_message_outgoing() - def cb(obj, jid, msg, keyID, forward_from, session, original_message, - subject, type_, msg_iq, xhtml, msg_id): - gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self, - jid=jid, message=msg, keyID=keyID, chatstate=obj.chatstate, - automatic_message=obj.automatic_message, msg_id=msg_id, additional_data=obj.additional_data)) - if obj.callback: - obj.callback(obj, msg_iq, *obj.callback_args) + gajim.nec.push_incoming_event(MessageSentEvent( + None, conn=self, jid=obj.jid, message=obj.message, keyID=obj.keyID, + chatstate=obj.chatstate, automatic_message=obj.automatic_message, + msg_id=obj.msg_id, additional_data=obj.additional_data)) + if obj.callback: + obj.callback(obj, obj.msg_iq, *obj.callback_args) - if not obj.is_loggable: - return - if isinstance(jid, list): - for j in jid: - if session is None: - session = self.get_or_create_session(j, '') - self.log_message(j, msg, forward_from, session, - original_message, subject, type_, xhtml, obj.additional_data) - else: - self.log_message(jid, msg, forward_from, session, - original_message, subject, type_, xhtml, obj.additional_data) - - cb(msg_id=obj.msg_id, **obj._cb_parameters) + 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) + 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) 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 3c56bb1e2..6a513d5fc 100644 --- a/src/common/zeroconf/connection_zeroconf.py +++ b/src/common/zeroconf/connection_zeroconf.py @@ -64,8 +64,8 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf): CommonConnection.__init__(self, name) self.is_zeroconf = True - gajim.ged.register_event_handler('message-outgoing', ged.OUT_CORE, - self._nec_message_outgoing) + gajim.ged.register_event_handler('stanza-message-outgoing', ged.OUT_CORE, + self._nec_stanza_message_outgoing) def get_config_values_or_default(self): """ @@ -332,42 +332,37 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf): title=_('Could not change status of account "%s"') % self.name, msg=_('Please check if avahi-daemon is running.'))) - def _nec_message_outgoing(self, obj): - if obj.account != self.name: - return + def _nec_stanza_message_outgoing(self, obj): - def cb(jid, msg, keyID, forward_from, session, original_message, subject, - type_, msg_iq, xhtml): - def on_send_ok(msg_id): - gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self, - jid=obj.jid, message=obj.message, keyID=obj.keyID, - automatic_message=obj.automatic_message, chatstate=None, - msg_id=msg_id)) - if obj.callback: - obj.callback(msg_iq, *obj.callback_args) + def on_send_ok(msg_id): + gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self, + jid=obj.jid, message=obj.message, keyID=obj.keyID, + automatic_message=obj.automatic_message, chatstate=None, + msg_id=msg_id)) + 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_) + 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_) - def on_send_not_ok(reason): - reason += ' ' + _('Your message could not be sent.') - gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self, - fjid=obj.jid, error_code=-1, error_msg=reason, msg=None, - time_=None, session=obj.session)) + def on_send_not_ok(reason): + reason += ' ' + _('Your message could not be sent.') + gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self, + fjid=obj.jid, error_code=-1, error_msg=reason, msg=None, + time_=None, session=obj.session)) - ret = self.connection.send(msg_iq, msg is not None, on_ok=on_send_ok, - on_not_ok=on_send_not_ok) + ret = self.connection.send( + obj.msg_iq, obj.message is not None, + on_ok=on_send_ok, on_not_ok=on_send_not_ok) - if ret == -1: - # Contact Offline - gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self, - fjid=jid, error_code=-1, error_msg=_( - 'Contact is offline. Your message could not be sent.'), - msg=None, time_=None, session=session)) - - self._prepare_message(obj, cb) + if ret == -1: + # Contact Offline + gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self, + fjid=obj.jid, error_code=-1, error_msg=_( + 'Contact is offline. Your message could not be sent.'), + msg=None, time_=None, session=obj.session)) def send_stanza(self, stanza): # send a stanza untouched