From f6b3a0c6cffa5ebf10d20c20b5cadd575c91fe81 Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 12 Feb 2015 12:17:01 +0100 Subject: Fix the uses of stanza.reply() This is relying on the stanzas being copied for each handler. We no longer do that for performance reasons, so instead of editing the copy in-place, stanza.reply() now returns a new stanza. --- slixmpp/plugins/xep_0009/rpc.py | 12 ++++++---- slixmpp/plugins/xep_0012/last_activity.py | 3 +-- slixmpp/plugins/xep_0027/gpg.py | 3 +-- slixmpp/plugins/xep_0030/disco.py | 4 ++-- slixmpp/plugins/xep_0047/ibb.py | 3 +-- slixmpp/plugins/xep_0047/stream.py | 6 ++--- slixmpp/plugins/xep_0050/adhoc.py | 6 ++--- slixmpp/plugins/xep_0054/vcard_temp.py | 2 +- slixmpp/plugins/xep_0065/proxy.py | 2 +- slixmpp/plugins/xep_0092/version.py | 2 +- slixmpp/plugins/xep_0202/time.py | 2 +- slixmpp/plugins/xep_0231/bob.py | 2 +- slixmpp/plugins/xep_0323/sensordata.py | 20 ++++++++-------- slixmpp/plugins/xep_0325/control.py | 12 +++++----- slixmpp/stanza/iq.py | 14 +++++------ slixmpp/stanza/message.py | 16 ++++++------- slixmpp/stanza/presence.py | 7 +++--- slixmpp/stanza/rootstanza.py | 40 +++++++++++++++---------------- slixmpp/xmlstream/stanzabase.py | 11 +++++---- 19 files changed, 83 insertions(+), 84 deletions(-) diff --git a/slixmpp/plugins/xep_0009/rpc.py b/slixmpp/plugins/xep_0009/rpc.py index 5ed8b84c..786b1d2f 100644 --- a/slixmpp/plugins/xep_0009/rpc.py +++ b/slixmpp/plugins/xep_0009/rpc.py @@ -93,7 +93,8 @@ class XEP_0009(BasePlugin): def _item_not_found(self, iq): payload = iq.get_payload() - iq.reply().error().set_payload(payload) + iq = iq.reply() + iq.error().set_payload(payload) iq['error']['code'] = '404' iq['error']['type'] = 'cancel' iq['error']['condition'] = 'item-not-found' @@ -101,7 +102,8 @@ class XEP_0009(BasePlugin): def _undefined_condition(self, iq): payload = iq.get_payload() - iq.reply().error().set_payload(payload) + iq = iq.reply() + iq.error().set_payload(payload) iq['error']['code'] = '500' iq['error']['type'] = 'cancel' iq['error']['condition'] = 'undefined-condition' @@ -109,7 +111,8 @@ class XEP_0009(BasePlugin): def _forbidden(self, iq): payload = iq.get_payload() - iq.reply().error().set_payload(payload) + iq = iq.reply() + iq.error().set_payload(payload) iq['error']['code'] = '403' iq['error']['type'] = 'auth' iq['error']['condition'] = 'forbidden' @@ -117,7 +120,8 @@ class XEP_0009(BasePlugin): def _recipient_unvailable(self, iq): payload = iq.get_payload() - iq.reply().error().set_payload(payload) + iq = iq.reply() + error().set_payload(payload) iq['error']['code'] = '404' iq['error']['type'] = 'wait' iq['error']['condition'] = 'recipient-unavailable' diff --git a/slixmpp/plugins/xep_0012/last_activity.py b/slixmpp/plugins/xep_0012/last_activity.py index 76a68c7a..c1f69a44 100644 --- a/slixmpp/plugins/xep_0012/last_activity.py +++ b/slixmpp/plugins/xep_0012/last_activity.py @@ -132,8 +132,7 @@ class XEP_0012(BasePlugin): if not isinstance(iq, Iq): reply = self.xmpp.Iq() else: - iq.reply() - reply = iq + reply = iq.reply() if jid not in self._last_activities: raise XMPPError('service-unavailable') diff --git a/slixmpp/plugins/xep_0027/gpg.py b/slixmpp/plugins/xep_0027/gpg.py index e9978416..a0b1df48 100644 --- a/slixmpp/plugins/xep_0027/gpg.py +++ b/slixmpp/plugins/xep_0027/gpg.py @@ -67,8 +67,7 @@ class XEP_0027(BasePlugin): register_stanza_plugin(Message, Encrypted) self.xmpp.add_event_handler('unverified_signed_presence', - self._handle_unverified_signed_presence, - threaded=True) + self._handle_unverified_signed_presence) self.xmpp.register_handler( Callback('Signed Presence', diff --git a/slixmpp/plugins/xep_0030/disco.py b/slixmpp/plugins/xep_0030/disco.py index 3fb005a9..c8c18ea7 100644 --- a/slixmpp/plugins/xep_0030/disco.py +++ b/slixmpp/plugins/xep_0030/disco.py @@ -634,7 +634,7 @@ class XEP_0030(BasePlugin): info['id'] = iq['id'] info.send() else: - iq.reply() + iq = iq.reply() if info: info = self._fix_default_info(info) iq.set_payload(info.xml) @@ -674,7 +674,7 @@ class XEP_0030(BasePlugin): if isinstance(items, Iq): items.send() else: - iq.reply() + iq = iq.reply() if items: iq.set_payload(items.xml) iq.send() diff --git a/slixmpp/plugins/xep_0047/ibb.py b/slixmpp/plugins/xep_0047/ibb.py index ce697f01..7c01c78a 100644 --- a/slixmpp/plugins/xep_0047/ibb.py +++ b/slixmpp/plugins/xep_0047/ibb.py @@ -191,8 +191,7 @@ class XEP_0047(BasePlugin): self.window_size) stream.stream_started.set() self.api['set_stream'](stream.self_jid, stream.sid, stream.peer_jid, stream) - iq.reply() - iq.send() + iq.reply().send() self.xmpp.event('ibb_stream_start', stream) self.xmpp.event('stream:%s:%s' % (sid, stream.peer_jid), stream) diff --git a/slixmpp/plugins/xep_0047/stream.py b/slixmpp/plugins/xep_0047/stream.py index a8e7ff2a..ba0b2cec 100644 --- a/slixmpp/plugins/xep_0047/stream.py +++ b/slixmpp/plugins/xep_0047/stream.py @@ -103,8 +103,7 @@ class IBBytestream(object): self.xmpp.event('ibb_stream_data', {'stream': self, 'data': data}) if isinstance(stanza, Iq): - stanza.reply() - stanza.send() + stanza.reply().send() def recv(self, *args, **kwargs): return self.read(block=True) @@ -134,8 +133,7 @@ class IBBytestream(object): def _closed(self, iq): self.stream_in_closed.set() self.stream_out_closed.set() - iq.reply() - iq.send() + iq.reply().send() self.xmpp.event('ibb_stream_end', self) def makefile(self, *args, **kwargs): diff --git a/slixmpp/plugins/xep_0050/adhoc.py b/slixmpp/plugins/xep_0050/adhoc.py index cb179a03..17c4f7d0 100644 --- a/slixmpp/plugins/xep_0050/adhoc.py +++ b/slixmpp/plugins/xep_0050/adhoc.py @@ -339,7 +339,7 @@ class XEP_0050(BasePlugin): for item in payload: register_stanza_plugin(Command, item.__class__, iterable=True) - iq.reply() + iq = iq.reply() iq['command']['node'] = session['node'] iq['command']['sessionid'] = session['id'] @@ -382,7 +382,7 @@ class XEP_0050(BasePlugin): if handler: handler(iq, session) del self.sessions[sessionid] - iq.reply() + iq = iq.reply() iq['command']['node'] = node iq['command']['sessionid'] = sessionid iq['command']['status'] = 'canceled' @@ -421,7 +421,7 @@ class XEP_0050(BasePlugin): del self.sessions[sessionid] - iq.reply() + iq = iq.reply() iq['command']['node'] = node iq['command']['sessionid'] = sessionid iq['command']['actions'] = [] diff --git a/slixmpp/plugins/xep_0054/vcard_temp.py b/slixmpp/plugins/xep_0054/vcard_temp.py index 9709c998..ae47a5f9 100644 --- a/slixmpp/plugins/xep_0054/vcard_temp.py +++ b/slixmpp/plugins/xep_0054/vcard_temp.py @@ -127,7 +127,7 @@ class XEP_0054(BasePlugin): if isinstance(vcard, Iq): vcard.send() else: - iq.reply() + iq = iq.reply() iq.append(vcard) iq.send() elif iq['type'] == 'set': diff --git a/slixmpp/plugins/xep_0065/proxy.py b/slixmpp/plugins/xep_0065/proxy.py index cb6affde..a1c2bc60 100644 --- a/slixmpp/plugins/xep_0065/proxy.py +++ b/slixmpp/plugins/xep_0065/proxy.py @@ -178,7 +178,7 @@ class XEP_0065(BasePlugin): else: raise XMPPError(etype='cancel', condition='item-not-found') - iq.reply() + iq = iq.reply() with self._sessions_lock: self._sessions[sid] = conn iq['socks']['sid'] = sid diff --git a/slixmpp/plugins/xep_0092/version.py b/slixmpp/plugins/xep_0092/version.py index 398f6b30..9decbabe 100644 --- a/slixmpp/plugins/xep_0092/version.py +++ b/slixmpp/plugins/xep_0092/version.py @@ -64,7 +64,7 @@ class XEP_0092(BasePlugin): Arguments: iq -- The Iq stanza containing the software version query. """ - iq.reply() + iq = iq.reply() iq['software_version']['name'] = self.software_name iq['software_version']['version'] = self.version iq['software_version']['os'] = self.os diff --git a/slixmpp/plugins/xep_0202/time.py b/slixmpp/plugins/xep_0202/time.py index 760ef26a..fbf6b4f0 100644 --- a/slixmpp/plugins/xep_0202/time.py +++ b/slixmpp/plugins/xep_0202/time.py @@ -72,7 +72,7 @@ class XEP_0202(BasePlugin): Arguments: iq -- The Iq time request stanza. """ - iq.reply() + iq = iq.reply() iq['entity_time']['time'] = self.local_time(iq['to']) iq.send() diff --git a/slixmpp/plugins/xep_0231/bob.py b/slixmpp/plugins/xep_0231/bob.py index d590c9dd..3c99ae6b 100644 --- a/slixmpp/plugins/xep_0231/bob.py +++ b/slixmpp/plugins/xep_0231/bob.py @@ -115,7 +115,7 @@ class XEP_0231(BasePlugin): data.send() return - iq.reply() + iq = iq.reply() iq.append(data) iq.send() diff --git a/slixmpp/plugins/xep_0323/sensordata.py b/slixmpp/plugins/xep_0323/sensordata.py index e032bb7b..21afb55a 100644 --- a/slixmpp/plugins/xep_0323/sensordata.py +++ b/slixmpp/plugins/xep_0323/sensordata.py @@ -303,11 +303,11 @@ class XEP_0323(BasePlugin): #print("added session: " + str(self.sessions)) - iq.reply() + iq = iq.reply() iq['accepted']['seqnr'] = seqnr if not request_delay_sec is None: iq['accepted']['queued'] = "true" - iq.send(block=False) + iq.send() self.sessions[session]["node_list"] = process_nodes @@ -327,11 +327,11 @@ class XEP_0323(BasePlugin): self._threaded_node_request(session, process_fields, req_flags) else: - iq.reply() + iq = iq.reply() iq['type'] = 'error' iq['rejected']['seqnr'] = seqnr iq['rejected']['error'] = error_msg - iq.send(block=False) + iq.send() def _threaded_node_request(self, session, process_fields, flags): """ @@ -515,21 +515,21 @@ class XEP_0323(BasePlugin): self.sessions[s]["commTimers"][n].cancel() # Confirm - iq.reply() + iq = iq.reply() iq['type'] = 'result' iq['cancelled']['seqnr'] = seqnr - iq.send(block=False) + iq.send() # Delete session del self.sessions[s] return # Could not find session, send reject - iq.reply() + iq = iq.reply() iq['type'] = 'error' iq['rejected']['seqnr'] = seqnr iq['rejected']['error'] = "Cancel request received, no matching request is active." - iq.send(block=False) + iq.send() # ================================================================= # Client side (data retriever) API @@ -610,7 +610,7 @@ class XEP_0323(BasePlugin): iq['req']._set_flags(flags) self.sessions[seqnr] = {"from": iq['from'], "to": iq['to'], "seqnr": seqnr, "callback": callback} - iq.send(block=False) + iq.send() return seqnr @@ -631,7 +631,7 @@ class XEP_0323(BasePlugin): iq['type'] = "get" iq['id'] = seqnr iq['cancel']['seqnr'] = seqnr - iq.send(block=False) + iq.send() def _get_new_seqnr(self): """ Returns a unique sequence number (unique across threads) """ diff --git a/slixmpp/plugins/xep_0325/control.py b/slixmpp/plugins/xep_0325/control.py index e1c16d40..74e067d1 100644 --- a/slixmpp/plugins/xep_0325/control.py +++ b/slixmpp/plugins/xep_0325/control.py @@ -273,7 +273,7 @@ class XEP_0325(BasePlugin): self._threaded_node_request(session, process_fields) else: - iq.reply() + iq = iq.reply() iq['type'] = 'error' iq['setResponse']['responseCode'] = "NotFound" if missing_node is not None: @@ -282,7 +282,7 @@ class XEP_0325(BasePlugin): iq['setResponse'].add_data(missing_field) iq['setResponse']['error']['var'] = "Output" iq['setResponse']['error']['text'] = error_msg - iq.send(block=False) + iq.send() def _handle_direct_set(self, msg): """ @@ -382,7 +382,7 @@ class XEP_0325(BasePlugin): iq['setResponse'].add_node(nodeId) iq['setResponse']['error']['var'] = "Output" iq['setResponse']['error']['text'] = "Timeout." - iq.send(block=False) + iq.send() ## TODO - should we send one timeout per node?? @@ -443,7 +443,7 @@ class XEP_0325(BasePlugin): iq['setResponse'].add_data(error_field) iq['setResponse']['error']['var'] = error_field iq['setResponse']['error']['text'] = error_msg - iq.send(block=False) + iq.send() # Drop communication with this device and check if we are done self.sessions[session]["nodeDone"][nodeId] = True @@ -463,7 +463,7 @@ class XEP_0325(BasePlugin): iq['type'] = "result" iq['id'] = self.sessions[session]['seqnr'] iq['setResponse']['responseCode'] = "OK" - iq.send(block=False) + iq.send() # The session is complete, delete it del self.sessions[session] @@ -526,7 +526,7 @@ class XEP_0325(BasePlugin): iq['set'].add_data(name=name, typename=typename, value=value) self.sessions[seqnr] = {"from": iq['from'], "to": iq['to'], "callback": callback} - iq.send(block=False) + iq.send() def set_command(self, from_jid, to_jid, fields, nodeIds=None): """ diff --git a/slixmpp/stanza/iq.py b/slixmpp/stanza/iq.py index e2b3c1f9..c9f29f17 100644 --- a/slixmpp/stanza/iq.py +++ b/slixmpp/stanza/iq.py @@ -88,10 +88,10 @@ class Iq(RootStanza): Overrides StanzaBase.unhandled. """ if self['type'] in ('get', 'set'): - self.reply() - self['error']['condition'] = 'feature-not-implemented' - self['error']['text'] = 'No handlers registered for this request.' - self.send() + reply = self.reply() + reply['error']['condition'] = 'feature-not-implemented' + reply['error']['text'] = 'No handlers registered for this request.' + reply.send() def set_payload(self, value): """ @@ -154,9 +154,9 @@ class Iq(RootStanza): clear -- Indicates if existing content should be removed before replying. Defaults to True. """ - self['type'] = 'result' - StanzaBase.reply(self, clear) - return self + new_iq = StanzaBase.reply(self, clear=clear) + new_iq['type'] = 'result' + return new_iq def send(self, callback=None, timeout=None, timeout_callback=None): """Send an stanza over the XML stream. diff --git a/slixmpp/stanza/message.py b/slixmpp/stanza/message.py index 09e44ad8..7f9e90f5 100644 --- a/slixmpp/stanza/message.py +++ b/slixmpp/stanza/message.py @@ -133,21 +133,19 @@ class Message(RootStanza): clear -- Indicates if existing content should be removed before replying. Defaults to True. """ - thread = self['thread'] - parent = self['parent_thread'] + new_message = StanzaBase.reply(self, clear) - StanzaBase.reply(self, clear) if self['type'] == 'groupchat': - self['to'] = self['to'].bare + new_message['to'] = new_message['to'].bare - self['thread'] = thread - self['parent_thread'] = parent + new_message['thread'] = self['thread'] + new_message['parent_thread'] = self['parent_thread'] - del self['id'] + del new_message['id'] if body is not None: - self['body'] = body - return self + new_message['body'] = body + return new_message def get_mucroom(self): """ diff --git a/slixmpp/stanza/presence.py b/slixmpp/stanza/presence.py index 2c77a878..e1b8c0bc 100644 --- a/slixmpp/stanza/presence.py +++ b/slixmpp/stanza/presence.py @@ -174,8 +174,9 @@ class Presence(RootStanza): clear -- Indicates if the stanza contents should be removed before replying. Defaults to True. """ + new_presence = StanzaBase.reply(self, clear) if self['type'] == 'unsubscribe': - self['type'] = 'unsubscribed' + new_presence['type'] = 'unsubscribed' elif self['type'] == 'subscribe': - self['type'] = 'subscribed' - return StanzaBase.reply(self, clear) + new_presence['type'] = 'subscribed' + return new_presence diff --git a/slixmpp/stanza/rootstanza.py b/slixmpp/stanza/rootstanza.py index 7bd0c32d..ff139382 100644 --- a/slixmpp/stanza/rootstanza.py +++ b/slixmpp/stanza/rootstanza.py @@ -46,37 +46,37 @@ class RootStanza(StanzaBase): # locally. Using the condition/text from that error # response could leak too much information, so we'll # only use a generic error here. - self.reply() - self['error']['condition'] = 'undefined-condition' - self['error']['text'] = 'External error' - self['error']['type'] = 'cancel' + reply = self.reply() + reply['error']['condition'] = 'undefined-condition' + reply['error']['text'] = 'External error' + reply['error']['type'] = 'cancel' log.warning('You should catch IqError exceptions') - self.send() + reply.send() elif isinstance(e, IqTimeout): - self.reply() - self['error']['condition'] = 'remote-server-timeout' - self['error']['type'] = 'wait' + reply = self.reply() + reply['error']['condition'] = 'remote-server-timeout' + reply['error']['type'] = 'wait' log.warning('You should catch IqTimeout exceptions') - self.send() + reply.send() elif isinstance(e, XMPPError): # We raised this deliberately - self.reply(clear=e.clear) - self['error']['condition'] = e.condition - self['error']['text'] = e.text - self['error']['type'] = e.etype + reply = self.reply(clear=e.clear) + reply['error']['condition'] = e.condition + reply['error']['text'] = e.text + reply['error']['type'] = e.etype if e.extension is not None: # Extended error tag extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension), e.extension_args) - self['error'].append(extxml) - self.send() + reply['error'].append(extxml) + reply.send() else: # We probably didn't raise this on purpose, so send an error stanza - self.reply() - self['error']['condition'] = 'undefined-condition' - self['error']['text'] = "Slixmpp got into trouble." - self['error']['type'] = 'cancel' - self.send() + reply = self.reply() + reply['error']['condition'] = 'undefined-condition' + reply['error']['text'] = "Slixmpp got into trouble." + reply['error']['type'] = 'cancel' + reply.send() # log the error log.exception('Error handling {%s}%s stanza', self.namespace, self.name) diff --git a/slixmpp/xmlstream/stanzabase.py b/slixmpp/xmlstream/stanzabase.py index 82064c1d..392fd55b 100644 --- a/slixmpp/xmlstream/stanzabase.py +++ b/slixmpp/xmlstream/stanzabase.py @@ -1538,16 +1538,17 @@ class StanzaBase(ElementBase): :param bool clear: Indicates if the stanza's contents should be removed. Defaults to ``True``. """ + new_stanza = copy.copy(self) # if it's a component, use from if self.stream and hasattr(self.stream, "is_component") and \ self.stream.is_component: - self['from'], self['to'] = self['to'], self['from'] + new_stanza['from'], new_stanza['to'] = self['to'], self['from'] else: - self['to'] = self['from'] - del self['from'] + new_stanza['to'] = self['from'] + del new_stanza['from'] if clear: - self.clear() - return self + new_stanza.clear() + return new_stanza def error(self): """Set the stanza's type to ``'error'``.""" -- cgit v1.2.3