source: ralphm-patches/roster_server.patch @ 34:e46c5701df9e

Last change on this file since 34:e46c5701df9e was 34:e46c5701df9e, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Add a bunch of new patches.

File size: 3.3 KB
  • wokkel/xmppim.py

    diff -r 09858bbc7f56 wokkel/xmppim.py
    a b  
    1212All of it should eventually move to Twisted.
    1313"""
    1414
     15from twisted.words.protocols.jabber import error, xmlstream
    1516from twisted.words.protocols.jabber.jid import JID
    1617from twisted.words.xish import domish
    1718
     
    2223NS_XML = 'http://www.w3.org/XML/1998/namespace'
    2324NS_ROSTER = 'jabber:iq:roster'
    2425
     26XPATH_ROSTER_GET = "//iq[@type='get']/query[@xmlns='%s']" % NS_ROSTER
     27XPATH_ROSTER_SET = "//iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
     28
    2529class Presence(domish.Element):
    2630    def __init__(self, to=None, type=None):
    2731        domish.Element.__init__(self, (None, "presence"))
     
    605609        self.groups = set()
    606610
    607611
     612
    608613class RosterClientProtocol(XMPPHandler):
    609614    """
    610615    Client side XMPP roster protocol.
     
    614619        ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
    615620        self.xmlstream.addObserver(ROSTER_SET, self._onRosterSet)
    616621
     622
    617623    def _parseRosterItem(self, element):
    618624        jid = JID(element['jid'])
    619625        item = RosterItem(jid)
     
    628634
    629635        return item
    630636
     637
    631638    def getRoster(self):
    632639        """
    633640        Retrieve contact list.
     
    683690            item = self._parseRosterItem(iq.query.item)
    684691            self.onRosterSet(item)
    685692
     693
    686694    def onRosterSet(self, item):
    687695        """
    688696        Called when a roster push for a new or update item was received.
     
    691699        @type item: L{RosterItem}
    692700        """
    693701
     702
    694703    def onRosterRemove(self, entity):
    695704        """
    696705        Called when a roster push for the removal of an item was received.
     
    699708        @type entity: L{JID}
    700709        """
    701710
     711
     712
     713class RosterServerProtocol(XMPPHandler):
     714    """
     715    XMPP subprotocol handler for the roster, server side.
     716    """
     717
     718    def connectionInitialized(self):
     719        self.xmlstream.addObserver(XPATH_ROSTER_GET, self._onRosterGet, -1)
     720        self.xmlstream.addObserver(XPATH_ROSTER_SET, self._onRosterSet, -1)
     721
     722
     723    def _toRosterReply(self, items, iq):
     724        response = xmlstream.toResponse(iq, 'result')
     725        response.addElement((NS_ROSTER, 'query'))
     726
     727        for item in items:
     728            element = response.query.addElement('item')
     729            element['jid'] = item['jid']
     730            if item['name']:
     731                element['name'] = item['name']
     732            element['subscription'] = item['subscription']
     733            if item['ask']:
     734                element['ask'] = item['ask']
     735
     736            for group in item['groups']:
     737                element.addElement('group', content=group)
     738
     739        return response
     740
     741
     742    def _onRosterGet(self, iq):
     743        iq.handled = True
     744
     745        d = self.getRoster(JID(iq["from"]))
     746        d.addCallback(self._toRosterReply, iq)
     747        d.addErrback(lambda _: error.ErrorStanza('internal-error').toResponse(iq))
     748        d.addBoth(self.send)
     749
     750
     751    def _onRosterSet(self, iq):
     752        iq.handled = True
     753        response = error.StanzaError('bad-request').toResponse(iq)
     754        self.send(response)
     755
     756
     757    def getRoster(self, entity):
     758        raise NotImplemented
     759
     760
     761
    702762class MessageProtocol(XMPPHandler):
    703763    """
    704764    Generic XMPP subprotocol handler for incoming message stanzas.
Note: See TracBrowser for help on using the repository browser.