diff src/plugins/plugin_xep_0085.py @ 654:5c5cf5bca240

plugin XEP-0085: improvement for sending "composing" state
author souliane <souliane@mailoo.org>
date Thu, 26 Sep 2013 10:44:57 +0200
parents 262d9d9ad27a
children b6c22d9f593a
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0085.py	Thu Sep 26 10:14:23 2013 +0200
+++ b/src/plugins/plugin_xep_0085.py	Thu Sep 26 10:44:57 2013 +0200
@@ -99,7 +99,7 @@
         host.trigger.add("MessageReceived", self.messageReceivedTrigger)
         host.trigger.add("sendMessageXml", self.sendMessageXmlTrigger)
         host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger)
-        #TODO: handle profile disconnexion (free memory in entity data)
+        # TODO: handle profile disconnexion (free memory in entity data)
 
         # args: to_s (jid as string), profile
         host.bridge.addMethod("chatStateComposing", ".plugin", in_sign='ss',
@@ -217,7 +217,7 @@
         """
         Launch the chat state machine on "active" state.
         """
-        # TODO: use also the resource in map key
+        # TODO: use also the JID resource in the map key
         to_jid = to_jid.userhostJID()
         profile = self.host.memory.getProfileName(profile_key)
         if profile is None:
@@ -227,18 +227,32 @@
 
     def chatStateComposing(self, to_jid_s, profile_key):
         """
-        Move to the "composing" state.
+        Move to the "composing" state. Since this method is called
+        from the front-end, it needs to check the values of the
+        parameter "Send chat state notifications" and the entity
+        data associated to the target JID.
+        TODO: try to optimize this method which is called often
         """
-        # TODO: use also the resource in map key
+        # check if the parameter is active
+        if not self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile_key):
+            return
+        # TODO: use also the JID resource in the map key
         to_jid = JID(to_jid_s).userhostJID()
         profile = self.host.memory.getProfileName(profile_key)
         if profile is None:
             raise exceptions.ProfileUnknownError
+        # check if notifications should be sent to this contact
+        contact_enabled = True
         try:
-            self.map[profile][to_jid]._onEvent("composing")
-        except:
-            # it just means that no first message has been sent already
-            return
+            contact_enabled = self.host.memory.getEntityData(
+                to_jid, [ENTITY_KEY], profile)[ENTITY_KEY]
+        except (exceptions.UnknownEntityError, KeyError):
+            # wait for the first message before sending states
+            pass
+        if not contact_enabled:
+            return True
+        # now we are sure that the state should be sent
+        self.map[profile][to_jid]._onEvent("composing")
 
 
 class ChatStateMachine: