"""
This code is released under the MIT license. The full package
is available at http://achievewith.us/svn/public/examples/xmppclients/
"""
import base
from pyxmpp.all import JID, Message
from pyxmpp.jabber.client import JabberClient
from threading import Thread
class PyxmppBot(base.JabberBot):
def __init__(self, **kw):
base.JabberBot.__init__(self, **kw)
self.connection = JabberClient(JID("%s@%s/PyxmppPyBot" % (self.username, self.server)),
self.password)
# Client connection and authorization happens in one step
self.connection.connect()
self.connection.stream.set_message_handler("normal", self.receive)
self.receiverThread = Thread(target=self.receiver)
self.receiverThread.start()
def send(self, user, message):
messageObject = Message(to_jid=user,
from_jid=self.connection.jid,
body=message)
self.connection.stream.send(messageObject)
def receive(self, stanza):
self.processMessage(stanza.get_from().as_utf8(),
stanza.get_body())
def receiver(self):
while True:
# Process received messages. Wait up to five seconds
# before returning if there are no queued messages.
self.connection.loop(5)
if __name__ == "__main__":
base.run_bot(PyxmppBot)