source: wokkel/generic.py @ 1:677f7d3cca75

Last change on this file since 1:677f7d3cca75 was 1:677f7d3cca75, checked in by Ralph Meijer <ralphm@…>, 14 years ago

Initial, heavily worked around code drop from the idavoll and mimir projects.

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