Mercurial > libervia-pubsub
annotate sat_pubsub/tap.py @ 242:a6170637690d
remote roster partial support
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 27 May 2012 15:36:06 +0200 |
parents | d2bdff8a4b45 |
children | 2a948abb77ed |
rev | line source |
---|---|
233 | 1 #!/usr/bin/python |
2 #-*- coding: utf-8 -*- | |
3 | |
4 """ | |
235
64f780413b82
fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents:
234
diff
changeset
|
5 Copyright (c) 2003-2011 Ralph Meijer |
233 | 6 Copyright (c) 2012 Jérôme Poisson |
7 | |
8 | |
9 This program is free software: you can redistribute it and/or modify | |
10 it under the terms of the GNU Affero General Public License as published by | |
11 the Free Software Foundation, either version 3 of the License, or | |
12 (at your option) any later version. | |
13 | |
14 This program is distributed in the hope that it will be useful, | |
15 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 GNU Affero General Public License for more details. | |
18 | |
19 You should have received a copy of the GNU Affero General Public License | |
20 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 -- | |
22 | |
23 This program is based on Idavoll (http://idavoll.ik.nu/), | |
24 originaly written by Ralph Meijer (http://ralphm.net/blog/) | |
25 It is sublicensed under AGPL v3 (or any later version) as allowed by the original | |
26 license. | |
27 | |
28 -- | |
29 | |
30 Here is a copy of the original license: | |
31 | |
235
64f780413b82
fixed Ralph Meijer copyright years (last commit was in 2011, not in 2009)
Goffi <goffi@goffi.org>
parents:
234
diff
changeset
|
32 Copyright (c) 2003-2011 Ralph Meijer |
233 | 33 |
34 Permission is hereby granted, free of charge, to any person obtaining | |
35 a copy of this software and associated documentation files (the | |
36 "Software"), to deal in the Software without restriction, including | |
37 without limitation the rights to use, copy, modify, merge, publish, | |
38 distribute, sublicense, and/or sell copies of the Software, and to | |
39 permit persons to whom the Software is furnished to do so, subject to | |
40 the following conditions: | |
41 | |
42 The above copyright notice and this permission notice shall be | |
43 included in all copies or substantial portions of the Software. | |
44 | |
45 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
46 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
47 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
48 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
49 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
50 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
51 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
52 | |
53 """ | |
155
5191ba7c4df8
Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents:
97
diff
changeset
|
54 |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
55 from twisted.application import service |
1 | 56 from twisted.python import usage |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
57 from twisted.words.protocols.jabber.jid import JID |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
58 |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
59 from wokkel.component import Component |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
60 from wokkel.disco import DiscoHandler |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
61 from wokkel.generic import FallbackHandler, VersionHandler |
222
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
62 from wokkel.iwokkel import IPubSubResource |
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
63 from wokkel.pubsub import PubSubService |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
64 |
234
51af2ed8bd50
replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents:
233
diff
changeset
|
65 from sat_pubsub import __version__ |
51af2ed8bd50
replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents:
233
diff
changeset
|
66 from sat_pubsub.backend import BackendService |
242 | 67 from sat_pubsub.remote_roster import RosterClient |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
68 |
1 | 69 class Options(usage.Options): |
156
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
70 optParameters = [ |
170
958e69630e52
Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents:
169
diff
changeset
|
71 ('jid', None, 'pubsub', 'JID this component will be available at'), |
958e69630e52
Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents:
169
diff
changeset
|
72 ('secret', None, 'secret', 'Jabber server component secret'), |
958e69630e52
Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents:
169
diff
changeset
|
73 ('rhost', None, '127.0.0.1', 'Jabber server host'), |
958e69630e52
Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents:
169
diff
changeset
|
74 ('rport', None, '5347', 'Jabber server port'), |
958e69630e52
Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents:
169
diff
changeset
|
75 ('backend', None, 'memory', 'Choice of storage backend'), |
190
6e6c89eca9db
Make sure optional PostgreSQL connect parameters are passed as None, require
Ralph Meijer <ralphm@ik.nu>
parents:
189
diff
changeset
|
76 ('dbuser', None, None, 'Database user (pgsql backend)'), |
170
958e69630e52
Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents:
169
diff
changeset
|
77 ('dbname', None, 'pubsub', 'Database name (pgsql backend)'), |
190
6e6c89eca9db
Make sure optional PostgreSQL connect parameters are passed as None, require
Ralph Meijer <ralphm@ik.nu>
parents:
189
diff
changeset
|
78 ('dbpass', None, None, 'Database password (pgsql backend)'), |
6e6c89eca9db
Make sure optional PostgreSQL connect parameters are passed as None, require
Ralph Meijer <ralphm@ik.nu>
parents:
189
diff
changeset
|
79 ('dbhost', None, None, 'Database host (pgsql backend)'), |
6e6c89eca9db
Make sure optional PostgreSQL connect parameters are passed as None, require
Ralph Meijer <ralphm@ik.nu>
parents:
189
diff
changeset
|
80 ('dbport', None, None, 'Database port (pgsql backend)'), |
156
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
81 ] |
76
66fac7cd9edc
Added -v (--verbose) flag to print out all traffic.
Ralph Meijer <ralphm@ik.nu>
parents:
42
diff
changeset
|
82 |
156
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
83 optFlags = [ |
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
84 ('verbose', 'v', 'Show traffic'), |
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
85 ('hide-nodes', None, 'Hide all nodes for disco') |
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
86 ] |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
87 |
156
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
88 def postOptions(self): |
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
89 if self['backend'] not in ['pgsql', 'memory']: |
6250905b72f6
Fix spacing errors. Do 0.5.0 release.
Ralph Meijer <ralphm@ik.nu>
parents:
155
diff
changeset
|
90 raise usage.UsageError, "Unknown backend!" |
1 | 91 |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
176
diff
changeset
|
92 self['jid'] = JID(self['jid']) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
176
diff
changeset
|
93 |
222
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
94 |
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
95 |
1 | 96 def makeService(config): |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
97 s = service.MultiService() |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
98 |
176 | 99 # Create backend service with storage |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
100 |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
101 if config['backend'] == 'pgsql': |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
102 from twisted.enterprise import adbapi |
234
51af2ed8bd50
replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents:
233
diff
changeset
|
103 from sat_pubsub.pgsql_storage import Storage |
227
8540825f85e0
Replaced unmaintained pyPgSQL by Psycopg 2
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
104 dbpool = adbapi.ConnectionPool('psycopg2', |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
105 user=config['dbuser'], |
218
479fc959af0e
Fix wrong use of configuration variables for pgsql db initialization.
Ralph Meijer <ralphm@ik.nu>
parents:
217
diff
changeset
|
106 password=config['dbpass'], |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
107 database=config['dbname'], |
218
479fc959af0e
Fix wrong use of configuration variables for pgsql db initialization.
Ralph Meijer <ralphm@ik.nu>
parents:
217
diff
changeset
|
108 host=config['dbhost'], |
204
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
109 port=config['dbport'], |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
110 cp_reconnect=True, |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
111 client_encoding='utf-8', |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
112 ) |
b4bf0a5ce50d
Implement storage facilities for the HTTP gateway.
Ralph Meijer <ralphm@ik.nu>
parents:
193
diff
changeset
|
113 st = Storage(dbpool) |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
114 elif config['backend'] == 'memory': |
234
51af2ed8bd50
replaced idavoll imports by sat_pubsub imports
Goffi <goffi@goffi.org>
parents:
233
diff
changeset
|
115 from sat_pubsub.memory_storage import Storage |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
116 st = Storage() |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
117 |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
118 bs = BackendService(st) |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
176
diff
changeset
|
119 bs.setName('backend') |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
120 bs.setServiceParent(s) |
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
121 |
176 | 122 # Set up XMPP server-side component with publish-subscribe capabilities |
123 | |
124 cs = Component(config["rhost"], int(config["rport"]), | |
177
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
176
diff
changeset
|
125 config["jid"].full(), config["secret"]) |
faf1c9bc2612
Add HTTP gateway in a separate plugin.
Ralph Meijer <ralphm@ik.nu>
parents:
176
diff
changeset
|
126 cs.setName('component') |
176 | 127 cs.setServiceParent(s) |
128 | |
129 cs.factory.maxDelay = 900 | |
130 | |
131 if config["verbose"]: | |
132 cs.logTraffic = True | |
133 | |
134 FallbackHandler().setHandlerParent(cs) | |
241
d2bdff8a4b45
changed version handler to SàT Pubsub
Goffi <goffi@goffi.org>
parents:
235
diff
changeset
|
135 VersionHandler(u'SàT Pubsub', __version__).setHandlerParent(cs) |
176 | 136 DiscoHandler().setHandlerParent(cs) |
137 | |
222
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
138 resource = IPubSubResource(bs) |
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
139 resource.hideNodes = config["hide-nodes"] |
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
140 resource.serviceJID = config["jid"] |
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
141 |
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
142 ps = PubSubService(resource) |
169
96afb248df5e
Fix typos in service creation. Make disco not respond when a resource is provided.
Ralph Meijer <ralphm@ik.nu>
parents:
167
diff
changeset
|
143 ps.setHandlerParent(cs) |
222
698af5d720ad
Reshape Idavoll as a PubSubResource.
Ralph Meijer <ralphm@ik.nu>
parents:
218
diff
changeset
|
144 resource.pubsubService = ps |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
145 |
242 | 146 rc = RosterClient() |
147 rc.setHandlerParent(cs) | |
148 bs.roster = rc | |
149 | |
167
ef22e4150caa
Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents:
163
diff
changeset
|
150 return s |