#!/usr/bin/python

# minimal

from twisted.words.protocols.jabber import client, jid
from twisted.xish import domish, xmlstream
from twisted.internet import reactor

me = jid.JID("greutly@swissjabber.ch/TwistedPython")
factory = client.basicClientFactory(me, "greut")

def gotMessage(message):
	# sorry for the __str__(), makes unicode happy
	print u"from: %s" % message["from"]
	for e in message.elements():
		if e.name == "body":
			print unicode(e.__str__())
			break

def authfailedEvent(xmlstream):
	global reactor
	print 'Auth failed!'
	reactor.stop()

def authd(xmlstream):
	print "authd"
	# need to send presence so clients know we're
	# actually online.
	presence = domish.Element(('jabber:client', 'presence'))
	presence.addElement('status').addContent('Online')
	
	xmlstream.send(presence)
	
	# add a callback for the messages
	xmlstream.addObserver('/message', gotMessage)

factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, authd)
factory.addBootstrap(client.BasicAuthenticator.AUTH_FAILED_EVENT, authfailedEvent)

reactor.connectTCP("swissjabber.ch", 5222, factory)
reactor.run()

