Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0277.py @ 643:262d9d9ad27a
plugin XEP-0085: renamed category and parameter
author | souliane <souliane@mailoo.org> |
---|---|
date | Sat, 07 Sep 2013 19:50:12 +0200 |
parents | 47d3a22b4629 |
children | 69a8bfd266a5 |
rev | line source |
---|---|
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
4 # SAT plugin for microblogging over XMPP (xep-0277) |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
10 # (at your option) any later version. |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
15 # GNU Affero General Public License for more details. |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
20 from logging import debug, info, warning, error |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
21 from twisted.words.protocols.jabber import jid |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
22 from sat.core import exceptions |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.tools.xml_tools import ElementParser |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
25 from wokkel import pubsub |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
26 from feed import atom |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 import uuid |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from time import time |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
29 import urlparse |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 NS_MICROBLOG = 'urn:xmpp:microblog:0' |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 PLUGIN_INFO = { |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
34 "name": "Microblogging over XMPP Plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
35 "import_name": "XEP-0277", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
36 "type": "XEP", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
37 "protocols": [], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
38 "dependencies": ["XEP-0163", "XEP-0060"], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
39 "main": "XEP_0277", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
40 "handler": "no", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
41 "description": _("""Implementation of microblogging Protocol""") |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 } |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
44 |
339
b0b773f432e5
plugin XEP-00277: setMicroblogAccess is now asynchronous
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
45 class NodeAccessChangeException(Exception): |
b0b773f432e5
plugin XEP-00277: setMicroblogAccess is now asynchronous
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
46 pass |
b0b773f432e5
plugin XEP-00277: setMicroblogAccess is now asynchronous
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
47 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
48 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
49 class XEP_0277(object): |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 def __init__(self, host): |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 info(_("Microblogging plugin initialization")) |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 self.host = host |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog) |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
343
diff
changeset
|
55 host.bridge.addMethod("getLastMicroblogs", ".plugin", |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
56 in_sign='sis', out_sign='aa{ss}', |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
57 method=self.getLastMicroblogs, |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
58 async=True, |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
59 doc={'summary': 'retrieve items', |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
60 'param_0': 'jid: publisher of wanted microblog', |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
61 'param_1': 'max_items: see XEP-0060 #6.5.7', |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
62 'param_2': '%(doc_profile)s', |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
63 'return': 'list of microblog data (dict)'}) |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
343
diff
changeset
|
64 host.bridge.addMethod("setMicroblogAccess", ".plugin", in_sign='ss', out_sign='', |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
65 method=self.setMicroblogAccess, |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
66 async=True, |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
67 doc={}) |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
68 |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
69 def parseCommentUrl(self, node_url): |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
70 parsed_url = urlparse.urlparse(node_url, 'xmpp') |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
71 service = jid.JID(parsed_url.path) |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
72 queries = parsed_url.query.split(';') |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
73 parsed_queries = dict() |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
74 for query in queries: |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
75 parsed_queries.update(urlparse.parse_qs(query)) |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
76 node = parsed_queries.get('node',[''])[0] |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
77 |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
78 if not node: |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
79 raise exceptions.DataError('Invalid comments link') |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
80 |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
81 return (service, node) |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
82 |
465
78e67a59d51d
plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
83 def item2mbdata(self, item): |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
84 """Convert an XML Item to microblog data used in bridge API |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
85 @param item: domish.Element of microblog item |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
86 @return: microblog data (dictionary)""" |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
87 try: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
88 entry_elt = filter(lambda x: x.name == "entry", item.children)[0] |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
89 except KeyError: |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
90 warning(_('No entry element in microblog item')) |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
91 return |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
92 _entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8')) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
93 microblog_data = {} |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
94 try: |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
95 microblog_data['content'] = _entry.title.text |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
96 if len(_entry.authors): |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
97 microblog_data['author'] = _entry.authors[0].name.text |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
98 microblog_data['timestamp'] = str(int(_entry.updated.tf)) |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
99 microblog_data['id'] = item['id'] |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
100 for link in _entry.links: |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
101 try: |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
102 if link.attrs["title"] == "comments": |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
103 microblog_data['comments'] = link.attrs["href"] |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
104 service, node = self.parseCommentUrl(microblog_data["comments"]) |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
105 microblog_data['comments_service'] = service.full() |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
106 microblog_data['comments_node'] = node |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
107 break |
623
47d3a22b4629
plugin XEP-0277: RuntimeError is now catched during link parsing (can happen if no service jid is found)
Goffi <goffi@goffi.org>
parents:
615
diff
changeset
|
108 except (KeyError, exceptions.DataError, RuntimeError): |
47d3a22b4629
plugin XEP-0277: RuntimeError is now catched during link parsing (can happen if no service jid is found)
Goffi <goffi@goffi.org>
parents:
615
diff
changeset
|
109 warning("Can't parse link") |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
110 continue |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
111 |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
112 except (AttributeError, KeyError): |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
113 error(_('Error while parsing atom entry for microblogging event')) |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
114 return {} |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
115 |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
116 ##XXX: workaround for Jappix behaviour |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
117 if not 'author' in microblog_data: |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
118 from xe import NestElement |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
119 try: |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
120 author = NestElement('author') |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
121 author.import_xml(str(_entry)) |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
122 microblog_data['author'] = author.nick.text |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
123 except: |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
124 error(_('Cannot find author')) |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
125 ##end workaround Jappix |
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
126 return microblog_data |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 def microblogCB(self, itemsEvent, profile): |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 for item in itemsEvent.items: |
465
78e67a59d51d
plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
130 microblog_data = self.item2mbdata(item) |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile) |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
133 def data2entry(self, data, profile): |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
134 """Convert a data dict to en entry usable to create an item |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
135 @param data: data dict as given by bridge method |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
136 @return: domish.Element""" |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
137 _uuid = unicode(uuid.uuid1()) |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 content = data['content'] |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
139 _entry = atom.Entry() |
297
c5554e2939dd
plugin XEP 0277: author for in request + author, updated management for out request
Goffi <goffi@goffi.org>
parents:
293
diff
changeset
|
140 #FIXME: need to escape html |
304 | 141 _entry.title = unicode(content).encode('utf-8') |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
142 _entry.author = atom.Author() |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
143 _entry.author.name = data.get('author', self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
144 _entry.updated = float(data.get('timestamp', time())) |
304 | 145 _entry.id = str(_uuid) |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
146 if 'comments' in data: |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
147 link = atom.Link() |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
148 link.attrs['href'] = data['comments'] |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
149 link.attrs['rel'] = 'replies' |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
150 link.attrs['title'] = 'comments' |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
151 _entry.links.append(link) |
304 | 152 _entry_elt = ElementParser()(str(_entry).decode('utf-8')) |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 item = pubsub.Item(payload=_entry_elt) |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 item['id'] = _uuid |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
155 return item |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
156 |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
157 def sendMicroblog(self, data, profile): |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
158 """Send XEP-0277's microblog data |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
159 @param data: must include content |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
160 @param profile: profile which send the mood""" |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
161 if 'content' not in data: |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
162 error(_("Microblog data must contain at least 'content' key")) |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
163 return 3 |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
164 content = data['content'] |
309
f1a3db8ee04a
plugin xep-0277: fixed bad data check
Goffi <goffi@goffi.org>
parents:
306
diff
changeset
|
165 if not content: |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
166 error(_("Microblog data's content value must not be empty")) |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
167 return 3 |
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
168 item = self.data2entry(data, profile) |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
169 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key=profile) |
293
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 return 0 |
42438e43104a
Plugin XEP-0277: first draft of microblogging over ip /!\ new dependencies added /!\
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 |
468
c97640c90a94
D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents:
465
diff
changeset
|
172 def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'): |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
173 """Get the last published microblogs |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
174 @param pub_jid: jid of the publisher |
465
78e67a59d51d
plugin groupblog: added getLastGroupBlogs method
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
175 @param max_items: how many microblogs we want to get |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
176 @param profile_key: profile key |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
177 """ |
302
9f3a6cf91668
plugin xep-0277: added getLastMicroblogs method
Goffi <goffi@goffi.org>
parents:
297
diff
changeset
|
178 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key) |
468
c97640c90a94
D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents:
465
diff
changeset
|
179 d.addCallback(lambda items: map(self.item2mbdata, items)) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
180 |
468
c97640c90a94
D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents:
465
diff
changeset
|
181 def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'): |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
182 """Create a microblog node on PEP with given access |
304 | 183 If the node already exists, it change options |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
184 @param access: Node access model, according to xep-0060 #4.5 |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
185 @param profile_key: profile key""" |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
186 |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
187 _jid, xmlstream = self.host.getJidNStream(profile_key) |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
188 if not _jid: |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
189 error(_("Can't find profile's jid")) |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
190 return |
615
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
191 C = self.host.plugins["XEP-0060"] |
6f4c31192c7c
plugins XEP-0060, XEP-0277, groupblog: comments implementation (first draft, not finished yet):
Goffi <goffi@goffi.org>
parents:
609
diff
changeset
|
192 _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} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
193 |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
194 def cb(result): |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
195 #Node is created with right permission |
304 | 196 debug(_("Microblog node has now access %s") % access) |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
197 |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
198 def fatal_err(s_error): |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
199 #Something went wrong |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
200 error(_("Can't set microblog access")) |
468
c97640c90a94
D-Bus Bridge: use inspection to name attribute + fix asynchronous calls for dynamically added method, it now use deferred return value instead of callback/errback attributes
Goffi <goffi@goffi.org>
parents:
465
diff
changeset
|
201 raise NodeAccessChangeException() |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
202 |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
203 def err_cb(s_error): |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
204 #If the node already exists, the condition is "conflict", |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
205 #else we have an unmanaged error |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
206 if s_error.value.condition == 'conflict': |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
207 #d = self.host.plugins["XEP-0060"].deleteNode(_jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key) |
304 | 208 #d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err)) |
209 change_node_options().addCallback(cb).addErrback(fatal_err) | |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
210 else: |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
211 fatal_err(s_error) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
212 |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
213 def create_node(): |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
214 return self.host.plugins["XEP-0060"].createNode(_jid.userhostJID(), NS_MICROBLOG, _options, profile_key=profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
215 |
304 | 216 def change_node_options(): |
306
169e7386650a
plugin xep-0277: bridge data is now converted in pubsub item in a separate function
Goffi <goffi@goffi.org>
parents:
304
diff
changeset
|
217 return self.host.plugins["XEP-0060"].setOptions(_jid.userhostJID(), NS_MICROBLOG, _jid.userhostJID(), _options, profile_key=profile_key) |
303
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
218 |
2b52a5da0978
plugin XEP_0277: microblog access model can now be changed
Goffi <goffi@goffi.org>
parents:
302
diff
changeset
|
219 create_node().addCallback(cb).addErrback(err_cb) |