From 015f662249eec9a0504bea1d614ad1138d8e19ab Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Thu, 18 Aug 2011 01:06:59 -0700 Subject: [PATCH] Update examples to work with Python3 (raw_input vs input) --- examples/adhoc_provider.py | 25 +++++++++++++++++++++++-- examples/adhoc_user.py | 2 ++ examples/config_component.py | 2 ++ examples/disco_browser.py | 2 ++ examples/echo_client.py | 4 +++- examples/muc.py | 16 +++++++++------- examples/ping.py | 2 ++ examples/proxy_echo_client.py | 2 ++ examples/roster_browser.py | 2 ++ examples/send_client.py | 2 ++ 10 files changed, 49 insertions(+), 10 deletions(-) diff --git a/examples/adhoc_provider.py b/examples/adhoc_provider.py index 3316a0c..5fa2633 100755 --- a/examples/adhoc_provider.py +++ b/examples/adhoc_provider.py @@ -24,6 +24,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class CommandBot(sleekxmpp.ClientXMPP): @@ -79,9 +81,21 @@ class CommandBot(sleekxmpp.ClientXMPP): here to persist across handler callbacks. """ form = self['xep_0004'].makeForm('form', 'Greeting') + form['instructions'] = 'Send a custom greeting to a JID' form.addField(var='greeting', ftype='text-single', label='Your greeting') + form.addField(var='recipient', + ftype='jid-single', + label='Who to greet') + form.addField(var='message', + ftype='text-multi', + label='Your message') + form.addField(var='demobool', + ftype='boolean', + label='Test booleans', + desc='Test default values too', + value=True) session['payload'] = form session['next'] = self._handle_command_complete @@ -123,8 +137,14 @@ class CommandBot(sleekxmpp.ClientXMPP): form = payload greeting = form['values']['greeting'] - self.send_message(mto=session['from'], - mbody="%s, World!" % greeting) + recipient = form['values']['recipient'] + message = '\n'.join(form['values']['message']) + + if recipient is None: + recipient = session['from'] + self.send_message(mto=recipient, + mbody="%s, %s!\n%s" % (greeting, recipient, message), + mtype='chat') # Having no return statement is the same as unsetting the 'payload' # and 'next' session values and returning the session. @@ -176,6 +196,7 @@ if __name__ == '__main__': xmpp.register_plugin('xep_0030') # Service Discovery xmpp.register_plugin('xep_0004') # Data Forms xmpp.register_plugin('xep_0050') # Adhoc Commands + xmpp.register_plugin('xep_0199', {'keepalive': True, 'frequency':15}) # If you are working with an OpenFire server, you may need # to adjust the SSL version used: diff --git a/examples/adhoc_user.py b/examples/adhoc_user.py index 5fba78d..ac157ed 100755 --- a/examples/adhoc_user.py +++ b/examples/adhoc_user.py @@ -24,6 +24,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class CommandUserBot(sleekxmpp.ClientXMPP): diff --git a/examples/config_component.py b/examples/config_component.py index cbb8e62..7a28739 100755 --- a/examples/config_component.py +++ b/examples/config_component.py @@ -27,6 +27,8 @@ from sleekxmpp.xmlstream.stanzabase import ET, registerStanzaPlugin if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class Config(ElementBase): diff --git a/examples/disco_browser.py b/examples/disco_browser.py index b2b96f9..0526bfc 100755 --- a/examples/disco_browser.py +++ b/examples/disco_browser.py @@ -25,6 +25,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class Disco(sleekxmpp.ClientXMPP): diff --git a/examples/echo_client.py b/examples/echo_client.py index cd1b1d0..122b20f 100755 --- a/examples/echo_client.py +++ b/examples/echo_client.py @@ -24,6 +24,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class EchoBot(sleekxmpp.ClientXMPP): @@ -34,7 +36,7 @@ class EchoBot(sleekxmpp.ClientXMPP): """ def __init__(self, jid, password): - sleekxmpp.ClientXMPP.__init__(self, jid, password) + sleekxmpp.ClientXMPP.__init__(self, jid, password, plugin_config={'feature_mechanisms': {'use_mech': 'PLAIN'}}) # The session_start event will be triggered when # the bot establishes its connection with the server diff --git a/examples/muc.py b/examples/muc.py index 8296cb6..96b5fb8 100755 --- a/examples/muc.py +++ b/examples/muc.py @@ -23,6 +23,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class MUCBot(sleekxmpp.ClientXMPP): @@ -59,7 +61,7 @@ class MUCBot(sleekxmpp.ClientXMPP): # muc::room@server::got_online, or muc::room@server::got_offline. self.add_event_handler("muc::%s::got_online" % self.room, self.muc_online) - + def start(self, event): """ @@ -76,15 +78,15 @@ class MUCBot(sleekxmpp.ClientXMPP): """ self.getRoster() self.sendPresence() - self.plugin['xep_0045'].joinMUC(self.room, - self.nick, + self.plugin['xep_0045'].joinMUC(self.room, + self.nick, # If a room password is needed, use: # password=the_room_password, wait=True) def muc_message(self, msg): """ - Process incoming message stanzas from any chat room. Be aware + Process incoming message stanzas from any chat room. Be aware that if you also have any handlers for the 'message' event, message stanzas may be processed by both handlers, so check the 'type' attribute when using a 'message' event handler. @@ -96,7 +98,7 @@ class MUCBot(sleekxmpp.ClientXMPP): otherwise you will create an infinite loop responding to your own messages. - This handler will reply to messages that mention + This handler will reply to messages that mention the bot's nickname. Arguments: @@ -112,12 +114,12 @@ class MUCBot(sleekxmpp.ClientXMPP): def muc_online(self, presence): """ Process a presence stanza from a chat room. In this case, - presences from users that have just come online are + presences from users that have just come online are handled by sending a welcome message that includes the user's nickname and role in the room. Arguments: - presence -- The received presence stanza. See the + presence -- The received presence stanza. See the documentation for the Presence stanza to see how else it may be used. """ diff --git a/examples/ping.py b/examples/ping.py index ae030c0..68772e6 100755 --- a/examples/ping.py +++ b/examples/ping.py @@ -24,6 +24,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class PingTest(sleekxmpp.ClientXMPP): diff --git a/examples/proxy_echo_client.py b/examples/proxy_echo_client.py index 4db9a55..3466dc9 100755 --- a/examples/proxy_echo_client.py +++ b/examples/proxy_echo_client.py @@ -24,6 +24,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class EchoBot(sleekxmpp.ClientXMPP): diff --git a/examples/roster_browser.py b/examples/roster_browser.py index a428324..4a58cc1 100644 --- a/examples/roster_browser.py +++ b/examples/roster_browser.py @@ -27,6 +27,8 @@ from sleekxmpp.exceptions import IqError, IqTimeout if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class RosterBrowser(sleekxmpp.ClientXMPP): diff --git a/examples/send_client.py b/examples/send_client.py index b367351..d1dafee 100755 --- a/examples/send_client.py +++ b/examples/send_client.py @@ -24,6 +24,8 @@ import sleekxmpp if sys.version_info < (3, 0): reload(sys) sys.setdefaultencoding('utf8') +else: + raw_input = input class SendMsgBot(sleekxmpp.ClientXMPP):