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.125 by root, Sat Nov 17 10:40:22 2012 UTC vs.
Revision 1.131 by root, Mon Nov 19 01:13:49 2012 UTC

7use Deliantra::Util (); 7use Deliantra::Util ();
8 8
9CONF MAX_DISCONNECT_TIME = 3600; 9CONF MAX_DISCONNECT_TIME = 3600;
10 10
11our $VALID_LOGIN = qr<^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z>; 11our $VALID_LOGIN = qr<^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z>;
12our %LOGIN_LOCK;
13
14# utility function to send messages to the client before
15# we have a player object to format them for. does not
16# escape anything.
17sub send_log ($$$) {
18 $_[0]->send_packet ("msg $_[2] log $_[1]");
19}
12 20
13sub query { 21sub query {
14 my ($ns, $flags, $text) = @_; 22 my ($ns, $flags, $text) = @_;
15 23
16 $ns->query ($flags, $text, Coro::rouse_cb); 24 $ns->query ($flags, $text, Coro::rouse_cb);
27 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 35 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9
28 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 36 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20
29 || $age > 700 * 86400 # 2 years for everybody else 37 || $age > 700 * 86400 # 2 years for everybody else
30} 38}
31 39
32sub check_playing { 40# return a guard object for a lock on the given username, if available
41sub login_guard {
33 my ($ns, $user) = @_; 42 my ($user) = @_;
34 43
44 exists $LOGIN_LOCK{$user}
45 and return undef;
46
35 return unless cf::player::find_active $user; 47 cf::player::find_active $user
48 and return undef;
36 49
37 $ns->send_drawinfo ( 50 undef $LOGIN_LOCK{$user};
38 "That player is already logged in on this server. " 51 Guard::guard { delete $LOGIN_LOCK{$user} }
39 . "If you want to create a new player, choose another name. "
40 . "If you have already a registered, make sure nobody "
41 . "else is using your account at this time. If you lost your connection "
42 . "then the server will likely timeout within a minute. If you still "
43 . "cannot log-in after a minute, you are still logged in. Make sure "
44 . "you do not have another client running. If you use windows, reboot, "
45 . "this will fix anything.",
46 cf::NDI_RED
47 );
48
49 1
50} 52}
51 53
52sub safe_spot($) { 54sub safe_spot($) {
53 my ($pl) = @_; 55 my ($pl) = @_;
54 56
92 $ob->message ( 94 $ob->message (
93 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " 95 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. "
94 . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " 96 . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. "
95 . "Maybe you can find comfort in the thought that your body was quite satisfying in taste... " 97 . "Maybe you can find comfort in the thought that your body was quite satisfying in taste... "
96 . "H<You disconnected too long without having used a savebed.>", 98 . "H<You disconnected too long without having used a savebed.>",
97 cf::NDI_RED 99 cf::NDI_RED | cf::NDI_REPLY
98 ); 100 );
99 # kill them. 101 # kill them.
100 # reminds me of the famous badness 10000 syndrome... 102 # reminds me of the famous badness 10000 syndrome...
101 $ob->stats->hp (-10000); #] if they survive this they deserved to live 103 $ob->stats->hp (-10000); #] if they survive this they deserved to live
102 my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy; 104 my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy;
106 $ob->message ( 108 $ob->message (
107 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " 109 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. "
108 . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. " 110 . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. "
109 . "Better use a savebed next time, much worse things could have happened... " 111 . "Better use a savebed next time, much worse things could have happened... "
110 . "H<You disconnected without having used a savebed. When you do that for too long, you might die.>", 112 . "H<You disconnected without having used a savebed. When you do that for too long, you might die.>",
111 cf::NDI_RED 113 cf::NDI_RED | cf::NDI_REPLY
112 ); 114 );
113 } 115 }
114 } else { 116 } else {
115 $ob->message ( 117 $ob->message (
116 "You didn't use a bed to reality to leave this realm. This is very dangerous, " 118 "You didn't use a bed to reality to leave this realm. This is very dangerous, "
117 . "as lots of things could happen when you leave by other means, such as cave-ins, " 119 . "as lots of things could happen when you leave by other means, such as cave-ins, "
118 . "or monsters suddenly snapping your body. Better use a savebed next time. " 120 . "or monsters suddenly snapping your body. Better use a savebed next time. "
119 . "H<Always apply a bed of reality to disconnect from the server.>", 121 . "H<Always apply a bed of reality to disconnect from the server.>",
120 cf::NDI_RED 122 cf::NDI_RED | cf::NDI_REPLY
121 ); 123 );
122 } 124 }
123 } 125 }
124 126
125 $ob->goto ($map, $x, $y); 127 $ob->goto ($map, $x, $y);
126} 128}
127 129
128sub encode_password($) { 130sub encode_password($) {
129 "!" . unpack "H*", $_[0] 131 unpack "H*", Deliantra::Util::hash_pw $_[0]
130} 132}
131 133
132sub compare_password($$) { 134sub compare_password($$) {
133 my ($pass, $token) = @_; 135 my ($pass, $token) = @_;
134 136
159 # the rest of this function is character creation 161 # the rest of this function is character creation
160 162
161 my $ns = $pl->ns; 163 my $ns = $pl->ns;
162 my $ob = $pl->ob; 164 my $ob = $pl->ob;
163 165
164 $Coro::current->{desc} = "addme(" . $ob->name . ") login";
165
166 delete $pl->{deny_save}; # set by new
167
168 if ($pl->{chargen} eq "init") { 166 if ($pl->{chargen} eq "init") {
167 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
168
169 # create the playerdir, if necessary, as chargen_race_done did it before 169 # create the playerdir, if necessary, as chargen_race_done did it before
170 # presumably because of unique maps 170 # presumably because of unique maps
171 aio_mkdir playerdir $pl, 0770; 171 aio_mkdir playerdir $pl, 0770;
172 delete $pl->{deny_save}; # set by new
172 $pl->save; 173 $pl->save;
173
174 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
175 174
176 $pl->{chargen} = "stats"; 175 $pl->{chargen} = "stats";
177 } 176 }
178 177
179 if ($pl->{chargen} eq "stats") { 178 if ($pl->{chargen} eq "stats") {
257 $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); 256 $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR);
258 } 257 }
259} 258}
260 259
261sub chargen { 260sub chargen {
262 my ($ns, $user, $hasah) = @_; 261 my ($ns, $user, $hash) = @_;
263
264 # lock again, too lazy to make this nicer
265 local $cf::LOGIN_LOCK{$user} = 1;
266 262
267 # just to make sure nothing is left over 263 # just to make sure nothing is left over
268 # normally, nothing is there. 264 # normally, nothing is there.
269 nuke_playerdir $user; 265 nuke_playerdir $user;
270 266
271 my $pl = cf::player::new $user; 267 my $pl = cf::player::new $user;
272 $pl->password (encode_password $pass); 268 $pl->password (unpack "H*", $hash);
273 $pl->connect ($ns); 269 $pl->connect ($ns);
274 270
275 $pl->{chargen} = "init"; 271 $pl->{chargen} = "init";
276 272
277 login $pl; 273 login $pl;
288 my ($user, $pass); 284 my ($user, $pass);
289 285
290 $ns->send_packet ("addme_success"); 286 $ns->send_packet ("addme_success");
291 287
292 for (;;) { 288 for (;;) {
293 $ns->send_drawinfo ( 289 delete $ns->{login_guard};
290
291 send_log $ns,
294 "Please enter your username now. If you are a new user, " 292 "Please enter your username now. If you are a new user, "
295 . "make one up that describes your character best. " 293 . "make one up that describes your character best. "
296 . "Only letters and digits are allowed, though.", 294 . "Only letters and digits are allowed, though.",
297 cf::NDI_BLUE 295 cf::NDI_BLUE | cf::NDI_REPLY
298 ); 296 ;
299 297
300 # read username 298 # read username
301 while () { 299 while () {
302 $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; 300 $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:";
303 301
304 if ($cf::LOGIN_LOCK{$user}) {
305 $ns->send_drawinfo (
306 "That username is currently used in another login session. "
307 . "Chose another, or wait till the other session has ended.",
308 cf::NDI_RED
309 );
310 } elsif ($user =~ $VALID_LOGIN) { 302 if ($user =~ $VALID_LOGIN) {
311 last; 303 last;
312 } else { 304 } else {
313 $ns->send_drawinfo ( 305 send_log $ns,
314 "Your username contains illegal characters " 306 "Your username contains illegal characters "
315 . "(only a-z, A-Z and 0-9 are allowed), " 307 . "(only a-z, A-Z and 0-9 are allowed), "
316 . "or is not between 3 and 20 characters in length.", 308 . "or is not between 3 and 20 characters in length.",
317 cf::NDI_RED 309 cf::NDI_RED | cf::NDI_REPLY
318 ); 310 ;
319 } 311 }
320 Coro::Timer::sleep 0.4; 312 Coro::Timer::sleep 0.4;
321 } 313 }
322 314
323 check_playing $ns, $user and next;
324
325 $Coro::current->{desc} = "addme($user) pass"; 315 $Coro::current->{desc} = "addme($user)";
326 316
327 $ns->send_drawinfo ( 317 send_log $ns,
328 "Welcome $user, please enter your password now. " 318 "Welcome $user, please enter your password now. "
329 . "New users should now choose a password. " 319 . "New users should now choose a password. "
330 . "Anything your client lets you enter is fine.", 320 . "Anything your client lets you enter is fine.",
331 cf::NDI_BLUE 321 cf::NDI_BLUE | cf::NDI_REPLY
332 ); 322 ;
333 323
334 # read password 324 # read password
335 while () { 325 while () {
336 $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; 326 $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:";
337 last if $pass =~ /.../; 327 last if $pass =~ /.../;
338 $ns->send_drawinfo ( 328 send_log $ns,
339 "Try to use at least three characters as your password please, " 329 "Try to use at least three characters as your password please, "
340 . "that cannot be too much to ask for :)", 330 . "that cannot be too much to ask for :)",
341 cf::NDI_RED 331 cf::NDI_RED | cf::NDI_REPLY
342 ); 332 ;
343 Coro::Timer::sleep 0.4; 333 Coro::Timer::sleep 0.4;
344 } 334 }
345 335
346 # lock this username for the remainder of this login session 336 $ns->{login_guard} = login_guard $user
347 if ($cf::LOGIN_LOCK{$user}) { 337 or do {
348 $ns->send_drawinfo ( 338 send_log $ns,
349 "That username is currently used in another login session. " 339 "That user is already logged in (or is logging in)."
350 . "Chose another, or wait till the other session has ended.", 340 . "Chose another, or wait till the other session has ended.",
351 cf::NDI_RED 341 cf::NDI_RED | cf::NDI_REPLY
342 ;
343 next;
352 ); 344 };
353 next;
354 }
355 local $cf::LOGIN_LOCK{$user} = 1;
356
357 check_playing $ns, $user and next;
358
359 $Coro::current->{desc} = "addme($user) check";
360 345
361 # try to read the user file and check the password 346 # try to read the user file and check the password
362 if (my $pl = cf::player::find $user) { 347 if (my $pl = cf::player::find $user) {
363 aio_stat $pl->path and next; 348 aio_stat $pl->path and next;
364 my $mtime = (stat _)[9]; 349 my $mtime = (stat _)[9];
374 login $pl; 359 login $pl;
375 return; 360 return;
376 } elsif (can_cleanup $pl, $mtime) { 361 } elsif (can_cleanup $pl, $mtime) {
377 Coro::Timer::sleep 1; 362 Coro::Timer::sleep 1;
378 363
379 $ns->send_drawinfo ( 364 send_log $ns,
380 "Player exists, but password does not match. If this is your account, " 365 "Player exists, but password does not match. If this is your account, "
381 . "please try again. If not, you can now decide to take over this account " 366 . "please try again. If not, you can now decide to take over this account "
382 . "because it has not been in-use for some time.", 367 . "because it has not been in-use for some time.",
383 cf::NDI_RED 368 cf::NDI_RED | cf::NDI_REPLY
384 ); 369 ;
385 370
386 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 371 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
387 or next; 372 or next;
388 373
389 # check if the file hasn't changed 374 # check if the file hasn't changed
394 379
395 # fall through to creation 380 # fall through to creation
396 } else { 381 } else {
397 Coro::Timer::sleep 1; 382 Coro::Timer::sleep 1;
398 383
399 $ns->send_drawinfo ( 384 send_log $ns,
400 "Wrong username or password. Please try again " 385 "Wrong username or password. Please try again "
401 . "(check for Numlock and other semi-obvious error sources).", 386 . "(check for Numlock and other semi-obvious error sources).",
402 cf::NDI_RED 387 cf::NDI_RED | cf::NDI_REPLY
403 ); 388 ;
404 next; 389 next;
405 } 390 }
406 } else { 391 } else {
407 # unable to load the playerfile: 392 # unable to load the playerfile:
408 # check whether the player dir exists, which means the file is corrupted or 393 # check whether the player dir exists, which means the file is corrupted or
409 # something very similar. 394 # something very similar.
410 if (!aio_stat cf::player::playerdir $user) { 395 if (!aio_stat cf::player::playerdir $user) {
411 $ns->send_drawinfo ( 396 send_log $ns,
412 "Unable to retrieve this player. It might be a locked or broken account. " 397 "Unable to retrieve this player. It might be a locked or broken account. "
413 . "If this is your account, ask a dungeon master for assistance. " 398 . "If this is your account, ask a dungeon master for assistance. "
414 . "Otherwise choose a different login name.", 399 . "Otherwise choose a different login name.",
415 cf::NDI_RED 400 cf::NDI_RED | cf::NDI_REPLY
416 ); 401 ;
417 next; 402 next;
418 } 403 }
419 } 404 }
420 405
421 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; 406 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
422 407
423 if ($pass2 ne $pass) { 408 if ($pass2 ne $pass) {
424 $ns->send_drawinfo ( 409 send_log $ns,
425 "The passwords do not match, please try again.", 410 "The passwords do not match, please try again.",
426 cf::NDI_RED 411 cf::NDI_RED | cf::NDI_REPLY
427 ); 412 ;
428 Coro::Timer::sleep 0.5; 413 Coro::Timer::sleep 0.5;
429 next; 414 next;
430 } 415 }
431 416
432 last; 417 last;
457 $reply->(0, $_[0]); 442 $reply->(0, $_[0]);
458 $ns->flush; # does not ensure that the data reaches the client - TODO 443 $ns->flush; # does not ensure that the data reaches the client - TODO
459 # need to do this in another thread, as this one gets canceled 444 # need to do this in another thread, as this one gets canceled
460 Coro::async_pool { 445 Coro::async_pool {
461 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack 446 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack
462 $ns->destroy; 447 $ns->destroy if $ns->valid;
463 }; 448 };
464 Coro::schedule; # do the destroy, should not return 449 Coro::schedule; # do the destroy, should not return
465 }; 450 };
466 451
467 $user =~ $VALID_LOGIN 452 $user =~ $VALID_LOGIN
468 or return $fail ( 453 or return $fail (
469 "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), " 454 "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), "
470 . "or is not between 3 and 20 characters in length." 455 . "or is not between 3 and 20 characters in length."
471 ); 456 );
472 457
458 $ns->{login_guard} = login_guard $user
459 or return $fail->("User name '$user' is in use - try another login name.");
460
473 cf::player::find $user 461 cf::player::find $user
474 and return $fail->("User '$user' already exists - choose another login name."); 462 and return $fail->("User name '$user' is already registered - choose another login name.");
475 463
476 chargen $ns, $user, Deliantra::Util::hash_pw $pass; 464 $reply->(1, "Account Created");
465
466 chargen $ns, $user, $pass;
477 }); 467 });
478}; 468};
479 469
480cf::register_async_exticmd login => sub { 470cf::register_async_exticmd login => sub {
481 my ($ns, $reply, $user, $hash) = @_; 471 my ($ns, $reply, $user, $hash) = @_;
482 472
483 $ns->{addme}++ and return $ns->destroy; 473 $ns->{addme}++ and return $ns->destroy;
484 474
485 $ns->async (sub { 475 $ns->async (sub {
486 local $cf::LOGIN_LOCK{$user} = 1;
487
488 $Coro::current->{desc} = "login($user) check"; 476 $Coro::current->{desc} = "login($user)";
489 477
490 my $fail = sub { 478 my $fail = sub {
491 $reply->(0, $_[0]); 479 $reply->(0, $_[0]);
492 $ns->flush; # does not ensure that the data reaches the client - TODO 480 $ns->flush; # does not ensure that the data reaches the client - TODO
493 # need to do this in another thread, as this one gets canceled 481 # need to do this in another thread, as this one gets canceled
494 Coro::async_pool { 482 Coro::async_pool {
495 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack 483 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack
496 $ns->destroy; 484 $ns->destroy if $ns->valid;
497 }; 485 };
498 Coro::schedule; # do the destroy, should not return 486 Coro::schedule; # do the destroy, should not return
499 }; 487 };
488
489 $ns->{login_guard} = login_guard $user
490 or return $fail->("User '$user' is currently playing or logging in in another session. If that is your "
491 . "user name, make sure you are not running two clients. When in doubt, reboot.");
500 492
501 # try to read the user file and check the password 493 # try to read the user file and check the password
502 my $pl = cf::player::find $user 494 my $pl = cf::player::find $user
503 or return $fail->("User '$user' does not exist - wrong spelling?"); 495 or return $fail->("User '$user' does not exist - wrong spelling?");
504 496

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines