Mercurial > libervia-pubsub
annotate src/twisted/plugins/pubsub.py @ 380:e81964db3cd6
administrators entities:
In config file, the "admins_jids_list" setting can now be set to give a list of bare jids of entities with administrator privileges.
This is not used yet but will be soon.
config is now available in backend using self.config.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 13 Jan 2019 18:56:17 +0100 |
parents | 9a787881b824 |
children | 77b52dbda89a |
rev | line source |
---|---|
233 | 1 #!/usr/bin/python |
2 #-*- coding: utf-8 -*- | |
3 | |
368 | 4 # Copyright (c) 2012-2018 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 |
380 | 53 import csv |
369 | 54 import sat_pubsub |
380 | 55 import sys |
369 | 56 from twisted.application.service import IServiceMaker |
57 from twisted.application import service | |
380 | 58 from twisted.python import usage, log |
369 | 59 from twisted.words.protocols.jabber.jid import JID |
60 from twisted.plugin import IPlugin | |
61 | |
62 from wokkel.component import Component | |
63 from wokkel.disco import DiscoHandler | |
64 from wokkel.generic import FallbackHandler, VersionHandler | |
65 from wokkel.iwokkel import IPubSubResource | |
66 from wokkel import data_form | |
67 from wokkel import pubsub | |
68 from wokkel import rsm | |
69 from wokkel import mam | |
70 from zope.interface import implements | |
71 | |
72 from sat_pubsub import const | |
73 from sat_pubsub import mam as pubsub_mam | |
375
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
74 from sat_pubsub.backend import BackendService, ExtraDiscoHandler |
369 | 75 from sat_pubsub.schema import SchemaHandler |
76 from sat_pubsub.privilege import PrivilegesHandler | |
77 from sat_pubsub.delegation import DelegationsHandler | |
78 from os.path import expanduser, realpath | |
79 import ConfigParser | |
80 | |
81 | |
380 | 82 def coerceListType(value): |
83 return csv.reader( | |
84 [value], delimiter=",", quotechar='"', skipinitialspace=True | |
85 ).next() | |
86 | |
87 | |
88 def coerceJidListType(value): | |
89 values = [JID(v) for v in coerceListType(value)] | |
90 if any((j.resource for j in values)): | |
91 raise ValueError(u"you must use bare jids") | |
92 return values | |
93 | |
94 | |
95 | |
369 | 96 OPT_PARAMETERS_BOTH = [ |
97 ['jid', None, None, 'JID this component will be available at'], | |
98 ['xmpp_pwd', None, None, 'XMPP server component password'], | |
99 ['rhost', None, '127.0.0.1', 'XMPP server host'], | |
100 ['rport', None, '5347', 'XMPP server port'], | |
101 ['backend', None, 'pgsql', 'Choice of storage backend'], | |
102 ['db_user', None, None, 'Database user (pgsql backend)'], | |
103 ['db_name', None, 'pubsub', 'Database name (pgsql backend)'], | |
104 ['db_pass', None, None, 'Database password (pgsql backend)'], | |
105 ['db_host', None, None, 'Database host (pgsql backend)'], | |
106 ['db_port', None, None, 'Database port (pgsql backend)'], | |
107 ] | |
108 # here for future use | |
380 | 109 OPT_PARAMETERS_CFG = [ |
110 ["admins_jids_list", None, [], "List of administrators' bare jids", | |
111 coerceJidListType] | |
112 ] | |
369 | 113 |
114 CONFIG_FILENAME = u'sat' | |
115 # List of the configuration filenames sorted by ascending priority | |
116 CONFIG_FILES = [realpath(expanduser(path) + CONFIG_FILENAME + '.conf') for path in ( | |
117 '/etc/', '/etc/{}/'.format(CONFIG_FILENAME), | |
118 '~/', '~/.', | |
119 '.config/', '.config/.', | |
120 '', '.')] | |
121 CONFIG_SECTION = 'pubsub' | |
122 | |
123 | |
124 class Options(usage.Options): | |
125 optParameters = OPT_PARAMETERS_BOTH | |
126 | |
127 optFlags = [ | |
128 ('verbose', 'v', 'Show traffic'), | |
129 ('hide-nodes', None, 'Hide all nodes for disco') | |
130 ] | |
131 | |
132 def __init__(self): | |
133 """Read SàT Pubsub configuration file in order to overwrite the hard-coded default values. | |
134 | |
135 Priority for the usage of the values is (from lowest to highest): | |
136 - hard-coded default values | |
137 - values from SàT configuration files | |
138 - values passed on the command line | |
139 """ | |
140 # If we do it the reading later: after the command line options have been parsed, there's no good way to know | |
141 # if the options values are the hard-coded ones or if they have been passed on the command line. | |
142 | |
143 # FIXME: must be refactored + code can be factorised with backend | |
144 config_parser = ConfigParser.SafeConfigParser() | |
145 config_parser.read(CONFIG_FILES) | |
146 for param in self.optParameters + OPT_PARAMETERS_CFG: | |
147 name = param[0] | |
148 try: | |
149 value = config_parser.get(CONFIG_SECTION, name) | |
150 if isinstance(value, unicode): | |
151 value = value.encode('utf-8') | |
152 try: | |
153 param[2] = param[4](value) | |
154 except IndexError: # the coerce method is optional | |
155 param[2] = value | |
380 | 156 except Exception as e: |
157 log.err(u'Invalid value for setting "{name}": {msg}'.format( | |
158 name=name, msg=e)) | |
159 sys.exit(1) | |
369 | 160 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
161 pass | |
162 usage.Options.__init__(self) | |
163 for opt_data in OPT_PARAMETERS_CFG: | |
164 self[opt_data[0]] = opt_data[2] | |
155 | 165 |
369 | 166 def postOptions(self): |
167 if self['backend'] not in ['pgsql', 'memory']: | |
168 raise usage.UsageError, "Unknown backend!" | |
169 if self['backend'] == 'memory': | |
170 raise NotImplementedError('memory backend is not available at the moment') | |
171 | |
172 self['jid'] = JID(self['jid']) if self['jid'] else None | |
173 | |
174 | |
175 class SatPubsubMaker(object): | |
176 implements(IServiceMaker, IPlugin) | |
177 tapname = "sat-pubsub" | |
178 description = u"Salut à Toi Publish-Subscribe Service Component".encode('utf-8') | |
179 options = Options | |
180 | |
181 def makeService(self, config): | |
182 if not config['jid'] or not config['xmpp_pwd']: | |
183 raise usage.UsageError("You must specify jid and xmpp_pwd") | |
184 s = service.MultiService() | |
185 | |
186 # Create backend service with storage | |
187 | |
188 if config['backend'] == 'pgsql': | |
189 from twisted.enterprise import adbapi | |
190 from sat_pubsub.pgsql_storage import Storage | |
191 from psycopg2.extras import NamedTupleConnection | |
192 keys_map = { | |
193 'db_user': 'user', | |
194 'db_pass': 'password', | |
195 'db_name': 'database', | |
196 'db_host': 'host', | |
197 'db_port': 'port', | |
198 } | |
199 kwargs = {} | |
200 for config_k, k in keys_map.iteritems(): | |
201 v = config.get(config_k) | |
202 if v is None: | |
203 continue | |
204 kwargs[k] = v | |
205 dbpool = adbapi.ConnectionPool('psycopg2', | |
206 cp_reconnect=True, | |
207 client_encoding='utf-8', | |
208 connection_factory=NamedTupleConnection, | |
209 **kwargs | |
210 ) | |
211 st = Storage(dbpool) | |
212 elif config['backend'] == 'memory': | |
213 from sat_pubsub.memory_storage import Storage | |
214 st = Storage() | |
215 | |
380 | 216 bs = BackendService(st, config) |
369 | 217 bs.setName('backend') |
218 bs.setServiceParent(s) | |
219 | |
220 # Set up XMPP server-side component with publish-subscribe capabilities | |
221 | |
222 cs = Component(config["rhost"], int(config["rport"]), | |
223 config["jid"].full(), config["xmpp_pwd"]) | |
224 cs.setName('component') | |
225 cs.setServiceParent(s) | |
226 | |
227 cs.factory.maxDelay = 900 | |
228 | |
229 if config["verbose"]: | |
230 cs.logTraffic = True | |
231 | |
232 FallbackHandler().setHandlerParent(cs) | |
233 VersionHandler(u'SàT Pubsub', sat_pubsub.__version__).setHandlerParent(cs) | |
234 DiscoHandler().setHandlerParent(cs) | |
235 | |
236 ph = PrivilegesHandler(config['jid']) | |
237 ph.setHandlerParent(cs) | |
238 bs.privilege = ph | |
239 | |
240 resource = IPubSubResource(bs) | |
241 resource.hideNodes = config["hide-nodes"] | |
242 resource.serviceJID = config["jid"] | |
243 | |
244 ps = (rsm if const.FLAG_ENABLE_RSM else pubsub).PubSubService(resource) | |
245 ps.setHandlerParent(cs) | |
246 resource.pubsubService = ps | |
247 | |
248 if const.FLAG_ENABLE_MAM: | |
249 mam_resource = pubsub_mam.MAMResource(bs) | |
250 mam_s = mam.MAMService(mam_resource) | |
251 mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_CATEGORY)) | |
252 mam_s.setHandlerParent(cs) | |
253 | |
254 sh = SchemaHandler() | |
255 sh.setHandlerParent(cs) | |
256 | |
375
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
257 # wokkel.pubsub doesn't handle non pubsub# disco |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
258 # 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
|
259 # to add them |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
260 # FIXME: propose a patch upstream to fix this situation |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
261 ed = ExtraDiscoHandler() |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
262 ed.setHandlerParent(cs) |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
263 |
369 | 264 # XXX: delegation must be instancied at the end, |
265 # because it does some MonkeyPatching on handlers | |
266 dh = DelegationsHandler() | |
267 dh.setHandlerParent(cs) | |
268 bs.delegation = dh | |
269 | |
270 return s | |
271 | |
272 serviceMaker = SatPubsubMaker() |