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@…>, 13 years ago

Add a bunch of new patches.

File size: 3.3 KB
RevLine 
[34]1diff -r 09858bbc7f56 wokkel/xmppim.py
2--- a/wokkel/xmppim.py  Fri Feb 12 09:37:28 2010 +0100
3+++ b/wokkel/xmppim.py  Fri Feb 12 09:40:15 2010 +0100
4@@ -12,6 +12,7 @@
5 All of it should eventually move to Twisted.
6 """
7 
8+from twisted.words.protocols.jabber import error, xmlstream
9 from twisted.words.protocols.jabber.jid import JID
10 from twisted.words.xish import domish
11 
12@@ -22,6 +23,9 @@
13 NS_XML = 'http://www.w3.org/XML/1998/namespace'
14 NS_ROSTER = 'jabber:iq:roster'
15 
16+XPATH_ROSTER_GET = "//iq[@type='get']/query[@xmlns='%s']" % NS_ROSTER
17+XPATH_ROSTER_SET = "//iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
18+
19 class Presence(domish.Element):
20     def __init__(self, to=None, type=None):
21         domish.Element.__init__(self, (None, "presence"))
22@@ -605,6 +609,7 @@
23         self.groups = set()
24 
25 
26+
27 class RosterClientProtocol(XMPPHandler):
28     """
29     Client side XMPP roster protocol.
30@@ -614,6 +619,7 @@
31         ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER
32         self.xmlstream.addObserver(ROSTER_SET, self._onRosterSet)
33 
34+
35     def _parseRosterItem(self, element):
36         jid = JID(element['jid'])
37         item = RosterItem(jid)
38@@ -628,6 +634,7 @@
39 
40         return item
41 
42+
43     def getRoster(self):
44         """
45         Retrieve contact list.
46@@ -683,6 +690,7 @@
47             item = self._parseRosterItem(iq.query.item)
48             self.onRosterSet(item)
49 
50+
51     def onRosterSet(self, item):
52         """
53         Called when a roster push for a new or update item was received.
54@@ -691,6 +699,7 @@
55         @type item: L{RosterItem}
56         """
57 
58+
59     def onRosterRemove(self, entity):
60         """
61         Called when a roster push for the removal of an item was received.
62@@ -699,6 +708,57 @@
63         @type entity: L{JID}
64         """
65 
66+
67+
68+class RosterServerProtocol(XMPPHandler):
69+    """
70+    XMPP subprotocol handler for the roster, server side.
71+    """
72+
73+    def connectionInitialized(self):
74+        self.xmlstream.addObserver(XPATH_ROSTER_GET, self._onRosterGet, -1)
75+        self.xmlstream.addObserver(XPATH_ROSTER_SET, self._onRosterSet, -1)
76+
77+
78+    def _toRosterReply(self, items, iq):
79+        response = xmlstream.toResponse(iq, 'result')
80+        response.addElement((NS_ROSTER, 'query'))
81+
82+        for item in items:
83+            element = response.query.addElement('item')
84+            element['jid'] = item['jid']
85+            if item['name']:
86+                element['name'] = item['name']
87+            element['subscription'] = item['subscription']
88+            if item['ask']:
89+                element['ask'] = item['ask']
90+
91+            for group in item['groups']:
92+                element.addElement('group', content=group)
93+
94+        return response
95+
96+
97+    def _onRosterGet(self, iq):
98+        iq.handled = True
99+
100+        d = self.getRoster(JID(iq["from"]))
101+        d.addCallback(self._toRosterReply, iq)
102+        d.addErrback(lambda _: error.ErrorStanza('internal-error').toResponse(iq))
103+        d.addBoth(self.send)
104+
105+
106+    def _onRosterSet(self, iq):
107+        iq.handled = True
108+        response = error.StanzaError('bad-request').toResponse(iq)
109+        self.send(response)
110+
111+
112+    def getRoster(self, entity):
113+        raise NotImplemented
114+
115+
116+
117 class MessageProtocol(XMPPHandler):
118     """
119     Generic XMPP subprotocol handler for incoming message stanzas.
Note: See TracBrowser for help on using the repository browser.