Mercurial > libervia-web
annotate libervia.tac @ 19:e8e3704eb97f
Added basic chat panel
- the chat panel show history, timestamp, and nickname (pretty similar to primitivus and wix chat window)
- JID has be rewritten to work with pyjamas, and is now in browser_side directory
- a widget can now be selected: the message send in uniBox will be sent to it if there is no explicit target prefix ("@something")
- a basic status panel is added under the uniBox, but not used yet
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 16 Apr 2011 01:46:01 +0200 |
parents | 9bf8ed012adc |
children | 8f4b1a8914c3 |
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 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
35 import re |
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) | |
19 | 57 |
58 def jsonrpc_getProfileJid(self): | |
59 """Return the jid of the profile""" | |
60 profile = self.session.sat_profile | |
61 self.session.sat_jid = self.sat_host.bridge.getParamA("JabberID", "Connection", profile_key=profile) | |
62 return self.session.sat_jid | |
0 | 63 |
64 def jsonrpc_getContacts(self): | |
65 """Return all passed args.""" | |
1 | 66 profile = self.session.sat_profile |
67 return self.sat_host.bridge.getContacts(profile) | |
19 | 68 |
69 def jsonrpc_sendMessage(self, to_jid, msg, subject, type): | |
70 """send message""" | |
71 profile = self.session.sat_profile | |
72 return self.sat_host.bridge.sendMessage(to_jid, msg, subject, type, profile) | |
0 | 73 |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
74 def jsonrpc_sendMblog(self, raw_text): |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
75 """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
|
76 profile = self.session.sat_profile |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
77 match = re.match(r'@(.+?): *(.*$)', raw_text) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
78 if match: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
79 recip = match.group(1) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
80 text = match.group(2) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
81 if recip == '@' and text: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
82 #This text if for the public microblog |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
83 print "Sending message to everybody" |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
84 return self.sat_host.bridge.sendPersonalEvent("MICROBLOG", {'content':text}, profile) |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
85 else: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
86 return self.sat_host.bridge.sendGroupBlog([recip], text, profile) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
87 |
19 | 88 def jsonrpc_getHistory(self, from_jid, to_jid, size): |
89 """Return history for the from_jid/to_jid couple""" | |
90 #FIXME: this method should definitely be asynchrone, need to fix it !!! | |
91 profile = self.session.sat_profile | |
92 try: | |
93 _jid = JID(self.session.sat_jid) | |
94 except: | |
95 error("No jid saved for this profile") | |
96 return {} | |
97 if JID(from_jid).userhost() != _jid.userhost() and JID(to_jid) != _jid.userhost(): | |
98 error("Trying to get history from a different jid, maybe a hack attempt ?") | |
99 return {} | |
100 return self.sat_host.bridge.getHistory(from_jid, to_jid, size) | |
101 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
102 |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
103 |
0 | 104 class Register(jsonrpc.JSONRPC): |
105 """This class manage the registration procedure with SàT | |
106 It provide an api for the browser, check password and setup the web server""" | |
107 | |
108 def __init__(self, sat_host): | |
109 jsonrpc.JSONRPC.__init__(self) | |
110 self.sat_host=sat_host | |
111 self.profiles_waiting={} | |
112 self.request=None | |
113 | |
114 def getWaitingRequest(self, profile): | |
115 """Tell if a profile is trying to log in""" | |
116 if self.profiles_waiting.has_key(profile): | |
117 return self.profiles_waiting[profile] | |
118 else: | |
119 return None | |
120 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
121 def _fillMblogNodes(self, result, session): |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
122 """Fill the microblog nodes association for this session""" |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
123 print "Filling session for %s with %s" % (session.sat_profile, result) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
124 session.sat_mblog_nodes = dict(result) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
125 |
0 | 126 def render(self, request): |
127 """ | |
128 Render method with some hacks: | |
129 - if login is requested, try to login with form data | |
130 - except login, every method is jsonrpc | |
131 - user doesn't need to be authentified for isRegistered, but must be for all other methods | |
132 """ | |
133 if request.postpath==['login']: | |
134 return self.login(request) | |
135 _session = request.getSession() | |
136 parsed = jsonrpclib.loads(request.content.read()) | |
137 if parsed.get("method")!="isRegistered": | |
138 #if we don't call login or isRegistered, we need to be identified | |
139 try: | |
140 profile = _session.sat_profile | |
141 except AttributeError: | |
142 #user is not identified, we return a jsonrpc fault | |
143 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
144 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
145 self.request = request | |
146 return jsonrpc.JSONRPC.render(self, request) | |
147 | |
148 def login(self, request): | |
149 """ | |
150 this method is called with the POST information from the registering form | |
151 it test if the password is ok, and log in if it's the case, | |
152 else it return an error | |
153 @param request: request of the register formulaire, must have "login" and "password" as arguments | |
154 @return: A constant indicating the state: | |
155 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login) | |
156 - AUTH ERROR: either the profile or the password is wrong | |
157 - ALREADY WAITING: a request has already be made for this profile | |
158 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError | |
159 """ | |
160 try: | |
161 _login = request.args['login'][0] | |
162 if _login.startswith('@'): | |
163 raise Exception('No profile_key allowed') | |
164 _pass = request.args['password'][0] | |
165 except KeyError: | |
166 return "BAD REQUEST" | |
167 | |
168 _profile_check = self.sat_host.bridge.getProfileName(_login) | |
169 _profile_pass = self.sat_host.bridge.getParamA("Password", "Connection", profile_key=_login) | |
170 | |
171 if not _profile_check or _profile_check != _login or _profile_pass != _pass: | |
172 return "AUTH ERROR" | |
173 | |
174 if self.profiles_waiting.has_key(_login): | |
175 return "ALREADY WAITING" | |
176 | |
177 if self.sat_host.bridge.isConnected(_login): | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
178 return self._logged(_login, request, finish=False) |
0 | 179 |
180 self.profiles_waiting[_login] = request | |
181 self.sat_host.bridge.connect(_login) | |
182 return server.NOT_DONE_YET | |
183 | |
184 def __cleanWaiting(self, login): | |
185 """Remove login from waiting queue""" | |
186 try: | |
187 del self.profiles_waiting[login] | |
188 except KeyError: | |
189 pass | |
190 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
191 def _logged(self, profile, request, finish=True): |
0 | 192 """Set everything when a user just logged |
193 and return "LOGGED" to the requester""" | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
194 self.__cleanWaiting(profile) |
0 | 195 _session = request.getSession() |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
196 _session.sat_profile = profile |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
197 d = defer.Deferred() |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
198 self.sat_host.bridge.getMblogNodes(profile, d.callback, d.errback) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
199 d.addCallback(self._fillMblogNodes, _session) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
200 if finish: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
201 request.write('LOGGED') |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
202 request.finish() |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
203 else: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
204 return "LOGGED" |
0 | 205 |
206 def _logginError(self, login, request, error_type): | |
207 """Something went wrong during loggin, return an error""" | |
208 self.__cleanWaiting(login) | |
209 return error_type | |
210 | |
211 def jsonrpc_isConnected(self): | |
212 _session = self.request.getSession() | |
213 profile = _session.sat_profile | |
214 return self.sat_host.bridge.isConnected(profile) | |
215 | |
216 def jsonrpc_connect(self): | |
217 _session = self.request.getSession() | |
218 profile = _session.sat_profile | |
219 if self.profiles_waiting.has_key(profile): | |
220 raise jsonrpclib.Fault('1','Already waiting') #FIXME: define some standard error codes for libervia | |
221 self.profiles_waiting[profile] = self.request | |
222 self.sat_host.bridge.connect(profile) | |
223 return server.NOT_DONE_YET | |
224 | |
225 def jsonrpc_isRegistered(self): | |
226 """Tell if the user is already registered""" | |
227 _session = self.request.getSession() | |
228 try: | |
229 profile = _session.sat_profile | |
230 except AttributeError: | |
231 return False | |
232 return True | |
233 | |
234 class SignalHandler(jsonrpc.JSONRPC): | |
235 | |
236 def __init__(self, sat_host): | |
237 Resource.__init__(self) | |
238 self.register=None | |
239 self.sat_host=sat_host | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
240 self.signalDeferred = {} |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
241 |
0 | 242 def plugRegister(self, register): |
243 self.register = register | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
244 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
245 def jsonrpc_getSignals(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
246 """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
|
247 @return: (signal, *signal_args)""" |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
248 _session = self.request.getSession() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
249 profile = _session.sat_profile |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
250 self.signalDeferred[profile] = defer.Deferred() |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
251 return self.signalDeferred[profile] |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
252 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
253 def getGenericCb(self, function_name): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
254 """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
|
255 function must have profile as last argument""" |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
256 def genericCb(*args): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
257 profile = args[-1] |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
258 if profile in self.signalDeferred: |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
259 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
|
260 else: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
261 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
|
262 return genericCb |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
263 |
0 | 264 def connected(self, profile): |
265 assert(self.register) #register must be plugged | |
266 request = self.register.getWaitingRequest(profile) | |
267 if request: | |
268 self.register._logged(profile, request) | |
269 | |
270 def connectionError(self, error_type, profile): | |
271 assert(self.register) #register must be plugged | |
272 request = self.register.getWaitingRequest(profile) | |
273 if request: #The user is trying to log in | |
274 if error_type == "AUTH_ERROR": | |
275 _error_t = "AUTH ERROR" | |
276 else: | |
277 _error_t = "UNKNOWN" | |
278 self.register._logginError(profile, request, _error_t) | |
279 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
280 def render(self, request): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
281 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
282 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
|
283 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
284 _session = request.getSession() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
285 parsed = jsonrpclib.loads(request.content.read()) |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
286 try: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
287 profile = _session.sat_profile |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
288 except AttributeError: |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
289 #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
|
290 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
|
291 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
|
292 self.request = request |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
293 return jsonrpc.JSONRPC.render(self, request) |
0 | 294 |
10 | 295 |
0 | 296 class Libervia(service.Service): |
297 | |
298 def __init__(self): | |
299 root = File("output/") | |
300 self.signal_handler = SignalHandler(self) | |
301 _register = Register(self) | |
302 self.signal_handler.plugRegister(_register) | |
303 self.sessions = {} #key = session value = user | |
304 ## bridge ## | |
305 try: | |
306 self.bridge=DBusBridgeFrontend() | |
307 except BridgeExceptionNoService: | |
308 print(u"Can't connect to SàT backend, are you sure it's launched ?") | |
309 import sys | |
310 sys.exit(1) | |
311 self.bridge.register("connected", self.signal_handler.connected) | |
312 self.bridge.register("connectionError", self.signal_handler.connectionError) | |
19 | 313 for signal_name in ['presenceUpdate', 'personalEvent', 'newMessage']: |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
314 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) |
10 | 315 root.putChild('json_signal_api', self.signal_handler) |
316 root.putChild('json_api', MethodHandler(self)) | |
317 root.putChild('register_api', _register) | |
318 root.putChild('blog', MicroBlog(self)) | |
319 root.putChild('css', File("server_css/")) | |
320 self.site = server.Site(root) | |
0 | 321 |
322 def startService(self): | |
323 reactor.listenTCP(8080, self.site) | |
1 | 324 |
0 | 325 def run(self): |
326 reactor.run() | |
327 | |
328 def stop(self): | |
329 reactor.stop() | |
330 | |
331 | |
332 | |
333 application = service.Application('Libervia') | |
334 service = Libervia() | |
335 service.setServiceParent(application) |