view src/plugins/plugin_xep_0277.py @ 751:1def5b7edf9f

core, bridge: better GenericException handling
author Goffi <goffi@goffi.org>
date Tue, 17 Dec 2013 00:56:39 +0100
parents 812dc38c0094
children 64dd7c0f4feb
line wrap: on
line source

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

# SAT plugin for microblogging over XMPP (xep-0277)
# Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org)

# 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/>.

from logging import debug, info, warning, error
from twisted.words.protocols.jabber import jid
from twisted.internet import defer
from sat.core import exceptions
from sat.tools.xml_tools import ElementParser

from wokkel import pubsub
from feed import atom
import uuid
from time import time
import urlparse
from cgi import escape

NS_MICROBLOG = 'urn:xmpp:microblog:0'
NS_XHTML = 'http://www.w3.org/1999/xhtml'

PLUGIN_INFO = {
    "name": "Microblogging over XMPP Plugin",
    "import_name": "XEP-0277",
    "type": "XEP",
    "protocols": [],
    "dependencies": ["XEP-0163", "XEP-0060", "TEXT-SYNTAXES"],
    "main": "XEP_0277",
    "handler": "no",
    "description": _("""Implementation of microblogging Protocol""")
}


class NodeAccessChangeException(Exception):
    pass


class XEP_0277(object):

    def __init__(self, host):
        info(_("Microblogging plugin initialization"))
        self.host = host
        self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog)
        host.bridge.addMethod("getLastMicroblogs", ".plugin",
                              in_sign='sis', out_sign='aa{ss}',
                              method=self.getLastMicroblogs,
                              async=True,
                              doc={'summary': 'retrieve items',
                                   'param_0': 'jid: publisher of wanted microblog',
                                   'param_1': 'max_items: see XEP-0060 #6.5.7',
                                   'param_2': '%(doc_profile)s',
                                   'return': 'list of microblog data (dict)'})
        host.bridge.addMethod("setMicroblogAccess", ".plugin", in_sign='ss', out_sign='',
                              method=self.setMicroblogAccess,
                              async=True,
                              doc={})

    def parseCommentUrl(self, node_url):
        parsed_url = urlparse.urlparse(node_url, 'xmpp')
        service = jid.JID(parsed_url.path)
        queries = parsed_url.query.split(';')
        parsed_queries = dict()
        for query in queries:
            parsed_queries.update(urlparse.parse_qs(query))
        node = parsed_queries.get('node',[''])[0]

        if not node:
            raise exceptions.DataError('Invalid comments link')

        return (service, node)

    @defer.inlineCallbacks
    def item2mbdata(self, item):
        """Convert an XML Item to microblog data used in bridge API
        @param item: domish.Element of microblog item
        @return: microblog data (dictionary)"""
        try:
            entry_elt = [child for child in item.elements() if child.name == "entry"][0]
        except IndexError:
            warning(_('No entry element in microblog item'))
            raise exceptions.DataError('no entry found')
        _entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8'))
        microblog_data = {}
        try:
            try:
                content_type =_entry.title.attrs['type'].lower()
            except KeyError:
                content_type = 'text'
            if content_type == 'xhtml':
                # TODO: proper check of body namespace
                microblog_data['xhtml'] = yield self.host.plugins["TEXT-SYNTAXES"].clean_xhtml(_entry.title.text)
                microblog_data['content'] = _entry.title.text # FIXME: must use text version of the microblog, or convert XHTML to text if not available
            else:
                microblog_data['content'] = _entry.title.text
            if len(_entry.authors):
                microblog_data['author'] = _entry.authors[0].name.text
            microblog_data['timestamp'] = str(int(_entry.updated.tf))
            microblog_data['id'] = item['id']
            for link in _entry.links:
                try:
                    if link.attrs["title"] == "comments":
                        microblog_data['comments'] = link.attrs["href"]
                        service, node = self.parseCommentUrl(microblog_data["comments"])
                        microblog_data['comments_service'] = service.full()
                        microblog_data['comments_node'] = node
                        break
                except (KeyError, exceptions.DataError, RuntimeError):
                    warning("Can't parse link")
                    continue

        except (AttributeError, KeyError):
            error(_('Error while parsing atom entry for microblogging event'))
            raise exceptions.DataError

        ##XXX: workaround for Jappix behaviour
        if not 'author' in microblog_data:
            from xe import NestElement
            try:
                author = NestElement('author')
                author.import_xml(str(_entry))
                microblog_data['author'] = author.nick.text
            except:
                error(_('Cannot find author'))
        ##end workaround Jappix

        defer.returnValue(microblog_data)

    def microblogCB(self, itemsEvent, profile):
        d = defer.Deferred()

        def manageItem(microblog_data):
            self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)

        for item in itemsEvent.items:
            d.addCallback(lambda ignore: self.item2mbdata(item))
            d.addCallback(manageItem)

        d.callback(None)
        return d

    @defer.inlineCallbacks
    def data2entry(self, data, profile):
        """Convert a data dict to en entry usable to create an item
        @param data: data dict as given by bridge method
        @return: deferred which fire domish.Element"""
        _uuid = unicode(uuid.uuid1())
        _entry = atom.Entry()

        if "rich" in data:
            synt = self.host.plugins["TEXT-SYNTAXES"]
            converted = yield synt.convert(data['rich'], synt.getCurrentSyntax(profile), "XHTML")
            content = u'<div xmlns="%s">%s</div>' % (NS_XHTML, converted)
            _entry.title.attrs['type'] = 'xhtml'
        else:
            content = escape(data['content'])
            _entry.title.attrs['type'] = 'text'
        _entry.title = unicode(content).encode('utf-8')

        _entry.author = atom.Author()
        _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8')
        _entry.updated = float(data.get('timestamp', time()))
        entry_id = data.get('id', str(_uuid))
        _entry.id = entry_id
        if 'comments' in data:
            link = atom.Link()
            link.attrs['href'] = data['comments']
            link.attrs['rel'] = 'replies'
            link.attrs['title'] = 'comments'
            _entry.links.append(link)
        _entry_elt = ElementParser()(str(_entry).decode('utf-8'))
        item = pubsub.Item(id=entry_id, payload=_entry_elt)
        defer.returnValue(item)

    @defer.inlineCallbacks
    def sendMicroblog(self, data, profile):
        """Send XEP-0277's microblog data
        @param data: must include content
        @param profile: profile which send the mood"""
        if 'content' not in data:
            error(_("Microblog data must contain at least 'content' key"))
            raise exceptions.DataError('no "content" key found')
        content = data['content']
        if not content:
            error(_("Microblog data's content value must not be empty"))
            raise exceptions.DataError('empty content')
        item = yield self.data2entry(data, profile)
        ret = yield self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile)
        defer.returnValue(ret)

    def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'):
        """Get the last published microblogs
        @param pub_jid: jid of the publisher
        @param max_items: how many microblogs we want to get
        @param profile_key: profile key
        """
        def resultToArray(result):
            ret = []
            for (success, value) in result:
                if success:
                    ret.append(value)
                else:
                    error('Error while getting last microblog')
            return ret

        d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
        d.addCallback(lambda items: defer.DeferredList(map(self.item2mbdata, items)))
        d.addCallback(resultToArray)
        return d

    def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'):
        """Create a microblog node on PEP with given access
        If the node already exists, it change options
        @param access: Node access model, according to xep-0060 #4.5
        @param profile_key: profile key"""

        _jid, xmlstream = self.host.getJidNStream(profile_key)
        if not _jid:
            error(_("Can't find profile's jid"))
            return
        C = self.host.plugins["XEP-0060"]
        _options = {C.OPT_ACCESS_MODEL: access, C.OPT_PERSIST_ITEMS: 1, C.OPT_MAX_ITEMS: -1, C.OPT_DELIVER_PAYLOADS: 1, C.OPT_SEND_ITEM_SUBSCRIBE: 1}

        def cb(result):
            #Node is created with right permission
            debug(_("Microblog node has now access %s") % access)

        def fatal_err(s_error):
            #Something went wrong
            error(_("Can't set microblog access"))
            raise NodeAccessChangeException()

        def err_cb(s_error):
            #If the node already exists, the condition is "conflict",
            #else we have an unmanaged error
            if s_error.value.condition == 'conflict':
                #d = self.host.plugins["XEP-0060"].deleteNode(_jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key)
                #d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err))
                change_node_options().addCallback(cb).addErrback(fatal_err)
            else:
                fatal_err(s_error)

        def create_node():
            return self.host.plugins["XEP-0060"].createNode(_jid.userhostJID(), NS_MICROBLOG, _options, profile_key=profile_key)

        def change_node_options():
            return self.host.plugins["XEP-0060"].setOptions(_jid.userhostJID(), NS_MICROBLOG, _jid.userhostJID(), _options, profile_key=profile_key)

        create_node().addCallback(cb).addErrback(err_cb)