Mercurial > libervia-pubsub
annotate twisted/plugins/pubsub.py @ 479:cfa40fa108a4
db: fix pubsub.sql syntax
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 11 May 2022 14:47:53 +0200 |
parents | b544109ab4c4 |
children | 4e8e8788bc86 |
rev | line source |
---|---|
414 | 1 #!/usr/bin/env python3 |
233 | 2 |
440 | 3 # Copyright (c) 2012-2021 Jérôme Poisson |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
4 # Copyright (c) 2003-2011 Ralph Meijer |
233 | 5 |
6 | |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # (at your option) any later version. |
233 | 11 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
12 # 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
|
13 # 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
|
14 # 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
|
15 # GNU Affero General Public License for more details. |
233 | 16 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
17 # 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
|
18 # 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
|
19 # -- |
233 | 20 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
21 # 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
|
22 # 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
|
23 # 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
|
24 # license. |
233 | 25 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
26 # -- |
233 | 27 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
28 # 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
|
29 |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
30 # Copyright (c) 2003-2011 Ralph Meijer |
233 | 31 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
32 # 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
|
33 # 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
|
34 # "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
|
35 # 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
|
36 # 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
|
37 # 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
|
38 # the following conditions: |
233 | 39 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
40 # 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
|
41 # included in all copies or substantial portions of the Software. |
233 | 42 |
312
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
43 # 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
|
44 # 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
|
45 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
5d7c3787672e
fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents:
311
diff
changeset
|
46 # 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
|
47 # 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
|
48 # 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
|
49 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
233 | 50 |
155 | 51 |
418
89736353f6be
install sat_tmp's twisted patches for Python 3.8 compatibility
Goffi <goffi@goffi.org>
parents:
417
diff
changeset
|
52 import sys |
380 | 53 import csv |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
54 import os |
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 from zope.interface import implementer |
369 | 58 from twisted.application.service import IServiceMaker |
59 from twisted.application import service | |
380 | 60 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
|
61 from twisted.plugin import IPlugin |
369 | 62 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
|
63 import sat_pubsub |
453
1a179ad10125
backend: service name can now be specified with `service_name` parameter
Goffi <goffi@goffi.org>
parents:
447
diff
changeset
|
64 from sat_pubsub import const |
369 | 65 |
66 | |
380 | 67 def coerceListType(value): |
414 | 68 return next(csv.reader( |
380 | 69 [value], delimiter=",", quotechar='"', skipinitialspace=True |
414 | 70 )) |
380 | 71 |
72 | |
73 def coerceJidListType(value): | |
74 values = [JID(v) for v in coerceListType(value)] | |
75 if any((j.resource for j in values)): | |
414 | 76 raise ValueError("you must use bare jids") |
380 | 77 return values |
78 | |
79 | |
460 | 80 def coerceJidDomainType(value): |
81 try: | |
82 jid_ = JID(value) | |
83 except Exception as e: | |
84 raise ValueError(f"JID set in configuration ({value!r}) is invalid: {e}") | |
85 if jid_.resource or jid_.user: | |
86 raise ValueError( | |
87 f"JID in configuration ({jid_!r}) must have no local part and no resource" | |
88 ) | |
89 return jid_ | |
90 | |
380 | 91 |
369 | 92 OPT_PARAMETERS_BOTH = [ |
460 | 93 ['jid', None, None, 'JID this component will be available at', coerceJidDomainType], |
369 | 94 ['xmpp_pwd', None, None, 'XMPP server component password'], |
460 | 95 ['server_jid', None, None, 'jid of the server this component is plugged to', |
96 coerceJidDomainType], | |
369 | 97 ['rhost', None, '127.0.0.1', 'XMPP server host'], |
98 ['rport', None, '5347', 'XMPP server port'], | |
99 ['backend', None, 'pgsql', 'Choice of storage backend'], | |
100 ['db_user', None, None, 'Database user (pgsql backend)'], | |
101 ['db_name', None, 'pubsub', 'Database name (pgsql backend)'], | |
102 ['db_pass', None, None, 'Database password (pgsql backend)'], | |
103 ['db_host', None, None, 'Database host (pgsql backend)'], | |
104 ['db_port', None, None, 'Database port (pgsql backend)'], | |
453
1a179ad10125
backend: service name can now be specified with `service_name` parameter
Goffi <goffi@goffi.org>
parents:
447
diff
changeset
|
105 ['service_name', None, const.SERVICE_NAME, 'Name of this Pubsub service'], |
369 | 106 ] |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
107 |
380 | 108 OPT_PARAMETERS_CFG = [ |
109 ["admins_jids_list", None, [], "List of administrators' bare jids", | |
110 coerceJidListType] | |
111 ] | |
369 | 112 |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
113 # prefix used for environment variables |
447
cc29a62fb64c
renaming following global Salut à Toi => Libervia renaming:
Goffi <goffi@goffi.org>
parents:
445
diff
changeset
|
114 ENV_PREFIX = "LIBERVIA_PUBSUB_" |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
115 # 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
|
116 # 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
|
117 # 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
|
118 # 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
|
119 ENV_OPT_MAP = { |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
120 # 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
|
121 'db_user': ['PGUSER'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
122 'db_name': ['PGDATABASE'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
123 'db_pass': ['PGPASSWORD'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
124 'db_host': ['PGHOST'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
125 'db_port': ['PGPORT'], |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
126 } |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
127 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
|
128 name = opt[0] |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
129 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
|
130 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
|
131 |
445
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
132 CONFIG_FILENAME = 'libervia' |
369 | 133 # List of the configuration filenames sorted by ascending priority |
445
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
134 CONFIG_FILES = ( |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
135 [realpath(expanduser(path) + CONFIG_FILENAME + '.conf') for path in ( |
369 | 136 '/etc/', '/etc/{}/'.format(CONFIG_FILENAME), |
137 '~/', '~/.', | |
138 '.config/', '.config/.', | |
426
6f8e1c180c83
added "~/.config/sat" as search patch for config
Goffi <goffi@goffi.org>
parents:
418
diff
changeset
|
139 '.config/{}/'.format(CONFIG_FILENAME), |
369 | 140 '', '.')] |
445
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
141 + |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
142 # "sat.conf" is the legacy name of the config file |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
143 [realpath(expanduser(path) + "sat.conf") for path in ( |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
144 '/etc/', '/etc/{}/'.format("sat"), |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
145 '~/', '~/.', |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
146 '.config/', '.config/.', |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
147 '.config/{}/'.format("sat"), |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
148 '', '.')] |
fe24bb60236f
core: update conf file name following project renaming:
Goffi <goffi@goffi.org>
parents:
440
diff
changeset
|
149 ) |
369 | 150 CONFIG_SECTION = 'pubsub' |
151 | |
152 | |
153 class Options(usage.Options): | |
154 optParameters = OPT_PARAMETERS_BOTH | |
155 | |
156 optFlags = [ | |
157 ('verbose', 'v', 'Show traffic'), | |
158 ('hide-nodes', None, 'Hide all nodes for disco') | |
159 ] | |
160 | |
161 def __init__(self): | |
460 | 162 """Read Libervia Pubsub configuration file in order to overwrite the hard-coded default values. |
369 | 163 |
164 Priority for the usage of the values is (from lowest to highest): | |
165 - hard-coded default values | |
166 - values from SàT configuration files | |
167 - values passed on the command line | |
168 """ | |
169 # If we do it the reading later: after the command line options have been parsed, there's no good way to know | |
170 # if the options values are the hard-coded ones or if they have been passed on the command line. | |
171 | |
172 # FIXME: must be refactored + code can be factorised with backend | |
414 | 173 config_parser = configparser.ConfigParser() |
369 | 174 config_parser.read(CONFIG_FILES) |
175 for param in self.optParameters + OPT_PARAMETERS_CFG: | |
176 name = param[0] | |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
177 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
|
178 # 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
|
179 value = os.getenv(env_name) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
180 if value is not None: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
181 self.setDefaultOption(param, value) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
182 break |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
183 else: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
184 # no environment variable set, let's try with configuration |
369 | 185 try: |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
186 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
|
187 self.setDefaultOption(param, value) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
188 except (configparser.NoSectionError, configparser.NoOptionError): |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
189 pass |
369 | 190 usage.Options.__init__(self) |
191 for opt_data in OPT_PARAMETERS_CFG: | |
192 self[opt_data[0]] = opt_data[2] | |
155 | 193 |
428
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
194 def setDefaultOption(self, param, value): |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
195 """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
|
196 |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
197 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
|
198 """ |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
199 try: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
200 param[2] = param[4](value) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
201 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
|
202 param[2] = value |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
203 except Exception as e: |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
204 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
|
205 name=name, msg=e)) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
206 sys.exit(1) |
34bd55179e22
environment variables can now be used to set options:
Goffi <goffi@goffi.org>
parents:
426
diff
changeset
|
207 |
369 | 208 def postOptions(self): |
209 if self['backend'] not in ['pgsql', 'memory']: | |
406 | 210 raise usage.UsageError("Unknown backend!") |
369 | 211 if self['backend'] == 'memory': |
212 raise NotImplementedError('memory backend is not available at the moment') | |
213 | |
214 | |
414 | 215 @implementer(IServiceMaker, IPlugin) |
369 | 216 class SatPubsubMaker(object): |
447
cc29a62fb64c
renaming following global Salut à Toi => Libervia renaming:
Goffi <goffi@goffi.org>
parents:
445
diff
changeset
|
217 tapname = "libervia-pubsub" |
cc29a62fb64c
renaming following global Salut à Toi => Libervia renaming:
Goffi <goffi@goffi.org>
parents:
445
diff
changeset
|
218 description = "Libervia's Publish-Subscribe Service Component" |
369 | 219 options = Options |
220 | |
221 def makeService(self, config): | |
417
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
222 from wokkel.component import Component |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
223 from wokkel.disco import DiscoHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
224 from wokkel.generic import FallbackHandler, VersionHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
225 from wokkel.iwokkel import IPubSubResource |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
226 from wokkel import data_form |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
227 from wokkel import pubsub |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
228 from wokkel import rsm |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
229 from wokkel import mam |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
230 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
|
231 from sat_pubsub import pubsub_admin |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
232 from sat_pubsub.backend import BackendService, ExtraDiscoHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
233 from sat_pubsub.privilege import PrivilegesHandler |
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
234 from sat_pubsub.delegation import DelegationsHandler |
478
b544109ab4c4
Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents:
460
diff
changeset
|
235 from sat_pubsub.pam import PAMHandler |
417
412d26a9b2c2
import sat/wokkel modules only when making service:
Goffi <goffi@goffi.org>
parents:
414
diff
changeset
|
236 |
369 | 237 if not config['jid'] or not config['xmpp_pwd']: |
238 raise usage.UsageError("You must specify jid and xmpp_pwd") | |
239 s = service.MultiService() | |
240 | |
241 # Create backend service with storage | |
242 | |
243 if config['backend'] == 'pgsql': | |
244 from twisted.enterprise import adbapi | |
245 from sat_pubsub.pgsql_storage import Storage | |
246 from psycopg2.extras import NamedTupleConnection | |
247 keys_map = { | |
248 'db_user': 'user', | |
249 'db_pass': 'password', | |
250 'db_name': 'database', | |
251 'db_host': 'host', | |
252 'db_port': 'port', | |
253 } | |
254 kwargs = {} | |
414 | 255 for config_k, k in keys_map.items(): |
369 | 256 v = config.get(config_k) |
257 if v is None: | |
258 continue | |
259 kwargs[k] = v | |
260 dbpool = adbapi.ConnectionPool('psycopg2', | |
261 cp_reconnect=True, | |
262 client_encoding='utf-8', | |
263 connection_factory=NamedTupleConnection, | |
264 **kwargs | |
265 ) | |
266 st = Storage(dbpool) | |
267 elif config['backend'] == 'memory': | |
406 | 268 raise NotImplementedError('memory backend is not available at the moment') |
369 | 269 |
380 | 270 bs = BackendService(st, config) |
369 | 271 bs.setName('backend') |
272 bs.setServiceParent(s) | |
273 | |
274 # Set up XMPP server-side component with publish-subscribe capabilities | |
275 | |
276 cs = Component(config["rhost"], int(config["rport"]), | |
277 config["jid"].full(), config["xmpp_pwd"]) | |
278 cs.setName('component') | |
279 cs.setServiceParent(s) | |
280 | |
281 cs.factory.maxDelay = 900 | |
282 | |
283 if config["verbose"]: | |
284 cs.logTraffic = True | |
285 | |
286 FallbackHandler().setHandlerParent(cs) | |
458
bc2e04a4d3c1
version handler name updated following global renaming
Goffi <goffi@goffi.org>
parents:
453
diff
changeset
|
287 VersionHandler('Libervia Pubsub', sat_pubsub.__version__).setHandlerParent(cs) |
369 | 288 DiscoHandler().setHandlerParent(cs) |
289 | |
290 ph = PrivilegesHandler(config['jid']) | |
291 ph.setHandlerParent(cs) | |
292 bs.privilege = ph | |
293 | |
478
b544109ab4c4
Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents:
460
diff
changeset
|
294 pam = PAMHandler(config["jid"]) |
b544109ab4c4
Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents:
460
diff
changeset
|
295 pam.setHandlerParent(cs) |
b544109ab4c4
Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents:
460
diff
changeset
|
296 bs.pam = pam |
b544109ab4c4
Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents:
460
diff
changeset
|
297 |
369 | 298 resource = IPubSubResource(bs) |
299 resource.hideNodes = config["hide-nodes"] | |
300 resource.serviceJID = config["jid"] | |
301 | |
302 ps = (rsm if const.FLAG_ENABLE_RSM else pubsub).PubSubService(resource) | |
303 ps.setHandlerParent(cs) | |
304 resource.pubsubService = ps | |
305 | |
306 if const.FLAG_ENABLE_MAM: | |
307 mam_resource = pubsub_mam.MAMResource(bs) | |
308 mam_s = mam.MAMService(mam_resource) | |
309 mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_CATEGORY)) | |
430 | 310 mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_FTS)) |
369 | 311 mam_s.setHandlerParent(cs) |
312 | |
382
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
313 pa = pubsub_admin.PubsubAdminHandler(bs) |
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
314 pa.setHandlerParent(cs) |
77b52dbda89a
pubsub_admin: Pubsub Admin experimental protocol first draft:
Goffi <goffi@goffi.org>
parents:
380
diff
changeset
|
315 |
375
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
316 # wokkel.pubsub doesn't handle non pubsub# disco |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
317 # 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
|
318 # to add them |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
319 # FIXME: propose a patch upstream to fix this situation |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
320 ed = ExtraDiscoHandler() |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
321 ed.setHandlerParent(cs) |
9a787881b824
implemented Order-By ProtoXEP (MAM + PubSub)
Goffi <goffi@goffi.org>
parents:
369
diff
changeset
|
322 |
369 | 323 # XXX: delegation must be instancied at the end, |
324 # because it does some MonkeyPatching on handlers | |
325 dh = DelegationsHandler() | |
326 dh.setHandlerParent(cs) | |
327 bs.delegation = dh | |
328 | |
329 return s | |
330 | |
331 serviceMaker = SatPubsubMaker() |