"""
This code is released under the MIT license. The full package
is available at http://achievewith.us/svn/public/examples/xmppclients/
"""
import base
import xmpp
from threading import Thread
class XmpppyBot(base.JabberBot):
def __init__(self, **kw):
base.JabberBot.__init__(self, **kw)
self.connection = xmpp.Client(self.server)
self.connection.connect()
self.connection.RegisterHandler("message", self.receive)
self.connection.auth(self.username,
self.password,
"XmpppyPyBot")
self.connection.sendInitPresence()
# We need to spawn a thread to periodically receive messages
self.receiverThread = Thread(target=self.receiver)
self.receiverThread.start()
def send(self, user, message):
message = xmpp.protocol.Message(to=user, body=message, typ='chat')
self.connection.send(message)
def receive(self, connection, message):
# Grab the sender and payload from chat messages
# and pass them on to the processMessage method
self.processMessage(message.getFrom(),
message.getBody())
def receiver(self):
while True:
# Process received messages. Wait up to five seconds
# before returning if there are no queued messages.
self.connection.Process(5)
if __name__ == "__main__":
base.run_bot(XmpppyBot)