2 # -*- coding: utf-8 -*-
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.
15 from optparse
import OptionParser
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
23 if sys
.version_info
< (3, 0):
25 sys
.setdefaultencoding('utf8')
28 class EchoBot(sleekxmpp
.ClientXMPP
):
31 A simple SleekXMPP bot that will echo messages it
32 receives, along with a short thank you message.
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
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
):
52 Process the session_start event.
54 Typical actions for the session_start event are
55 requesting the roster and broadcasting an intial
59 event -- An empty dictionary. The session_start
60 event does not provide any additional
66 def message(self
, msg
):
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
74 msg -- The received message stanza. See the documentation
75 for stanza objects and the Message stanza to see
78 msg
.reply("Thanks for sending\n%(body)s" % msg
).send()
81 if __name__
== '__main__':
82 # Setup the command line arguments.
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",
99 optp
.add_option("-p", "--password", dest
="password",
100 help="password to use")
102 opts
, args
= optp
.parse_args()
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
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.
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
124 # if xmpp.connect(('talk.google.com', 5222)):
126 xmpp
.process(threaded
=False)
129 print("Unable to connect.")