By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Jabber in Twisted

P: n/a
Does anybody have experience in writing Jabber clients with Twisted? I
came across a simple sample[0], and tried to build out from it, but for
some reason it's barely working.

When I run the application in the attached script, the logon goes fine,
and I can receive messages sent to the account, but it never seems to
receive subscription requests. When I ran it on an account that already
had some items on the contact list, I did receive <presence> messages to
indcate that a user changed status, but I don't receive <presence
type="subscribe"> messages...

Ultimately, I tacked a "/*" handler on to the code to see if I was
receiving any packets at all, but the only thing that seems to trigger
anything are messages.

This is very frustrating for me. Is it a problem with the code, or am I
missing something in the Jabber protocol?

Any help would be greatly appreciated.

- Rico

P.S: Please don't mind the code, I know it's a bit (a lot) hackish right
now, but my first step is to get it running. I'll make it beautiful
afterwards ;).

[0] http://randomthoughts.vandorp.ca/WK/blog/706?t=item

##### code starts here #####

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

from twisted.internet import reactor

name = 'Foo'
server='bar.com'
resource = 'PythonBot'
password = 'foobar'
me = '%s@%s/%s' % (name, server, resource)

thexmlstream = None
tryandregister = 1

def initOnline(xmlstream):
global factory
print 'Initializing...'
xmlstream.addObserver('/message', gotMessage)
xmlstream.addObserver('/presence', gotPresence)
xmlstream.addObserver('/iq', gotIq)
xmlstream.addObserver('/*', gotSomething)

def authd(xmlstream):
thexmlstream = xmlstream
print "we've authd!"
print repr(xmlstream)

#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)

initOnline(xmlstream)

def gotMessage(el):
print 'Got message: %s' % str(el.attributes)

def gotSomething(el):
print 'Got something: %s -> %s' % (el.name, str(el.attributes))

def gotIq(el):
print 'Got IQ: %s' % str(el.attributes)

def gotPresence(el):
print 'We got a presence message!'
print repr(el.attributes)
try:
t = el.attributes['type']
if t == 'subscribe':
# Grant every subscription request
xmlstream.send(domish.Element(('jabber:client',
'presence'), attribs={
'from': me,
'to':el.attributes['from'],
'type':'subscribed'
}))
except KeyError:
# Big fat ignore
pass

def invaliduserEvent(xmlstream):
print 'Invalid user!'
global tryandregister
if tryandregister:
tryandregister = 0
print 'Attempting to register...'
global factory
factory.authenticator.registerAccount(name, password)
else:
global reactor
reactor.stop()

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

def registerfailedEvent(xmlstream):
global reactor
print 'Register failed!'
reactor.stop()

myJid = jid.JID(me)
secret = password
factory = client.basicClientFactory(myJid,secret)

# Register authentication callbacks
factory.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, authd)
factory.addBootstrap(client.BasicAuthenticator.INV ALID_USER_EVENT,
invaliduserEvent)
factory.addBootstrap(client.BasicAuthenticator.AUT H_FAILED_EVENT,
authfailedEvent)
factory.addBootstrap(client.BasicAuthenticator.REG ISTER_FAILED_EVENT,
registerfailedEvent)

# Go!
reactor.connectTCP(server, 5222, factory)
reactor.run()
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Rico Huijbers wrote:
Does anybody have experience in writing Jabber clients with Twisted? I
came across a simple sample[0], and tried to build out from it, but for
some reason it's barely working.

When I run the application in the attached script, the logon goes fine,


I think you'll get far more attention from the right people if
you subscribe to the twisted mailing list, and post your
inquiry there.

As with Zope, and some other large, non-standard packages, the dedicated
Twisted mailing list is where the core folks hang out. Some show up
here from time to time and answer questions, but you're doing yourself
a disservice by limiting the input to this general newsgroup/mailing
list.

-Peter
Jul 18 '05 #2

P: n/a
Peter Hansen wrote:
Rico Huijbers wrote:
Does anybody have experience in writing Jabber clients with Twisted? I
came across a simple sample[0], and tried to build out from it, but
for some reason it's barely working.

When I run the application in the attached script, the logon goes fine,

I think you'll get far more attention from the right people if
you subscribe to the twisted mailing list, and post your
inquiry there.

As with Zope, and some other large, non-standard packages, the dedicated
Twisted mailing list is where the core folks hang out. Some show up
here from time to time and answer questions, but you're doing yourself
a disservice by limiting the input to this general newsgroup/mailing
list.


Yes, I saw that, but since I find it easier to post to and read
newsgroups, and I already saw a couple of Twisted-related threads in
here, I figured I'd try here first.

But I'll take your suggestion and try again on the Twisted mailing list.

Thanks for your reply.

- Rico
Jul 18 '05 #3

P: n/a
On Mon, May 24, 2004 at 04:42:46PM +0200, Rico Huijbers wrote:
[...]

Yes, I saw that, but since I find it easier to post to and read
newsgroups, and I already saw a couple of Twisted-related threads in
here, I figured I'd try here first.


There's always the gmane gateway:
nntp://news.gmane.org/gmane.comp.python.twisted

-Andrew.
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.