view sat_pubsub/mam.py @ 282:7d54ff2eeaf2

actually retrieve the MAM messages
author souliane <souliane@mailoo.org>
date Thu, 15 Jan 2015 22:00:49 +0100
parents 30895c49ebd2
children a776544d84e5
line wrap: on
line source

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

"""
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.rsm import RSMRequest
from sat.tmp.wokkel.mam import MAMPrefs, IMAMResource

NS_CLIENT = 'jabber:client'


class MAMResource(object):

    implements(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: The RSM answer.
        @rtype: L{RSMResponse<wokkel.rsm.RSMResponse>}
        """
        ext_data = {}
        if mam.form:
            ext_data['filters'] = mam.form.fields.values()
        if mam.rsm is None:
            mam.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':
                    rsm_elt = elt
                elif elt.name == 'item':
                    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)
        return d.addCallback(cb)

    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 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 MAMPrefs()