Mercurial > libervia-web
annotate libervia.tac @ 10:c28a4840e1a8
server: microblog resource
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 25 Mar 2011 00:32:58 +0100 |
parents | 154d4caa57f4 |
children | 331c093e4eb3 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Libervia: a Salut à Toi frontend | |
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU Affero General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU Affero General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU Affero General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from twisted.application import internet, service | |
23 from twisted.internet import glib2reactor | |
24 glib2reactor.install() | |
25 from twisted.internet import reactor, defer | |
26 | |
27 from twisted.web import server | |
28 from twisted.web import error as weberror | |
29 from twisted.web.static import File | |
30 from twisted.web.resource import Resource | |
10 | 31 from twisted.words.protocols.jabber.jid import JID |
0 | 32 from txjsonrpc.web import jsonrpc |
33 from txjsonrpc import jsonrpclib | |
34 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService | |
35 | |
10 | 36 from server_side.blog import MicroBlog |
37 | |
0 | 38 TIMEOUT = 120 #Session's time out, after that the user will be disconnected |
39 | |
40 | |
41 class MethodHandler(jsonrpc.JSONRPC): | |
42 | |
43 def __init__(self, sat_host): | |
44 jsonrpc.JSONRPC.__init__(self) | |
45 self.sat_host=sat_host | |
46 | |
47 def render(self, request): | |
1 | 48 self.session = request.getSession() |
0 | 49 try: |
1 | 50 profile = self.session.sat_profile |
0 | 51 except AttributeError: |
52 #user is not identified, we return a jsonrpc fault | |
53 parsed = jsonrpclib.loads(request.content.read()) | |
54 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
55 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
56 return jsonrpc.JSONRPC.render(self, request) | |
57 | |
58 | |
59 def jsonrpc_getContacts(self): | |
60 """Return all passed args.""" | |
1 | 61 profile = self.session.sat_profile |
62 return self.sat_host.bridge.getContacts(profile) | |
0 | 63 |
64 class Register(jsonrpc.JSONRPC): | |
65 """This class manage the registration procedure with SàT | |
66 It provide an api for the browser, check password and setup the web server""" | |
67 | |
68 def __init__(self, sat_host): | |
69 jsonrpc.JSONRPC.__init__(self) | |
70 self.sat_host=sat_host | |
71 self.profiles_waiting={} | |
72 self.request=None | |
73 | |
74 def getWaitingRequest(self, profile): | |
75 """Tell if a profile is trying to log in""" | |
76 if self.profiles_waiting.has_key(profile): | |
77 return self.profiles_waiting[profile] | |
78 else: | |
79 return None | |
80 | |
81 def render(self, request): | |
82 """ | |
83 Render method with some hacks: | |
84 - if login is requested, try to login with form data | |
85 - except login, every method is jsonrpc | |
86 - user doesn't need to be authentified for isRegistered, but must be for all other methods | |
87 """ | |
88 if request.postpath==['login']: | |
89 return self.login(request) | |
90 _session = request.getSession() | |
91 parsed = jsonrpclib.loads(request.content.read()) | |
92 if parsed.get("method")!="isRegistered": | |
93 #if we don't call login or isRegistered, we need to be identified | |
94 try: | |
95 profile = _session.sat_profile | |
96 except AttributeError: | |
97 #user is not identified, we return a jsonrpc fault | |
98 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
99 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
100 self.request = request | |
101 return jsonrpc.JSONRPC.render(self, request) | |
102 | |
103 def login(self, request): | |
104 """ | |
105 this method is called with the POST information from the registering form | |
106 it test if the password is ok, and log in if it's the case, | |
107 else it return an error | |
108 @param request: request of the register formulaire, must have "login" and "password" as arguments | |
109 @return: A constant indicating the state: | |
110 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login) | |
111 - AUTH ERROR: either the profile or the password is wrong | |
112 - ALREADY WAITING: a request has already be made for this profile | |
113 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError | |
114 """ | |
115 try: | |
116 _login = request.args['login'][0] | |
117 if _login.startswith('@'): | |
118 raise Exception('No profile_key allowed') | |
119 _pass = request.args['password'][0] | |
120 except KeyError: | |
121 return "BAD REQUEST" | |
122 | |
123 _profile_check = self.sat_host.bridge.getProfileName(_login) | |
124 _profile_pass = self.sat_host.bridge.getParamA("Password", "Connection", profile_key=_login) | |
125 | |
126 if not _profile_check or _profile_check != _login or _profile_pass != _pass: | |
127 return "AUTH ERROR" | |
128 | |
129 if self.profiles_waiting.has_key(_login): | |
130 return "ALREADY WAITING" | |
131 | |
132 if self.sat_host.bridge.isConnected(_login): | |
133 return self._logged(_login, request) | |
134 | |
135 self.profiles_waiting[_login] = request | |
136 self.sat_host.bridge.connect(_login) | |
137 return server.NOT_DONE_YET | |
138 | |
139 def __cleanWaiting(self, login): | |
140 """Remove login from waiting queue""" | |
141 try: | |
142 del self.profiles_waiting[login] | |
143 except KeyError: | |
144 pass | |
145 | |
146 def _logged(self, login, request): | |
147 """Set everything when a user just logged | |
148 and return "LOGGED" to the requester""" | |
149 self.__cleanWaiting(login) | |
150 _session = request.getSession() | |
151 _session.sat_profile = login | |
152 return 'LOGGED' | |
153 | |
154 def _logginError(self, login, request, error_type): | |
155 """Something went wrong during loggin, return an error""" | |
156 self.__cleanWaiting(login) | |
157 return error_type | |
158 | |
159 def jsonrpc_isConnected(self): | |
160 _session = self.request.getSession() | |
161 profile = _session.sat_profile | |
162 return self.sat_host.bridge.isConnected(profile) | |
163 | |
164 def jsonrpc_connect(self): | |
165 _session = self.request.getSession() | |
166 profile = _session.sat_profile | |
167 if self.profiles_waiting.has_key(profile): | |
168 raise jsonrpclib.Fault('1','Already waiting') #FIXME: define some standard error codes for libervia | |
169 self.profiles_waiting[profile] = self.request | |
170 self.sat_host.bridge.connect(profile) | |
171 return server.NOT_DONE_YET | |
172 | |
173 def jsonrpc_isRegistered(self): | |
174 """Tell if the user is already registered""" | |
175 _session = self.request.getSession() | |
176 try: | |
177 profile = _session.sat_profile | |
178 except AttributeError: | |
179 return False | |
180 return True | |
181 | |
182 class SignalHandler(jsonrpc.JSONRPC): | |
183 | |
184 def __init__(self, sat_host): | |
185 Resource.__init__(self) | |
186 self.register=None | |
187 self.sat_host=sat_host | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
188 self.signalDeferred = {} |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
189 |
0 | 190 def plugRegister(self, register): |
191 self.register = register | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
192 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
193 def jsonrpc_getSignals(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
194 """Keep the connection alive until a signal is received, then send it |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
195 @return: (signal, *signal_args)""" |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
196 _session = self.request.getSession() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
197 profile = _session.sat_profile |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
198 self.signalDeferred[profile] = defer.Deferred() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
199 return self.signalDeferred[profile] |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
200 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
201 def getGenericCb(self, function_name): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
202 """Return a generic function which send all params to signalDeferred.callback |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
203 function must have profile as last argument""" |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
204 def genericCb(*args): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
205 profile = args[-1] |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
206 if profile in self.signalDeferred: |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
207 self.signalDeferred[profile].callback((function_name,args[:-1])) |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
208 else: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
209 print("Warning: signal [%s] can't be sent" % function_name) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
210 return genericCb |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
211 |
0 | 212 def connected(self, profile): |
213 assert(self.register) #register must be plugged | |
214 request = self.register.getWaitingRequest(profile) | |
215 if request: | |
216 self.register._logged(profile, request) | |
217 | |
218 def connectionError(self, error_type, profile): | |
219 assert(self.register) #register must be plugged | |
220 request = self.register.getWaitingRequest(profile) | |
221 if request: #The user is trying to log in | |
222 if error_type == "AUTH_ERROR": | |
223 _error_t = "AUTH ERROR" | |
224 else: | |
225 _error_t = "UNKNOWN" | |
226 self.register._logginError(profile, request, _error_t) | |
227 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
228 def render(self, request): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
229 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
230 Render method wich reject access if user is not identified |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
231 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
232 _session = request.getSession() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
233 parsed = jsonrpclib.loads(request.content.read()) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
234 try: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
235 profile = _session.sat_profile |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
236 except AttributeError: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
237 #user is not identified, we return a jsonrpc fault |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
238 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
239 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
240 self.request = request |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
241 return jsonrpc.JSONRPC.render(self, request) |
0 | 242 |
10 | 243 |
0 | 244 class Libervia(service.Service): |
245 | |
246 def __init__(self): | |
247 root = File("output/") | |
248 self.signal_handler = SignalHandler(self) | |
249 _register = Register(self) | |
250 self.signal_handler.plugRegister(_register) | |
251 self.sessions = {} #key = session value = user | |
252 ## bridge ## | |
253 try: | |
254 self.bridge=DBusBridgeFrontend() | |
255 except BridgeExceptionNoService: | |
256 print(u"Can't connect to SàT backend, are you sure it's launched ?") | |
257 import sys | |
258 sys.exit(1) | |
259 self.bridge.register("connected", self.signal_handler.connected) | |
260 self.bridge.register("connectionError", self.signal_handler.connectionError) | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
261 for signal_name in ['presenceUpdate', 'personalEvent']: |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
262 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) |
10 | 263 root.putChild('json_signal_api', self.signal_handler) |
264 root.putChild('json_api', MethodHandler(self)) | |
265 root.putChild('register_api', _register) | |
266 root.putChild('blog', MicroBlog(self)) | |
267 root.putChild('css', File("server_css/")) | |
268 self.site = server.Site(root) | |
0 | 269 |
270 def startService(self): | |
271 reactor.listenTCP(8080, self.site) | |
1 | 272 |
0 | 273 def run(self): |
274 reactor.run() | |
275 | |
276 def stop(self): | |
277 reactor.stop() | |
278 | |
279 | |
280 | |
281 application = service.Application('Libervia') | |
282 service = Libervia() | |
283 service.setServiceParent(application) |