comparison 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
comparison
equal deleted inserted replaced
653:e2eff3c7ad02 654:5c5cf5bca240
97 97
98 # triggers from core 98 # triggers from core
99 host.trigger.add("MessageReceived", self.messageReceivedTrigger) 99 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
100 host.trigger.add("sendMessageXml", self.sendMessageXmlTrigger) 100 host.trigger.add("sendMessageXml", self.sendMessageXmlTrigger)
101 host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger) 101 host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger)
102 #TODO: handle profile disconnexion (free memory in entity data) 102 # TODO: handle profile disconnexion (free memory in entity data)
103 103
104 # args: to_s (jid as string), profile 104 # args: to_s (jid as string), profile
105 host.bridge.addMethod("chatStateComposing", ".plugin", in_sign='ss', 105 host.bridge.addMethod("chatStateComposing", ".plugin", in_sign='ss',
106 out_sign='', method=self.chatStateComposing) 106 out_sign='', method=self.chatStateComposing)
107 107
215 215
216 def __chatStateActive(self, to_jid, mess_type, profile_key): 216 def __chatStateActive(self, to_jid, mess_type, profile_key):
217 """ 217 """
218 Launch the chat state machine on "active" state. 218 Launch the chat state machine on "active" state.
219 """ 219 """
220 # TODO: use also the resource in map key 220 # TODO: use also the JID resource in the map key
221 to_jid = to_jid.userhostJID() 221 to_jid = to_jid.userhostJID()
222 profile = self.host.memory.getProfileName(profile_key) 222 profile = self.host.memory.getProfileName(profile_key)
223 if profile is None: 223 if profile is None:
224 raise exceptions.ProfileUnknownError 224 raise exceptions.ProfileUnknownError
225 self.__chatStateInit(to_jid, mess_type, profile) 225 self.__chatStateInit(to_jid, mess_type, profile)
226 self.map[profile][to_jid]._onEvent("active") 226 self.map[profile][to_jid]._onEvent("active")
227 227
228 def chatStateComposing(self, to_jid_s, profile_key): 228 def chatStateComposing(self, to_jid_s, profile_key):
229 """ 229 """
230 Move to the "composing" state. 230 Move to the "composing" state. Since this method is called
231 """ 231 from the front-end, it needs to check the values of the
232 # TODO: use also the resource in map key 232 parameter "Send chat state notifications" and the entity
233 data associated to the target JID.
234 TODO: try to optimize this method which is called often
235 """
236 # check if the parameter is active
237 if not self.host.memory.getParamA(PARAM_NAME, PARAM_KEY, profile_key=profile_key):
238 return
239 # TODO: use also the JID resource in the map key
233 to_jid = JID(to_jid_s).userhostJID() 240 to_jid = JID(to_jid_s).userhostJID()
234 profile = self.host.memory.getProfileName(profile_key) 241 profile = self.host.memory.getProfileName(profile_key)
235 if profile is None: 242 if profile is None:
236 raise exceptions.ProfileUnknownError 243 raise exceptions.ProfileUnknownError
244 # check if notifications should be sent to this contact
245 contact_enabled = True
237 try: 246 try:
238 self.map[profile][to_jid]._onEvent("composing") 247 contact_enabled = self.host.memory.getEntityData(
239 except: 248 to_jid, [ENTITY_KEY], profile)[ENTITY_KEY]
240 # it just means that no first message has been sent already 249 except (exceptions.UnknownEntityError, KeyError):
241 return 250 # wait for the first message before sending states
251 pass
252 if not contact_enabled:
253 return True
254 # now we are sure that the state should be sent
255 self.map[profile][to_jid]._onEvent("composing")
242 256
243 257
244 class ChatStateMachine: 258 class ChatStateMachine:
245 """ 259 """
246 This class represents a chat state, between one profile and 260 This class represents a chat state, between one profile and