Mercurial > libervia-backend
view src/plugins/plugin_xep_0277.py @ 301:e33b3a777f10
plugin xep-0060: added getEntry method
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 21 Feb 2011 01:36:52 +0100 |
parents | c5554e2939dd |
children | 9f3a6cf91668 |
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, Author 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 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'] except AttributeError, KeyError: error(_('Error while parsing atom entry for microblogging event')) return ##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 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() #FIXME: need to escape html _entry.title = content.encode('utf-8') _entry.author = 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 = _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