changeset 2994:94708a7d3ecf

core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant: an old hardcoded value was used in several places to detect if an entity is a MUC, but this value was not valid anymore. This has been fixed, and ENTITY_TYPE_MUC constant is now used instead. This fixes message type autodetection for "groupchat" messages. fixes 300
author Goffi <goffi@goffi.org>
date Tue, 09 Jul 2019 09:06:45 +0200
parents d58dccd9e4b4
children 5ecce65631a2
files sat/core/constants.py sat/core/xmpp.py sat/memory/memory.py sat/plugins/plugin_exp_parrot.py sat/plugins/plugin_xep_0045.py sat/plugins/plugin_xep_0085.py
diffstat 6 files changed, 39 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/sat/core/constants.py	Mon Jul 08 19:11:29 2019 +0200
+++ b/sat/core/constants.py	Tue Jul 09 09:06:45 2019 +0200
@@ -94,7 +94,8 @@
     ENTITY_ALL_RESOURCES = "@ALL_RESOURCES@"
     ENTITY_MAIN_RESOURCE = "@MAIN_RESOURCE@"
     ENTITY_CAP_HASH = "CAP_HASH"
-    ENTITY_TYPE = "TYPE"
+    ENTITY_TYPE = "type"
+    ENTITY_TYPE_MUC = "MUC"
 
     ## Roster jids selection ##
     PUBLIC = "PUBLIC"
--- a/sat/core/xmpp.py	Mon Jul 08 19:11:29 2019 +0200
+++ b/sat/core/xmpp.py	Tue Jul 09 09:06:45 2019 +0200
@@ -566,14 +566,14 @@
                                            # the type is not 'groupchat'
                 # we may have a groupchat message, we check if the we know this jid
                 try:
-                    entity_type = self.host_app.memory.getEntityData(
-                        data["to"], ["type"], self.profile
-                    )["type"]
+                    entity_type = self.host_app.memory.getEntityDatum(
+                        data["to"], C.ENTITY_TYPE, self.profile
+                    )
                     # FIXME: should entity_type manage resources ?
                 except (exceptions.UnknownEntityError, KeyError):
                     entity_type = "contact"
 
-                if entity_type == "chatroom":
+                if entity_type == C.ENTITY_TYPE_MUC:
                     data["type"] = C.MESS_TYPE_GROUPCHAT
                 else:
                     data["type"] = C.MESS_TYPE_CHAT
--- a/sat/memory/memory.py	Mon Jul 08 19:11:29 2019 +0200
+++ b/sat/memory/memory.py	Tue Jul 09 09:06:45 2019 +0200
@@ -849,11 +849,12 @@
         """Set a misc data for an entity
 
         If key was registered with setSignalOnUpdate, a signal will be sent to frontends
-        @param entity_jid: JID of the entity, C.ENTITY_ALL_RESOURCES for all resources of all entities,
-                           C.ENTITY_ALL for all entities (all resources + bare jids)
-        @param key: key to set (eg: "type")
-        @param value: value for this key (eg: "chatroom")
-        @param silent(bool): if True, doesn't send signal to frontend, even if there is a signal flag (see setSignalOnUpdate)
+        @param entity_jid: JID of the entity, C.ENTITY_ALL_RESOURCES for all resources of
+            all entities, C.ENTITY_ALL for all entities (all resources + bare jids)
+        @param key: key to set (eg: C.ENTITY_TYPE)
+        @param value: value for this key (eg: C.ENTITY_TYPE_MUC)
+        @param silent(bool): if True, doesn't send signal to frontend, even if there is a
+            signal flag (see setSignalOnUpdate)
         @param profile_key: %(doc_profile_key)s
         """
         client = self.host.getClient(profile_key)
@@ -886,7 +887,7 @@
 
         @param entity_jid: JID of the entity, C.ENTITY_ALL_RESOURCES for all resources of all entities,
                            C.ENTITY_ALL for all entities (all resources + bare jids)
-        @param key: key to delete (eg: "type")
+        @param key: key to delete (eg: C.ENTITY_TYPE)
         @param profile_key: %(doc_profile_key)s
 
         @raise exceptions.UnknownEntityError: if entity is not in cache
--- a/sat/plugins/plugin_exp_parrot.py	Mon Jul 08 19:11:29 2019 +0200
+++ b/sat/plugins/plugin_exp_parrot.py	Tue Jul 09 09:06:45 2019 +0200
@@ -94,12 +94,11 @@
             lang = e.getAttribute("lang") or ""
 
             try:
-                entity_type = self.host.memory.getEntityData(from_jid, ["type"], profile)[
-                    "type"
-                ]
+                entity_type = self.host.memory.getEntityData(
+                    from_jid, [C.ENTITY_TYPE], profile)[C.ENTITY_TYPE]
             except (UnknownEntityError, KeyError):
                 entity_type = "contact"
-            if entity_type == "chatroom":
+            if entity_type == C.ENTITY_TYPE_MUC:
                 src_txt = from_jid.resource
                 if src_txt == self.host.plugins["XEP-0045"].getRoomNick(
                     client, from_jid.userhostJID()
--- a/sat/plugins/plugin_xep_0045.py	Mon Jul 08 19:11:29 2019 +0200
+++ b/sat/plugins/plugin_xep_0045.py	Tue Jul 09 09:06:45 2019 +0200
@@ -58,7 +58,6 @@
 ROOM_USER_JOINED = 'ROOM_USER_JOINED'
 ROOM_USER_LEFT = 'ROOM_USER_LEFT'
 OCCUPANT_KEYS = ('nick', 'entity', 'affiliation', 'role')
-ENTITY_TYPE_MUC = "MUC"
 ROOM_STATE_OCCUPANTS = "occupants"
 ROOM_STATE_SELF_PRESENCE = "self-presence"
 ROOM_STATE_LIVE = "live"
@@ -1039,17 +1038,24 @@
 
     def userJoinedRoom(self, room, user):
         if user.nick == room.nick:
-            # we have received our own nick, this mean that the full room roster was received
+            # we have received our own nick,
+            # this mean that the full room roster was received
             self.changeRoomState(room, ROOM_STATE_SELF_PRESENCE)
-            log.debug(u"room {room} joined with nick {nick}".format(room=room.occupantJID.userhost(), nick=user.nick))
-            # We set type so we don't have to use a deferred with disco to check entity type
-            self.host.memory.updateEntityData(room.roomJID, C.ENTITY_TYPE, ENTITY_TYPE_MUC, profile_key=self.client.profile)
+            log.debug(u"room {room} joined with nick {nick}".format(
+                room=room.occupantJID.userhost(), nick=user.nick))
+            # we set type so we don't have to use a deferred
+            # with disco to check entity type
+            self.host.memory.updateEntityData(
+                room.roomJID, C.ENTITY_TYPE, C.ENTITY_TYPE_MUC,
+                profile_key=self.client.profile)
         elif room.state not in (ROOM_STATE_OCCUPANTS, ROOM_STATE_LIVE):
-            log.warning(u"Received user presence data in a room before its initialisation (current state: {state}),"
-                "this is not standard! Ignoring it: {room} ({nick})".format(
-                state=room.state,
-                room=room.roomJID.userhost(),
-                nick=user.nick))
+            log.warning(
+                u"Received user presence data in a room before its initialisation "
+                u"(current state: {state}),"
+                u"this is not standard! Ignoring it: {room} ({nick})".format(
+                    state=room.state,
+                    room=room.roomJID.userhost(),
+                    nick=user.nick))
             return
         else:
             if not room.fully_joined.called:
@@ -1058,8 +1064,10 @@
                 self._changing_nicks.remove(user.nick)
             except KeyError:
                 # this is a new user
-                log.debug(_(u"user {nick} has joined room {room_id}").format(nick=user.nick, room_id=room.occupantJID.userhost()))
-                if not self.host.trigger.point("MUC user joined", room, user, self.client.profile):
+                log.debug(_(u"user {nick} has joined room {room_id}").format(
+                    nick=user.nick, room_id=room.occupantJID.userhost()))
+                if not self.host.trigger.point(
+                        "MUC user joined", room, user, self.client.profile):
                     return
 
                 extra = {'info_type': ROOM_USER_JOINED,
--- a/sat/plugins/plugin_xep_0085.py	Mon Jul 08 19:11:29 2019 +0200
+++ b/sat/plugins/plugin_xep_0085.py	Tue Jul 09 09:06:45 2019 +0200
@@ -257,8 +257,9 @@
         @return: bool
         """
         try:
-            type_ = self.host.memory.getEntityDatum(to_jid.userhostJID(), "type", profile)
-            if type_ == "chatroom":  # FIXME: should not use disco instead ?
+            type_ = self.host.memory.getEntityDatum(
+                to_jid.userhostJID(), C.ENTITY_TYPE, profile)
+            if type_ == C.ENTITY_TYPE_MUC:
                 return True
         except (exceptions.UnknownEntityError, KeyError):
             pass