source: doc/listings/xmppim/roster_client_versioning.py @ 173:6b0eb01b5744

Last change on this file since 173:6b0eb01b5744 was 173:6b0eb01b5744, checked in by Ralph Meijer <ralphm@…>, 10 years ago

Add support for roster versioning.

File size: 1.6 KB
Line 
1import sys
2from twisted.python import log
3from twisted.internet import reactor
4from twisted.words.protocols.jabber.jid import JID
5from wokkel.client import XMPPClient
6from wokkel.xmppim import RosterClientProtocol
7
8class RosterHandler(RosterClientProtocol):
9    roster = None
10
11    def gotRoster(self, roster):
12        if roster is None:
13            print "The cached roster is up-to-date!"
14            return
15
16        print 'Got roster (%r):' % (roster.version,)
17        self.roster = roster
18        for entity, item in roster.iteritems():
19            print %r (%r)' % (entity, item.name or '')
20
21    def connectionInitialized(self):
22        RosterClientProtocol.connectionInitialized(self)
23        if self.roster is not None:
24            version = self.roster.version
25        else:
26            version = ""
27        d = self.getRoster(version)
28        d.addCallback(self.gotRoster)
29        d.addErrback(log.err)
30
31        reactor.callLater(15, self.xmlstream.sendFooter)
32
33    def removeReceived(self, request):
34        print 'Contact %r was removed.' % (request.item.entity,)
35        del self.roster[request.item.entity]
36        self.roster.version = request.version
37
38    def setReceived(self, request):
39        print 'Contact %r (%r) was updated.' % (request.item.entity,
40                                                request.item.name)
41        self.roster[request.item.entity] = request.item
42        self.roster.version = request.version
43
44USER_JID, PASSWORD = sys.argv[1:3]
45client = XMPPClient(JID(USER_JID), PASSWORD)
46roster = RosterHandler()
47roster.setHandlerParent(client)
48
49client.startService()
50reactor.run()
Note: See TracBrowser for help on using the repository browser.