Mercurial > libervia-backend
annotate src/plugins/plugin_misc_groupblog.py @ 462:d9456d94cd12
plugin groupblog: next-gen group blog first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 14 Mar 2012 23:45:01 +0100 |
parents | cf005701624b |
children | 78e67a59d51d |
rev | line source |
---|---|
307 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for microbloging with roster access | |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
307 | 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 | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
23 from twisted.internet import protocol, defer |
307 | 24 from twisted.words.protocols.jabber import 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 | |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
30 from wokkel import disco, pubsub, data_form |
307 | 31 from feed.atom import Entry, Author |
32 import uuid | |
33 from time import time | |
34 | |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
35 NS_PUBSUB = 'http://jabber.org/protocol/pubsub' |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
36 #NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
37 NS_PUBSUB_EXP = NS_PUBSUB #XXX: we can't use custom namespace as Wokkel's PubSubService use official NS |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
38 NS_PUBSUB_ITEM_ACCESS = NS_PUBSUB_EXP + "#item-access" |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
39 NS_PUBSUB_CREATOR_JID_CHECK = NS_PUBSUB_EXP + "#creator-jid-check" |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
40 NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config" |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
41 NS_PUBSUB_AUTO_CREATE = NS_PUBSUB + "#auto-create" |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
42 OPT_ROSTER_GROUPS_ALLOWED = 'pubsub#roster_groups_allowed' |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
43 OPT_ACCESS_MODEL = 'pubsub#access_model' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
44 OPT_PERSIST_ITEMS = 'pubsub#persist_items' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
45 OPT_MAX_ITEMS = 'pubsub#max_items' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
46 OPT_NODE_TYPE = 'pubsub#node_type' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
47 OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type' |
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
48 OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth' |
307 | 49 TYPE_COLLECTION = 'collection' |
50 | |
51 PLUGIN_INFO = { | |
52 "name": "Group blogging throught collections", | |
53 "import_name": "groupblog", | |
54 "type": "MISC", | |
55 "protocols": [], | |
56 "dependencies": ["XEP-0277"], | |
57 "main": "GroupBlog", | |
58 "handler": "no", | |
59 "description": _("""Implementation of microblogging with roster access""") | |
60 } | |
61 | |
62 class NodeCreationError(Exception): | |
63 pass | |
64 | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
65 class NodeDeletionError(Exception): |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
66 pass |
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
67 |
307 | 68 class GroupBlog(): |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
69 """This class use a SàT PubSub Service to manage access on microblog""" |
307 | 70 |
71 def __init__(self, host): | |
72 info(_("Group blog plugin initialization")) | |
73 self.host = host | |
74 | |
372
f964dcec1611
core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents:
340
diff
changeset
|
75 host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='asss', out_sign='', |
307 | 76 method=self.sendGroupBlog, |
77 doc = { 'summary':"Send a microblog to a list of groups", | |
78 'param_0':'list of groups which can read the microblog', | |
79 'param_1':'text to send', | |
80 'param_2':'%(doc_profile)s' | |
81 }) | |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
82 |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
83 def _publishMblog(self, service, groups, message, client): |
307 | 84 """Actually publish the message on the group blog |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
85 @param service: jid of the item-access pubsub service |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
86 @param groups: set of groups allowed to see the item |
307 | 87 @param message: message to publish |
311
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
88 @param client: SatXMPPClient of the published""" |
0aa6ca6cdbdd
plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents:
310
diff
changeset
|
89 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile) |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
90 form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
91 field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=groups) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
92 form.addField(field) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
93 mblog_item.addChild(form.toElement()) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
94 defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile) |
307 | 95 defer_blog.addErrback(self._mblogPublicationFailed) |
96 | |
97 def _mblogPublicationFailed(self, failure): | |
98 #TODO | |
340
2572351d875a
plugin groupblog: removed breakpoints, but error handling still need work
Goffi <goffi@goffi.org>
parents:
326
diff
changeset
|
99 pass |
307 | 100 |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
101 @defer.inlineCallbacks |
307 | 102 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'): |
103 """Publish a microblog to the node associated to the groups | |
104 If the node doesn't exist, it is created, then the message is posted | |
105 @param groups: list of groups allowed to retrieve the microblog | |
106 @param message: microblog | |
107 @profile_key: %(doc_profile)s | |
108 """ | |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
109 print "sendGroupBlog" |
307 | 110 profile = self.host.memory.getProfileName(profile_key) |
111 if not profile: | |
112 error(_("Unknown profile")) | |
113 return | |
114 | |
115 client = self.host.getClient(profile) | |
116 if not client: | |
117 error(_('No client for this profile key: %s') % profile_key) | |
118 return | |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
119 |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
120 yield client.client_initialized #we want to be sure that the client is initialized |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
121 |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
122 #we first check that we have a item-access pubsub server |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
123 if not hasattr(client,"item_access_pubsub"): |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
124 debug(_('Looking for item-access power pubsub server')) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
125 #we don't have any pubsub server featuring item access yet |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
126 test = self.host.memory.getServerServiceEntities("pubsub", "service", profile) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
127 client.item_access_pubsub = None |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
128 for entity in self.host.memory.getServerServiceEntities("pubsub", "service", profile): |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
129 disco = yield client.disco.requestInfo(entity) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
130 if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features): |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
131 info(_("item-access powered pubsub service found: [%s]") % entity.full()) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
132 client.item_access_pubsub = entity |
308
ce3607b7198d
plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents:
307
diff
changeset
|
133 |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
134 if not client.item_access_pubsub: |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
135 error(_("No item-access powered pubsub server found, can't post group blog")) |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
136 return |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
137 |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
138 _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
139 #TODO: send an error signal if user want to post to non existant groups |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
140 |
462
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
141 self._publishMblog(client.item_access_pubsub, _groups, message, client) |
d9456d94cd12
plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
142 |
310
53adec87d1d7
plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents:
308
diff
changeset
|
143 |