backend, frontends: message refactoring (huge commit):

/!\ several features are temporarily disabled, like notifications in frontends

next step in refactoring, with the following changes:
    - jp: updated jp message to follow changes in backend/bridge
    - jp: added --lang, --subject, --subject_lang, and --type options to jp message + fixed unicode handling for jid
    - quick_frontend (QuickApp, QuickChat):
        - follow backend changes
        - refactored chat, message are now handled in OrderedDict and uid are kept so they can be updated
        - Message and Occupant classes handle metadata, so frontend just have to display them
    - Primitivus (Chat):
        - follow backend/QuickFrontend changes
        - info & standard messages are handled in the same MessageWidget class
        - improved/simplified handling of messages, removed update() method
        - user joined/left messages are merged when next to each other
        - a separator is shown when message is received while widget is out of focus, so user can quickly see the new messages
        - affiliation/role are shown (in a basic way for now) in occupants panel
        - removed "/me" messages handling, as it will be done by a backend plugin
        - message language is displayed when available (only one language per message for now)
        - fixed :history and :search commands
    - core (constants): new constants for messages type, XML namespace, entity type
    - core: *Message methods renamed to follow new code sytle (e.g. sendMessageToBridge => messageSendToBridge)
    - core (messages handling): fixed handling of language
    - core (messages handling): mes_data['from'] and ['to'] are now jid.JID
    - core (core.xmpp): reorganised message methods, added getNick() method to client.roster
    - plugin text commands: fixed plugin and adapted to new messages behaviour. client is now used in arguments instead of profile
    - plugins: added information for cancellation reason in CancelError calls
    - plugin XEP-0045: various improvments, but this plugin still need work:
        - trigger is used to avoid message already handled by the plugin to be handled a second time
        - changed the way to handle history, the last message from DB is checked and we request only messages since this one, in seconds (thanks Poezio folks :))
        - subject reception is waited before sending the roomJoined signal, this way we are sure that everything including history is ready
        - cmd_* method now follow the new convention with client instead of profile
        - roomUserJoined and roomUserLeft messages are removed, the events are now handled with info message with a "ROOM_USER_JOINED" info subtype
    - probably other forgotten stuffs :p
This commit is contained in:
Goffi 2016-06-20 18:41:53 +02:00
commit a7de473107
3 changed files with 987 additions and 598 deletions

View file

@ -82,6 +82,7 @@ class Const(object):
ENTITY_ALL_RESOURCES = '@ALL_RESOURCES@'
ENTITY_MAIN_RESOURCE = '@MAIN_RESOURCE@'
ENTITY_CAP_HASH = 'CAP_HASH'
ENTITY_TYPE = 'TYPE'
## Roster jids selection ##
@ -98,6 +99,11 @@ class Const(object):
MESS_TYPE_GROUPCHAT = 'groupchat'
MESS_TYPE_HEADLINE = 'headline'
MESS_TYPE_NORMAL = 'normal'
MESS_TYPE_AUTO = 'auto' # magic value to let the backend guess the type
MESS_TYPE_STANDARD = (MESS_TYPE_CHAT, MESS_TYPE_ERROR, MESS_TYPE_GROUPCHAT, MESS_TYPE_HEADLINE, MESS_TYPE_NORMAL)
MESS_TYPE_ALL = MESS_TYPE_STANDARD + (MESS_TYPE_INFO, MESS_TYPE_AUTO)
MESS_EXTRA_INFO = "info_type"
## Presence ##
@ -113,6 +119,7 @@ class Const(object):
## Common namespaces ##
NS_XML = 'http://www.w3.org/XML/1998/namespace'
NS_CLIENT = 'jabber:client'
NS_FORWARD = 'urn:xmpp:forward:0'
NS_DELAY = 'urn:xmpp:delay'

View file

@ -95,7 +95,7 @@ class SAT(service.Service):
self.bridge.register("getParamsUI", self.memory.getParamsUI)
self.bridge.register("getParamsCategories", self.memory.getParamsCategories)
self.bridge.register("paramsRegisterApp", self.memory.paramsRegisterApp)
self.bridge.register("historyGet", self.memory.historyGet)
self.bridge.register("historyGet", self.memory._historyGet)
self.bridge.register("setPresence", self._setPresence)
self.bridge.register("subscription", self.subscription)
self.bridge.register("addContact", self._addContact)
@ -553,11 +553,11 @@ class SAT(service.Service):
for lang, subject in data["subject"].iteritems():
subject_elt = message_elt.addElement("subject", content=subject)
if lang:
subject_elt['xml:lang'] = lang
subject_elt[(C.NS_XML, 'lang')] = lang
for lang, message in data["message"].iteritems():
body_elt = message_elt.addElement("body", content=message)
if lang:
body_elt['xml:lang'] = lang
body_elt[(C.NS_XML, 'lang')] = lang
try:
thread = data['extra']['thread']
except KeyError:
@ -646,21 +646,21 @@ class SAT(service.Service):
pre_xml_treatments.addCallback(lambda dummy: self.generateMessageXML(data))
pre_xml_treatments.chainDeferred(post_xml_treatments)
post_xml_treatments.addCallback(self._sendMessageToStream, client)
post_xml_treatments.addCallback(self.messageSendToStream, client)
if send_only:
log.debug(_("Triggers, storage and echo have been inhibited by the 'send_only' parameter"))
else:
post_xml_treatments.addCallback(self._storeMessage, client)
post_xml_treatments.addCallback(self.sendMessageToBridge, client)
post_xml_treatments.addCallback(self.messageAddToHistory, client)
post_xml_treatments.addCallback(self.messageSendToBridge, client)
post_xml_treatments.addErrback(self._cancelErrorTrap)
pre_xml_treatments.callback(data)
return pre_xml_treatments
def _cancelErrorTrap(failure):
def _cancelErrorTrap(self, failure):
"""A message sending can be cancelled by a plugin treatment"""
failure.trap(exceptions.CancelError)
def _sendMessageToStream(self, data, client):
def messageSendToStream(self, data, client):
"""Actualy send the message to the server
@param data: message data dictionnary
@ -669,7 +669,7 @@ class SAT(service.Service):
client.xmlstream.send(data['xml'])
return data
def _storeMessage(self, data, client):
def messageAddToHistory(self, data, client):
"""Store message into database (for local history)
@param data: message data dictionnary
@ -678,13 +678,13 @@ class SAT(service.Service):
if data["type"] != C.MESS_TYPE_GROUPCHAT:
# we don't add groupchat message to history, as we get them back
# and they will be added then
if data['message']: # we need a message to save something
if data['message'] or data['subject']: # we need a message to store
self.memory.addToHistory(client, data)
else:
log.warning(u"No message found") # empty body should be managed by plugins before this point
return data
def sendMessageToBridge(self, data, client):
def messageSendToBridge(self, data, client):
"""Send message to bridge, so frontends can display it
@param data: message data dictionnary
@ -693,7 +693,7 @@ class SAT(service.Service):
if data["type"] != C.MESS_TYPE_GROUPCHAT:
# we don't send groupchat message to bridge, as we get them back
# and they will be added the
if data['message']: # we need a message to send something
if data['message'] or data['subject']: # we need a message to send something
# We send back the message, so all frontends are aware of it
self.bridge.messageNew(data['uid'], data['timestamp'], data['from'].full(), data['to'].full(), data['message'], data['subject'], data['type'], data['extra'], profile=client.profile)
else:
@ -1039,6 +1039,7 @@ class SAT(service.Service):
def importMenu(self, path, callback, security_limit=C.NO_SECURITY_LIMIT, help_string="", type_=C.MENU_GLOBAL):
"""register a new menu for frontends
@param path: path to go to the menu (category/subcategory/.../item), must be an iterable (e.g.: ("File", "Open"))
/!\ use D_() instead of _() for translations (e.g. (D_("File"), D_("Open")))
@param callback: method to be called when menuitem is selected, callable or a callback id (string) as returned by [registerCallback]

View file

@ -25,7 +25,7 @@ from twisted.words.protocols.jabber import xmlstream
from twisted.words.protocols.jabber import error
from twisted.words.protocols.jabber import jid
from twisted.python import failure
from wokkel import client, disco, xmppim, generic, iwokkel
from wokkel import client as wokkel_client, disco, xmppim, generic, iwokkel
from wokkel import delay
from sat.core.log import getLogger
log = getLogger(__name__)
@ -36,13 +36,13 @@ import calendar
import uuid
class SatXMPPClient(client.XMPPClient):
class SatXMPPClient(wokkel_client.XMPPClient):
implements(iwokkel.IDisco)
def __init__(self, host_app, profile, user_jid, password, host=None, port=C.XMPP_C2S_PORT, max_retries=C.XMPP_MAX_RETRIES):
# XXX: DNS SRV records are checked when the host is not specified.
# If no SRV record is found, the host is directly extracted from the JID.
client.XMPPClient.__init__(self, user_jid, password, host or None, port or C.XMPP_C2S_PORT)
wokkel_client.XMPPClient.__init__(self, user_jid, password, host or None, port or C.XMPP_C2S_PORT)
self.factory.clientConnectionLost = self.connectionLost
self.factory.maxRetries = max_retries
self.__connected = False
@ -80,7 +80,7 @@ class SatXMPPClient(client.XMPPClient):
def _authd(self, xmlstream):
if not self.host_app.trigger.point("XML Initialized", xmlstream, self.profile):
return
client.XMPPClient._authd(self, xmlstream)
wokkel_client.XMPPClient._authd(self, xmlstream)
self.__connected = True
log.info(_("********** [%s] CONNECTED **********") % self.profile)
self.streamInitialized()
@ -112,7 +112,7 @@ class SatXMPPClient(client.XMPPClient):
log.error(_(u"ERROR: XMPP connection failed for profile '%(profile)s': %(reason)s" % {'profile': self.profile, 'reason': reason}))
self.conn_deferred.errback(reason.value)
try:
client.XMPPClient.initializationFailed(self, reason)
wokkel_client.XMPPClient.initializationFailed(self, reason)
except:
# we already chained an errback, no need to raise an exception
pass
@ -152,8 +152,8 @@ class SatMessageProtocol(xmppim.MessageProtocol):
message = {}
subject = {}
extra = {}
data = {"from": message_elt['from'],
"to": message_elt['to'],
data = {"from": jid.JID(message_elt['from']),
"to": jid.JID(message_elt['to']),
"uid": message_elt.getAttribute('uid', unicode(uuid.uuid4())), # XXX: uid is not a standard attribute but may be added by plugins
"message": message,
"subject": subject,
@ -169,11 +169,11 @@ class SatMessageProtocol(xmppim.MessageProtocol):
# message
for e in message_elt.elements(C.NS_CLIENT, 'body'):
message[e.getAttribute('xml:lang','')] = unicode(e)
message[e.getAttribute((C.NS_XML,'lang'),'')] = unicode(e)
# subject
for e in message_elt.elements(C.NS_CLIENT, 'subject'):
subject[e.getAttribute('xml:lang','')] = unicode(e)
subject[e.getAttribute((C.NS_XML, 'lang'),'')] = unicode(e)
# delay and timestamp
try:
@ -187,42 +187,39 @@ class SatMessageProtocol(xmppim.MessageProtocol):
if parsed_delay.sender:
data['delay_sender'] = parsed_delay.sender.full()
def skipEmptyMessage(data):
if not data['message'] and not data['extra']:
raise failure.Failure(exceptions.CancelError())
return data
def bridgeSignal(data):
try:
data['extra']['received_timestamp'] = data['received_timestamp']
data['extra']['delay_sender'] = data['delay_sender']
except KeyError:
pass
if data is not None:
self.host.bridge.messageNew(data['uid'], data['timestamp'], data['from'].full(), data['to'].full(), data['message'], data['subject'], data['type'], data['extra'], profile=client.profile)
return data
def addToHistory(data):
data['from'] = jid.JID(data['from'])
data['to'] = jid.JID(data['to'])
self.host.memory.addToHistory(client, data)
return data
def treatmentsEb(failure_):
failure_.trap(exceptions.SkipHistory)
return data
def cancelErrorTrap(failure_):
"""A message sending can be cancelled by a plugin treatment"""
failure_.trap(exceptions.CancelError)
post_treat.addCallback(skipEmptyMessage)
post_treat.addCallback(addToHistory)
post_treat.addErrback(treatmentsEb)
post_treat.addCallback(bridgeSignal)
post_treat.addErrback(cancelErrorTrap)
post_treat.addCallback(self.skipEmptyMessage)
post_treat.addCallback(self.addToHistory, client)
post_treat.addErrback(self.treatmentsEb)
post_treat.addCallback(self.bridgeSignal, client, data)
post_treat.addErrback(self.cancelErrorTrap)
post_treat.callback(data)
def skipEmptyMessage(self, data):
if not data['message'] and not data['extra'] and not data['subject']:
raise failure.Failure(exceptions.CancelError("Cancelled empty message"))
return data
def addToHistory(self, data, client):
return self.host.memory.addToHistory(client, data)
def treatmentsEb(self, failure_):
failure_.trap(exceptions.SkipHistory)
def bridgeSignal(self, dummy, client, data):
try:
data['extra']['received_timestamp'] = data['received_timestamp']
data['extra']['delay_sender'] = data['delay_sender']
except KeyError:
pass
if data is not None:
self.host.bridge.messageNew(data['uid'], data['timestamp'], data['from'].full(), data['to'].full(), data['message'], data['subject'], data['type'], data['extra'], profile=client.profile)
return data
def cancelErrorTrap(self, failure_):
"""A message sending can be cancelled by a plugin treatment"""
failure_.trap(exceptions.CancelError)
class SatRosterProtocol(xmppim.RosterClientProtocol):
@ -283,8 +280,10 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def getAttributes(self, item):
"""Return dictionary of attributes as used in bridge from a RosterItem
@param item: RosterItem
@return: dictionary of attributes"""
@return: dictionary of attributes
"""
item_attr = {'to': unicode(item.subscriptionTo),
'from': unicode(item.subscriptionFrom),
'ask': unicode(item.ask)
@ -339,8 +338,9 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
def getItem(self, entity_jid):
"""Return RosterItem for a given jid
@param entity_jid: jid of the contact
@return: RosterItem or None if contact is not in cache
@param entity_jid(jid.JID): jid of the contact
@return(RosterItem, None): RosterItem instance
None if contact is not in cache
"""
return self._jids.get(entity_jid, None)
@ -384,6 +384,18 @@ class SatRosterProtocol(xmppim.RosterClientProtocol):
else:
raise ValueError(u'Unexpected type_ {}'.format(type_))
def getNick(self, entity_jid):
"""Return a nick name for an entity
return nick choosed by user if available
else return user part of entity_jid
"""
item = self.getItem(entity_jid)
if item is None:
return entity_jid.user
else:
return item.name or entity_jid.user
class SatPresenceProtocol(xmppim.PresenceClientProtocol):
@ -474,7 +486,9 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
return
self.send(presence_elt)
@defer.inlineCallbacks
def subscribed(self, entity):
yield self.parent.roster.got_roster
xmppim.PresenceClientProtocol.subscribed(self, entity)
self.host.memory.delWaitingSub(entity.userhost(), self.parent.profile)
item = self.parent.roster.getItem(entity)
@ -494,8 +508,10 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
log.debug(_(u"unsubscription confirmed for [%s]") % entity.userhost())
self.host.bridge.subscribe('unsubscribed', entity.userhost(), self.parent.profile)
@defer.inlineCallbacks
def subscribeReceived(self, entity):
log.debug(_(u"subscription request from [%s]") % entity.userhost())
yield self.parent.roster.got_roster
item = self.parent.roster.getItem(entity)
if item and item.subscriptionTo:
# We automatically accept subscription if we are already subscribed to contact presence
@ -505,8 +521,10 @@ class SatPresenceProtocol(xmppim.PresenceClientProtocol):
self.host.memory.addWaitingSub('subscribe', entity.userhost(), self.parent.profile)
self.host.bridge.subscribe('subscribe', entity.userhost(), self.parent.profile)
@defer.inlineCallbacks
def unsubscribeReceived(self, entity):
log.debug(_(u"unsubscription asked for [%s]") % entity.userhost())
yield self.parent.roster.got_roster
item = self.parent.roster.getItem(entity)
if item and item.subscriptionFrom: # we automatically remove contact
log.debug(_('automatic contact deletion'))