comparison sat/plugins/plugin_misc_groupblog.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_misc_groupblog.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for microbloging with roster access
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 log = getLogger(__name__)
24 from twisted.internet import defer
25 from sat.core import exceptions
26 from wokkel import disco, data_form, iwokkel
27 from zope.interface import implements
28 from sat.tools.common import data_format
29
30 try:
31 from twisted.words.protocols.xmlstream import XMPPHandler
32 except ImportError:
33 from wokkel.subprotocols import XMPPHandler
34
35 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
36 NS_GROUPBLOG = 'http://salut-a-toi.org/protocol/groupblog'
37 #NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features
38 NS_PUBSUB_EXP = NS_PUBSUB # XXX: we can't use custom namespace as Wokkel's PubSubService use official NS
39 NS_PUBSUB_GROUPBLOG = NS_PUBSUB_EXP + "#groupblog"
40 NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config"
41
42
43 PLUGIN_INFO = {
44 C.PI_NAME: "Group blogging through collections",
45 C.PI_IMPORT_NAME: "GROUPBLOG",
46 C.PI_TYPE: "MISC",
47 C.PI_PROTOCOLS: [],
48 C.PI_DEPENDENCIES: ["XEP-0277"],
49 C.PI_MAIN: "GroupBlog",
50 C.PI_HANDLER: "yes",
51 C.PI_DESCRIPTION: _("""Implementation of microblogging fine permissions""")
52 }
53
54
55 class GroupBlog(object):
56 """This class use a SàT PubSub Service to manage access on microblog"""
57
58 def __init__(self, host):
59 log.info(_("Group blog plugin initialization"))
60 self.host = host
61 self._p = self.host.plugins["XEP-0060"]
62 host.trigger.add("XEP-0277_item2data", self._item2dataTrigger)
63 host.trigger.add("XEP-0277_data2entry", self._data2entryTrigger)
64 host.trigger.add("XEP-0277_comments", self._commentsTrigger)
65
66 ## plugin management methods ##
67
68 def getHandler(self, client):
69 return GroupBlog_handler()
70
71 @defer.inlineCallbacks
72 def profileConnected(self, client):
73 try:
74 yield self.host.checkFeatures(client, (NS_PUBSUB_GROUPBLOG,))
75 except exceptions.FeatureNotFound:
76 client.server_groupblog_available = False
77 log.warning(_(u"Server is not able to manage item-access pubsub, we can't use group blog"))
78 else:
79 client.server_groupblog_available = True
80 log.info(_(u"Server can manage group blogs"))
81
82 def getFeatures(self, profile):
83 try:
84 client = self.host.getClient(profile)
85 except exceptions.ProfileNotSetError:
86 return {}
87 try:
88 return {'available': C.boolConst(client.server_groupblog_available)}
89 except AttributeError:
90 if self.host.isConnected(profile):
91 log.debug("Profile is not connected, service is not checked yet")
92 else:
93 log.error("client.server_groupblog_available should be available !")
94 return {}
95
96 def _item2dataTrigger(self, item_elt, entry_elt, microblog_data):
97 """Parse item to find group permission elements"""
98 config_form = data_form.findForm(item_elt, NS_PUBSUB_ITEM_CONFIG)
99 if config_form is None:
100 return
101 access_model = config_form.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN)
102 if access_model == self._p.ACCESS_PUBLISHER_ROSTER:
103 data_format.iter2dict('group', config_form.fields[self._p.OPT_ROSTER_GROUPS_ALLOWED].values, microblog_data)
104
105 def _data2entryTrigger(self, client, mb_data, entry_elt, item_elt):
106 """Build fine access permission if needed
107
108 This trigger check if "group*" key are present,
109 and create a fine item config to restrict view to these groups
110 """
111 groups = list(data_format.dict2iter('group', mb_data))
112 if not groups:
113 return
114 if not client.server_groupblog_available:
115 raise exceptions.CancelError(u"GroupBlog is not available")
116 log.debug(u"This entry use group blog")
117 form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
118 access = data_form.Field(None, self._p.OPT_ACCESS_MODEL, value=self._p.ACCESS_PUBLISHER_ROSTER)
119 allowed = data_form.Field(None, self._p.OPT_ROSTER_GROUPS_ALLOWED, values=groups)
120 form.addField(access)
121 form.addField(allowed)
122 item_elt.addChild(form.toElement())
123
124 def _commentsTrigger(self, client, mb_data, options):
125 """This method is called when a comments node is about to be created
126
127 It changes the access mode to roster if needed, and give the authorized groups
128 """
129 if "group" in mb_data:
130 options[self._p.OPT_ACCESS_MODEL] = self._p.ACCESS_PUBLISHER_ROSTER
131 options[self._p.OPT_ROSTER_GROUPS_ALLOWED] = list(data_format.dict2iter('group', mb_data))
132
133
134 class GroupBlog_handler(XMPPHandler):
135 implements(iwokkel.IDisco)
136
137 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
138 return [disco.DiscoFeature(NS_GROUPBLOG)]
139
140 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
141 return []