comparison libervia/backend/plugins/plugin_misc_groupblog.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_misc_groupblog.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT plugin for microbloging with roster access
5 # Copyright (C) 2009-2021 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 libervia.backend.core.i18n import _
21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger
23
24 log = getLogger(__name__)
25 from twisted.internet import defer
26 from libervia.backend.core import exceptions
27 from wokkel import disco, data_form, iwokkel
28 from zope.interface import implementer
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 = (
39 NS_PUBSUB
40 ) # XXX: we can't use custom namespace as Wokkel's PubSubService use official NS
41 NS_PUBSUB_GROUPBLOG = NS_PUBSUB_EXP + "#groupblog"
42 NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config"
43
44
45 PLUGIN_INFO = {
46 C.PI_NAME: "Group blogging through collections",
47 C.PI_IMPORT_NAME: "GROUPBLOG",
48 C.PI_TYPE: "MISC",
49 C.PI_PROTOCOLS: [],
50 C.PI_DEPENDENCIES: ["XEP-0277"],
51 C.PI_MAIN: "GroupBlog",
52 C.PI_HANDLER: "yes",
53 C.PI_DESCRIPTION: _("""Implementation of microblogging fine permissions"""),
54 }
55
56
57 class GroupBlog(object):
58 """This class use a SàT PubSub Service to manage access on microblog"""
59
60 def __init__(self, host):
61 log.info(_("Group blog plugin initialization"))
62 self.host = host
63 self._p = self.host.plugins["XEP-0060"]
64 host.trigger.add("XEP-0277_item2data", self._item_2_data_trigger)
65 host.trigger.add("XEP-0277_data2entry", self._data_2_entry_trigger)
66 host.trigger.add("XEP-0277_comments", self._comments_trigger)
67
68 ## plugin management methods ##
69
70 def get_handler(self, client):
71 return GroupBlog_handler()
72
73 @defer.inlineCallbacks
74 def profile_connected(self, client):
75 try:
76 yield self.host.check_features(client, (NS_PUBSUB_GROUPBLOG,))
77 except exceptions.FeatureNotFound:
78 client.server_groupblog_available = False
79 log.warning(
80 _(
81 "Server is not able to manage item-access pubsub, we can't use group blog"
82 )
83 )
84 else:
85 client.server_groupblog_available = True
86 log.info(_("Server can manage group blogs"))
87
88 def features_get(self, profile):
89 try:
90 client = self.host.get_client(profile)
91 except exceptions.ProfileNotSetError:
92 return {}
93 try:
94 return {"available": C.bool_const(client.server_groupblog_available)}
95 except AttributeError:
96 if self.host.is_connected(profile):
97 log.debug("Profile is not connected, service is not checked yet")
98 else:
99 log.error("client.server_groupblog_available should be available !")
100 return {}
101
102 def _item_2_data_trigger(self, item_elt, entry_elt, microblog_data):
103 """Parse item to find group permission elements"""
104 config_form = data_form.findForm(item_elt, NS_PUBSUB_ITEM_CONFIG)
105 if config_form is None:
106 return
107 access_model = config_form.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN)
108 if access_model == self._p.ACCESS_PUBLISHER_ROSTER:
109 opt = self._p.OPT_ROSTER_GROUPS_ALLOWED
110 microblog_data['groups'] = config_form.fields[opt].values
111
112 def _data_2_entry_trigger(self, client, mb_data, entry_elt, item_elt):
113 """Build fine access permission if needed
114
115 This trigger check if "group*" key are present,
116 and create a fine item config to restrict view to these groups
117 """
118 groups = mb_data.get('groups', [])
119 if not groups:
120 return
121 if not client.server_groupblog_available:
122 raise exceptions.CancelError("GroupBlog is not available")
123 log.debug("This entry use group blog")
124 form = data_form.Form("submit", formNamespace=NS_PUBSUB_ITEM_CONFIG)
125 access = data_form.Field(
126 None, self._p.OPT_ACCESS_MODEL, value=self._p.ACCESS_PUBLISHER_ROSTER
127 )
128 allowed = data_form.Field(None, self._p.OPT_ROSTER_GROUPS_ALLOWED, values=groups)
129 form.addField(access)
130 form.addField(allowed)
131 item_elt.addChild(form.toElement())
132
133 def _comments_trigger(self, client, mb_data, options):
134 """This method is called when a comments node is about to be created
135
136 It changes the access mode to roster if needed, and give the authorized groups
137 """
138 if "group" in mb_data:
139 options[self._p.OPT_ACCESS_MODEL] = self._p.ACCESS_PUBLISHER_ROSTER
140 options[self._p.OPT_ROSTER_GROUPS_ALLOWED] = mb_data['groups']
141
142 @implementer(iwokkel.IDisco)
143 class GroupBlog_handler(XMPPHandler):
144
145 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
146 return [disco.DiscoFeature(NS_GROUPBLOG)]
147
148 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
149 return []