Remove deprecation warnings
[slixmpp.git] / examples / echo_client.py
blob99967d5f6ac45f2420fb1b70441875c52edfd636
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """
5 SleekXMPP: The Sleek XMPP Library
6 Copyright (C) 2010 Nathanael C. Fritz
7 This file is part of SleekXMPP.
9 See the file LICENSE for copying permission.
10 """
12 import sys
13 import logging
14 import time
15 from optparse import OptionParser
17 import sleekxmpp
19 # Python versions before 3.0 do not use UTF-8 encoding
20 # by default. To ensure that Unicode is handled properly
21 # throughout SleekXMPP, we will set the default encoding
22 # ourselves to UTF-8.
23 if sys.version_info < (3, 0):
24 reload(sys)
25 sys.setdefaultencoding('utf8')
28 class EchoBot(sleekxmpp.ClientXMPP):
30 """
31 A simple SleekXMPP bot that will echo messages it
32 receives, along with a short thank you message.
33 """
35 def __init__(self, jid, password):
36 sleekxmpp.ClientXMPP.__init__(self, jid, password)
38 # The session_start event will be triggered when
39 # the bot establishes its connection with the server
40 # and the XML streams are ready for use. We want to
41 # listen for this event so that we we can intialize
42 # our roster.
43 self.add_event_handler("session_start", self.start)
45 # The message event is triggered whenever a message
46 # stanza is received. Be aware that that includes
47 # MUC messages and error messages.
48 self.add_event_handler("message", self.message)
50 def start(self, event):
51 """
52 Process the session_start event.
54 Typical actions for the session_start event are
55 requesting the roster and broadcasting an intial
56 presence stanza.
58 Arguments:
59 event -- An empty dictionary. The session_start
60 event does not provide any additional
61 data.
62 """
63 self.getRoster()
64 self.sendPresence()
66 def message(self, msg):
67 """
68 Process incoming message stanzas. Be aware that this also
69 includes MUC messages and error messages. It is usually
70 a good idea to check the messages's type before processing
71 or sending replies.
73 Arguments:
74 msg -- The received message stanza. See the documentation
75 for stanza objects and the Message stanza to see
76 how it may be used.
77 """
78 msg.reply("Thanks for sending\n%(body)s" % msg).send()
81 if __name__ == '__main__':
82 # Setup the command line arguments.
83 optp = OptionParser()
85 # Output verbosity options.
86 optp.add_option('-q', '--quiet', help='set logging to ERROR',
87 action='store_const', dest='loglevel',
88 const=logging.ERROR, default=logging.INFO)
89 optp.add_option('-d', '--debug', help='set logging to DEBUG',
90 action='store_const', dest='loglevel',
91 const=logging.DEBUG, default=logging.INFO)
92 optp.add_option('-v', '--verbose', help='set logging to COMM',
93 action='store_const', dest='loglevel',
94 const=5, default=logging.INFO)
96 # JID and password options.
97 optp.add_option("-j", "--jid", dest="jid",
98 help="JID to use")
99 optp.add_option("-p", "--password", dest="password",
100 help="password to use")
102 opts, args = optp.parse_args()
104 # Setup logging.
105 logging.basicConfig(level=opts.loglevel,
106 format='%(levelname)-8s %(message)s')
108 # Setup the EchoBot and register plugins. Note that while plugins may
109 # have interdependencies, the order in which you register them does
110 # not matter.
111 xmpp = EchoBot(opts.jid, opts.password)
112 xmpp.registerPlugin('xep_0030') # Service Discovery
113 xmpp.registerPlugin('xep_0004') # Data Forms
114 xmpp.registerPlugin('xep_0060') # PubSub
115 xmpp.registerPlugin('xep_0199') # XMPP Ping
117 # Connect to the XMPP server and start processing XMPP stanzas.
118 if xmpp.connect():
119 # If you do not have the pydns library installed, you will need
120 # to manually specify the name of the server if it does not match
121 # the one in the JID. For example, to use Google Talk you would
122 # need to use:
124 # if xmpp.connect(('talk.google.com', 5222)):
125 # ...
126 xmpp.process(threaded=False)
127 print("Done")
128 else:
129 print("Unable to connect.")