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.127 by root, Sat Nov 17 12:00:31 2012 UTC vs.
Revision 1.130 by root, Sun Nov 18 10:00:55 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;
12 13
13sub query { 14sub query {
14 my ($ns, $flags, $text) = @_; 15 my ($ns, $flags, $text) = @_;
15 16
16 $ns->query ($flags, $text, Coro::rouse_cb); 17 $ns->query ($flags, $text, Coro::rouse_cb);
27 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 28 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9
28 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 29 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20
29 || $age > 700 * 86400 # 2 years for everybody else 30 || $age > 700 * 86400 # 2 years for everybody else
30} 31}
31 32
32sub check_playing { 33# return a guard object for a lock on the given username, if available
34sub login_guard {
33 my ($ns, $user) = @_; 35 my ($user) = @_;
34 36
37 exists $LOGIN_LOCK{$user}
38 and return undef;
39
35 return unless cf::player::find_active $user; 40 cf::player::find_active $user
41 and return undef;
36 42
37 $ns->send_drawinfo ( 43 undef $LOGIN_LOCK{$user};
38 "That player is already logged in on this server. " 44 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} 45}
51 46
52sub safe_spot($) { 47sub safe_spot($) {
53 my ($pl) = @_; 48 my ($pl) = @_;
54 49
92 $ob->message ( 87 $ob->message (
93 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " 88 "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. " 89 . "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... " 90 . "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.>", 91 . "H<You disconnected too long without having used a savebed.>",
97 cf::NDI_RED 92 cf::NDI_RED | cf::NDI_REPLY
98 ); 93 );
99 # kill them. 94 # kill them.
100 # reminds me of the famous badness 10000 syndrome... 95 # reminds me of the famous badness 10000 syndrome...
101 $ob->stats->hp (-10000); #] if they survive this they deserved to live 96 $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; 97 my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy;
106 $ob->message ( 101 $ob->message (
107 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " 102 "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. " 103 . "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... " 104 . "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.>", 105 . "H<You disconnected without having used a savebed. When you do that for too long, you might die.>",
111 cf::NDI_RED 106 cf::NDI_RED | cf::NDI_REPLY
112 ); 107 );
113 } 108 }
114 } else { 109 } else {
115 $ob->message ( 110 $ob->message (
116 "You didn't use a bed to reality to leave this realm. This is very dangerous, " 111 "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, " 112 . "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. " 113 . "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.>", 114 . "H<Always apply a bed of reality to disconnect from the server.>",
120 cf::NDI_RED 115 cf::NDI_RED | cf::NDI_REPLY
121 ); 116 );
122 } 117 }
123 } 118 }
124 119
125 $ob->goto ($map, $x, $y); 120 $ob->goto ($map, $x, $y);
158 # handle character creation, if neccessary 153 # handle character creation, if neccessary
159 # the rest of this function is character creation 154 # the rest of this function is character creation
160 155
161 my $ns = $pl->ns; 156 my $ns = $pl->ns;
162 my $ob = $pl->ob; 157 my $ob = $pl->ob;
163
164 $Coro::current->{desc} = "addme(" . $ob->name . ") login";
165 158
166 if ($pl->{chargen} eq "init") { 159 if ($pl->{chargen} eq "init") {
167 $ob->goto ($pl->maplevel, $ob->x, $ob->y); 160 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
168 161
169 # create the playerdir, if necessary, as chargen_race_done did it before 162 # create the playerdir, if necessary, as chargen_race_done did it before
258} 251}
259 252
260sub chargen { 253sub chargen {
261 my ($ns, $user, $hash) = @_; 254 my ($ns, $user, $hash) = @_;
262 255
263 # lock again, too lazy to make this nicer
264 local $cf::LOGIN_LOCK{$user} = 1;
265
266 # just to make sure nothing is left over 256 # just to make sure nothing is left over
267 # normally, nothing is there. 257 # normally, nothing is there.
268 nuke_playerdir $user; 258 nuke_playerdir $user;
269 259
270 my $pl = cf::player::new $user; 260 my $pl = cf::player::new $user;
287 my ($user, $pass); 277 my ($user, $pass);
288 278
289 $ns->send_packet ("addme_success"); 279 $ns->send_packet ("addme_success");
290 280
291 for (;;) { 281 for (;;) {
292 $ns->send_drawinfo ( 282 delete $ns->{login_guard};
283
284 $ns->send_msg (log =>
293 "Please enter your username now. If you are a new user, " 285 "Please enter your username now. If you are a new user, "
294 . "make one up that describes your character best. " 286 . "make one up that describes your character best. "
295 . "Only letters and digits are allowed, though.", 287 . "Only letters and digits are allowed, though.",
296 cf::NDI_BLUE 288 cf::NDI_BLUE | cf::NDI_REPLY
297 ); 289 );
298 290
299 # read username 291 # read username
300 while () { 292 while () {
301 $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; 293 $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:";
302 294
303 if ($cf::LOGIN_LOCK{$user}) {
304 $ns->send_drawinfo (
305 "That username is currently used in another login session. "
306 . "Chose another, or wait till the other session has ended.",
307 cf::NDI_RED
308 );
309 } elsif ($user =~ $VALID_LOGIN) { 295 if ($user =~ $VALID_LOGIN) {
310 last; 296 last;
311 } else { 297 } else {
312 $ns->send_drawinfo ( 298 $ns->send_msg (log =>
313 "Your username contains illegal characters " 299 "Your username contains illegal characters "
314 . "(only a-z, A-Z and 0-9 are allowed), " 300 . "(only a-z, A-Z and 0-9 are allowed), "
315 . "or is not between 3 and 20 characters in length.", 301 . "or is not between 3 and 20 characters in length.",
316 cf::NDI_RED 302 cf::NDI_RED | cf::NDI_REPLY
317 ); 303 );
318 } 304 }
319 Coro::Timer::sleep 0.4; 305 Coro::Timer::sleep 0.4;
320 } 306 }
321 307
322 check_playing $ns, $user and next;
323
324 $Coro::current->{desc} = "addme($user) pass"; 308 $Coro::current->{desc} = "addme($user)";
325 309
326 $ns->send_drawinfo ( 310 $ns->send_msg (log =>
327 "Welcome $user, please enter your password now. " 311 "Welcome $user, please enter your password now. "
328 . "New users should now choose a password. " 312 . "New users should now choose a password. "
329 . "Anything your client lets you enter is fine.", 313 . "Anything your client lets you enter is fine.",
330 cf::NDI_BLUE 314 cf::NDI_BLUE | cf::NDI_REPLY
331 ); 315 );
332 316
333 # read password 317 # read password
334 while () { 318 while () {
335 $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; 319 $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:";
336 last if $pass =~ /.../; 320 last if $pass =~ /.../;
337 $ns->send_drawinfo ( 321 $ns->send_msg (log =>
338 "Try to use at least three characters as your password please, " 322 "Try to use at least three characters as your password please, "
339 . "that cannot be too much to ask for :)", 323 . "that cannot be too much to ask for :)",
340 cf::NDI_RED 324 cf::NDI_RED | cf::NDI_REPLY
341 ); 325 );
342 Coro::Timer::sleep 0.4; 326 Coro::Timer::sleep 0.4;
343 } 327 }
344 328
345 # lock this username for the remainder of this login session 329 $ns->{login_guard} = login_guard $user
346 if ($cf::LOGIN_LOCK{$user}) { 330 or do {
347 $ns->send_drawinfo ( 331 $ns->send_msg (log =>
348 "That username is currently used in another login session. " 332 "That user is already logged in (or is logging in)."
349 . "Chose another, or wait till the other session has ended.", 333 . "Chose another, or wait till the other session has ended.",
350 cf::NDI_RED 334 cf::NDI_RED | cf::NDI_REPLY
335 );
336 next;
351 ); 337 };
352 next;
353 }
354 local $cf::LOGIN_LOCK{$user} = 1;
355
356 check_playing $ns, $user and next;
357
358 $Coro::current->{desc} = "addme($user) check";
359 338
360 # try to read the user file and check the password 339 # try to read the user file and check the password
361 if (my $pl = cf::player::find $user) { 340 if (my $pl = cf::player::find $user) {
362 aio_stat $pl->path and next; 341 aio_stat $pl->path and next;
363 my $mtime = (stat _)[9]; 342 my $mtime = (stat _)[9];
373 login $pl; 352 login $pl;
374 return; 353 return;
375 } elsif (can_cleanup $pl, $mtime) { 354 } elsif (can_cleanup $pl, $mtime) {
376 Coro::Timer::sleep 1; 355 Coro::Timer::sleep 1;
377 356
378 $ns->send_drawinfo ( 357 $ns->send_msg (log =>
379 "Player exists, but password does not match. If this is your account, " 358 "Player exists, but password does not match. If this is your account, "
380 . "please try again. If not, you can now decide to take over this account " 359 . "please try again. If not, you can now decide to take over this account "
381 . "because it has not been in-use for some time.", 360 . "because it has not been in-use for some time.",
382 cf::NDI_RED 361 cf::NDI_RED | cf::NDI_REPLY
383 ); 362 );
384 363
385 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 364 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
386 or next; 365 or next;
387 366
393 372
394 # fall through to creation 373 # fall through to creation
395 } else { 374 } else {
396 Coro::Timer::sleep 1; 375 Coro::Timer::sleep 1;
397 376
398 $ns->send_drawinfo ( 377 $ns->send_msg (log =>
399 "Wrong username or password. Please try again " 378 "Wrong username or password. Please try again "
400 . "(check for Numlock and other semi-obvious error sources).", 379 . "(check for Numlock and other semi-obvious error sources).",
401 cf::NDI_RED 380 cf::NDI_RED | cf::NDI_REPLY
402 ); 381 );
403 next; 382 next;
404 } 383 }
405 } else { 384 } else {
406 # unable to load the playerfile: 385 # unable to load the playerfile:
407 # check whether the player dir exists, which means the file is corrupted or 386 # check whether the player dir exists, which means the file is corrupted or
408 # something very similar. 387 # something very similar.
409 if (!aio_stat cf::player::playerdir $user) { 388 if (!aio_stat cf::player::playerdir $user) {
410 $ns->send_drawinfo ( 389 $ns->send_msg ( log =>
411 "Unable to retrieve this player. It might be a locked or broken account. " 390 "Unable to retrieve this player. It might be a locked or broken account. "
412 . "If this is your account, ask a dungeon master for assistance. " 391 . "If this is your account, ask a dungeon master for assistance. "
413 . "Otherwise choose a different login name.", 392 . "Otherwise choose a different login name.",
414 cf::NDI_RED 393 cf::NDI_RED | cf::NDI_REPLY
415 ); 394 );
416 next; 395 next;
417 } 396 }
418 } 397 }
419 398
420 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; 399 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
421 400
422 if ($pass2 ne $pass) { 401 if ($pass2 ne $pass) {
423 $ns->send_drawinfo ( 402 $ns->send_msg (log =>
424 "The passwords do not match, please try again.", 403 "The passwords do not match, please try again.",
425 cf::NDI_RED 404 cf::NDI_RED | cf::NDI_REPLY
426 ); 405 );
427 Coro::Timer::sleep 0.5; 406 Coro::Timer::sleep 0.5;
428 next; 407 next;
429 } 408 }
430 409
467 or return $fail ( 446 or return $fail (
468 "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), " 447 "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), "
469 . "or is not between 3 and 20 characters in length." 448 . "or is not between 3 and 20 characters in length."
470 ); 449 );
471 450
451 $ns->{login_guard} = login_guard $user
452 or return $fail->("User name '$user' is in use - try another login name.");
453
472 cf::player::find $user 454 cf::player::find $user
473 and return $fail->("User '$user' already exists - choose another login name."); 455 and return $fail->("User name '$user' is already registered - choose another login name.");
456
457 $reply->(1, "Account Created");
474 458
475 chargen $ns, $user, $pass; 459 chargen $ns, $user, $pass;
476 }); 460 });
477}; 461};
478 462
480 my ($ns, $reply, $user, $hash) = @_; 464 my ($ns, $reply, $user, $hash) = @_;
481 465
482 $ns->{addme}++ and return $ns->destroy; 466 $ns->{addme}++ and return $ns->destroy;
483 467
484 $ns->async (sub { 468 $ns->async (sub {
485 local $cf::LOGIN_LOCK{$user} = 1;
486
487 $Coro::current->{desc} = "login($user) check"; 469 $Coro::current->{desc} = "login($user)";
488 470
489 my $fail = sub { 471 my $fail = sub {
490 $reply->(0, $_[0]); 472 $reply->(0, $_[0]);
491 $ns->flush; # does not ensure that the data reaches the client - TODO 473 $ns->flush; # does not ensure that the data reaches the client - TODO
492 # need to do this in another thread, as this one gets canceled 474 # need to do this in another thread, as this one gets canceled
494 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack 476 Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack
495 $ns->destroy if $ns->valid; 477 $ns->destroy if $ns->valid;
496 }; 478 };
497 Coro::schedule; # do the destroy, should not return 479 Coro::schedule; # do the destroy, should not return
498 }; 480 };
481
482 $ns->{login_guard} = login_guard $user
483 or return $fail->("User '$user' is currently playing or logging in in another session. If that is your "
484 . "user name, make sure you are not running two clients. When in doubt, reboot.");
499 485
500 # try to read the user file and check the password 486 # try to read the user file and check the password
501 my $pl = cf::player::find $user 487 my $pl = cf::player::find $user
502 or return $fail->("User '$user' does not exist - wrong spelling?"); 488 or return $fail->("User '$user' does not exist - wrong spelling?");
503 489

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines