view wokkel/delay.py @ 17:fd6957dfa8c6 0.6.0

tmp (wokkel.pubsub): implemented missing notifyRetract
author Goffi <goffi@goffi.org>
date Wed, 25 Nov 2015 15:35:21 +0100
parents a07bf3fb4b54
children
line wrap: on
line source

# -*- coding: utf-8 -*-
# -*- test-case-name: wokkel.test.test_delay -*-
#
# SàT adaptation for wokkel.delay
# Copyright (C) 2015 Adien Cossa (souliane@mailoo.org)
# Copyright (c) 2003-2012 Ralph Meijer.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# --

# This program is based on wokkel (https://wokkel.ik.nu/),
# originaly written by Ralph Meijer
# It is sublicensed under AGPL v3 (or any later version) as allowed by the original
# license.

# --

# Here is a copy of the original license:

# Copyright (c) 2003-2012 Ralph Meijer.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Copyright (c) Ralph Meijer.
# See LICENSE for details.

"""
Delayed Delivery.

Support for comunicating Delayed Delivery information as specified by
U{XEP-0203<http://xmpp.org/extensions/xep-0203.html>} and its predecessor
U{XEP-0091<http://xmpp.org/extensions/xep-0091.html>}.
"""

from dateutil.parser import parse
from dateutil.tz import tzutc

from twisted.words.protocols.jabber.jid import InvalidFormat, JID
from twisted.words.xish import domish

NS_DELAY = 'urn:xmpp:delay'
NS_JABBER_DELAY = 'jabber:x:delay'

class Delay(object):
    """
    Delayed Delivery information.

    Instances of this class represent delayed delivery information that can be
    parsed from and rendered into both XEP-0203 and legacy XEP-0091 formats.

    @ivar stamp: The timestamp the stanza was originally sent.
    @type stamp: L{datetime.datetime}
    @ivar sender: The optional entity that originally sent the stanza or
        delayed its delivery.
    @type sender: L{JID}
    """

    def __init__(self, stamp, sender=None):
        self.stamp = stamp
        self.sender = sender


    def toElement(self, legacy=False):
        """
        Render this instance into a domish Element.

        @param legacy: If C{True}, use the legacy XEP-0091 format.
        @type legacy: C{bool}
        """
        if not self.stamp:
            raise ValueError("stamp is required")
        if self.stamp.tzinfo is None:
            raise ValueError("stamp is not offset-aware")

        if legacy:
            element = domish.Element((NS_JABBER_DELAY, 'x'))
            stampFormat = '%Y%m%dT%H:%M:%S'
        else:
            element = domish.Element((NS_DELAY, 'delay'))
            stampFormat = '%Y-%m-%dT%H:%M:%SZ'

        stamp = self.stamp.astimezone(tzutc())
        element['stamp'] = stamp.strftime(stampFormat)

        if self.sender:
            element['from'] = self.sender.full()

        return element


    @staticmethod
    def fromElement(element):
        """
        Create an instance from a domish Element.
        """
        try:
            stamp = parse(element[u'stamp'])

            # Assume UTC if no timezone was given
            if stamp.tzinfo is None:
                stamp = stamp.replace(tzinfo=tzutc())
        except (KeyError, ValueError, TypeError):
            stamp = None

        try:
            sender = JID(element[u'from'])
        except (KeyError, InvalidFormat):
            sender = None

        delay = Delay(stamp, sender)
        return delay



class DelayMixin(object):
    """
    Mixin for parsing delayed delivery information from stanzas.

    This can be used as a mixin for subclasses of L{wokkel.generic.Stanza}
    for parsing delayed delivery information. If both XEP-0203 and XEP-0091
    formats are present, the former takes precedence.
    """

    delay = None

    childParsers = {
            (NS_DELAY, 'delay'): '_childParser_delay',
            (NS_JABBER_DELAY, 'x'): '_childParser_legacyDelay',
            }


    def _childParser_delay(self, element):
        self.delay = Delay.fromElement(element)


    def _childParser_legacyDelay(self, element):
        if not self.delay:
            self.delay = Delay.fromElement(element)