… | |
… | |
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 | # delete a player directory, be non-blocking AND synchronous... |
135 | sub encode_password($) { |
132 | # (that's hard, so we crap out and fork). |
136 | # crypt $_[0], |
|
|
137 | # join '', |
|
|
138 | # ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)] |
|
|
139 | "!" . unpack "H*", $_[0] |
|
|
140 | } |
|
|
141 | |
|
|
142 | sub compare_password($$) { |
|
|
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 |
133 | sub nuke_playerdir { |
155 | sub nuke_playerdir { |
134 | my ($user) = @_; |
156 | my ($user) = @_; |
135 | |
157 | |
|
|
158 | my $lock = cf::lock_acquire "ext::login::nuke_playerdir"; |
|
|
159 | |
136 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
160 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
137 | |
|
|
138 | cf::fork_call { |
|
|
139 | rename "$PLAYERDIR/$user", $temp; |
161 | aio_rename "$PLAYERDIR/$user", $temp; |
140 | system "rm", "-rf", $temp; |
162 | IO::AIO::aio_rmtree $temp; |
141 | }; |
|
|
142 | } |
163 | } |
143 | |
164 | |
144 | cf::client->attach (on_addme => sub { |
165 | cf::client->attach (on_addme => sub { |
145 | my ($ns) = @_; |
166 | my ($ns) = @_; |
146 | |
167 | |
… | |
… | |
224 | |
245 | |
225 | # try to read the user file and check the password |
246 | # try to read the user file and check the password |
226 | if (my $pl = cf::player::find $user) { |
247 | if (my $pl = cf::player::find $user) { |
227 | aio_stat $pl->path and next; |
248 | aio_stat $pl->path and next; |
228 | my $mtime = (stat _)[9]; |
249 | my $mtime = (stat _)[9]; |
229 | my $hash = $pl->password; |
250 | my $token = $pl->password; |
230 | |
251 | |
231 | 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# |
232 | nuke_str $pass; |
254 | nuke_str $pass; |
233 | # password matches, wonderful |
255 | # password matches, wonderful |
234 | my $pl = cf::player::find $user or next; |
256 | my $pl = cf::player::find $user or next; |
235 | $pl->connect ($ns); |
257 | $pl->connect ($ns); |
236 | enter_map $pl; |
258 | enter_map $pl; |
… | |
… | |
268 | ); |
290 | ); |
269 | next; |
291 | next; |
270 | } |
292 | } |
271 | } else { |
293 | } else { |
272 | # unable to load the playerfile: |
294 | # unable to load the playerfile: |
273 | # 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 |
274 | # something very similar. |
296 | # something very similar. |
275 | if (!aio_stat cf::player::playerdir $user) { |
297 | if (!aio_stat cf::player::playerdir $user) { |
276 | $ns->send_drawinfo ( |
298 | $ns->send_drawinfo ( |
277 | "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. " |
278 | . "If this is your account, ask a dungeon master for assistance. " |
300 | . "If this is your account, ask a dungeon master for assistance. " |
… | |
… | |
285 | |
307 | |
286 | # the rest of this function is character creation |
308 | # the rest of this function is character creation |
287 | $Coro::current->{desc} = "addme($user) chargen"; |
309 | $Coro::current->{desc} = "addme($user) chargen"; |
288 | |
310 | |
289 | # just to make sure nothing is left over |
311 | # just to make sure nothing is left over |
|
|
312 | # normally, nothing is there. |
290 | nuke_playerdir $user; |
313 | nuke_playerdir $user; |
291 | |
314 | |
292 | 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."; |
293 | |
316 | |
294 | if ($pass2 ne $pass) { |
317 | if ($pass2 ne $pass) { |
… | |
… | |
303 | } |
326 | } |
304 | |
327 | |
305 | nuke_str $pass2; |
328 | nuke_str $pass2; |
306 | |
329 | |
307 | my $pl = cf::player::new $user; |
330 | my $pl = cf::player::new $user; |
308 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
331 | $pl->password (encode_password $pass); |
309 | nuke_str $pass; |
332 | nuke_str $pass; |
310 | $pl->connect ($ns); |
333 | $pl->connect ($ns); |
311 | my $ob = $pl->ob; |
334 | my $ob = $pl->ob; |
312 | |
335 | |
313 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
336 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
… | |
… | |
385 | $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
408 | $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
386 | } |
409 | } |
387 | }); |
410 | }); |
388 | }); |
411 | }); |
389 | |
412 | |
|
|
413 | cf::register_command password => sub { |
|
|
414 | my ($pl, $arg) = @_; |
|
|
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 | |
|
|
423 | my (@args) = split /\s+/, $arg; |
|
|
424 | my ($player, $new_pw) = @args; |
|
|
425 | |
|
|
426 | if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { |
|
|
427 | $pl->message ( |
|
|
428 | "Usage: password <player> [<new password>]", |
|
|
429 | cf::NDI_UNIQUE | cf::NDI_REPLY); |
|
|
430 | return; |
|
|
431 | } |
|
|
432 | |
|
|
433 | if ($new_pw eq '') { |
|
|
434 | $new_pw = |
|
|
435 | join '', |
|
|
436 | map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] } |
|
|
437 | 1..9; |
|
|
438 | } |
|
|
439 | |
|
|
440 | cf::async { |
|
|
441 | my $plc = cf::player::find $player; |
|
|
442 | if ($plc) { |
|
|
443 | $plc->password (encode_password $new_pw); |
|
|
444 | $pl->message ( |
|
|
445 | "Ok, changed password of '$player' to '$new_pw'!", |
|
|
446 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
|
|
447 | } else { |
|
|
448 | $pl->message ( |
|
|
449 | "Fail! Couldn't set password for '$player', " |
|
|
450 | . "he doesn't seem to exist!", |
|
|
451 | cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); |
|
|
452 | } |
|
|
453 | }; |
|
|
454 | }; |
|
|
455 | |
390 | cf::register_command quit => sub { |
456 | cf::register_command quit => sub { |
391 | my ($ob, $arg) = @_; |
457 | my ($ob, $arg) = @_; |
392 | |
458 | |
393 | $ob->send_msg (undef, |
459 | $ob->send_msg (undef, |
394 | "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " |
460 | "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " |
… | |
… | |
404 | $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 { |
405 | if ($_[0] !~ /^[yY]/) { |
471 | if ($_[0] !~ /^[yY]/) { |
406 | $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); |
407 | } else { |
473 | } else { |
408 | $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); |
409 | $pl->ns->flush; |
475 | cf::async { |
410 | cf::async { $pl->quit_character }; |
476 | $pl->quit_character; |
|
|
477 | }; |
411 | } |
478 | } |
412 | }); |
479 | }); |
413 | }; |
480 | }; |
414 | |
481 | |
415 | cf::object->attach ( |
482 | cf::object->attach ( |
… | |
… | |
421 | |
488 | |
422 | my $pl = $ob->contr; |
489 | my $pl = $ob->contr; |
423 | |
490 | |
424 | # update respawn position |
491 | # update respawn position |
425 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
492 | $pl->savebed ($bed->map->path, $bed->x, $bed->y); |
426 | cf::async { $pl->save }; |
|
|
427 | |
493 | |
|
|
494 | cf::async { |
428 | 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; |
429 | ext::highscore::check $ob; |
496 | ext::highscore::check $ob; |
430 | |
497 | |
|
|
498 | $pl->save; |
|
|
499 | |
431 | $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); |
432 | |
501 | |
|
|
502 | my $ns = $pl->ns |
|
|
503 | or return; |
|
|
504 | |
433 | $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 { |
434 | if ($_[0] !~ /^[yY]/) { |
506 | if ($_[0] !~ /^[yY]/) { |
435 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
507 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); |
436 | $pl->deactivate; |
508 | $pl->deactivate; |
437 | $pl->ns->destroy; |
509 | $pl->ns->destroy; |
438 | } else { |
510 | } |
439 | cf::async { $pl->save }; |
|
|
440 | } |
511 | }); |
441 | }); |
512 | }; |
442 | }, |
513 | }, |
443 | ); |
514 | ); |
444 | |
515 | |
445 | cf::player->attach ( |
516 | cf::player->attach ( |
446 | on_login => sub { |
517 | on_login => sub { |
… | |
… | |
461 | unless safe_spot $pl; |
532 | unless safe_spot $pl; |
462 | } |
533 | } |
463 | }, |
534 | }, |
464 | ); |
535 | ); |
465 | |
536 | |
466 | |
|
|