view src/plugins/plugin_xep_0277.py @ 293:42438e43104a

Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\ - the xe and pyfeed dependencies were added (see setup.py)
author Goffi <goffi@goffi.org>
date Tue, 08 Feb 2011 02:16:26 +0100
parents
children c5554e2939dd
line wrap: on
line source

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

"""
SAT plugin for microblogging over XMPP (xep-0277)
Copyright (C) 2009, 2010, 2011  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 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 General Public License for more details.

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

from logging import debug, info, error
from twisted.internet import protocol
from twisted.words.protocols.jabber import client, jid
from twisted.words.protocols.jabber import error as jab_error
import twisted.internet.error
from twisted.words.xish import domish
from sat.tools.xml_tools import ElementParser

from wokkel import disco,pubsub
from feed.atom import Entry
import uuid
from time import time

NS_MICROBLOG = 'urn:xmpp:microblog:0'

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

class XEP_0277():

    def __init__(self, host):
        info(_("Microblogging plugin initialization"))
        self.host = host
        self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog)

    def microblogCB(self, itemsEvent, profile):
        _entry = None
        for item in itemsEvent.items:
            try:
                entry_elt = filter (lambda x:x.name == "entry", item.children)[0]
            except KeyError:
                warning(_('No entry element in microblog item'))
                return
            _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8'))
            microblog_data={}
            try:
                microblog_data['content'] = _entry.title.text
                microblog_data['timestamp'] = str(int(_entry.updated.tf))
                microblog_data['id'] = item['id']
            except AttributeError, KeyError:
                error(_('Error while parsing atom entry for microblogging event'))
                return
            self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)

    def sendMicroblog(self, data, profile):
        """Send XEP-0277's microblog data
        @param data: must include content
        @param profile: profile which send the mood"""
        if not data.has_key('content'):
            error(_("Microblog data must contain at least 'content' key"))
            return 3
        content = data['content']
        if not content:
            error(_("Microblog data's content value must not be empty"))
        _uuid = unicode(uuid.uuid1())
        _entry = Entry()
        _entry.title = content.encode('utf-8')
        _entry.updated = time()
        _entry.id = _uuid
        _entry_elt = ElementParser()(str(_entry))
        item = pubsub.Item(payload=_entry_elt)
        item['id'] = _uuid
        self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile)
        return 0