source: wokkel/generic.py @ 185:a7e10201fb3c

Last change on this file since 185:a7e10201fb3c was 185:a7e10201fb3c, checked in by Ralph Meijer <ralphm@…>, 7 years ago

Do proper fallback for Request.stanzaType.

If stanzaType is not passed to the constructor of
wokkel.generic.Request, it should default to the value in the class
variable, not get.

Author: ralphm
Fixes: #80

  • Property exe set to *
File size: 10.1 KB
Line 
1# -*- test-case-name: wokkel.test.test_generic -*-
2#
3# Copyright (c) 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, protocol
13from twisted.python import reflect
14from twisted.python.deprecate import deprecated
15from twisted.python.versions import Version
16from twisted.words.protocols.jabber import error, jid, xmlstream
17from twisted.words.protocols.jabber.xmlstream import toResponse
18from twisted.words.xish import domish, utility
19from twisted.words.xish.xmlstream import BootstrapMixin
20
21from wokkel.iwokkel import IDisco
22from wokkel.subprotocols import XMPPHandler
23
24IQ_GET = '/iq[@type="get"]'
25IQ_SET = '/iq[@type="set"]'
26
27NS_VERSION = 'jabber:iq:version'
28VERSION = IQ_GET + '/query[@xmlns="' + NS_VERSION + '"]'
29
30def parseXml(string):
31    """
32    Parse serialized XML into a DOM structure.
33
34    @param string: The serialized XML to be parsed, UTF-8 encoded.
35    @type string: C{str}.
36    @return: The DOM structure, or C{None} on empty or incomplete input.
37    @rtype: L{domish.Element}
38    """
39    roots = []
40    results = []
41    elementStream = domish.elementStream()
42    elementStream.DocumentStartEvent = roots.append
43    elementStream.ElementEvent = lambda elem: roots[0].addChild(elem)
44    elementStream.DocumentEndEvent = lambda: results.append(roots[0])
45    elementStream.parse(string)
46    return results and results[0] or None
47
48
49
50def stripNamespace(rootElement):
51    namespace = rootElement.uri
52
53    def strip(element):
54        if element.uri == namespace:
55            element.uri = None
56            if element.defaultUri == namespace:
57                element.defaultUri = None
58            for child in element.elements():
59                strip(child)
60
61    if namespace is not None:
62        strip(rootElement)
63
64    return rootElement
65
66
67
68class FallbackHandler(XMPPHandler):
69    """
70    XMPP subprotocol handler that catches unhandled iq requests.
71
72    Unhandled iq requests are replied to with a service-unavailable stanza
73    error.
74    """
75
76    def connectionInitialized(self):
77        self.xmlstream.addObserver(IQ_SET, self.iqFallback, -1)
78        self.xmlstream.addObserver(IQ_GET, self.iqFallback, -1)
79
80    def iqFallback(self, iq):
81        if iq.handled == True:
82            return
83
84        reply = error.StanzaError('service-unavailable')
85        self.xmlstream.send(reply.toResponse(iq))
86
87
88
89class VersionHandler(XMPPHandler):
90    """
91    XMPP subprotocol handler for XMPP Software Version.
92
93    This protocol is described in
94    U{XEP-0092<http://xmpp.org/extensions/xep-0092.html>}.
95    """
96
97    implements(IDisco)
98
99    def __init__(self, name, version):
100        self.name = name
101        self.version = version
102
103    def connectionInitialized(self):
104        self.xmlstream.addObserver(VERSION, self.onVersion)
105
106    def onVersion(self, iq):
107        response = toResponse(iq, "result")
108
109        query = response.addElement((NS_VERSION, "query"))
110        query.addElement("name", content=self.name)
111        query.addElement("version", content=self.version)
112        self.send(response)
113
114        iq.handled = True
115
116    def getDiscoInfo(self, requestor, target, node):
117        info = set()
118
119        if not node:
120            from wokkel import disco
121            info.add(disco.DiscoFeature(NS_VERSION))
122
123        return defer.succeed(info)
124
125    def getDiscoItems(self, requestor, target, node):
126        return defer.succeed([])
127
128
129
130class XmlPipe(object):
131    """
132    XML stream pipe.
133
134    Connects two objects that communicate stanzas through an XML stream like
135    interface. Each of the ends of the pipe (sink and source) can be used to
136    send XML stanzas to the other side, or add observers to process XML stanzas
137    that were sent from the other side.
138
139    XML pipes are usually used in place of regular XML streams that are
140    transported over TCP. This is the reason for the use of the names source
141    and sink for both ends of the pipe. The source side corresponds with the
142    entity that initiated the TCP connection, whereas the sink corresponds with
143    the entity that accepts that connection. In this object, though, the source
144    and sink are treated equally.
145
146    Unlike Jabber
147    L{XmlStream<twisted.words.protocols.jabber.xmlstream.XmlStream>}s, the sink
148    and source objects are assumed to represent an eternal connected and
149    initialized XML stream. As such, events corresponding to connection,
150    disconnection, initialization and stream errors are not dispatched or
151    processed.
152
153    @ivar source: Source XML stream.
154    @ivar sink: Sink XML stream.
155    """
156
157    def __init__(self):
158        self.source = utility.EventDispatcher()
159        self.sink = utility.EventDispatcher()
160        self.source.send = lambda obj: self.sink.dispatch(obj)
161        self.sink.send = lambda obj: self.source.dispatch(obj)
162
163
164
165class Stanza(object):
166    """
167    Abstract representation of a stanza.
168
169    @ivar sender: The sending entity.
170    @type sender: L{jid.JID}
171    @ivar recipient: The receiving entity.
172    @type recipient: L{jid.JID}
173    """
174
175    recipient = None
176    sender = None
177    stanzaKind = None
178    stanzaID = None
179    stanzaType = None
180
181    def __init__(self, recipient=None, sender=None):
182        self.recipient = recipient
183        self.sender = sender
184
185
186    @classmethod
187    def fromElement(Class, element):
188        """
189        Create a stanza from a L{domish.Element}.
190        """
191        stanza = Class()
192        stanza.parseElement(element)
193        return stanza
194
195
196    def parseElement(self, element):
197        """
198        Parse the stanza element.
199
200        This is called with the stanza's element when a L{Stanza} is
201        created using L{fromElement}. It parses the stanza's core attributes
202        (addressing, type and id), strips the namespace from the stanza
203        element for easier transport across streams and passes on
204        child elements for further parsing.
205
206        Child element parsers are defined by providing a C{childParsers}
207        attribute on a subclass, as a mapping from (URI, name) to the name
208        of the handler on C{self}. C{parseElement} will accumulate
209        C{childParsers} from its class hierarchy, iterate over the child
210        elements and pass it to matching handlers based on the child element's
211        URI and name. The special key of C{None} can be used to pass all
212        child elements to.
213        """
214        if element.hasAttribute('from'):
215            self.sender = jid.internJID(element['from'])
216        if element.hasAttribute('to'):
217            self.recipient = jid.internJID(element['to'])
218        self.stanzaType = element.getAttribute('type')
219        self.stanzaID = element.getAttribute('id')
220
221        # Save element
222        stripNamespace(element)
223        self.element = element
224
225        # accumulate all childHandlers in the class hierarchy of Class
226        handlers = {}
227        reflect.accumulateClassDict(self.__class__, 'childParsers', handlers)
228
229        for child in element.elements():
230            try:
231                handler = handlers[child.uri, child.name]
232            except KeyError:
233                try:
234                    handler = handlers[None]
235                except KeyError:
236                    continue
237
238            getattr(self, handler)(child)
239
240
241    def toElement(self):
242        element = domish.Element((None, self.stanzaKind))
243        if self.sender is not None:
244            element['from'] = self.sender.full()
245        if self.recipient is not None:
246            element['to'] = self.recipient.full()
247        if self.stanzaType:
248            element['type'] = self.stanzaType
249        if self.stanzaID:
250            element['id'] = self.stanzaID
251        return element
252
253
254
255class ErrorStanza(Stanza):
256
257    def parseElement(self, element):
258        Stanza.parseElement(self, element)
259        self.exception = error.exceptionFromStanza(element)
260
261
262
263class Request(Stanza):
264    """
265    IQ request stanza.
266
267    This is a base class for IQ get or set stanzas, to be used with
268    L{wokkel.subprotocols.StreamManager.request}.
269    """
270
271    stanzaKind = 'iq'
272    stanzaType = 'get'
273    timeout = None
274
275    childParsers = {None: 'parseRequest'}
276
277    def __init__(self, recipient=None, sender=None, stanzaType=None):
278        Stanza.__init__(self, recipient=recipient, sender=sender)
279        if stanzaType is not None:
280            self.stanzaType = stanzaType
281
282
283    def parseRequest(self, element):
284        """
285        Called with the request's child element for parsing.
286
287        When a request instance is created using L{fromElement}, this method
288        is called with the child element of the iq. Override this method for
289        parsing the request's payload.
290        """
291
292
293    def toElement(self):
294        element = Stanza.toElement(self)
295
296        if not self.stanzaID:
297            element.addUniqueId()
298            self.stanzaID = element['id']
299
300        return element
301
302
303
304class DeferredXmlStreamFactory(BootstrapMixin, protocol.ClientFactory):
305    protocol = xmlstream.XmlStream
306
307    def __init__(self, authenticator):
308        BootstrapMixin.__init__(self)
309
310        self.authenticator = authenticator
311
312        deferred = defer.Deferred()
313        self.deferred = deferred
314        self.addBootstrap(xmlstream.STREAM_AUTHD_EVENT, self.deferred.callback)
315        self.addBootstrap(xmlstream.INIT_FAILED_EVENT, deferred.errback)
316
317
318    def buildProtocol(self, addr):
319        """
320        Create an instance of XmlStream.
321
322        A new authenticator instance will be created and passed to the new
323        XmlStream. Registered bootstrap event observers are installed as well.
324        """
325        xs = self.protocol(self.authenticator)
326        xs.factory = self
327        self.installBootstraps(xs)
328        return xs
329
330
331    def clientConnectionFailed(self, connector, reason):
332        self.deferred.errback(reason)
333
334
335
336@deprecated(Version("Wokkel", 0, 8, 0), "unicode.encode('idna')")
337def prepareIDNName(name):
338    """
339    Encode a unicode IDN Domain Name into its ACE equivalent.
340
341    This will encode the domain labels, separated by allowed dot code points,
342    to their ASCII Compatible Encoding (ACE) equivalent, using punycode. The
343    result is an ASCII byte string of the encoded labels, separated by the
344    standard full stop.
345    """
346    return name.encode('idna')
Note: See TracBrowser for help on using the repository browser.