source: wokkel/delay.py @ 247:290573dcb97a

Last change on this file since 247:290573dcb97a was 216:c458fa7f5f0f, checked in by Ralph Meijer <ralphm@…>, 7 years ago

Handle TypeError? on malformed timestamp value.

This works around an obscure bug in dateutil reported by Adrien Cossa:
https://bugs.launchpad.net/dateutil/+bug/1247643.

File size: 3.2 KB
Line 
1# -*- test-case-name: wokkel.test.test_delay -*-
2#
3# Copyright (c) Ralph Meijer.
4# See LICENSE for details.
5
6"""
7Delayed Delivery.
8
9Support for comunicating Delayed Delivery information as specified by
10U{XEP-0203<http://xmpp.org/extensions/xep-0203.html>} and its predecessor
11U{XEP-0091<http://xmpp.org/extensions/xep-0091.html>}.
12"""
13
14from __future__ import division, absolute_import
15
16from dateutil.parser import parse
17from dateutil.tz import tzutc
18
19from twisted.words.protocols.jabber.jid import InvalidFormat, JID
20from twisted.words.xish import domish
21
22NS_DELAY = 'urn:xmpp:delay'
23NS_JABBER_DELAY = 'jabber:x:delay'
24
25class Delay(object):
26    """
27    Delayed Delivery information.
28
29    Instances of this class represent delayed delivery information that can be
30    parsed from and rendered into both XEP-0203 and legacy XEP-0091 formats.
31
32    @ivar stamp: The timestamp the stanza was originally sent.
33    @type stamp: L{datetime.datetime}
34    @ivar sender: The optional entity that originally sent the stanza or
35        delayed its delivery.
36    @type sender: L{JID}
37    """
38
39    def __init__(self, stamp, sender=None):
40        self.stamp = stamp
41        self.sender = sender
42
43
44    def toElement(self, legacy=False):
45        """
46        Render this instance into a domish Element.
47
48        @param legacy: If C{True}, use the legacy XEP-0091 format.
49        @type legacy: C{bool}
50        """
51        if not self.stamp:
52            raise ValueError("stamp is required")
53        if self.stamp.tzinfo is None:
54            raise ValueError("stamp is not offset-aware")
55
56        if legacy:
57            element = domish.Element((NS_JABBER_DELAY, 'x'))
58            stampFormat = '%Y%m%dT%H:%M:%S'
59        else:
60            element = domish.Element((NS_DELAY, 'delay'))
61            stampFormat = '%Y-%m-%dT%H:%M:%SZ'
62
63        stamp = self.stamp.astimezone(tzutc())
64        element['stamp'] = stamp.strftime(stampFormat)
65
66        if self.sender:
67            element['from'] = self.sender.full()
68
69        return element
70
71
72    @staticmethod
73    def fromElement(element):
74        """
75        Create an instance from a domish Element.
76        """
77        try:
78            stamp = parse(element[u'stamp'])
79
80            # Assume UTC if no timezone was given
81            if stamp.tzinfo is None:
82                stamp = stamp.replace(tzinfo=tzutc())
83        except (KeyError, ValueError, TypeError):
84            stamp = None
85
86        try:
87            sender = JID(element[u'from'])
88        except (KeyError, InvalidFormat):
89            sender = None
90
91        delay = Delay(stamp, sender)
92        return delay
93
94
95
96class DelayMixin(object):
97    """
98    Mixin for parsing delayed delivery information from stanzas.
99
100    This can be used as a mixin for subclasses of L{wokkel.generic.Stanza}
101    for parsing delayed delivery information. If both XEP-0203 and XEP-0091
102    formats are present, the former takes precedence.
103    """
104
105    delay = None
106
107    childParsers = {
108            (NS_DELAY, 'delay'): '_childParser_delay',
109            (NS_JABBER_DELAY, 'x'): '_childParser_legacyDelay',
110            }
111
112
113    def _childParser_delay(self, element):
114        self.delay = Delay.fromElement(element)
115
116
117    def _childParser_legacyDelay(self, element):
118        if not self.delay:
119            self.delay = Delay.fromElement(element)
Note: See TracBrowser for help on using the repository browser.