summaryrefslogtreecommitdiff
path: root/examples/custom_stanzas/custom_stanza_provider.py
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2012-05-14 22:12:52 -0700
committerLance Stout <lancestout@gmail.com>2012-05-14 22:12:52 -0700
commite0bcd5d72245908200afde1ea4c4cb980f054f8d (patch)
tree8b9d7a1fd5c9b05d2dd4933668ea008983eabdb2 /examples/custom_stanzas/custom_stanza_provider.py
parentba854e7d85870dd90307c0c2f408d10d83e4fc33 (diff)
downloadslixmpp-e0bcd5d72245908200afde1ea4c4cb980f054f8d.tar.gz
slixmpp-e0bcd5d72245908200afde1ea4c4cb980f054f8d.tar.bz2
slixmpp-e0bcd5d72245908200afde1ea4c4cb980f054f8d.tar.xz
slixmpp-e0bcd5d72245908200afde1ea4c4cb980f054f8d.zip
Add more documentation to the custom stanza examples.
Diffstat (limited to 'examples/custom_stanzas/custom_stanza_provider.py')
-rwxr-xr-xexamples/custom_stanzas/custom_stanza_provider.py40
1 files changed, 31 insertions, 9 deletions
diff --git a/examples/custom_stanzas/custom_stanza_provider.py b/examples/custom_stanzas/custom_stanza_provider.py
index 16fc6c30..40a77204 100755
--- a/examples/custom_stanzas/custom_stanza_provider.py
+++ b/examples/custom_stanzas/custom_stanza_provider.py
@@ -37,8 +37,8 @@ else:
class ActionBot(sleekxmpp.ClientXMPP):
"""
- A simple SleekXMPP bot that provides a basic
- adhoc command.
+ A simple SleekXMPP bot that receives a custom stanza
+ from another client.
"""
def __init__(self, jid, password):
@@ -54,7 +54,12 @@ class ActionBot(sleekxmpp.ClientXMPP):
self.registerHandler(
Callback('Some custom iq',
StanzaPath('iq@type=set/action'),
- self._handleAction))
+ self._handle_action))
+
+ self.add_event_handler('custom_action',
+ self._handle_action_event,
+ threaded=True)
+
register_stanza_plugin(Iq, Action)
def start(self, event):
@@ -73,17 +78,34 @@ class ActionBot(sleekxmpp.ClientXMPP):
self.send_presence()
self.get_roster()
- def _handleAction(self, iq):
- if iq['action']['method'] == 'is_prime' and iq['action']['param'] == '2':
- print("got message: " + str(iq))
+ def _handle_action(self, iq):
+ """
+ Raise an event for the stanza so that it can be processed in its
+ own thread without blocking the main stanza processing loop.
+ """
+ self.event('custom_action', iq)
+
+ def _handle_action_event(self, iq):
+ """
+ Respond to the custom action event.
+
+ Since one of the actions is to disconnect, this
+ event handler needs to be run in threaded mode, by
+ using `threaded=True` in the `add_event_handler` call.
+ """
+ method = iq['action']['method']
+ param = iq['action']['param']
+
+ if method == 'is_prime' and param == '2':
+ print("got message: %s" % iq)
iq.reply()
iq['action']['status'] = 'done'
iq.send()
- elif iq['action']['method'] == 'bye':
- print("got message: " + str(iq))
+ elif method == 'bye':
+ print("got message: %s" % iq)
self.disconnect()
else:
- print("got message: " + str(iq))
+ print("got message: %s" % iq)
iq.reply()
iq['action']['status'] = 'error'
iq.send()