Mercurial > libervia-web
annotate libervia.tac @ 11:331c093e4eb3
magicBox is now able to send global microblog
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Mar 2011 17:53:12 +0100 |
parents | c28a4840e1a8 |
children | 9bf8ed012adc |
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 def jsonrpc_getContacts(self): | |
59 """Return all passed args.""" | |
1 | 60 profile = self.session.sat_profile |
61 return self.sat_host.bridge.getContacts(profile) | |
0 | 62 |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
63 def jsonrpc_sendMblog(self, raw_text): |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
64 """Parse raw_text of the microblog box, and send message consequently""" |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
65 profile = self.session.sat_profile |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
66 if raw_text.startswith('@@: '): |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
67 #This text if for the public microblog |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
68 text = raw_text[4:] |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
69 if text: |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
70 return self.sat_host.bridge.sendPersonalEvent("MICROBLOG", {'content':text}, profile) |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
71 |
0 | 72 class Register(jsonrpc.JSONRPC): |
73 """This class manage the registration procedure with SàT | |
74 It provide an api for the browser, check password and setup the web server""" | |
75 | |
76 def __init__(self, sat_host): | |
77 jsonrpc.JSONRPC.__init__(self) | |
78 self.sat_host=sat_host | |
79 self.profiles_waiting={} | |
80 self.request=None | |
81 | |
82 def getWaitingRequest(self, profile): | |
83 """Tell if a profile is trying to log in""" | |
84 if self.profiles_waiting.has_key(profile): | |
85 return self.profiles_waiting[profile] | |
86 else: | |
87 return None | |
88 | |
89 def render(self, request): | |
90 """ | |
91 Render method with some hacks: | |
92 - if login is requested, try to login with form data | |
93 - except login, every method is jsonrpc | |
94 - user doesn't need to be authentified for isRegistered, but must be for all other methods | |
95 """ | |
96 if request.postpath==['login']: | |
97 return self.login(request) | |
98 _session = request.getSession() | |
99 parsed = jsonrpclib.loads(request.content.read()) | |
100 if parsed.get("method")!="isRegistered": | |
101 #if we don't call login or isRegistered, we need to be identified | |
102 try: | |
103 profile = _session.sat_profile | |
104 except AttributeError: | |
105 #user is not identified, we return a jsonrpc fault | |
106 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
107 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
108 self.request = request | |
109 return jsonrpc.JSONRPC.render(self, request) | |
110 | |
111 def login(self, request): | |
112 """ | |
113 this method is called with the POST information from the registering form | |
114 it test if the password is ok, and log in if it's the case, | |
115 else it return an error | |
116 @param request: request of the register formulaire, must have "login" and "password" as arguments | |
117 @return: A constant indicating the state: | |
118 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login) | |
119 - AUTH ERROR: either the profile or the password is wrong | |
120 - ALREADY WAITING: a request has already be made for this profile | |
121 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError | |
122 """ | |
123 try: | |
124 _login = request.args['login'][0] | |
125 if _login.startswith('@'): | |
126 raise Exception('No profile_key allowed') | |
127 _pass = request.args['password'][0] | |
128 except KeyError: | |
129 return "BAD REQUEST" | |
130 | |
131 _profile_check = self.sat_host.bridge.getProfileName(_login) | |
132 _profile_pass = self.sat_host.bridge.getParamA("Password", "Connection", profile_key=_login) | |
133 | |
134 if not _profile_check or _profile_check != _login or _profile_pass != _pass: | |
135 return "AUTH ERROR" | |
136 | |
137 if self.profiles_waiting.has_key(_login): | |
138 return "ALREADY WAITING" | |
139 | |
140 if self.sat_host.bridge.isConnected(_login): | |
141 return self._logged(_login, request) | |
142 | |
143 self.profiles_waiting[_login] = request | |
144 self.sat_host.bridge.connect(_login) | |
145 return server.NOT_DONE_YET | |
146 | |
147 def __cleanWaiting(self, login): | |
148 """Remove login from waiting queue""" | |
149 try: | |
150 del self.profiles_waiting[login] | |
151 except KeyError: | |
152 pass | |
153 | |
154 def _logged(self, login, request): | |
155 """Set everything when a user just logged | |
156 and return "LOGGED" to the requester""" | |
157 self.__cleanWaiting(login) | |
158 _session = request.getSession() | |
159 _session.sat_profile = login | |
160 return 'LOGGED' | |
161 | |
162 def _logginError(self, login, request, error_type): | |
163 """Something went wrong during loggin, return an error""" | |
164 self.__cleanWaiting(login) | |
165 return error_type | |
166 | |
167 def jsonrpc_isConnected(self): | |
168 _session = self.request.getSession() | |
169 profile = _session.sat_profile | |
170 return self.sat_host.bridge.isConnected(profile) | |
171 | |
172 def jsonrpc_connect(self): | |
173 _session = self.request.getSession() | |
174 profile = _session.sat_profile | |
175 if self.profiles_waiting.has_key(profile): | |
176 raise jsonrpclib.Fault('1','Already waiting') #FIXME: define some standard error codes for libervia | |
177 self.profiles_waiting[profile] = self.request | |
178 self.sat_host.bridge.connect(profile) | |
179 return server.NOT_DONE_YET | |
180 | |
181 def jsonrpc_isRegistered(self): | |
182 """Tell if the user is already registered""" | |
183 _session = self.request.getSession() | |
184 try: | |
185 profile = _session.sat_profile | |
186 except AttributeError: | |
187 return False | |
188 return True | |
189 | |
190 class SignalHandler(jsonrpc.JSONRPC): | |
191 | |
192 def __init__(self, sat_host): | |
193 Resource.__init__(self) | |
194 self.register=None | |
195 self.sat_host=sat_host | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
196 self.signalDeferred = {} |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
197 |
0 | 198 def plugRegister(self, register): |
199 self.register = register | |
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 jsonrpc_getSignals(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
202 """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
|
203 @return: (signal, *signal_args)""" |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
204 _session = self.request.getSession() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
205 profile = _session.sat_profile |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
206 self.signalDeferred[profile] = defer.Deferred() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
207 return self.signalDeferred[profile] |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
208 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
209 def getGenericCb(self, function_name): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
210 """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
|
211 function must have profile as last argument""" |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
212 def genericCb(*args): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
213 profile = args[-1] |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
214 if profile in self.signalDeferred: |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
215 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
|
216 else: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
217 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
|
218 return genericCb |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
219 |
0 | 220 def connected(self, profile): |
221 assert(self.register) #register must be plugged | |
222 request = self.register.getWaitingRequest(profile) | |
223 if request: | |
224 self.register._logged(profile, request) | |
225 | |
226 def connectionError(self, error_type, profile): | |
227 assert(self.register) #register must be plugged | |
228 request = self.register.getWaitingRequest(profile) | |
229 if request: #The user is trying to log in | |
230 if error_type == "AUTH_ERROR": | |
231 _error_t = "AUTH ERROR" | |
232 else: | |
233 _error_t = "UNKNOWN" | |
234 self.register._logginError(profile, request, _error_t) | |
235 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
236 def render(self, request): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
237 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
238 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
|
239 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
240 _session = request.getSession() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
241 parsed = jsonrpclib.loads(request.content.read()) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
242 try: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
243 profile = _session.sat_profile |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
244 except AttributeError: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
245 #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
|
246 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
|
247 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
|
248 self.request = request |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
249 return jsonrpc.JSONRPC.render(self, request) |
0 | 250 |
10 | 251 |
0 | 252 class Libervia(service.Service): |
253 | |
254 def __init__(self): | |
255 root = File("output/") | |
256 self.signal_handler = SignalHandler(self) | |
257 _register = Register(self) | |
258 self.signal_handler.plugRegister(_register) | |
259 self.sessions = {} #key = session value = user | |
260 ## bridge ## | |
261 try: | |
262 self.bridge=DBusBridgeFrontend() | |
263 except BridgeExceptionNoService: | |
264 print(u"Can't connect to SàT backend, are you sure it's launched ?") | |
265 import sys | |
266 sys.exit(1) | |
267 self.bridge.register("connected", self.signal_handler.connected) | |
268 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
|
269 for signal_name in ['presenceUpdate', 'personalEvent']: |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
270 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) |
10 | 271 root.putChild('json_signal_api', self.signal_handler) |
272 root.putChild('json_api', MethodHandler(self)) | |
273 root.putChild('register_api', _register) | |
274 root.putChild('blog', MicroBlog(self)) | |
275 root.putChild('css', File("server_css/")) | |
276 self.site = server.Site(root) | |
0 | 277 |
278 def startService(self): | |
279 reactor.listenTCP(8080, self.site) | |
1 | 280 |
0 | 281 def run(self): |
282 reactor.run() | |
283 | |
284 def stop(self): | |
285 reactor.stop() | |
286 | |
287 | |
288 | |
289 application = service.Application('Libervia') | |
290 service = Libervia() | |
291 service.setServiceParent(application) |