… | |
… | |
3 | # login handling |
3 | # login handling |
4 | |
4 | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
7 | |
7 | |
8 | our $MAX_DISCONNECT_TIME = $cf::CFG{max_disconnect_time} || 3600; |
8 | CONF MAX_DISCONNECT_TIME = 3600; |
9 | |
9 | |
10 | # paranoia function to overwrite a string-in-place |
10 | # paranoia function to overwrite a string-in-place |
11 | sub nuke_str { |
11 | sub nuke_str { |
12 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
12 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
13 | } |
13 | } |
… | |
… | |
59 | my $m = $ob->map |
59 | my $m = $ob->map |
60 | or return; |
60 | or return; |
61 | my $x = $ob->x; |
61 | my $x = $ob->x; |
62 | my $y = $ob->y; |
62 | my $y = $ob->y; |
63 | |
63 | |
|
|
64 | # never happens normally, but helps when shell users make mistakes |
|
|
65 | $m->linkable |
|
|
66 | or return 1; |
|
|
67 | |
64 | # return 0;#d# |
68 | # return 0;#d# |
65 | # warn join ":", $m->at ($x, $y);#d# |
69 | # warn join ":", $m->at ($x, $y);#d# |
66 | # warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; |
70 | # warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; |
67 | # return 0; |
71 | # return 0; |
68 | |
72 | |
… | |
… | |
87 | if ($time && $m) { |
91 | if ($time && $m) { |
88 | if ($time < $m->{instantiate_time}) { |
92 | if ($time < $m->{instantiate_time}) { |
89 | # the map was reset in the meantime |
93 | # the map was reset in the meantime |
90 | my $age = $cf::RUNTIME - $time; |
94 | my $age = $cf::RUNTIME - $time; |
91 | |
95 | |
92 | warn $ob->name, " map reset after logout, logout age $age (>= $MAX_DISCONNECT_TIME)\n";#d# |
96 | cf::info $ob->name, " map reset after logout, logout age $age (>= $MAX_DISCONNECT_TIME)\n";#d# |
93 | |
97 | |
94 | if ($age >= $MAX_DISCONNECT_TIME) { |
98 | if ($age >= $MAX_DISCONNECT_TIME) { |
95 | $ob->message ( |
99 | $ob->message ( |
96 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
100 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
97 | . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " |
101 | . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " |
… | |
… | |
126 | } |
130 | } |
127 | |
131 | |
128 | $ob->goto ($map, $x, $y); |
132 | $ob->goto ($map, $x, $y); |
129 | } |
133 | } |
130 | |
134 | |
131 | sub encode_password { |
135 | sub encode_password($) { |
132 | crypt $_[0], |
136 | # crypt $_[0], |
133 | join '', |
137 | # join '', |
134 | ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)] |
138 | # ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)] |
|
|
139 | "!" . unpack "H*", $_[0] |
135 | } |
140 | } |
136 | |
141 | |
137 | # delete a player directory, be non-blocking AND synchronous... |
142 | sub compare_password($$) { |
138 | # (that's hard, so we crap out and fork). |
143 | my ($pass, $token) = @_; |
|
|
144 | |
|
|
145 | if ($token =~ /!!(.*)/) { |
|
|
146 | return +(substr $pass, 0, 8) eq pack "H*", $1; |
|
|
147 | } elsif ($token =~ /!(.*)/) { |
|
|
148 | return $pass eq pack "H*", $1; |
|
|
149 | } else { |
|
|
150 | return $token eq crypt $pass, $token; |
|
|
151 | } |
|
|
152 | } |
|
|
153 | |
|
|
154 | # delete a player directory |
139 | sub nuke_playerdir { |
155 | sub nuke_playerdir { |
140 | my ($user) = @_; |
156 | my ($user) = @_; |
141 | |
157 | |
|
|
158 | my $lock = cf::lock_acquire "ext::login::nuke_playerdir"; |
|
|
159 | |
142 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
160 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
143 | |
|
|
144 | cf::fork_call { |
|
|
145 | rename "$PLAYERDIR/$user", $temp; |
161 | aio_rename "$PLAYERDIR/$user", $temp; |
146 | system "rm", "-rf", $temp; |
162 | IO::AIO::aio_rmtree $temp; |
147 | }; |
|
|
148 | } |
163 | } |
149 | |
164 | |
150 | cf::client->attach (on_addme => sub { |
165 | cf::client->attach (on_addme => sub { |
151 | my ($ns) = @_; |
166 | my ($ns) = @_; |
152 | |
167 | |
… | |
… | |
230 | |
245 | |
231 | # try to read the user file and check the password |
246 | # try to read the user file and check the password |
232 | if (my $pl = cf::player::find $user) { |
247 | if (my $pl = cf::player::find $user) { |
233 | aio_stat $pl->path and next; |
248 | aio_stat $pl->path and next; |
234 | my $mtime = (stat _)[9]; |
249 | my $mtime = (stat _)[9]; |
235 | my $hash = $pl->password; |
250 | my $token = $pl->password; |
236 | |
251 | |
237 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
252 | 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# |
238 | nuke_str $pass; |
254 | nuke_str $pass; |
239 | # password matches, wonderful |
255 | # password matches, wonderful |
240 | my $pl = cf::player::find $user or next; |
256 | my $pl = cf::player::find $user or next; |
241 | $pl->connect ($ns); |
257 | $pl->connect ($ns); |
242 | enter_map $pl; |
258 | enter_map $pl; |
… | |
… | |
274 | ); |
290 | ); |
275 | next; |
291 | next; |
276 | } |
292 | } |
277 | } else { |
293 | } else { |
278 | # unable to load the playerfile: |
294 | # unable to load the playerfile: |
279 | # check wether the player dir exists, which means the file is corrupted or |
295 | # check whether the player dir exists, which means the file is corrupted or |
280 | # something very similar. |
296 | # something very similar. |
281 | if (!aio_stat cf::player::playerdir $user) { |
297 | if (!aio_stat cf::player::playerdir $user) { |
282 | $ns->send_drawinfo ( |
298 | $ns->send_drawinfo ( |
283 | "Unable to retrieve this player. It might be a locked or broken account. " |
299 | "Unable to retrieve this player. It might be a locked or broken account. " |
284 | . "If this is your account, ask a dungeon master for assistance. " |
300 | . "If this is your account, ask a dungeon master for assistance. " |
… | |
… | |
291 | |
307 | |
292 | # the rest of this function is character creation |
308 | # the rest of this function is character creation |
293 | $Coro::current->{desc} = "addme($user) chargen"; |
309 | $Coro::current->{desc} = "addme($user) chargen"; |
294 | |
310 | |
295 | # just to make sure nothing is left over |
311 | # just to make sure nothing is left over |
|
|
312 | # normally, nothing is there. |
296 | nuke_playerdir $user; |
313 | nuke_playerdir $user; |
297 | |
314 | |
298 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
315 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
299 | |
316 | |
300 | if ($pass2 ne $pass) { |
317 | if ($pass2 ne $pass) { |
… | |
… | |
394 | }); |
411 | }); |
395 | |
412 | |
396 | cf::register_command password => sub { |
413 | cf::register_command password => sub { |
397 | my ($pl, $arg) = @_; |
414 | my ($pl, $arg) = @_; |
398 | |
415 | |
|
|
416 | unless ($pl->flag (cf::FLAG_WIZ)) { |
|
|
417 | $pl->message ( |
|
|
418 | "The password can currently only changed by a DM.", |
|
|
419 | cf::NDI_UNIQUE | cf::NDI_REPLY); |
|
|
420 | return; |
|
|
421 | } |
|
|
422 | |
399 | my (@args) = split /\s+/, $arg; |
423 | my (@args) = split /\s+/, $arg; |
400 | |
|
|
401 | my ($new_pw, $player); |
|
|
402 | |
|
|
403 | if ($pl->flag (cf::FLAG_WIZ)) { |
|
|
404 | ($player, $new_pw) = @args; |
424 | my ($player, $new_pw) = @args; |
405 | } else { |
|
|
406 | $new_pw = $args[0]; |
|
|
407 | } |
|
|
408 | |
425 | |
409 | if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { |
426 | if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { |
410 | $pl->message ( |
427 | $pl->message ( |
411 | "Usage: password <player> [<new password>]", |
428 | "Usage: password <player> [<new password>]", |
412 | cf::NDI_UNIQUE | cf::NDI_REPLY); |
429 | cf::NDI_UNIQUE | cf::NDI_REPLY); |
413 | return; |
430 | return; |
414 | } elsif (!$pl->flag (cf::FLAG_WIZ) && $new_pw eq '') { |
|
|
415 | $pl->message ( |
|
|
416 | "Usage: password <new password>", |
|
|
417 | cf::NDI_UNIQUE | cf::NDI_REPLY); |
|
|
418 | return; |
|
|
419 | } |
431 | } |
420 | |
432 | |
421 | if ($player ne '' && $pl->flag (cf::FLAG_WIZ)) { |
|
|
422 | unless ($new_pw ne '') { |
433 | if ($new_pw eq '') { |
423 | $new_pw = |
434 | $new_pw = |
424 | join '', |
435 | join '', |
425 | map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] } |
436 | map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] } |
426 | 1..9; |
437 | 1..9; |
427 | } |
438 | } |
428 | |
439 | |
429 | cf::async { |
440 | cf::async { |
430 | my $plc = cf::player::find $player; |
441 | my $plc = cf::player::find $player; |
431 | if ($plc) { |
442 | if ($plc) { |
432 | $plc->password (encode_password $new_pw); |
443 | $plc->password (encode_password $new_pw); |
433 | $pl->message ( |
|
|
434 | "Ok, changed password of '$player' to '$new_pw'!", |
|
|
435 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
|
|
436 | } else { |
|
|
437 | $pl->message ( |
|
|
438 | "Fail! Couldn't set password for '$player', " |
|
|
439 | . "he doesn't seem to exist!", |
|
|
440 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
|
|
441 | } |
|
|
442 | }; |
|
|
443 | } else { |
|
|
444 | my $change = delete $pl->{password_change}; |
|
|
445 | warn "CHECK: @$change | $pl->{password_change} <<<<<\n"; |
|
|
446 | |
|
|
447 | if ($change && (time - $change->[0]) < 60) { |
|
|
448 | $pl->message ( |
444 | $pl->message ( |
449 | "Ok, changed your password!", |
445 | "Ok, changed password of '$player' to '$new_pw'!", |
450 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
446 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
451 | $pl->contr->password (encode_password $new_pw); |
|
|
452 | |
|
|
453 | } else { |
447 | } else { |
454 | $pl->message ( |
448 | $pl->message ( |
455 | "Ok, please confirm your new password by sending " |
449 | "Fail! Couldn't set password for '$player', " |
456 | . "the command again within one minute!", |
450 | . "he doesn't seem to exist!", |
457 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
451 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
458 | $pl->{password_change} = [time, $new_pw]; |
|
|
459 | } |
452 | } |
460 | } |
453 | }; |
461 | }; |
454 | }; |
462 | |
455 | |
463 | cf::register_command quit => sub { |
456 | cf::register_command quit => sub { |
464 | my ($ob, $arg) = @_; |
457 | my ($ob, $arg) = @_; |
465 | |
458 | |
… | |
… | |
477 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { |
470 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { |
478 | if ($_[0] !~ /^[yY]/) { |
471 | if ($_[0] !~ /^[yY]/) { |
479 | $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
472 | $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
480 | } else { |
473 | } else { |
481 | $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
474 | $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
482 | $pl->ns->flush; |
|
|
483 | cf::async { |
475 | cf::async { |
484 | ext::highscore::check $pl->ob; |
|
|
485 | $pl->quit_character; |
476 | $pl->quit_character; |
486 | }; |
477 | }; |
487 | } |
478 | } |
488 | }); |
479 | }); |
489 | }; |
480 | }; |
… | |
… | |
497 | |
488 | |
498 | my $pl = $ob->contr; |
489 | my $pl = $ob->contr; |
499 | |
490 | |
500 | # update respawn position |
491 | # update respawn position |
501 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
492 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
502 | cf::async { $pl->save }; |
|
|
503 | |
493 | |
|
|
494 | cf::async { |
504 | my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; |
495 | my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; |
505 | ext::highscore::check $ob; |
496 | ext::highscore::check $ob; |
506 | |
497 | |
|
|
498 | $pl->save; |
|
|
499 | |
507 | $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); |
500 | $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); |
508 | |
501 | |
|
|
502 | my $ns = $pl->ns |
|
|
503 | or return; |
|
|
504 | |
509 | $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
505 | $ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { |
510 | if ($_[0] !~ /^[yY]/) { |
506 | if ($_[0] !~ /^[yY]/) { |
511 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
507 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
512 | $pl->deactivate; |
508 | $pl->deactivate; |
513 | $pl->ns->destroy; |
509 | $pl->ns->destroy; |
514 | } else { |
510 | } |
515 | cf::async { $pl->save }; |
|
|
516 | } |
511 | }); |
517 | }); |
512 | }; |
518 | }, |
513 | }, |
519 | ); |
514 | ); |
520 | |
515 | |
521 | cf::player->attach ( |
516 | cf::player->attach ( |
522 | on_login => sub { |
517 | on_login => sub { |
… | |
… | |
537 | unless safe_spot $pl; |
532 | unless safe_spot $pl; |
538 | } |
533 | } |
539 | }, |
534 | }, |
540 | ); |
535 | ); |
541 | |
536 | |
542 | |
|
|