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
This commit is contained in:
parent
298ba4d939
commit
bbb5c0f50d
2 changed files with 75 additions and 94 deletions
|
@ -263,7 +263,7 @@ class CommonConnection:
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def _prepare_message(self, obj, callback):
|
def _prepare_message(self, obj):
|
||||||
|
|
||||||
if not self.connection or self.connected < 2:
|
if not self.connection or self.connected < 2:
|
||||||
return 1
|
return 1
|
||||||
|
@ -296,12 +296,12 @@ class CommonConnection:
|
||||||
return
|
return
|
||||||
|
|
||||||
if obj.keyID and self.USE_GPG:
|
if obj.keyID and self.USE_GPG:
|
||||||
self._encrypt_message(obj, callback)
|
self._encrypt_message(obj)
|
||||||
return
|
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
|
obj.xhtml = None
|
||||||
if obj.keyID == 'UNKNOWN':
|
if obj.keyID == 'UNKNOWN':
|
||||||
error = _('Neither the remote presence is signed, nor a key was '
|
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],
|
self.gpg.encrypt, [obj.message, key_list, True],
|
||||||
_on_encrypted, [])
|
_on_encrypted, [])
|
||||||
else:
|
else:
|
||||||
self._finished_encrypt(
|
self._finished_encrypt(obj, msgenc=msgenc,
|
||||||
obj, callback, msgenc=msgenc, error=error)
|
error=error)
|
||||||
gajim.nec.push_incoming_event(GPGTrustKeyEvent(None,
|
gajim.nec.push_incoming_event(GPGTrustKeyEvent(None,
|
||||||
conn=self, keyID=error.split(' ')[-1],
|
conn=self, keyID=error.split(' ')[-1],
|
||||||
callback=_on_always_trust))
|
callback=_on_always_trust))
|
||||||
else:
|
else:
|
||||||
self._finished_encrypt(
|
self._finished_encrypt(obj, msgenc=msgenc, error=error)
|
||||||
obj, callback, msgenc=msgenc, error=error)
|
|
||||||
gajim.thread_interface(
|
gajim.thread_interface(
|
||||||
self.gpg.encrypt, [obj.message, key_list, False],
|
self.gpg.encrypt, [obj.message, key_list, False],
|
||||||
_on_encrypted, [])
|
_on_encrypted, [])
|
||||||
return
|
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:
|
if error:
|
||||||
gajim.nec.push_incoming_event(
|
gajim.nec.push_incoming_event(
|
||||||
MessageNotSentEvent(
|
MessageNotSentEvent(
|
||||||
None, conn=self, jid=obj.jid, message=obj.message,
|
None, conn=self, jid=obj.jid, message=obj.message,
|
||||||
error=error, time_=time.time(), session=obj.session))
|
error=error, time_=time.time(), session=obj.session))
|
||||||
return
|
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:
|
if msgenc:
|
||||||
msgtxt = '[This message is *encrypted* (See :XEP:`27`]'
|
msgtxt = '[This message is *encrypted* (See :XEP:`27`]'
|
||||||
lang = os.getenv('LANG')
|
lang = os.getenv('LANG')
|
||||||
|
@ -383,11 +382,7 @@ class CommonConnection:
|
||||||
if obj.session.enable_encryption:
|
if obj.session.enable_encryption:
|
||||||
obj.correction_msg = obj.session.encrypt_stanza(obj.correction_msg)
|
obj.correction_msg = obj.session.encrypt_stanza(obj.correction_msg)
|
||||||
|
|
||||||
if callback:
|
self._push_stanza_message_outgoing(obj, obj.correction_msg)
|
||||||
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
|
return
|
||||||
|
|
||||||
if obj.type_ == 'chat':
|
if obj.type_ == 'chat':
|
||||||
|
@ -498,10 +493,19 @@ class CommonConnection:
|
||||||
msg_iq.addChild(name='no-store',
|
msg_iq.addChild(name='no-store',
|
||||||
namespace=nbxmpp.NS_MSG_HINTS)
|
namespace=nbxmpp.NS_MSG_HINTS)
|
||||||
|
|
||||||
if callback:
|
self._push_stanza_message_outgoing(obj, msg_iq)
|
||||||
callback(obj.jid, obj.message, obj.keyID, obj.forward_from,
|
|
||||||
obj.session, obj.original_message, obj.subject, obj.type_,
|
def _push_stanza_message_outgoing(self, obj, msg_iq):
|
||||||
msg_iq, obj.xhtml)
|
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,
|
def log_message(self, jid, msg, forward_from, session, original_message,
|
||||||
subject, type_, xhtml=None, additional_data=None):
|
subject, type_, xhtml=None, additional_data=None):
|
||||||
|
@ -2118,53 +2122,35 @@ class Connection(CommonConnection, ConnectionHandlers):
|
||||||
if obj.account != self.name:
|
if obj.account != self.name:
|
||||||
return
|
return
|
||||||
|
|
||||||
# parameters of this function are packet into _cb_parameters for later usage in _nec_stanza_message_outgoing's cb()
|
self._prepare_message(obj)
|
||||||
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)
|
|
||||||
|
|
||||||
def _nec_stanza_message_outgoing(self, obj):
|
def _nec_stanza_message_outgoing(self, obj):
|
||||||
if obj.conn.name != self.name:
|
if obj.conn.name != self.name:
|
||||||
return
|
return
|
||||||
obj.msg_id = self.connection.send(obj.msg_iq, now=obj.now)
|
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()
|
gajim.nec.push_incoming_event(MessageSentEvent(
|
||||||
def cb(obj, jid, msg, keyID, forward_from, session, original_message,
|
None, conn=self, jid=obj.jid, message=obj.message, keyID=obj.keyID,
|
||||||
subject, type_, msg_iq, xhtml, msg_id):
|
chatstate=obj.chatstate, automatic_message=obj.automatic_message,
|
||||||
gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self,
|
msg_id=obj.msg_id, additional_data=obj.additional_data))
|
||||||
jid=jid, message=msg, keyID=keyID, chatstate=obj.chatstate,
|
if obj.callback:
|
||||||
automatic_message=obj.automatic_message, msg_id=msg_id, additional_data=obj.additional_data))
|
obj.callback(obj, obj.msg_iq, *obj.callback_args)
|
||||||
if obj.callback:
|
|
||||||
obj.callback(obj, msg_iq, *obj.callback_args)
|
|
||||||
|
|
||||||
if not obj.is_loggable:
|
if not obj.is_loggable:
|
||||||
return
|
return
|
||||||
if isinstance(jid, list):
|
if isinstance(obj.jid, list):
|
||||||
for j in jid:
|
for j in obj.jid:
|
||||||
if session is None:
|
if obj.session is None:
|
||||||
session = self.get_or_create_session(j, '')
|
obj.session = self.get_or_create_session(j, '')
|
||||||
self.log_message(j, msg, forward_from, session,
|
self.log_message(
|
||||||
original_message, subject, type_, xhtml, obj.additional_data)
|
j, obj.message, obj.forward_from, obj.session,
|
||||||
else:
|
obj.original_message, obj.subject, obj.type_, obj.xhtml,
|
||||||
self.log_message(jid, msg, forward_from, session,
|
obj.additional_data)
|
||||||
original_message, subject, type_, xhtml, obj.additional_data)
|
else:
|
||||||
|
self.log_message(
|
||||||
cb(msg_id=obj.msg_id, **obj._cb_parameters)
|
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'):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -64,8 +64,8 @@ class ConnectionZeroconf(CommonConnection, ConnectionHandlersZeroconf):
|
||||||
CommonConnection.__init__(self, name)
|
CommonConnection.__init__(self, name)
|
||||||
self.is_zeroconf = True
|
self.is_zeroconf = True
|
||||||
|
|
||||||
gajim.ged.register_event_handler('message-outgoing', ged.OUT_CORE,
|
gajim.ged.register_event_handler('stanza-message-outgoing', ged.OUT_CORE,
|
||||||
self._nec_message_outgoing)
|
self._nec_stanza_message_outgoing)
|
||||||
|
|
||||||
def get_config_values_or_default(self):
|
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,
|
title=_('Could not change status of account "%s"') % self.name,
|
||||||
msg=_('Please check if avahi-daemon is running.')))
|
msg=_('Please check if avahi-daemon is running.')))
|
||||||
|
|
||||||
def _nec_message_outgoing(self, obj):
|
def _nec_stanza_message_outgoing(self, obj):
|
||||||
if obj.account != self.name:
|
|
||||||
return
|
|
||||||
|
|
||||||
def cb(jid, msg, keyID, forward_from, session, original_message, subject,
|
def on_send_ok(msg_id):
|
||||||
type_, msg_iq, xhtml):
|
gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self,
|
||||||
def on_send_ok(msg_id):
|
jid=obj.jid, message=obj.message, keyID=obj.keyID,
|
||||||
gajim.nec.push_incoming_event(MessageSentEvent(None, conn=self,
|
automatic_message=obj.automatic_message, chatstate=None,
|
||||||
jid=obj.jid, message=obj.message, keyID=obj.keyID,
|
msg_id=msg_id))
|
||||||
automatic_message=obj.automatic_message, chatstate=None,
|
if obj.callback:
|
||||||
msg_id=msg_id))
|
obj.callback(obj.msg_iq, *obj.callback_args)
|
||||||
if obj.callback:
|
|
||||||
obj.callback(msg_iq, *obj.callback_args)
|
|
||||||
|
|
||||||
if not obj.is_loggable:
|
if not obj.is_loggable:
|
||||||
return
|
return
|
||||||
self.log_message(obj.jid, obj.message, obj.forward_from,
|
self.log_message(obj.jid, obj.message, obj.forward_from,
|
||||||
obj.session, obj.original_message, obj.subject, obj.type_)
|
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.')
|
||||||
gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
|
gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
|
||||||
fjid=obj.jid, error_code=-1, error_msg=reason, msg=None,
|
fjid=obj.jid, error_code=-1, error_msg=reason, msg=None,
|
||||||
time_=None, session=obj.session))
|
time_=None, session=obj.session))
|
||||||
|
|
||||||
ret = self.connection.send(msg_iq, msg is not None, on_ok=on_send_ok,
|
ret = self.connection.send(
|
||||||
on_not_ok=on_send_not_ok)
|
obj.msg_iq, obj.message is not None,
|
||||||
|
on_ok=on_send_ok, on_not_ok=on_send_not_ok)
|
||||||
|
|
||||||
if ret == -1:
|
if ret == -1:
|
||||||
# Contact Offline
|
# Contact Offline
|
||||||
gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
|
gajim.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
|
||||||
fjid=jid, error_code=-1, error_msg=_(
|
fjid=obj.jid, error_code=-1, error_msg=_(
|
||||||
'Contact is offline. Your message could not be sent.'),
|
'Contact is offline. Your message could not be sent.'),
|
||||||
msg=None, time_=None, session=session))
|
msg=None, time_=None, session=obj.session))
|
||||||
|
|
||||||
self._prepare_message(obj, cb)
|
|
||||||
|
|
||||||
def send_stanza(self, stanza):
|
def send_stanza(self, stanza):
|
||||||
# send a stanza untouched
|
# send a stanza untouched
|
||||||
|
|
Loading…
Add table
Reference in a new issue