comparison src/plugins/plugin_xep_0085.py @ 697:0c84fb112d70

core: sendMessage triggers now use a treatments deferred; - treaments deferred can be used by plugins to change XML elements before sending them, in a similar way as for newMessage - sendMessageXml trigger became useless, so it as been removed in favor of the new deferred
author Goffi <goffi@goffi.org>
date Wed, 13 Nov 2013 13:57:36 +0100
parents 8004c7d4aba7
children d731ae066158
comparison
equal deleted inserted replaced
696:f1a2831d549d 697:0c84fb112d70
95 # parameter value is retrieved before each use 95 # parameter value is retrieved before each use
96 host.memory.updateParams(self.params) 96 host.memory.updateParams(self.params)
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("sendMessage", self.sendMessageTrigger)
101 host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger) 101 host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger)
102 # TODO: handle profile disconnection (free memory in entity data) 102 # TODO: handle profile disconnection (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',
166 # there must be only one state according to the XEP 166 # there must be only one state according to the XEP
167 self.host.bridge.chatStateReceived(message.getAttribute("from"), state, profile) 167 self.host.bridge.chatStateReceived(message.getAttribute("from"), state, profile)
168 break 168 break
169 return True 169 return True
170 170
171 def sendMessageXmlTrigger(self, message, mess_data, profile): 171 def sendMessageTrigger(self, mess_data, treatments, profile):
172 """ 172 """
173 Eventually add the chat state to the message and initiate 173 Eventually add the chat state to the message and initiate
174 the state machine when sending an "active" state. 174 the state machine when sending an "active" state.
175 """ 175 """
176 to_jid = JID(message.getAttribute("to")) 176 def treatment(mess_data):
177 if not self.__checkActivation(to_jid, forceEntityData=True, profile=profile): 177 message = mess_data['xml']
178 return True 178 to_jid = JID(message.getAttribute("to"))
179 try: 179 if not self.__checkActivation(to_jid, forceEntityData=True, profile=profile):
180 # message with a body always mean active state 180 return True
181 domish.generateElementsNamed(message.elements(), name="body").next() 181 try:
182 message.addElement('active', NS_CHAT_STATES) 182 # message with a body always mean active state
183 # launch the chat state machine (init the timer) 183 domish.generateElementsNamed(message.elements(), name="body").next()
184 self.__chatStateActive(to_jid, mess_data["type"], profile) 184 message.addElement('active', NS_CHAT_STATES)
185 except StopIteration: 185 # launch the chat state machine (init the timer)
186 if "chat_state" in mess_data["options"]: 186 self.__chatStateActive(to_jid, mess_data["type"], profile)
187 state = mess_data["options"]["chat_state"] 187 except StopIteration:
188 assert(state in CHAT_STATES) 188 if "chat_state" in mess_data["options"]:
189 message.addElement(state, NS_CHAT_STATES) 189 state = mess_data["options"]["chat_state"]
190 assert(state in CHAT_STATES)
191 message.addElement(state, NS_CHAT_STATES)
192 return mess_data
193
194 treatments.addCallback(treatment)
190 return True 195 return True
191 196
192 def __checkActivation(self, to_jid, forceEntityData, profile): 197 def __checkActivation(self, to_jid, forceEntityData, profile):
193 """ 198 """
194 @param to_joid: the contact's JID 199 @param to_joid: the contact's JID