Update examples to work with Python3 (raw_input vs input)

This commit is contained in:
Lance Stout 2011-08-18 01:06:59 -07:00
parent 8d998d71a3
commit 015f662249
10 changed files with 49 additions and 10 deletions

View file

@ -24,6 +24,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class CommandBot(sleekxmpp.ClientXMPP): class CommandBot(sleekxmpp.ClientXMPP):
@ -79,9 +81,21 @@ class CommandBot(sleekxmpp.ClientXMPP):
here to persist across handler callbacks. here to persist across handler callbacks.
""" """
form = self['xep_0004'].makeForm('form', 'Greeting') form = self['xep_0004'].makeForm('form', 'Greeting')
form['instructions'] = 'Send a custom greeting to a JID'
form.addField(var='greeting', form.addField(var='greeting',
ftype='text-single', ftype='text-single',
label='Your greeting') 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['payload'] = form
session['next'] = self._handle_command_complete session['next'] = self._handle_command_complete
@ -123,8 +137,14 @@ class CommandBot(sleekxmpp.ClientXMPP):
form = payload form = payload
greeting = form['values']['greeting'] greeting = form['values']['greeting']
self.send_message(mto=session['from'], recipient = form['values']['recipient']
mbody="%s, World!" % greeting) 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' # Having no return statement is the same as unsetting the 'payload'
# and 'next' session values and returning the session. # 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_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data Forms xmpp.register_plugin('xep_0004') # Data Forms
xmpp.register_plugin('xep_0050') # Adhoc Commands 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 # If you are working with an OpenFire server, you may need
# to adjust the SSL version used: # to adjust the SSL version used:

View file

@ -24,6 +24,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class CommandUserBot(sleekxmpp.ClientXMPP): class CommandUserBot(sleekxmpp.ClientXMPP):

View file

@ -27,6 +27,8 @@ from sleekxmpp.xmlstream.stanzabase import ET, registerStanzaPlugin
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class Config(ElementBase): class Config(ElementBase):

View file

@ -25,6 +25,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class Disco(sleekxmpp.ClientXMPP): class Disco(sleekxmpp.ClientXMPP):

View file

@ -24,6 +24,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class EchoBot(sleekxmpp.ClientXMPP): class EchoBot(sleekxmpp.ClientXMPP):
@ -34,7 +36,7 @@ class EchoBot(sleekxmpp.ClientXMPP):
""" """
def __init__(self, jid, password): 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 session_start event will be triggered when
# the bot establishes its connection with the server # the bot establishes its connection with the server

View file

@ -23,6 +23,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class MUCBot(sleekxmpp.ClientXMPP): class MUCBot(sleekxmpp.ClientXMPP):
@ -59,7 +61,7 @@ class MUCBot(sleekxmpp.ClientXMPP):
# muc::room@server::got_online, or muc::room@server::got_offline. # muc::room@server::got_online, or muc::room@server::got_offline.
self.add_event_handler("muc::%s::got_online" % self.room, self.add_event_handler("muc::%s::got_online" % self.room,
self.muc_online) self.muc_online)
def start(self, event): def start(self, event):
""" """
@ -76,15 +78,15 @@ class MUCBot(sleekxmpp.ClientXMPP):
""" """
self.getRoster() self.getRoster()
self.sendPresence() self.sendPresence()
self.plugin['xep_0045'].joinMUC(self.room, self.plugin['xep_0045'].joinMUC(self.room,
self.nick, self.nick,
# If a room password is needed, use: # If a room password is needed, use:
# password=the_room_password, # password=the_room_password,
wait=True) wait=True)
def muc_message(self, msg): 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, that if you also have any handlers for the 'message' event,
message stanzas may be processed by both handlers, so check message stanzas may be processed by both handlers, so check
the 'type' attribute when using a 'message' event handler. 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 otherwise you will create an infinite loop responding
to your own messages. to your own messages.
This handler will reply to messages that mention This handler will reply to messages that mention
the bot's nickname. the bot's nickname.
Arguments: Arguments:
@ -112,12 +114,12 @@ class MUCBot(sleekxmpp.ClientXMPP):
def muc_online(self, presence): def muc_online(self, presence):
""" """
Process a presence stanza from a chat room. In this case, 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 handled by sending a welcome message that includes
the user's nickname and role in the room. the user's nickname and role in the room.
Arguments: Arguments:
presence -- The received presence stanza. See the presence -- The received presence stanza. See the
documentation for the Presence stanza documentation for the Presence stanza
to see how else it may be used. to see how else it may be used.
""" """

View file

@ -24,6 +24,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class PingTest(sleekxmpp.ClientXMPP): class PingTest(sleekxmpp.ClientXMPP):

View file

@ -24,6 +24,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class EchoBot(sleekxmpp.ClientXMPP): class EchoBot(sleekxmpp.ClientXMPP):

View file

@ -27,6 +27,8 @@ from sleekxmpp.exceptions import IqError, IqTimeout
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class RosterBrowser(sleekxmpp.ClientXMPP): class RosterBrowser(sleekxmpp.ClientXMPP):

View file

@ -24,6 +24,8 @@ import sleekxmpp
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
reload(sys) reload(sys)
sys.setdefaultencoding('utf8') sys.setdefaultencoding('utf8')
else:
raw_input = input
class SendMsgBot(sleekxmpp.ClientXMPP): class SendMsgBot(sleekxmpp.ClientXMPP):