Mercurial > libervia-web
annotate libervia.tac @ 52:4419ef07bb2b
browser side: adding contact, first draft
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 27 May 2011 01:52:29 +0200 |
parents | 72c51a4839cc |
children | f25c4077f6b9 |
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 | |
46 | 22 #You need do adapt the following consts to your server |
23 _REG_EMAIL_FROM = "NOREPLY@libervia.org" | |
24 _REG_EMAIL_SERVER = "localhost" | |
25 _REG_ADMIN_EMAIL = "goffi@goffi.org" | |
26 _NEW_ACCOUNT_SERVER = "localhost" | |
27 _NEW_ACCOUNT_DOMAIN = "tazar.int" | |
28 _NEW_ACCOUNT_RESOURCE = "libervia" | |
29 | |
0 | 30 from twisted.application import internet, service |
31 from twisted.internet import glib2reactor | |
32 glib2reactor.install() | |
33 from twisted.internet import reactor, defer | |
46 | 34 from twisted.mail.smtp import sendmail |
0 | 35 from twisted.web import server |
36 from twisted.web import error as weberror | |
37 from twisted.web.static import File | |
38 from twisted.web.resource import Resource | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
39 from twisted.python.components import registerAdapter |
10 | 40 from twisted.words.protocols.jabber.jid import JID |
0 | 41 from txjsonrpc.web import jsonrpc |
42 from txjsonrpc import jsonrpclib | |
43 from sat_frontends.bridge.DBus import DBusBridgeFrontend,BridgeExceptionNoService | |
46 | 44 from email.mime.text import MIMEText |
45 from logging import debug, info, warning, error | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
46 import re |
36 | 47 import glob |
48 import os.path | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
49 import sys |
10 | 50 from server_side.blog import MicroBlog |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
51 from zope.interface import Interface, Attribute, implements |
10 | 52 |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
53 TIMEOUT = 10 #Session's time out, after that the user will be disconnected |
36 | 54 LIBERVIA_DIR = "output/" |
55 CARDS_DIR = "cards/" | |
0 | 56 |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
57 class ISATSession(Interface): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
58 profile = Attribute("Sat profile") |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
59 jid = Attribute("JID associated with the profile") |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
60 |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
61 class SATSession(object): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
62 implements(ISATSession) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
63 def __init__(self, session): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
64 self.profile = None |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
65 self.jid = None |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
66 |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
67 class LiberviaSession(server.Session): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
68 sessionTimeout = TIMEOUT |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
69 |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
70 def __init__(self, *args, **kwargs): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
71 self.__lock = False |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
72 server.Session.__init__(self, *args, **kwargs) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
73 |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
74 def lock(self): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
75 """Prevent session from expiring""" |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
76 self.__lock = True |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
77 self._expireCall.reset(sys.maxint) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
78 |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
79 def unlock(self): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
80 """Allow session to expire again, and touch it""" |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
81 self.__lock = False |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
82 self.touch() |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
83 |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
84 def touch(self): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
85 if not self.__lock: |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
86 server.Session.touch(self) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
87 |
46 | 88 class SATActionIDHandler(object): |
89 """Manage SàT action id lifecycle""" | |
90 ID_LIFETIME = 30 #after this time (in seconds), id will be suppressed and action result will be ignored | |
91 | |
92 def __init__(self): | |
93 self.waiting_ids = {} | |
94 | |
95 def waitForId(self, id, callback, *args, **kwargs): | |
96 """Wait for an action result | |
97 @param id: id to wait for | |
98 @param callback: method to call when action gave a result back | |
99 @param *args: additional argument to pass to callback | |
100 @param **kwargs: idem""" | |
101 self.waiting_ids[id] = (callback, args, kwargs) | |
102 reactor.callLater(self.ID_LIFETIME, self.purgeID, id) | |
103 | |
104 def purgeID(self, id): | |
105 """Called when an id has not be handled in time""" | |
106 if id in self.waiting_ids: | |
107 warning ("action of id %s has not been managed, id is now ignored" % id) | |
108 del self.waiting_ids[id] | |
109 | |
110 def actionResultCb(self, answer_type, id, data): | |
111 """Manage the actionResult signal""" | |
112 if id in self.waiting_ids: | |
113 callback, args, kwargs = self.waiting_ids[id] | |
114 del self.waiting_ids[id] | |
115 callback(answer_type, id, data, *args, **kwargs) | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
116 |
0 | 117 class MethodHandler(jsonrpc.JSONRPC): |
118 | |
119 def __init__(self, sat_host): | |
120 jsonrpc.JSONRPC.__init__(self) | |
121 self.sat_host=sat_host | |
122 | |
123 def render(self, request): | |
1 | 124 self.session = request.getSession() |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
125 profile = ISATSession(self.session).profile |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
126 if not profile: |
0 | 127 #user is not identified, we return a jsonrpc fault |
128 parsed = jsonrpclib.loads(request.content.read()) | |
129 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
130 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
131 return jsonrpc.JSONRPC.render(self, request) | |
19 | 132 |
133 def jsonrpc_getProfileJid(self): | |
134 """Return the jid of the profile""" | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
135 sat_session = ISATSession(self.session) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
136 profile = sat_session.profile |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
137 sat_session.sat_jid = JID(self.sat_host.bridge.getParamA("JabberID", "Connection", profile_key=profile)) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
138 return sat_session.sat_jid.full() |
0 | 139 |
140 def jsonrpc_getContacts(self): | |
141 """Return all passed args.""" | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
142 profile = ISATSession(self.session).profile |
1 | 143 return self.sat_host.bridge.getContacts(profile) |
20 | 144 |
145 def jsonrpc_setStatus(self, status): | |
146 """Change the status""" | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
147 profile = ISATSession(self.session).profile |
20 | 148 print "new status received:", status |
149 self.sat_host.bridge.setPresence('', '', 0, {'':status}, profile) | |
150 | |
19 | 151 |
152 def jsonrpc_sendMessage(self, to_jid, msg, subject, type): | |
153 """send message""" | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
154 profile = ISATSession(self.session).profile |
19 | 155 return self.sat_host.bridge.sendMessage(to_jid, msg, subject, type, profile) |
0 | 156 |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
157 def jsonrpc_sendMblog(self, raw_text): |
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
158 """Parse raw_text of the microblog box, and send message consequently""" |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
159 profile = ISATSession(self.session).profile |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
160 match = re.match(r'@(.+?): *(.*$)', raw_text) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
161 if match: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
162 recip = match.group(1) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
163 text = match.group(2) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
164 if recip == '@' and text: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
165 #This text if for the public microblog |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
166 print "Sending message to everybody" |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
167 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
|
168 else: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
169 return self.sat_host.bridge.sendGroupBlog([recip], text, profile) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
170 |
20 | 171 def jsonrpc_getPresenceStatus(self): |
172 """Get Presence information for connected contacts""" | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
173 profile = ISATSession(self.session).profile |
20 | 174 return self.sat_host.bridge.getPresenceStatus(profile) |
175 | |
19 | 176 def jsonrpc_getHistory(self, from_jid, to_jid, size): |
177 """Return history for the from_jid/to_jid couple""" | |
178 #FIXME: this method should definitely be asynchrone, need to fix it !!! | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
179 sat_session = ISATSession(self.session) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
180 profile = sat_session.profile |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
181 sat_jid = sat_session.jid |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
182 if not sat_jid: |
19 | 183 error("No jid saved for this profile") |
184 return {} | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
185 if JID(from_jid).userhost() != sat_jid.userhost() and JID(to_jid).userhost() != sat_jid.userhost(): |
19 | 186 error("Trying to get history from a different jid, maybe a hack attempt ?") |
187 return {} | |
24 | 188 return self.sat_host.bridge.getHistory(from_jid, to_jid, size) |
19 | 189 |
50 | 190 def jsonrpc_joinMUC(self, room_jid, nick): |
191 """Join a Multi-User Chat room""" | |
192 profile = ISATSession(self.session).profile | |
193 try: | |
194 room_jid = JID(room_jid) | |
195 except: | |
196 warning('Invalid room jid') | |
197 return | |
198 self.sat_host.bridge.joinMUC(room_jid.host, room_jid.user, nick, profile) | |
199 | |
30
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
200 def jsonrpc_getRoomJoined(self): |
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
201 """Return list of room already joined by user""" |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
202 profile = ISATSession(self.session).profile |
30
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
203 return self.sat_host.bridge.getRoomJoined(profile) |
7684e3ceb12d
server_side: added getRoomJoined method
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
204 |
24 | 205 def jsonrpc_launchTarotGame(self, other_players): |
206 """Create a room, invite the other players and start a Tarot game""" | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
207 profile = ISATSession(self.session).profile |
24 | 208 self.sat_host.bridge.tarotGameLaunch(other_players, profile) |
11
331c093e4eb3
magicBox is now able to send global microblog
Goffi <goffi@goffi.org>
parents:
10
diff
changeset
|
209 |
36 | 210 def jsonrpc_getTarotCardsPaths(self): |
211 """Give the path of all the tarot cards""" | |
212 return map(lambda x: x[len(LIBERVIA_DIR):],glob.glob(os.path.join(LIBERVIA_DIR,CARDS_DIR,'*_*.png'))); | |
213 | |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
214 def jsonrpc_tarotGameReady(self, player, referee): |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
215 """Tell to the server that we are ready to start the game""" |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
216 profile = ISATSession(self.session).profile |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
217 self.sat_host.bridge.tarotGameReady(player, referee) |
36 | 218 |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
219 def jsonrpc_tarotGameContratChoosed(self, player_nick, referee, contrat): |
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
220 """Tell to the server that we are ready to start the game""" |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
221 profile = ISATSession(self.session).profile |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
222 self.sat_host.bridge.tarotGameContratChoosed(player_nick, referee, contrat, profile) |
39
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
223 |
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
224 def jsonrpc_tarotGamePlayCards(self, player_nick, referee, cards): |
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
225 """Tell to the server that we are ready to start the game""" |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
226 profile = ISATSession(self.session).profile |
39
305e81c7a32c
Tarot game: a game can now be finished
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
227 self.sat_host.bridge.tarotGamePlayCards(player_nick, referee, cards, profile) |
36 | 228 |
0 | 229 class Register(jsonrpc.JSONRPC): |
230 """This class manage the registration procedure with SàT | |
231 It provide an api for the browser, check password and setup the web server""" | |
232 | |
233 def __init__(self, sat_host): | |
234 jsonrpc.JSONRPC.__init__(self) | |
235 self.sat_host=sat_host | |
236 self.profiles_waiting={} | |
237 self.request=None | |
238 | |
239 def getWaitingRequest(self, profile): | |
240 """Tell if a profile is trying to log in""" | |
241 if self.profiles_waiting.has_key(profile): | |
242 return self.profiles_waiting[profile] | |
243 else: | |
244 return None | |
245 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
246 def _fillMblogNodes(self, result, session): |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
247 """Fill the microblog nodes association for this session""" |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
248 session.sat_mblog_nodes = dict(result) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
249 |
0 | 250 def render(self, request): |
251 """ | |
252 Render method with some hacks: | |
253 - if login is requested, try to login with form data | |
254 - except login, every method is jsonrpc | |
255 - user doesn't need to be authentified for isRegistered, but must be for all other methods | |
256 """ | |
257 if request.postpath==['login']: | |
258 return self.login(request) | |
259 _session = request.getSession() | |
260 parsed = jsonrpclib.loads(request.content.read()) | |
261 if parsed.get("method")!="isRegistered": | |
262 #if we don't call login or isRegistered, we need to be identified | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
263 profile = ISATSession(_session).profile |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
264 if not profile: |
0 | 265 #user is not identified, we return a jsonrpc fault |
266 fault = jsonrpclib.Fault(0, "Not allowed") #FIXME: define some standard error codes for libervia | |
267 return jsonrpc.JSONRPC._cbRender(self, fault, request, parsed.get('id'), parsed.get('jsonrpc')) | |
268 self.request = request | |
269 return jsonrpc.JSONRPC.render(self, request) | |
270 | |
271 def login(self, request): | |
272 """ | |
273 this method is called with the POST information from the registering form | |
274 it test if the password is ok, and log in if it's the case, | |
275 else it return an error | |
276 @param request: request of the register formulaire, must have "login" and "password" as arguments | |
277 @return: A constant indicating the state: | |
278 - BAD REQUEST: something is wrong in the request (bad arguments, profile_key for login) | |
279 - AUTH ERROR: either the profile or the password is wrong | |
280 - ALREADY WAITING: a request has already be made for this profile | |
281 - server.NOT_DONE_YET: the profile is being processed, the return value will be given by self._logged or self._logginError | |
282 """ | |
46 | 283 if 'new_account' in request.args: |
284 return self._registerNewAccount(request.args) | |
285 | |
0 | 286 try: |
287 _login = request.args['login'][0] | |
288 if _login.startswith('@'): | |
289 raise Exception('No profile_key allowed') | |
290 _pass = request.args['password'][0] | |
291 except KeyError: | |
292 return "BAD REQUEST" | |
293 | |
294 _profile_check = self.sat_host.bridge.getProfileName(_login) | |
295 _profile_pass = self.sat_host.bridge.getParamA("Password", "Connection", profile_key=_login) | |
296 | |
297 if not _profile_check or _profile_check != _login or _profile_pass != _pass: | |
298 return "AUTH ERROR" | |
299 | |
300 if self.profiles_waiting.has_key(_login): | |
301 return "ALREADY WAITING" | |
302 | |
303 if self.sat_host.bridge.isConnected(_login): | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
304 return self._logged(_login, request, finish=False) |
0 | 305 |
306 self.profiles_waiting[_login] = request | |
307 self.sat_host.bridge.connect(_login) | |
308 return server.NOT_DONE_YET | |
309 | |
46 | 310 def _postAccountCreation(self, answer_type, id, data, profile): |
311 """Called when a account has just been created, | |
312 setup stuff has microblog access""" | |
313 def _connected(ignore): | |
314 mblog_d = defer.Deferred() | |
315 self.sat_host.bridge.setMicroblogAccess("open", profile, lambda: mblog_d.callback(None), mblog_d.errback) | |
316 mblog_d.addBoth(lambda ignore: self.sat_host.bridge.disconnect(profile)) | |
317 | |
318 d = defer.Deferred() | |
319 self.sat_host.bridge.asyncConnect(profile, lambda: d.callback(None), d.errback) | |
320 d.addCallback(_connected) | |
321 | |
322 def _registerNewAccount(self, args): | |
323 """Create a new account, or return error | |
324 @param args: dict of args as given by the form | |
325 @return: "REGISTRATION" in case of success""" | |
326 try: | |
327 profile = login = args['login'][0] | |
328 email = args['email'][0] | |
329 except KeyError: | |
330 return "BAD REQUEST" | |
331 if not re.match(r'^[a-z0-9_-]+$', login, re.IGNORECASE) or \ | |
332 not re.match(r'^.+@.+\..+', email, re.IGNORECASE): | |
333 return "BAD REQUEST" | |
334 #_charset = [chr(i) for i in range(0x21,0x7F)] #XXX: this charset seems to have some issues with openfire | |
335 _charset = [chr(i) for i in range(0x30,0x3A) + range(0x41,0x5B) + range (0x61,0x7B)] | |
336 import random | |
337 random.seed() | |
338 password = ''.join([random.choice(_charset) for i in range(15)]) | |
339 | |
340 if login in self.sat_host.bridge.getProfilesList(): #FIXME: must use a deferred + create a new profile check method | |
341 return "ALREADY EXISTS" | |
342 | |
343 #we now create the profile | |
344 self.sat_host.bridge.createProfile(login) | |
345 #FIXME: values must be in a config file instead of hardcoded | |
346 self.sat_host.bridge.setParam("JabberID", "%s@%s/%s" % (login, _NEW_ACCOUNT_DOMAIN, _NEW_ACCOUNT_RESOURCE), "Connection", profile) | |
347 self.sat_host.bridge.setParam("Server", _NEW_ACCOUNT_SERVER, "Connection", profile) | |
348 self.sat_host.bridge.setParam("Password", password, "Connection", profile) | |
349 #and the account | |
350 action_id = self.sat_host.bridge.registerNewAccount(login, password, email, "tazar.int", 5222) | |
351 self.sat_host.action_handler.waitForId(action_id, self._postAccountCreation, profile) | |
352 | |
353 #time to send the email | |
354 | |
355 _email_host = _REG_EMAIL_SERVER | |
356 _email_from = _REG_EMAIL_FROM | |
357 | |
358 def email_ok(ignore): | |
359 print ("Account creation email sent to %s" % email) | |
360 | |
361 def email_ko(ignore): | |
362 #TODO: return error code to user | |
363 error ("Failed to send email to %s" % email) | |
364 | |
365 body = (u"""Welcome to Libervia, a Salut à Toi project part | |
366 | |
367 /!\\ WARNING, THIS IS ONLY A TECHNICAL DEMO, DON'T USE THIS ACCOUNT FOR ANY SERIOUS PURPOSE /!\\ | |
368 | |
369 Here are your connection informations: | |
370 login: %(login)s | |
371 password: %(password)s | |
372 | |
50 | 373 Your Jabber ID (JID) is: %(jid)s |
374 | |
46 | 375 Any feedback welcome |
376 | |
377 Cheers | |
50 | 378 Goffi""" % { 'login': login, 'password': password, 'jid':"%s@%s" % (login, _NEW_ACCOUNT_DOMAIN) }).encode('utf-8') |
46 | 379 msg = MIMEText(body, 'plain', 'UTF-8') |
380 msg['Subject'] = 'Libervia account created' | |
381 msg['From'] = _email_from | |
382 msg['To'] = email | |
383 | |
384 d = sendmail(_email_host, _email_from, email, msg.as_string()) | |
385 d.addCallbacks(email_ok, email_ko) | |
386 | |
387 #email to the administrator | |
388 | |
389 body = (u"""New account created: %(login)s [%(email)s]""" % { 'login': login, 'email': email }).encode('utf-8') | |
390 msg = MIMEText(body, 'plain', 'UTF-8') | |
391 msg['Subject'] = 'Libervia new account created' | |
392 msg['From'] = _email_from | |
393 msg['To'] = _REG_ADMIN_EMAIL | |
394 | |
395 d = sendmail(_email_host, _email_from, email, msg.as_string()) | |
396 d.addCallbacks(email_ok, email_ko) | |
397 return "REGISTRATION" | |
398 | |
0 | 399 def __cleanWaiting(self, login): |
400 """Remove login from waiting queue""" | |
401 try: | |
402 del self.profiles_waiting[login] | |
403 except KeyError: | |
404 pass | |
405 | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
406 def _logged(self, profile, request, finish=True): |
0 | 407 """Set everything when a user just logged |
408 and return "LOGGED" to the requester""" | |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
409 self.__cleanWaiting(profile) |
0 | 410 _session = request.getSession() |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
411 sat_session = ISATSession(_session) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
412 if sat_session.profile: |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
413 error (_('/!\\ Session has already a profile, this should NEVER happen !')) |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
414 sat_session.profile = profile |
24 | 415 self.sat_host.prof_connected.add(profile) |
45
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
416 |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
417 def onExpire(): |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
418 try: |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
419 #We purge the queue |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
420 del self.sat_host.signal_handler.queue[profile] |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
421 except KeyError: |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
422 pass |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
423 #and now we deconnect the profile |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
424 self.sat_host.bridge.disconnect(profile) |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
425 |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
426 _session.notifyOnExpire(onExpire) |
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
427 |
14
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
428 d = defer.Deferred() |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
429 self.sat_host.bridge.getMblogNodes(profile, d.callback, d.errback) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
430 d.addCallback(self._fillMblogNodes, _session) |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
431 if finish: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
432 request.write('LOGGED') |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
433 request.finish() |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
434 else: |
9bf8ed012adc
- Group microblog management, first draft
Goffi <goffi@goffi.org>
parents:
11
diff
changeset
|
435 return "LOGGED" |
0 | 436 |
437 def _logginError(self, login, request, error_type): | |
438 """Something went wrong during loggin, return an error""" | |
439 self.__cleanWaiting(login) | |
440 return error_type | |
441 | |
442 def jsonrpc_isConnected(self): | |
443 _session = self.request.getSession() | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
444 profile = ISATSession(_session).profile |
0 | 445 return self.sat_host.bridge.isConnected(profile) |
446 | |
447 def jsonrpc_connect(self): | |
448 _session = self.request.getSession() | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
449 profile = ISATSession(_session).profile |
0 | 450 if self.profiles_waiting.has_key(profile): |
451 raise jsonrpclib.Fault('1','Already waiting') #FIXME: define some standard error codes for libervia | |
452 self.profiles_waiting[profile] = self.request | |
453 self.sat_host.bridge.connect(profile) | |
454 return server.NOT_DONE_YET | |
455 | |
456 def jsonrpc_isRegistered(self): | |
457 """Tell if the user is already registered""" | |
458 _session = self.request.getSession() | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
459 profile = ISATSession(_session).profile |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
460 return bool(profile) |
0 | 461 |
462 class SignalHandler(jsonrpc.JSONRPC): | |
463 | |
464 def __init__(self, sat_host): | |
465 Resource.__init__(self) | |
466 self.register=None | |
467 self.sat_host=sat_host | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
468 self.signalDeferred = {} |
45
7f106052326f
server side: user is now disconnected on session end, and queue is purged
Goffi <goffi@goffi.org>
parents:
44
diff
changeset
|
469 self.queue = {} |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
470 |
0 | 471 def plugRegister(self, register): |
472 self.register = register | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
473 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
474 def jsonrpc_getSignals(self): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
475 """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
|
476 @return: (signal, *signal_args)""" |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
477 _session = self.request.getSession() |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
478 profile = ISATSession(_session).profile |
24 | 479 if profile in self.queue: #if we have signals to send in queue |
480 if self.queue[profile]: | |
481 return self.queue[profile].pop(0) | |
482 else: | |
483 #the queue is empty, we delete the profile from queue | |
484 del self.queue[profile] | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
485 _session.lock() #we don't want the session to expire as long as this connection is active |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
486 def unlock(ignore): |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
487 _session.unlock() |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
488 self.signalDeferred[profile] = defer.Deferred() |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
489 self.request.notifyFinish().addBoth(unlock) |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
490 return self.signalDeferred[profile] |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
491 |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
492 def getGenericCb(self, function_name): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
493 """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
|
494 function must have profile as last argument""" |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
495 def genericCb(*args): |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
496 profile = args[-1] |
24 | 497 if not profile in self.sat_host.prof_connected: |
498 return | |
3
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
499 if profile in self.signalDeferred: |
154d4caa57f4
server side: proper profile management in signals generic callback
Goffi <goffi@goffi.org>
parents:
2
diff
changeset
|
500 self.signalDeferred[profile].callback((function_name,args[:-1])) |
24 | 501 del self.signalDeferred[profile] |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
502 else: |
24 | 503 if not self.queue.has_key(profile): |
504 self.queue[profile] = [] | |
505 self.queue[profile].append((function_name, args[:-1])) | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
506 return genericCb |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
507 |
0 | 508 def connected(self, profile): |
509 assert(self.register) #register must be plugged | |
510 request = self.register.getWaitingRequest(profile) | |
511 if request: | |
512 self.register._logged(profile, request) | |
513 | |
514 def connectionError(self, error_type, profile): | |
515 assert(self.register) #register must be plugged | |
516 request = self.register.getWaitingRequest(profile) | |
517 if request: #The user is trying to log in | |
518 if error_type == "AUTH_ERROR": | |
519 _error_t = "AUTH ERROR" | |
520 else: | |
521 _error_t = "UNKNOWN" | |
522 self.register._logginError(profile, request, _error_t) | |
523 | |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
524 def render(self, request): |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
525 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
526 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
|
527 """ |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
528 _session = request.getSession() |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
529 parsed = jsonrpclib.loads(request.content.read()) |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
530 profile = ISATSession(_session).profile |
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
531 if not profile: |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
532 #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
|
533 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
|
534 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
|
535 self.request = request |
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
536 return jsonrpc.JSONRPC.render(self, request) |
0 | 537 |
10 | 538 |
0 | 539 class Libervia(service.Service): |
540 | |
541 def __init__(self): | |
36 | 542 root = File(LIBERVIA_DIR) |
0 | 543 self.signal_handler = SignalHandler(self) |
544 _register = Register(self) | |
545 self.signal_handler.plugRegister(_register) | |
546 self.sessions = {} #key = session value = user | |
24 | 547 self.prof_connected = set() #Profiles connected |
46 | 548 self.action_handler = SATActionIDHandler() |
0 | 549 ## bridge ## |
550 try: | |
551 self.bridge=DBusBridgeFrontend() | |
552 except BridgeExceptionNoService: | |
553 print(u"Can't connect to SàT backend, are you sure it's launched ?") | |
554 sys.exit(1) | |
555 self.bridge.register("connected", self.signal_handler.connected) | |
556 self.bridge.register("connectionError", self.signal_handler.connectionError) | |
46 | 557 self.bridge.register("actionResult", self.action_handler.actionResultCb, "request") |
37
b306aa090438
Tarot game: game launching (first hand showed), and contract selection
Goffi <goffi@goffi.org>
parents:
36
diff
changeset
|
558 for signal_name in ['presenceUpdate', 'personalEvent', 'newMessage', 'roomJoined', 'roomUserJoined', 'roomUserLeft', 'tarotGameStarted', 'tarotGameNew', |
41
7782a786b2f0
Tarot game: score is now shown (need to use XMLUI later)
Goffi <goffi@goffi.org>
parents:
39
diff
changeset
|
559 'tarotGameChooseContrat', 'tarotGameShowCards', 'tarotGameInvalidCards', 'tarotGameCardsPlayed', 'tarotGameYourTurn', 'tarotGameScore']: |
2
669c531a857e
signals handling and first draft of microblogging
Goffi <goffi@goffi.org>
parents:
1
diff
changeset
|
560 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) |
10 | 561 root.putChild('json_signal_api', self.signal_handler) |
562 root.putChild('json_api', MethodHandler(self)) | |
563 root.putChild('register_api', _register) | |
564 root.putChild('blog', MicroBlog(self)) | |
565 root.putChild('css', File("server_css/")) | |
566 self.site = server.Site(root) | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
567 self.site.sessionFactory = LiberviaSession |
0 | 568 |
569 def startService(self): | |
570 reactor.listenTCP(8080, self.site) | |
1 | 571 |
0 | 572 def run(self): |
573 reactor.run() | |
574 | |
575 def stop(self): | |
576 reactor.stop() | |
577 | |
578 | |
44
2744dd31e8a5
server side: Session management refactoring
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
579 registerAdapter(SATSession, server.Session, ISATSession) |
0 | 580 application = service.Application('Libervia') |
581 service = Libervia() | |
582 service.setServiceParent(application) |