annotate libervia.tac @ 14:9bf8ed012adc

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