Mercurial > libervia-pubsub
annotate twisted/plugins/pubsub.py @ 430:5a0ada3b61ca
Full-Text Search implementation:
/!\ pgsql schema needs to be updated /!\
/!\ Minimal PostgreSQL required version is now 12 /!\
A new options is available to specify main language of a node. By default a `generic`
language is used (which uses the `simple` configuration in PostgreSQL). When a node owner
changes the language, the index is rebuilt accordingly. It is possible to have item
specific language for multilingual nodes (but for the moment the search is done with node
language, so the results won't be good). If an item language is explicitely set in
`item_languages`, the FTS configuration won't be affected by node FTS language setting.
Search is parsed with `websearch_to_tsquery` for now, but this parser doesn't handle
prefix matching, so it may be replaced in the future.
SetConfiguration now only updates the modified values, this avoid triggering the FTS
re-indexing on each config change. `_checkNodeExists` is not called anymore as we can
check if a row has been modified to see if the node exists, this avoid a useless query.
Item storing has been slighly improved with a useless SELECT and condition removed.
To avoid 2 schema updates in a row, the `sat_pubsub_update_5_6.sql` file also prepares the
implementation of XEP-0346 by updating nodes with a schema and creating the suitable
template nodes.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 11 Dec 2020 17:18:52 +0100 |
parents | 34bd55179e22 |
children | 5e8b8ef5c862 |
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 |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
55 import os |
418
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
56 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
|
57 import configparser |
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
58 # 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
|
59 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
|
60 install_twisted_patches() |
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
61 from zope.interface import implementer |
369 | 62 from twisted.application.service import IServiceMaker |
63 from twisted.application import service | |
380 | 64 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
|
65 from twisted.plugin import IPlugin |
369 | 66 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
|
67 import sat_pubsub |
369 | 68 |
69 | |
70 | |
71 | |
380 | 72 def coerceListType(value): |
414 | 73 return next(csv.reader( |
380 | 74 [value], delimiter=",", quotechar='"', skipinitialspace=True |
414 | 75 )) |
380 | 76 |
77 | |
78 def coerceJidListType(value): | |
79 values = [JID(v) for v in coerceListType(value)] | |
80 if any((j.resource for j in values)): | |
414 | 81 raise ValueError("you must use bare jids") |
380 | 82 return values |
83 | |
84 | |
85 | |
369 | 86 OPT_PARAMETERS_BOTH = [ |
87 ['jid', None, None, 'JID this component will be available at'], | |
88 ['xmpp_pwd', None, None, 'XMPP server component password'], | |
89 ['rhost', None, '127.0.0.1', 'XMPP server host'], | |
90 ['rport', None, '5347', 'XMPP server port'], | |
91 ['backend', None, 'pgsql', 'Choice of storage backend'], | |
92 ['db_user', None, None, 'Database user (pgsql backend)'], | |
93 ['db_name', None, 'pubsub', 'Database name (pgsql backend)'], | |
94 ['db_pass', None, None, 'Database password (pgsql backend)'], | |
95 ['db_host', None, None, 'Database host (pgsql backend)'], | |
96 ['db_port', None, None, 'Database port (pgsql backend)'], | |
97 ] | |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
98 |
380 | 99 OPT_PARAMETERS_CFG = [ |
100 ["admins_jids_list", None, [], "List of administrators' bare jids", | |
101 coerceJidListType] | |
102 ] | |
369 | 103 |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
104 # prefix used for environment variables |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
105 ENV_PREFIX = "SAT_PUBSUB_" |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
106 # mapping from option name to environment variables to use |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
107 # each parameter name links to a list of variable environment name |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
108 # if an environment variable of one of the names exists it will be used |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
109 # as default value, with priority over config file |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
110 ENV_OPT_MAP = { |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
111 # we use the same environment variables as PostgreSQL |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
112 'db_user': ['PGUSER'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
113 'db_name': ['PGDATABASE'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
114 'db_pass': ['PGPASSWORD'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
115 'db_host': ['PGHOST'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
116 'db_port': ['PGPORT'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
117 } |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
118 for opt in OPT_PARAMETERS_BOTH + OPT_PARAMETERS_CFG: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
119 name = opt[0] |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
120 env_name = f"{ENV_PREFIX}{name.upper()}" |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
121 ENV_OPT_MAP.setdefault(name, []).append(env_name) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
122 |
414 | 123 CONFIG_FILENAME = 'sat' |
369 | 124 # List of the configuration filenames sorted by ascending priority |
125 CONFIG_FILES = [realpath(expanduser(path) + CONFIG_FILENAME + '.conf') for path in ( | |
126 '/etc/', '/etc/{}/'.format(CONFIG_FILENAME), | |
127 '~/', '~/.', | |
128 '.config/', '.config/.', | |
426
6f8e1c180c83
added "~/.config/sat" as search patch for config
Goffi <goffi@goffi.org>
parents:
418
diff
changeset
|
129 '.config/{}/'.format(CONFIG_FILENAME), |
369 | 130 '', '.')] |
131 CONFIG_SECTION = 'pubsub' | |
132 | |
133 | |
134 class Options(usage.Options): | |
135 optParameters = OPT_PARAMETERS_BOTH | |
136 | |
137 optFlags = [ | |
138 ('verbose', 'v', 'Show traffic'), | |
139 ('hide-nodes', None, 'Hide all nodes for disco') | |
140 ] | |
141 | |
142 def __init__(self): | |
143 """Read SàT Pubsub configuration file in order to overwrite the hard-coded default values. | |
144 | |
145 Priority for the usage of the values is (from lowest to highest): | |
146 - hard-coded default values | |
147 - values from SàT configuration files | |
148 - values passed on the command line | |
149 """ | |
150 # If we do it the reading later: after the command line options have been parsed, there's no good way to know | |
151 # if the options values are the hard-coded ones or if they have been passed on the command line. | |
152 | |
153 # FIXME: must be refactored + code can be factorised with backend | |
414 | 154 config_parser = configparser.ConfigParser() |
369 | 155 config_parser.read(CONFIG_FILES) |
156 for param in self.optParameters + OPT_PARAMETERS_CFG: | |
157 name = param[0] | |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
158 for env_name in ENV_OPT_MAP[name]: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
159 # we first check if value is set as an environment variable |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
160 value = os.getenv(env_name) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
161 if value is not None: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
162 self.setDefaultOption(param, value) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
163 break |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
164 else: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
165 # no environment variable set, let's try with configuration |
369 | 166 try: |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
167 value = config_parser.get(CONFIG_SECTION, name) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
168 self.setDefaultOption(param, value) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
169 except (configparser.NoSectionError, configparser.NoOptionError): |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
170 pass |
369 | 171 usage.Options.__init__(self) |
172 for opt_data in OPT_PARAMETERS_CFG: | |
173 self[opt_data[0]] = opt_data[2] | |
155 | 174 |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
175 def setDefaultOption(self, param, value): |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
176 """Set default option value using coerce method when needed |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
177 |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
178 If the value is invalid, we quit the program with exit code 1 |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
179 """ |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
180 try: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
181 param[2] = param[4](value) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
182 except IndexError: # the coerce method is optional |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
183 param[2] = value |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
184 except Exception as e: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
185 log.err('Invalid value for setting "{name}": {msg}'.format( |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
186 name=name, msg=e)) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
187 sys.exit(1) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
188 |
369 | 189 def postOptions(self): |
190 if self['backend'] not in ['pgsql', 'memory']: | |
406 | 191 raise usage.UsageError("Unknown backend!") |
369 | 192 if self['backend'] == 'memory': |
193 raise NotImplementedError('memory backend is not available at the moment') | |
194 | |
195 self['jid'] = JID(self['jid']) if self['jid'] else None | |
196 | |
197 | |
414 | 198 @implementer(IServiceMaker, IPlugin) |
369 | 199 class SatPubsubMaker(object): |
200 tapname = "sat-pubsub" | |
414 | 201 description = "Salut à Toi Publish-Subscribe Service Component" |
369 | 202 options = Options |
203 | |
204 def makeService(self, config): | |
417
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
205 from wokkel.component import Component |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
206 from wokkel.disco import DiscoHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
207 from wokkel.generic import FallbackHandler, VersionHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
208 from wokkel.iwokkel import IPubSubResource |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
209 from wokkel import data_form |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
210 from wokkel import pubsub |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
211 from wokkel import rsm |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
212 from wokkel import mam |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
213 from sat_pubsub import const |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
214 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
|
215 from sat_pubsub import pubsub_admin |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
216 from sat_pubsub.backend import BackendService, ExtraDiscoHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
217 from sat_pubsub.schema import SchemaHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
218 from sat_pubsub.privilege import PrivilegesHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
219 from sat_pubsub.delegation import DelegationsHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
220 |
369 | 221 if not config['jid'] or not config['xmpp_pwd']: |
222 raise usage.UsageError("You must specify jid and xmpp_pwd") | |
223 s = service.MultiService() | |
224 | |
225 # Create backend service with storage | |
226 | |
227 if config['backend'] == 'pgsql': | |
228 from twisted.enterprise import adbapi | |
229 from sat_pubsub.pgsql_storage import Storage | |
230 from psycopg2.extras import NamedTupleConnection | |
231 keys_map = { | |
232 'db_user': 'user', | |
233 'db_pass': 'password', | |
234 'db_name': 'database', | |
235 'db_host': 'host', | |
236 'db_port': 'port', | |
237 } | |
238 kwargs = {} | |
414 | 239 for config_k, k in keys_map.items(): |
369 | 240 v = config.get(config_k) |
241 if v is None: | |
242 continue | |
243 kwargs[k] = v | |
244 dbpool = adbapi.ConnectionPool('psycopg2', | |
245 cp_reconnect=True, | |
246 client_encoding='utf-8', | |
247 connection_factory=NamedTupleConnection, | |
248 **kwargs | |
249 ) | |
250 st = Storage(dbpool) | |
251 elif config['backend'] == 'memory': | |
406 | 252 raise NotImplementedError('memory backend is not available at the moment') |
369 | 253 |
380 | 254 bs = BackendService(st, config) |
369 | 255 bs.setName('backend') |
256 bs.setServiceParent(s) | |
257 | |
258 # Set up XMPP server-side component with publish-subscribe capabilities | |
259 | |
260 cs = Component(config["rhost"], int(config["rport"]), | |
261 config["jid"].full(), config["xmpp_pwd"]) | |
262 cs.setName('component') | |
263 cs.setServiceParent(s) | |
264 | |
265 cs.factory.maxDelay = 900 | |
266 | |
267 if config["verbose"]: | |
268 cs.logTraffic = True | |
269 | |
270 FallbackHandler().setHandlerParent(cs) | |
414 | 271 VersionHandler('SàT Pubsub', sat_pubsub.__version__).setHandlerParent(cs) |
369 | 272 DiscoHandler().setHandlerParent(cs) |
273 | |
274 ph = PrivilegesHandler(config['jid']) | |
275 ph.setHandlerParent(cs) | |
276 bs.privilege = ph | |
277 | |
278 resource = IPubSubResource(bs) | |
279 resource.hideNodes = config["hide-nodes"] | |
280 resource.serviceJID = config["jid"] | |
281 | |
282 ps = (rsm if const.FLAG_ENABLE_RSM else pubsub).PubSubService(resource) | |
283 ps.setHandlerParent(cs) | |
284 resource.pubsubService = ps | |
285 | |
286 if const.FLAG_ENABLE_MAM: | |
287 mam_resource = pubsub_mam.MAMResource(bs) | |
288 mam_s = mam.MAMService(mam_resource) | |
289 mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_CATEGORY)) | |
430 | 290 mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_FTS)) |
369 | 291 mam_s.setHandlerParent(cs) |
292 | |
382
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
293 pa = pubsub_admin.PubsubAdminHandler(bs) |
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
294 pa.setHandlerParent(cs) |
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
295 |
369 | 296 sh = SchemaHandler() |
297 sh.setHandlerParent(cs) | |
298 | |
375
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
299 # wokkel.pubsub doesn't handle non pubsub# disco |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
300 # 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
|
301 # to add them |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
302 # FIXME: propose a patch upstream to fix this situation |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
303 ed = ExtraDiscoHandler() |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
304 ed.setHandlerParent(cs) |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
305 |
369 | 306 # XXX: delegation must be instancied at the end, |
307 # because it does some MonkeyPatching on handlers | |
308 dh = DelegationsHandler() | |
309 dh.setHandlerParent(cs) | |
310 bs.delegation = dh | |
311 | |
312 return s | |
313 | |
314 serviceMaker = SatPubsubMaker() |