ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/login.ext
(Generate patch)

Comparing deliantra/server/ext/login.ext (file contents):
Revision 1.118 by root, Thu Nov 8 00:16:07 2012 UTC vs.
Revision 1.123 by root, Fri Nov 16 12:02:46 2012 UTC

2 2
3# login handling 3# login handling
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7use Deliantra::Util ();
7 8
8CONF MAX_DISCONNECT_TIME = 3600; 9CONF MAX_DISCONNECT_TIME = 3600;
9
10# paranoia function to overwrite a string-in-place
11sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13}
14 10
15sub query { 11sub query {
16 my ($ns, $flags, $text) = @_; 12 my ($ns, $flags, $text) = @_;
17 13
18 $ns->query ($flags, $text, Coro::rouse_cb); 14 $ns->query ($flags, $text, Coro::rouse_cb);
62 my $y = $ob->y; 58 my $y = $ob->y;
63 59
64 # never happens normally, but helps when shell users make mistakes 60 # never happens normally, but helps when shell users make mistakes
65 $m->linkable 61 $m->linkable
66 or return 1; 62 or return 1;
67
68# return 0;#d#
69# warn join ":", $m->at ($x, $y);#d#
70# warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n";
71# return 0;
72 63
73 scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y) 64 scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y)
74} 65}
75 66
76sub enter_map { 67sub enter_map {
131 122
132 $ob->goto ($map, $x, $y); 123 $ob->goto ($map, $x, $y);
133} 124}
134 125
135sub encode_password($) { 126sub encode_password($) {
136# crypt $_[0],
137# join '',
138# ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)]
139 "!" . unpack "H*", $_[0] 127 "!" . unpack "H*", $_[0]
140} 128}
141 129
142sub compare_password($$) { 130sub compare_password($$) {
143 my ($pass, $token) = @_; 131 my ($pass, $token) = @_;
160 my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; 148 my $temp = "$PLAYERDIR/~$Coro::current~deleting~";
161 aio_rename "$PLAYERDIR/$user", $temp; 149 aio_rename "$PLAYERDIR/$user", $temp;
162 IO::AIO::aio_rmtree $temp; 150 IO::AIO::aio_rmtree $temp;
163} 151}
164 152
153sub login_done {
154 my ($pl) = @_;
155
156 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) {
157 $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR);
158 }
159}
160
161sub chargen {
162 my ($ns, $user, $pass) = @_;
163
164 # the rest of this function is character creation
165 $Coro::current->{desc} = "addme($user) chargen";
166
167 # just to make sure nothing is left over
168 # normally, nothing is there.
169 nuke_playerdir $user;
170
171 my $pl = cf::player::new $user;
172 $pl->password (encode_password $pass);
173 $pl->connect ($ns);
174 my $ob = $pl->ob;
175
176 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
177
178 while () {
179 $ob->update_stats;
180 $pl->save_stats;
181
182 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
183 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
184
185 if ($res =~ /^[Nn]/) {
186 last;
187 } elsif ($res > 0 && $res <= 7) {
188 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
189
190 if ($swap > 0 && $swap <= 7) {
191 $ob->swap_stats ($res - 1, $swap - 1);
192 }
193 } else {
194 $ob->roll_stats;
195 }
196
197 Coro::Timer::sleep 0.05;
198 }
199
200 $ob->set_animation (2);
201 $ob->add_statbonus;
202
203 while () {
204 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
205 my $msg = $ob->msg;
206 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
207 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
208
209 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
210 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
211
212 last if $res =~ /[dD]/;
213
214 $pl->chargen_race_next;
215 Coro::Timer::sleep 0.05;
216 }
217
218 # create the playerdir, if necessary, as chargen_race_done did it before
219 # presumably because of unique maps
220 aio_mkdir playerdir $pl, 0770;
221 $pl->chargen_race_done;
222
223 while () {
224 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
225 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
226
227 if ($res =~ /^[fF]/) {
228 $pl->gender (1);
229 last;
230 } elsif ($res =~ /^[mM]/) {
231 $pl->gender (0);
232 last;
233 }
234 Coro::Timer::sleep 0.05;
235 }
236
237 $ob->reply (undef, "Welcome to Deliantra!");
238
239 # XXX: Workaround for delayed client ext protocol handshake
240 $pl->esrv_new_player;
241
242 delete $pl->{deny_save};
243}
244
165cf::client->attach (on_addme => sub { 245cf::client->attach (on_addme => sub {
166 my ($ns) = @_; 246 my ($ns) = @_;
167 247
168 $ns->pl and return $ns->destroy; 248 $ns->{addme}++ and return $ns->destroy;
169 249
170 $ns->async (sub { 250 $ns->async (sub {
171 $Coro::current->{desc} = "addme init"; 251 $Coro::current->{desc} = "addme init";
172 252
173 my ($user, $pass); 253 my ($user, $pass);
248 aio_stat $pl->path and next; 328 aio_stat $pl->path and next;
249 my $mtime = (stat _)[9]; 329 my $mtime = (stat _)[9];
250 my $token = $pl->password; 330 my $token = $pl->password;
251 331
252 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) { 332 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) {
253 $pl->password (encode_password $pass); # make sure we store the new encoding #d# 333 # player exists and passwords match - we can proceed
254 nuke_str $pass; 334
255 # password matches, wonderful 335 # password matches, wonderful
256 my $pl = cf::player::find $user or next; 336 my $pl = cf::player::find $user or next;
257 $pl->connect ($ns); 337 $pl->connect ($ns);
258 enter_map $pl; 338 enter_map $pl;
339 login_done $pl;
259 last; 340 return;
260 } elsif (can_cleanup $pl, $mtime) { 341 } elsif (can_cleanup $pl, $mtime) {
261 Coro::Timer::sleep 1; 342 Coro::Timer::sleep 1;
262 343
263 $ns->send_drawinfo ( 344 $ns->send_drawinfo (
264 "Player exists, but password does not match. If this is your account, " 345 "Player exists, but password does not match. If this is your account, "
265 . "please try again. If not, you can now decide to take over this account " 346 . "please try again. If not, you can now decide to take over this account "
266 . "because it has not been in-use for some time.", 347 . "because it has not been in-use for some time.",
267 cf::NDI_RED 348 cf::NDI_RED
268 ); 349 );
269 350
270 #TODO: nuke_str
271 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 351 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
272 or next; 352 or next;
273 353
274 # check if the file hasn't changed 354 # check if the file hasn't changed
275 aio_stat cf::player::path $user and next; 355 aio_stat cf::player::path $user and next;
277 357
278 $pl->quit_character; 358 $pl->quit_character;
279 359
280 # fall through to creation 360 # fall through to creation
281 } else { 361 } else {
282 nuke_str $pass;
283
284 Coro::Timer::sleep 1; 362 Coro::Timer::sleep 1;
285 363
286 $ns->send_drawinfo ( 364 $ns->send_drawinfo (
287 "Wrong username or password. Please try again " 365 "Wrong username or password. Please try again "
288 . "(check for Numlock and other semi-obvious error sources).", 366 . "(check for Numlock and other semi-obvious error sources).",
303 ); 381 );
304 next; 382 next;
305 } 383 }
306 } 384 }
307 385
308 # the rest of this function is character creation
309 $Coro::current->{desc} = "addme($user) chargen";
310
311 # just to make sure nothing is left over
312 # normally, nothing is there.
313 nuke_playerdir $user;
314
315 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; 386 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
316 387
317 if ($pass2 ne $pass) { 388 if ($pass2 ne $pass) {
318 nuke_str $pass;
319 nuke_str $pass2;
320 $ns->send_drawinfo ( 389 $ns->send_drawinfo (
321 "The passwords do not match, please try again.", 390 "The passwords do not match, please try again.",
322 cf::NDI_RED 391 cf::NDI_RED
323 ); 392 );
324 Coro::Timer::sleep 0.5; 393 Coro::Timer::sleep 0.5;
325 next; 394 next;
326 } 395 }
327 396
328 nuke_str $pass2;
329
330 my $pl = cf::player::new $user;
331 $pl->password (encode_password $pass);
332 nuke_str $pass;
333 $pl->connect ($ns);
334 my $ob = $pl->ob;
335
336 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
337
338 while () {
339 $ob->update_stats;
340 $pl->save_stats;
341
342 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
343 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
344
345 if ($res =~ /^[Nn]/) {
346 last;
347 } elsif ($res > 0 && $res <= 7) {
348 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
349
350 if ($swap > 0 && $swap <= 7) {
351 $ob->swap_stats ($res - 1, $swap - 1);
352 }
353 } else {
354 $ob->roll_stats;
355 }
356
357 Coro::Timer::sleep 0.05;
358 }
359
360 $ob->set_animation (2);
361 $ob->add_statbonus;
362
363 while () {
364 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
365 my $msg = $ob->msg;
366 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
367 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
368
369 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
370 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
371
372 last if $res =~ /[dD]/;
373
374 $pl->chargen_race_next;
375 Coro::Timer::sleep 0.05;
376 }
377
378 # create the playerdir, if necessary, as chargen_race_done did it before
379 # presumably because of unique maps
380 aio_mkdir playerdir $pl, 0770;
381 $pl->chargen_race_done;
382
383 while () {
384 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
385 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
386
387 if ($res =~ /^[fF]/) {
388 $pl->gender (1);
389 last;
390 } elsif ($res =~ /^[mM]/) {
391 $pl->gender (0);
392 last;
393 }
394 Coro::Timer::sleep 0.05;
395 }
396
397 $ob->reply (undef, "Welcome to Deliantra!");
398
399 # XXX: Workaround for delayed client ext protocol handshake
400 $pl->esrv_new_player;
401
402 delete $pl->{deny_save};
403
404 last; 397 last;
405 } 398 }
406 399
407 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { 400 # lock again, too layz to make this nicer
408 $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); 401 local $cf::LOGIN_LOCK{$user} = 1;
409 } 402
403 chargen $ns, $user, $pass;
404 login_done $ns->pl;
410 }); 405 });
411}); 406});
407
408cf::client->attach (
409 on_version => sub {
410 my ($ns, $arg) = @_;
411
412 srand 1;#d#
413 $ns->{nonces} = [map { join "", map { chr rand 256 } 0..63 } 1..2];
414 $ns->ext_msg (nonces => @{ $ns->{nonces} });
415 },
416);
417
418#cf::register_async_exticmd create_login => sub {
419# my ($ns, $reply, $user, $pass) = @_;
420#
421# $ns->{addme}++ and return $ns->destroy;
422#};
423
424cf::register_async_exticmd login => sub {
425 my ($ns, $reply, $user, $hash) = @_;
426
427 $ns->{addme}++ and return $ns->destroy;
428
429 $ns->async (sub {
430 local $cf::LOGIN_LOCK{$user} = 1;
431
432 $Coro::current->{desc} = "login($user) check";
433
434 my $fail = sub {
435 $reply->(0, $_[0]);
436 $ns->flush; # does not ensure that the data reaches the client - TODO
437 # need to do this in another thread, as this one gets canceled
438 Coro::async_pool {
439 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack
440 $ns->destroy;
441 };
442 Coro::schedule; # do the destroy, should not return
443 };
444
445 # try to read the user file and check the password
446 my $pl = cf::player::find $user
447 or return $fail->("User '$user' does not exist - wrong spelling?");
448
449 aio_stat $pl->path
450 and return $ns->destroy;
451
452 my $mtime = (stat _)[9];
453 my $token = $pl->password;
454
455 $token = $token =~ /^!/
456 ? Deliantra::Util::hash_pw pack "H*", substr $token, 1
457 : pack "H*", $token;
458
459 $token = Deliantra::Util::auth_pw $token, $ns->{nonces}[0], $ns->{nonces}[1];
460
461 $token eq $hash
462 or $cf::CFG{ext_login_nocheck}
463 or return $fail->("User exists, but the password doesn't match - check your spelling, NumLock/CapsLock etc.");
464
465 # player exists and passwords match - we can proceed
466
467 $pl->connect ($ns);
468 enter_map $pl;
469 login_done $pl;
470
471 $reply->(1, "Success");
472 });
473};
412 474
413cf::register_command password => sub { 475cf::register_command password => sub {
414 my ($pl, $arg) = @_; 476 my ($pl, $arg) = @_;
415 477
416 unless ($pl->flag (cf::FLAG_WIZ)) { 478 unless ($pl->flag (cf::FLAG_WIZ)) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines