view sat_pubsub/mam.py @ 316:cca47e9977a5

mam: minor improvments (module import and some checks)
author Goffi <goffi@goffi.org>
date Sun, 03 Jan 2016 18:33:22 +0100
parents a776544d84e5
children 54d90c73b8b5
line wrap: on
line source

#!/usr/bin/python
#-*- coding: utf-8 -*-

# Copyright (c) 2016 Jérôme Poisson
# Copyright (c) 2015-2016 Adrien Cossa
#
# 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/>.

"""
XMPP Message Archive Management protocol.

This protocol is specified in
U{XEP-0313<http://xmpp.org/extensions/xep-0313.html>}.
"""


from zope.interface import implements

from twisted.words.xish import domish

from sat_pubsub import const
from sat_pubsub.backend import PubSubResourceFromBackend
from wokkel.pubsub import NS_PUBSUB_EVENT

from dateutil import parser

# TODO: change this when RSM and MAM are in wokkel
from sat.tmp.wokkel import rsm
from sat.tmp.wokkel import mam

NS_CLIENT = 'jabber:client'


class MAMResource(object):

    implements(mam.IMAMResource)

    def __init__(self, backend):
        self.backend = backend

    def onArchiveRequest(self, mam, requestor):
        """

        @param mam: The MAM <query/> request.
        @type mam: L{MAMQueryReques<wokkel.mam.MAMQueryRequest>}

        @param requestor: JID of the requestor.
        @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @return: A tuple with list of message data (id, element, data) and RSM element
        @rtype: C{tuple}
        """
        # FIXME: bad result ordering
        ext_data = {}
        if mam.form:
            ext_data['filters'] = mam.form.fields.values()
        if mam.rsm is None:
            mam.rsm = rsm.RSMRequest(const.VAL_RSM_MAX_DEFAULT)
        ext_data['rsm'] = mam.rsm

        d = self.backend.getItems(mam.node, requestor, mam.rsm.max, None, ext_data)

        def make_message(elt):
            # XXX: http://xmpp.org/extensions/xep-0297.html#sect-idp629952 (rule 3)
            message = domish.Element((NS_CLIENT, "message"))
            event = message.addElement((NS_PUBSUB_EVENT, "event"))
            items = event.addElement('items')
            items["node"] = mam.node
            items.addChild(elt)
            return message

        def cb(elts):
            msg_data = []
            rsm_elt = None
            for elt in elts:
                if elt.name == 'set' and elt.uri == rsm.NS_RSM:
                    assert rsm_elt is None
                    rsm_elt = elt
                elif elt.name == 'item':
                    # FIXME: this is not good as it is dependant on payload
                    # TODO: remove this and use date field in database
                    date = parser.parse(''.join(elt.entry.published.children))
                    msg_data.append([elt['id'], make_message(elt), date])
            return (msg_data, rsm_elt)

        d.addErrback(PubSubResourceFromBackend._mapErrors)
        d.addCallback(cb)
        return d

    def onPrefsGetRequest(self, requestor):
        """

        @param requestor: JID of the requestor.
        @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @return: The current settings.
        @rtype: L{wokkel.mam.MAMPrefs}
        """
        # TODO: return the actual current settings
        return mam.MAMPrefs()

    def onPrefsSetRequest(self, prefs, requestor):
        """

        @param prefs: The new settings to set.
        @type prefs: L{wokkel.mam.MAMPrefs}

        @param requestor: JID of the requestor.
        @type requestor: L{JID<twisted.words.protocols.jabber.jid.JID>}

        @return: The settings that have actually been set.
        @rtype: L{wokkel.mam.MAMPrefs}
        """
        # TODO: set the new settings and return them
        return mam.MAMPrefs()