source: wokkel/generic.py @ 20:480ba9e6ee3c

Last change on this file since 20:480ba9e6ee3c was 20:480ba9e6ee3c, checked in by Ralph Meijer <ralphm@…>, 14 years ago

Fix broken version handler.

File size: 2.1 KB
Line 
1# -*- test-case-name: wokkel.test.test_generic -*-
2#
3# Copyright (c) 2003-2008 Ralph Meijer
4# See LICENSE for details.
5
6"""
7Generic XMPP protocol helpers.
8"""
9
10from zope.interface import implements
11
12from twisted.internet import defer
13from twisted.words.protocols.jabber import error
14from twisted.words.protocols.jabber.xmlstream import toResponse
15
16from wokkel import disco
17from wokkel.iwokkel import IDisco
18from wokkel.subprotocols import XMPPHandler
19
20IQ_GET = '/iq[@type="get"]'
21IQ_SET = '/iq[@type="set"]'
22
23NS_VERSION = 'jabber:iq:version'
24VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]'
25
26class 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
47class 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([])
Note: See TracBrowser for help on using the repository browser.