Mercurial > libervia-pubsub
annotate twisted/plugins/pubsub.py @ 422:c21f31355ab9
configuration: "max_items" option:
"max_items" is implemented using a text-single field, as it is done in the XEP-0060
example (there is no real formal description). When changing the node configuration, the
max_items can't be set to a number lower than the total number of items in the node (the
configuration will then be rejected), this is to avoid accidental deletion of items.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 10 Mar 2020 11:11:38 +0100 |
parents | 89736353f6be |
children | 6f8e1c180c83 |
rev | line source |
---|---|
414 | 1 #!/usr/bin/env python3 |
233 | 2 #-*- coding: utf-8 -*- |
3 | |
384 | 4 # Copyright (c) 2012-2019 Jérôme Poisson |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
5 # Copyright (c) 2003-2011 Ralph Meijer |
233 | 6 |
7 | |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
8 # This program is free software: you can redistribute it and/or modify |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
9 # it under the terms of the GNU Affero General Public License as published by |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
10 # the Free Software Foundation, either version 3 of the License, or |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
11 # (at your option) any later version. |
233 | 12 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
13 # This program is distributed in the hope that it will be useful, |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
16 # GNU Affero General Public License for more details. |
233 | 17 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
18 # You should have received a copy of the GNU Affero General Public License |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
20 # -- |
233 | 21 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
22 # This program is based on Idavoll (http://idavoll.ik.nu/), |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
23 # originaly written by Ralph Meijer (http://ralphm.net/blog/) |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
24 # It is sublicensed under AGPL v3 (or any later version) as allowed by the original |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
25 # license. |
233 | 26 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
27 # -- |
233 | 28 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
29 # Here is a copy of the original license: |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
30 |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
31 # Copyright (c) 2003-2011 Ralph Meijer |
233 | 32 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
33 # Permission is hereby granted, free of charge, to any person obtaining |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
34 # a copy of this software and associated documentation files (the |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
35 # "Software"), to deal in the Software without restriction, including |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
36 # without limitation the rights to use, copy, modify, merge, publish, |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
37 # distribute, sublicense, and/or sell copies of the Software, and to |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
38 # permit persons to whom the Software is furnished to do so, subject to |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
39 # the following conditions: |
233 | 40 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
41 # The above copyright notice and this permission notice shall be |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
42 # included in all copies or substantial portions of the Software. |
233 | 43 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
44 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
45 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
46 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
47 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
48 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
49 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
50 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
233 | 51 |
155 | 52 |
418
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
53 import sys |
380 | 54 import csv |
418
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
55 from os.path import expanduser, realpath |
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
56 import configparser |
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
57 # patch for Python 3.8 compatibility |
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
58 from sat_tmp.twisted import install as install_twisted_patches |
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
59 install_twisted_patches() |
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
60 from zope.interface import implementer |
369 | 61 from twisted.application.service import IServiceMaker |
62 from twisted.application import service | |
380 | 63 from twisted.python import usage, log |
418
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
64 from twisted.plugin import IPlugin |
369 | 65 from twisted.words.protocols.jabber.jid import JID |
418
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
66 import sat_pubsub |
369 | 67 |
68 | |
69 | |
70 | |
380 | 71 def coerceListType(value): |
414 | 72 return next(csv.reader( |
380 | 73 [value], delimiter=",", quotechar='"', skipinitialspace=True |
414 | 74 )) |
380 | 75 |
76 | |
77 def coerceJidListType(value): | |
78 values = [JID(v) for v in coerceListType(value)] | |
79 if any((j.resource for j in values)): | |
414 | 80 raise ValueError("you must use bare jids") |
380 | 81 return values |
82 | |
83 | |
84 | |
369 | 85 OPT_PARAMETERS_BOTH = [ |
86 ['jid', None, None, 'JID this component will be available at'], | |
87 ['xmpp_pwd', None, None, 'XMPP server component password'], | |
88 ['rhost', None, '127.0.0.1', 'XMPP server host'], | |
89 ['rport', None, '5347', 'XMPP server port'], | |
90 ['backend', None, 'pgsql', 'Choice of storage backend'], | |
91 ['db_user', None, None, 'Database user (pgsql backend)'], | |
92 ['db_name', None, 'pubsub', 'Database name (pgsql backend)'], | |
93 ['db_pass', None, None, 'Database password (pgsql backend)'], | |
94 ['db_host', None, None, 'Database host (pgsql backend)'], | |
95 ['db_port', None, None, 'Database port (pgsql backend)'], | |
96 ] | |
97 # here for future use | |
380 | 98 OPT_PARAMETERS_CFG = [ |
99 ["admins_jids_list", None, [], "List of administrators' bare jids", | |
100 coerceJidListType] | |
101 ] | |
369 | 102 |
414 | 103 CONFIG_FILENAME = 'sat' |
369 | 104 # List of the configuration filenames sorted by ascending priority |
105 CONFIG_FILES = [realpath(expanduser(path) + CONFIG_FILENAME + '.conf') for path in ( | |
106 '/etc/', '/etc/{}/'.format(CONFIG_FILENAME), | |
107 '~/', '~/.', | |
108 '.config/', '.config/.', | |
109 '', '.')] | |
110 CONFIG_SECTION = 'pubsub' | |
111 | |
112 | |
113 class Options(usage.Options): | |
114 optParameters = OPT_PARAMETERS_BOTH | |
115 | |
116 optFlags = [ | |
117 ('verbose', 'v', 'Show traffic'), | |
118 ('hide-nodes', None, 'Hide all nodes for disco') | |
119 ] | |
120 | |
121 def __init__(self): | |
122 """Read SàT Pubsub configuration file in order to overwrite the hard-coded default values. | |
123 | |
124 Priority for the usage of the values is (from lowest to highest): | |
125 - hard-coded default values | |
126 - values from SàT configuration files | |
127 - values passed on the command line | |
128 """ | |
129 # If we do it the reading later: after the command line options have been parsed, there's no good way to know | |
130 # if the options values are the hard-coded ones or if they have been passed on the command line. | |
131 | |
132 # FIXME: must be refactored + code can be factorised with backend | |
414 | 133 config_parser = configparser.ConfigParser() |
369 | 134 config_parser.read(CONFIG_FILES) |
135 for param in self.optParameters + OPT_PARAMETERS_CFG: | |
136 name = param[0] | |
137 try: | |
138 value = config_parser.get(CONFIG_SECTION, name) | |
139 try: | |
140 param[2] = param[4](value) | |
141 except IndexError: # the coerce method is optional | |
142 param[2] = value | |
380 | 143 except Exception as e: |
414 | 144 log.err('Invalid value for setting "{name}": {msg}'.format( |
380 | 145 name=name, msg=e)) |
146 sys.exit(1) | |
414 | 147 except (configparser.NoSectionError, configparser.NoOptionError): |
369 | 148 pass |
149 usage.Options.__init__(self) | |
150 for opt_data in OPT_PARAMETERS_CFG: | |
151 self[opt_data[0]] = opt_data[2] | |
155 | 152 |
369 | 153 def postOptions(self): |
154 if self['backend'] not in ['pgsql', 'memory']: | |
406 | 155 raise usage.UsageError("Unknown backend!") |
369 | 156 if self['backend'] == 'memory': |
157 raise NotImplementedError('memory backend is not available at the moment') | |
158 | |
159 self['jid'] = JID(self['jid']) if self['jid'] else None | |
160 | |
161 | |
414 | 162 @implementer(IServiceMaker, IPlugin) |
369 | 163 class SatPubsubMaker(object): |
164 tapname = "sat-pubsub" | |
414 | 165 description = "Salut à Toi Publish-Subscribe Service Component" |
369 | 166 options = Options |
167 | |
168 def makeService(self, config): | |
417
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
169 from wokkel.component import Component |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
170 from wokkel.disco import DiscoHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
171 from wokkel.generic import FallbackHandler, VersionHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
172 from wokkel.iwokkel import IPubSubResource |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
173 from wokkel import data_form |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
174 from wokkel import pubsub |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
175 from wokkel import rsm |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
176 from wokkel import mam |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
177 from sat_pubsub import const |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
178 from sat_pubsub import mam as pubsub_mam |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
179 from sat_pubsub import pubsub_admin |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
180 from sat_pubsub.backend import BackendService, ExtraDiscoHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
181 from sat_pubsub.schema import SchemaHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
182 from sat_pubsub.privilege import PrivilegesHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
183 from sat_pubsub.delegation import DelegationsHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
184 |
369 | 185 if not config['jid'] or not config['xmpp_pwd']: |
186 raise usage.UsageError("You must specify jid and xmpp_pwd") | |
187 s = service.MultiService() | |
188 | |
189 # Create backend service with storage | |
190 | |
191 if config['backend'] == 'pgsql': | |
192 from twisted.enterprise import adbapi | |
193 from sat_pubsub.pgsql_storage import Storage | |
194 from psycopg2.extras import NamedTupleConnection | |
195 keys_map = { | |
196 'db_user': 'user', | |
197 'db_pass': 'password', | |
198 'db_name': 'database', | |
199 'db_host': 'host', | |
200 'db_port': 'port', | |
201 } | |
202 kwargs = {} | |
414 | 203 for config_k, k in keys_map.items(): |
369 | 204 v = config.get(config_k) |
205 if v is None: | |
206 continue | |
207 kwargs[k] = v | |
208 dbpool = adbapi.ConnectionPool('psycopg2', | |
209 cp_reconnect=True, | |
210 client_encoding='utf-8', | |
211 connection_factory=NamedTupleConnection, | |
212 **kwargs | |
213 ) | |
214 st = Storage(dbpool) | |
215 elif config['backend'] == 'memory': | |
406 | 216 raise NotImplementedError('memory backend is not available at the moment') |
369 | 217 |
380 | 218 bs = BackendService(st, config) |
369 | 219 bs.setName('backend') |
220 bs.setServiceParent(s) | |
221 | |
222 # Set up XMPP server-side component with publish-subscribe capabilities | |
223 | |
224 cs = Component(config["rhost"], int(config["rport"]), | |
225 config["jid"].full(), config["xmpp_pwd"]) | |
226 cs.setName('component') | |
227 cs.setServiceParent(s) | |
228 | |
229 cs.factory.maxDelay = 900 | |
230 | |
231 if config["verbose"]: | |
232 cs.logTraffic = True | |
233 | |
234 FallbackHandler().setHandlerParent(cs) | |
414 | 235 VersionHandler('SàT Pubsub', sat_pubsub.__version__).setHandlerParent(cs) |
369 | 236 DiscoHandler().setHandlerParent(cs) |
237 | |
238 ph = PrivilegesHandler(config['jid']) | |
239 ph.setHandlerParent(cs) | |
240 bs.privilege = ph | |
241 | |
242 resource = IPubSubResource(bs) | |
243 resource.hideNodes = config["hide-nodes"] | |
244 resource.serviceJID = config["jid"] | |
245 | |
246 ps = (rsm if const.FLAG_ENABLE_RSM else pubsub).PubSubService(resource) | |
247 ps.setHandlerParent(cs) | |
248 resource.pubsubService = ps | |
249 | |
250 if const.FLAG_ENABLE_MAM: | |
251 mam_resource = pubsub_mam.MAMResource(bs) | |
252 mam_s = mam.MAMService(mam_resource) | |
253 mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_CATEGORY)) | |
254 mam_s.setHandlerParent(cs) | |
255 | |
382
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
256 pa = pubsub_admin.PubsubAdminHandler(bs) |
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
257 pa.setHandlerParent(cs) |
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
258 |
369 | 259 sh = SchemaHandler() |
260 sh.setHandlerParent(cs) | |
261 | |
375
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
262 # wokkel.pubsub doesn't handle non pubsub# disco |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
263 # and we need to announce other feature, so this is a workaround |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
264 # to add them |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
265 # FIXME: propose a patch upstream to fix this situation |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
266 ed = ExtraDiscoHandler() |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
267 ed.setHandlerParent(cs) |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
268 |
369 | 269 # XXX: delegation must be instancied at the end, |
270 # because it does some MonkeyPatching on handlers | |
271 dh = DelegationsHandler() | |
272 dh.setHandlerParent(cs) | |
273 bs.delegation = dh | |
274 | |
275 return s | |
276 | |
277 serviceMaker = SatPubsubMaker() |