1 | # -*- test-case-name: wokkel.test.test_generic -*- |
---|
2 | # |
---|
3 | # Copyright (c) 2003-2008 Ralph Meijer |
---|
4 | # See LICENSE for details. |
---|
5 | |
---|
6 | """ |
---|
7 | Generic XMPP protocol helpers. |
---|
8 | """ |
---|
9 | |
---|
10 | from zope.interface import implements |
---|
11 | |
---|
12 | from twisted.internet import defer |
---|
13 | from twisted.words.protocols.jabber import error |
---|
14 | from twisted.words.protocols.jabber.xmlstream import toResponse |
---|
15 | |
---|
16 | from wokkel import disco |
---|
17 | from wokkel.iwokkel import IDisco |
---|
18 | from wokkel.subprotocols import XMPPHandler |
---|
19 | |
---|
20 | IQ_GET = '/iq[@type="get"]' |
---|
21 | IQ_SET = '/iq[@type="set"]' |
---|
22 | |
---|
23 | NS_VERSION = 'jabber:iq:version' |
---|
24 | VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]' |
---|
25 | |
---|
26 | class FallbackHandler(XMPPHandler): |
---|
27 | """ |
---|
28 | XMPP subprotocol handler that catches unhandled iq requests. |
---|
29 | |
---|
30 | Unhandled iq requests are replied to with a service-unavailable stanza |
---|
31 | error. |
---|
32 | """ |
---|
33 | |
---|
34 | def connectionInitialized(self): |
---|
35 | self.xmlstream.addObserver(IQ_SET, self.iqFallback, -1) |
---|
36 | self.xmlstream.addObserver(IQ_GET, self.iqFallback, -1) |
---|
37 | |
---|
38 | def iqFallback(self, iq): |
---|
39 | if iq.handled == True: |
---|
40 | return |
---|
41 | |
---|
42 | reply = error.StanzaError('service-unavailable') |
---|
43 | self.xmlstream.send(reply.toResponse(iq)) |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | class VersionHandler(XMPPHandler): |
---|
48 | """ |
---|
49 | XMPP subprotocol handler for XMPP Software Version. |
---|
50 | |
---|
51 | This protocol is described in |
---|
52 | U{XEP-0092<http://www.xmpp.org/extensions/xep-0092.html>}. |
---|
53 | """ |
---|
54 | |
---|
55 | implements(IDisco) |
---|
56 | |
---|
57 | def __init__(self, name, version): |
---|
58 | self.name = name |
---|
59 | self.version = version |
---|
60 | |
---|
61 | def connectionInitialized(self): |
---|
62 | self.xmlstream.addObserver(VERSION, self.onVersion) |
---|
63 | |
---|
64 | def onVersion(self, iq): |
---|
65 | response = toResponse(iq, "result") |
---|
66 | |
---|
67 | query = response.addElement((NS_VERSION, "query")) |
---|
68 | name = query.addElement("name", content=self.name) |
---|
69 | version = query.addElement("version", content=self.version) |
---|
70 | self.send(response) |
---|
71 | |
---|
72 | iq.handled = True |
---|
73 | |
---|
74 | def getDiscoInfo(self, requestor, target, node): |
---|
75 | info = set() |
---|
76 | |
---|
77 | if not node: |
---|
78 | info.add(disco.DiscoFeature(NS_VERSION)) |
---|
79 | |
---|
80 | return defer.succeed(info) |
---|
81 | |
---|
82 | def getDiscoItems(self, requestor, target, node): |
---|
83 | return defer.succeed([]) |
---|