1 | """ |
---|
2 | A one-off XMPP client. |
---|
3 | """ |
---|
4 | |
---|
5 | from twisted.application import service |
---|
6 | from twisted.internet import reactor |
---|
7 | from twisted.python import log |
---|
8 | from twisted.words.protocols.jabber.jid import JID |
---|
9 | from twisted.words.protocols.jabber.xmlstream import IQ |
---|
10 | |
---|
11 | from wokkel import client |
---|
12 | |
---|
13 | NS_VERSION = 'jabber:iq:version' |
---|
14 | |
---|
15 | def getVersion(xmlstream, target): |
---|
16 | def cb(result): |
---|
17 | version = {} |
---|
18 | for element in result.query.elements(): |
---|
19 | if (element.uri == NS_VERSION and |
---|
20 | element.name in ('name', 'version')): |
---|
21 | version[element.name] = unicode(element) |
---|
22 | return version |
---|
23 | |
---|
24 | iq = IQ(xmlstream, 'get') |
---|
25 | iq.addElement((NS_VERSION, 'query')) |
---|
26 | d = iq.send(target.full()) |
---|
27 | d.addCallback(cb) |
---|
28 | return d |
---|
29 | |
---|
30 | def printVersion(version): |
---|
31 | print "Name: %s" % version['name'].encode('utf-8') |
---|
32 | print "Version: %s" % version['version'].encode('utf-8') |
---|
33 | |
---|
34 | jid = JID("user@example.org") |
---|
35 | password = 'secret' |
---|
36 | |
---|
37 | application = service.Application('XMPP client') |
---|
38 | |
---|
39 | factory = client.DeferredClientFactory(jid, password) |
---|
40 | factory.streamManager.logTraffic = True |
---|
41 | |
---|
42 | d = client.clientCreator(factory) |
---|
43 | d.addCallback(lambda _: getVersion(factory.streamManager.xmlstream, |
---|
44 | JID(jid.host))) |
---|
45 | d.addCallback(printVersion) |
---|
46 | d.addCallback(lambda _: factory.streamManager.xmlstream.sendFooter()) |
---|
47 | d.addErrback(log.err) |
---|
48 | d.addBoth(lambda _: reactor.callLater(1, reactor.stop)) |
---|