source: ralphm-patches/roster_server.patch @ 48:3688f23c1c3a

Last change on this file since 48:3688f23c1c3a was 37:03cd0cb8548c, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Create factory for accepting c2s connections, split out session manager.

File size: 2.9 KB
  • wokkel/xmppim.py

    diff -r 7b9f484b0b44 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"))
     
    636640
    637641
    638642
     643
    639644class RosterClientProtocol(XMPPHandler):
    640645    """
    641646    Client side XMPP roster protocol.
     
    645650        ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
    646651        self.xmlstream.addObserver(ROSTER_SET, self._onRosterSet)
    647652
     653
    648654    def _parseRosterItem(self, element):
    649655        jid = JID(element['jid'])
    650656        item = RosterItem(jid)
     
    659665
    660666        return item
    661667
     668
    662669    def getRoster(self):
    663670        """
    664671        Retrieve contact list.
     
    713720            item = self._parseRosterItem(iq.query.item)
    714721            self.onRosterSet(item)
    715722
     723
    716724    def onRosterSet(self, item):
    717725        """
    718726        Called when a roster push for a new or update item was received.
     
    721729        @type item: L{RosterItem}
    722730        """
    723731
     732
    724733    def onRosterRemove(self, entity):
    725734        """
    726735        Called when a roster push for the removal of an item was received.
     
    729738        @type entity: L{JID}
    730739        """
    731740
     741
     742
     743class RosterServerProtocol(XMPPHandler):
     744    """
     745    XMPP subprotocol handler for the roster, server side.
     746    """
     747
     748    def connectionInitialized(self):
     749        self.xmlstream.addObserver(XPATH_ROSTER_GET, self._onRosterGet)
     750        self.xmlstream.addObserver(XPATH_ROSTER_SET, self._onRosterSet)
     751
     752
     753    def _toRosterReply(self, roster, iq):
     754        response = xmlstream.toResponse(iq, 'result')
     755        response.addElement((NS_ROSTER, 'query'))
     756
     757        for item in roster:
     758            response.query.addChild(item.toElement())
     759
     760        return response
     761
     762
     763    def _onRosterGet(self, iq):
     764        iq.handled = True
     765
     766        d = self.getRoster(JID(iq["from"]))
     767        d.addCallback(self._toRosterReply, iq)
     768        d.addErrback(lambda _: error.ErrorStanza('internal-error').toResponse(iq))
     769        d.addBoth(self.send)
     770
     771
     772    def _onRosterSet(self, iq):
     773        iq.handled = True
     774        response = error.StanzaError('bad-request').toResponse(iq)
     775        self.send(response)
     776
     777
     778    def getRoster(self, entity):
     779        raise NotImplemented
     780
     781
     782
    732783class MessageProtocol(XMPPHandler):
    733784    """
    734785    Generic XMPP subprotocol handler for incoming message stanzas.
Note: See TracBrowser for help on using the repository browser.