annotate src/twisted/plugins/pubsub.py @ 374:40e5edd7ea11

storage: changed items order: items were so far ordered by item_id, i.e. by order of creation. This has been changed to order them by date of last update, so an item overwritting an old one appears on top (with former behaviour, it was staying at the same place). The XEP is not clear on that, but this seems more in correct with § 6.5.7 which says "implementations SHOULD return the N most recent (as opposed to the N oldest) items".
author Goffi <goffi@goffi.org>
date Thu, 03 Jan 2019 20:31:03 +0100
parents dabee42494ac
children 9a787881b824
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
1 #!/usr/bin/python
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
2 #-*- coding: utf-8 -*-
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
3
368
618a92080812 date update
Goffi <goffi@goffi.org>
parents: 365
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
6
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
26
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 311
diff changeset
27 # --
233
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
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
564ae55219e1 sublicensed under AGPL V3
Goffi <goffi@goffi.org>
parents: 192
diff changeset
51
155
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52
369
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
53 import sat_pubsub
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
54 from twisted.application.service import IServiceMaker
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
55 from twisted.application import service
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
56 from twisted.python import usage
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
57 from twisted.words.protocols.jabber.jid import JID
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
58 from twisted.plugin import IPlugin
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
59
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
60 from wokkel.component import Component
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
61 from wokkel.disco import DiscoHandler
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
62 from wokkel.generic import FallbackHandler, VersionHandler
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
63 from wokkel.iwokkel import IPubSubResource
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
64 from wokkel import data_form
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
65 from wokkel import pubsub
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
66 from wokkel import rsm
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
67 from wokkel import mam
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
68 from zope.interface import implements
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
69
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
70 from sat_pubsub import const
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
71 from sat_pubsub import mam as pubsub_mam
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
72 from sat_pubsub.backend import BackendService
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
73 from sat_pubsub.schema import SchemaHandler
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
74 from sat_pubsub.privilege import PrivilegesHandler
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
75 from sat_pubsub.delegation import DelegationsHandler
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
76 from os.path import expanduser, realpath
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
77 import ConfigParser
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
78
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
79
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
80 OPT_PARAMETERS_BOTH = [
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
81 ['jid', None, None, 'JID this component will be available at'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
82 ['xmpp_pwd', None, None, 'XMPP server component password'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
83 ['rhost', None, '127.0.0.1', 'XMPP server host'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
84 ['rport', None, '5347', 'XMPP server port'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
85 ['backend', None, 'pgsql', 'Choice of storage backend'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
86 ['db_user', None, None, 'Database user (pgsql backend)'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
87 ['db_name', None, 'pubsub', 'Database name (pgsql backend)'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
88 ['db_pass', None, None, 'Database password (pgsql backend)'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
89 ['db_host', None, None, 'Database host (pgsql backend)'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
90 ['db_port', None, None, 'Database port (pgsql backend)'],
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
91 ]
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
92 # here for future use
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
93 OPT_PARAMETERS_CFG = []
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
94
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
95 CONFIG_FILENAME = u'sat'
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
96 # List of the configuration filenames sorted by ascending priority
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
97 CONFIG_FILES = [realpath(expanduser(path) + CONFIG_FILENAME + '.conf') for path in (
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
98 '/etc/', '/etc/{}/'.format(CONFIG_FILENAME),
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
99 '~/', '~/.',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
100 '.config/', '.config/.',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
101 '', '.')]
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
102 CONFIG_SECTION = 'pubsub'
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
103
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
104
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
105 class Options(usage.Options):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
106 optParameters = OPT_PARAMETERS_BOTH
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
107
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
108 optFlags = [
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
109 ('verbose', 'v', 'Show traffic'),
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
110 ('hide-nodes', None, 'Hide all nodes for disco')
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
111 ]
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
112
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
113 def __init__(self):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
114 """Read SàT Pubsub configuration file in order to overwrite the hard-coded default values.
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
115
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
116 Priority for the usage of the values is (from lowest to highest):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
117 - hard-coded default values
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
118 - values from SàT configuration files
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
119 - values passed on the command line
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
120 """
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
121 # If we do it the reading later: after the command line options have been parsed, there's no good way to know
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
122 # if the options values are the hard-coded ones or if they have been passed on the command line.
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
123
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
124 # FIXME: must be refactored + code can be factorised with backend
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
125 config_parser = ConfigParser.SafeConfigParser()
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
126 config_parser.read(CONFIG_FILES)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
127 for param in self.optParameters + OPT_PARAMETERS_CFG:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
128 name = param[0]
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
129 try:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
130 value = config_parser.get(CONFIG_SECTION, name)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
131 if isinstance(value, unicode):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
132 value = value.encode('utf-8')
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
133 try:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
134 param[2] = param[4](value)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
135 except IndexError: # the coerce method is optional
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
136 param[2] = value
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
137 except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
138 pass
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
139 usage.Options.__init__(self)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
140 for opt_data in OPT_PARAMETERS_CFG:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
141 self[opt_data[0]] = opt_data[2]
155
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
142
369
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
143 def postOptions(self):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
144 if self['backend'] not in ['pgsql', 'memory']:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
145 raise usage.UsageError, "Unknown backend!"
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
146 if self['backend'] == 'memory':
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
147 raise NotImplementedError('memory backend is not available at the moment')
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
148
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
149 self['jid'] = JID(self['jid']) if self['jid'] else None
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
150
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
151
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
152 class SatPubsubMaker(object):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
153 implements(IServiceMaker, IPlugin)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
154 tapname = "sat-pubsub"
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
155 description = u"Salut à Toi Publish-Subscribe Service Component".encode('utf-8')
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
156 options = Options
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
157
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
158 def makeService(self, config):
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
159 if not config['jid'] or not config['xmpp_pwd']:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
160 raise usage.UsageError("You must specify jid and xmpp_pwd")
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
161 s = service.MultiService()
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
162
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
163 # Create backend service with storage
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
164
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
165 if config['backend'] == 'pgsql':
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
166 from twisted.enterprise import adbapi
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
167 from sat_pubsub.pgsql_storage import Storage
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
168 from psycopg2.extras import NamedTupleConnection
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
169 keys_map = {
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
170 'db_user': 'user',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
171 'db_pass': 'password',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
172 'db_name': 'database',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
173 'db_host': 'host',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
174 'db_port': 'port',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
175 }
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
176 kwargs = {}
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
177 for config_k, k in keys_map.iteritems():
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
178 v = config.get(config_k)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
179 if v is None:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
180 continue
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
181 kwargs[k] = v
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
182 dbpool = adbapi.ConnectionPool('psycopg2',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
183 cp_reconnect=True,
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
184 client_encoding='utf-8',
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
185 connection_factory=NamedTupleConnection,
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
186 **kwargs
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
187 )
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
188 st = Storage(dbpool)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
189 elif config['backend'] == 'memory':
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
190 from sat_pubsub.memory_storage import Storage
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
191 st = Storage()
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
192
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
193 bs = BackendService(st)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
194 bs.setName('backend')
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
195 bs.setServiceParent(s)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
196
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
197 # Set up XMPP server-side component with publish-subscribe capabilities
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
198
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
199 cs = Component(config["rhost"], int(config["rport"]),
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
200 config["jid"].full(), config["xmpp_pwd"])
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
201 cs.setName('component')
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
202 cs.setServiceParent(s)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
203
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
204 cs.factory.maxDelay = 900
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
205
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
206 if config["verbose"]:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
207 cs.logTraffic = True
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
208
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
209 FallbackHandler().setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
210 VersionHandler(u'SàT Pubsub', sat_pubsub.__version__).setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
211 DiscoHandler().setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
212
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
213 ph = PrivilegesHandler(config['jid'])
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
214 ph.setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
215 bs.privilege = ph
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
216
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
217 resource = IPubSubResource(bs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
218 resource.hideNodes = config["hide-nodes"]
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
219 resource.serviceJID = config["jid"]
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
220
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
221 ps = (rsm if const.FLAG_ENABLE_RSM else pubsub).PubSubService(resource)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
222 ps.setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
223 resource.pubsubService = ps
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
224
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
225 if const.FLAG_ENABLE_MAM:
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
226 mam_resource = pubsub_mam.MAMResource(bs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
227 mam_s = mam.MAMService(mam_resource)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
228 mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_CATEGORY))
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
229 mam_s.setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
230
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
231 sh = SchemaHandler()
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
232 sh.setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
233
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
234 # XXX: delegation must be instancied at the end,
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
235 # because it does some MonkeyPatching on handlers
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
236 dh = DelegationsHandler()
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
237 dh.setHandlerParent(cs)
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
238 bs.delegation = dh
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
239
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
240 return s
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
241
dabee42494ac config file + cleaning:
Goffi <goffi@goffi.org>
parents: 368
diff changeset
242 serviceMaker = SatPubsubMaker()