comparison 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
comparison
equal deleted inserted replaced
292:f7bd973bba5a 293:42438e43104a
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT plugin for microblogging over XMPP (xep-0277)
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, error
23 from twisted.internet import protocol
24 from twisted.words.protocols.jabber import client, jid
25 from twisted.words.protocols.jabber import error as jab_error
26 import twisted.internet.error
27 from twisted.words.xish import domish
28 from sat.tools.xml_tools import ElementParser
29
30 from wokkel import disco,pubsub
31 from feed.atom import Entry
32 import uuid
33 from time import time
34
35 NS_MICROBLOG = 'urn:xmpp:microblog:0'
36
37 PLUGIN_INFO = {
38 "name": "Microblogging over XMPP Plugin",
39 "import_name": "XEP-0277",
40 "type": "XEP",
41 "protocols": [],
42 "dependencies": ["XEP-0163"],
43 "main": "XEP_0277",
44 "handler": "no",
45 "description": _("""Implementation of microblogging Protocol""")
46 }
47
48 class XEP_0277():
49
50 def __init__(self, host):
51 info(_("Microblogging plugin initialization"))
52 self.host = host
53 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog)
54
55 def microblogCB(self, itemsEvent, profile):
56 _entry = None
57 for item in itemsEvent.items:
58 try:
59 entry_elt = filter (lambda x:x.name == "entry", item.children)[0]
60 except KeyError:
61 warning(_('No entry element in microblog item'))
62 return
63 _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8'))
64 microblog_data={}
65 try:
66 microblog_data['content'] = _entry.title.text
67 microblog_data['timestamp'] = str(int(_entry.updated.tf))
68 microblog_data['id'] = item['id']
69 except AttributeError, KeyError:
70 error(_('Error while parsing atom entry for microblogging event'))
71 return
72 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
73
74 def sendMicroblog(self, data, profile):
75 """Send XEP-0277's microblog data
76 @param data: must include content
77 @param profile: profile which send the mood"""
78 if not data.has_key('content'):
79 error(_("Microblog data must contain at least 'content' key"))
80 return 3
81 content = data['content']
82 if not content:
83 error(_("Microblog data's content value must not be empty"))
84 _uuid = unicode(uuid.uuid1())
85 _entry = Entry()
86 _entry.title = content.encode('utf-8')
87 _entry.updated = time()
88 _entry.id = _uuid
89 _entry_elt = ElementParser()(str(_entry))
90 item = pubsub.Item(payload=_entry_elt)
91 item['id'] = _uuid
92 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile)
93 return 0
94