Merge branch 'master' into 'master'

Sending Message Refactoring

See merge request !80
This commit is contained in:
Philipp Hörist 2017-04-08 18:38:55 +02:00
commit 8a789763a2
3 changed files with 187 additions and 233 deletions

View File

@ -263,232 +263,202 @@ 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):
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':
error = _('Neither the remote presence is signed, nor a key was '
'assigned.')
elif keyID.endswith('MISMATCH'):
error = _('The contact\'s key (%s) does not match the key assigned '
'in Gajim.' % 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, [msg, 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)
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],
_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)
if obj.keyID and self.USE_GPG:
self._encrypt_message(obj)
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._build_message_stanza(obj)
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):
msgenc, error = output
def _encrypt_message(self, obj):
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, 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, msgenc=msgenc, error=error)
gajim.thread_interface(
self.gpg.encrypt, [obj.message, key_list, False],
_on_encrypted, [])
return
self._finished_encrypt(obj, error=error)
if msgenc and not error:
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, msgenc)
def _build_message_stanza(self, obj, 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(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)
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))
else:
msgtxt = obj.message
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):
fjid = obj.get_full_jid()
if correction_msg:
id_ = correction_msg.getID()
if correction_msg.getTag('replace'):
correction_msg.delChild('replace')
correction_msg.setTag('replace', attrs={'id': id_},
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 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()
# 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)
self._push_stanza_message_outgoing(obj, obj.correction_msg)
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)
msg_iq.setTag('x', namespace=nbxmpp.NS_ENCRYPTED).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
# <body> 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 +468,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)
@ -523,9 +493,19 @@ class CommonConnection:
msg_iq.addChild(name='no-store',
namespace=nbxmpp.NS_MSG_HINTS)
if callback:
callback(jid, msg, keyID, forward_from, session, original_message,
subject, type_, msg_iq, 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):
@ -2142,58 +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.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)
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'):
"""

View File

@ -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

View File

@ -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,47 +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.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)
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