#! perl # mandatory depends=highscore # login handling use Fcntl; use Coro::AIO; our $MAX_DISCONNECT_TIME = $cf::CFG{max_disconnect_time} || 3600; # paranoia function to overwrite a string-in-place sub nuke_str { substr $_[0], 0, (length $_[0]), "x" x length $_[0] } sub query { my ($ns, $flags, $text) = @_; $ns->query ($flags, $text, Coro::rouse_cb); Coro::rouse_wait } sub can_cleanup { my ($pl, $mtime) = @_; my $age = time - $mtime; my $level = $pl->ob->level; ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 || $age > 700 * 86400 # 2 years for everybody else } sub check_playing { my ($ns, $user) = @_; return unless cf::player::find_active $user; $ns->send_drawinfo ( "That player is already logged in on this server. " . "If you want to create a new player, choose another name. " . "If you have already a registered, make sure nobody " . "else is using your account at this time. If you lost your connection " . "then the server will likely timeout within a minute. If you still " . "cannot log-in after a minute, you are still logged in. Make sure " . "you do not have another client running. If you use windows, reboot, " . "this will fix anything.", cf::NDI_RED ); 1 } sub safe_spot($) { my ($pl) = @_; my $ob = $pl->ob; my $m = $ob->map or return; my $x = $ob->x; my $y = $ob->y; # return 0;#d# # warn join ":", $m->at ($x, $y);#d# # warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; # return 0; scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y) } sub enter_map { my ($pl) = @_; my $ob = $pl->ob; my ($map, $x, $y) = $ob->{_link_pos} ? @{delete $ob->{_link_pos}} : ($pl->maplevel, $ob->x, $ob->y); $ob->enter_link; my $m = cf::map::find $map; my $time = delete $pl->{unclean_save}; if ($time && $m) { if ($time < $m->{instantiate_time}) { # the map was reset in the meantime my $age = $cf::RUNTIME - $time; warn $ob->name, " map reset after logout, logout age $age (>= $MAX_DISCONNECT_TIME)\n";#d# if ($age >= $MAX_DISCONNECT_TIME) { $ob->message ( "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " . "Maybe you can find comfort in the thought that your body was quite satisfying in taste... " . "H", cf::NDI_RED ); # kill them. # reminds me of the famous badness 10000 syndrome... $ob->stats->hp (-10000); #] if they survive this they deserved to live my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy; } else { ($map, $x, $y) = $pl->savebed; $ob->message ( "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. " . "Better use a savebed next time, much worse things could have happened... " . "H", cf::NDI_RED ); } } else { $ob->message ( "You didn't use a bed to reality to leave this realm. This is very dangerous, " . "as lots of things could happen when you leave by other means, such as cave-ins, " . "or monsters suddenly snapping your body. Better use a savebed next time. " . "H", cf::NDI_RED ); } } $ob->goto ($map, $x, $y); } sub encode_password { crypt $_[0], join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)] } # delete a player directory, be non-blocking AND synchronous... # (that's hard, so we crap out and fork). sub nuke_playerdir { my ($user) = @_; my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; cf::fork_call { rename "$PLAYERDIR/$user", $temp; system "rm", "-rf", $temp; }; } cf::client->attach (on_addme => sub { my ($ns) = @_; $ns->pl and return $ns->destroy; $ns->async (sub { $Coro::current->{desc} = "addme init"; my ($user, $pass); $ns->send_packet ("addme_success"); for (;;) { $ns->send_drawinfo ( "Please enter your username now. If you are a new user, " . "make one up that describes your character best. " . "Only letters and digits are allowed, though.", cf::NDI_BLUE ); # read username while () { $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; if ($cf::LOGIN_LOCK{$user}) { $ns->send_drawinfo ( "That username is currently used in another login session. " . "Chose another, or wait till the other session has ended.", cf::NDI_RED ); } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z/) { last; } else { $ns->send_drawinfo ( "Your username contains illegal characters " . "(only a-z, A-Z and 0-9 are allowed), " . "or is not between 3 and 20 characters in length.", cf::NDI_RED ); } Coro::Timer::sleep 0.4; } check_playing $ns, $user and next; $Coro::current->{desc} = "addme($user) pass"; $ns->send_drawinfo ( "Welcome $user, please enter your password now. " . "New users should now choose a password. " . "Anything your client lets you enter is fine.", cf::NDI_BLUE ); # read password while () { $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; last if $pass =~ /.../; $ns->send_drawinfo ( "Try to use at least three characters as your password please, " . "that cannot be too much to ask for :)", cf::NDI_RED ); Coro::Timer::sleep 0.4; } # lock this username for the remainder of this login session if ($cf::LOGIN_LOCK{$user}) { $ns->send_drawinfo ( "That username is currently used in another login session. " . "Chose another, or wait till the other session has ended.", cf::NDI_RED ); next; } local $cf::LOGIN_LOCK{$user} = 1; check_playing $ns, $user and next; $Coro::current->{desc} = "addme($user) check"; # try to read the user file and check the password if (my $pl = cf::player::find $user) { aio_stat $pl->path and next; my $mtime = (stat _)[9]; my $hash = $pl->password; if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { nuke_str $pass; # password matches, wonderful my $pl = cf::player::find $user or next; $pl->connect ($ns); enter_map $pl; last; } elsif (can_cleanup $pl, $mtime) { Coro::Timer::sleep 1; $ns->send_drawinfo ( "Player exists, but password does not match. If this is your account, " . "please try again. If not, you can now decide to take over this account " . "because it has not been in-use for some time.", cf::NDI_RED ); #TODO: nuke_str (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ or next; # check if the file hasn't changed aio_stat cf::player::path $user and next; $mtime == (stat _)[9] or next; $pl->quit_character; # fall through to creation } else { nuke_str $pass; Coro::Timer::sleep 1; $ns->send_drawinfo ( "Wrong username or password. Please try again " . "(check for Numlock and other semi-obvious error sources).", cf::NDI_RED ); next; } } else { # unable to load the playerfile: # check wether the player dir exists, which means the file is corrupted or # something very similar. if (!aio_stat cf::player::playerdir $user) { $ns->send_drawinfo ( "Unable to retrieve this player. It might be a locked or broken account. " . "If this is your account, ask a dungeon master for assistance. " . "Otherwise choose a different login name.", cf::NDI_RED ); next; } } # the rest of this function is character creation $Coro::current->{desc} = "addme($user) chargen"; # just to make sure nothing is left over nuke_playerdir $user; my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; if ($pass2 ne $pass) { nuke_str $pass; nuke_str $pass2; $ns->send_drawinfo ( "The passwords do not match, please try again.", cf::NDI_RED ); Coro::Timer::sleep 0.5; next; } nuke_str $pass2; my $pl = cf::player::new $user; $pl->password (encode_password $pass); nuke_str $pass; $pl->connect ($ns); my $ob = $pl->ob; $ob->goto ($pl->maplevel, $ob->x, $ob->y); while () { $ob->update_stats; $pl->save_stats; my $res = query $ns, cf::CS_QUERY_SINGLECHAR, "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?"; if ($res =~ /^[Nn]/) { last; } elsif ($res > 0 && $res <= 7) { my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?"; if ($swap > 0 && $swap <= 7) { $ob->swap_stats ($res - 1, $swap - 1); } } else { $ob->roll_stats; } Coro::Timer::sleep 0.05; } $ob->set_animation (2); $ob->add_statbonus; while () { $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1); my $msg = $ob->msg; $msg =~ s/(?<=\S)\n(?=\S)/ /g; $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE); my $res = query $ns, cf::CS_QUERY_SINGLECHAR, "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; last if $res =~ /[dD]/; $pl->chargen_race_next; Coro::Timer::sleep 0.05; } # create the playerdir, if necessary, as chargen_race_done did it before # presumably because of unique maps aio_mkdir playerdir $pl, 0770; $pl->chargen_race_done; while () { my $res = query $ns, cf::CS_QUERY_SINGLECHAR, "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; if ($res =~ /^[fF]/) { $pl->gender (1); last; } elsif ($res =~ /^[mM]/) { $pl->gender (0); last; } Coro::Timer::sleep 0.05; } $ob->reply (undef, "Welcome to Deliantra!"); # XXX: Workaround for delayed client ext protocol handshake $pl->esrv_new_player; delete $pl->{deny_save}; last; } if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); } }); }); cf::register_command password => sub { my ($pl, $arg) = @_; my (@args) = split /\s+/, $arg; my ($new_pw, $player); if ($pl->flag (cf::FLAG_WIZ)) { ($player, $new_pw) = @args; } else { $new_pw = $args[0]; } if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { $pl->message ( "Usage: password []", cf::NDI_UNIQUE | cf::NDI_REPLY); return; } elsif (!$pl->flag (cf::FLAG_WIZ) && $new_pw eq '') { $pl->message ( "Usage: password ", cf::NDI_UNIQUE | cf::NDI_REPLY); return; } if ($player ne '' && $pl->flag (cf::FLAG_WIZ)) { unless ($new_pw ne '') { $new_pw = join '', map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] } 1..9; } cf::async { my $plc = cf::player::find $player; if ($plc) { $plc->password (encode_password $new_pw); $pl->message ( "Ok, changed password of '$player' to '$new_pw'!", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); } else { $pl->message ( "Fail! Couldn't set password for '$player', " . "he doesn't seem to exist!", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); } }; } else { my $change = delete $pl->{password_change}; warn "CHECK: @$change | $pl->{password_change} <<<<<\n"; if ($change && (time - $change->[0]) < 60) { $pl->message ( "Ok, changed your password!", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); $pl->contr->password (encode_password $new_pw); } else { $pl->message ( "Ok, please confirm your new password by sending " . "the command again within one minute!", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); $pl->{password_change} = [time, $new_pw]; } } }; cf::register_command quit => sub { my ($ob, $arg) = @_; $ob->send_msg (undef, "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. " . "If you are sure you want to do this, then use the quit_character command instead of quit.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); }; cf::register_command quit_character => sub { my ($ob, $arg) = @_; my $pl = $ob->contr; $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { if ($_[0] !~ /^[yY]/) { $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); } else { $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); $pl->ns->flush; cf::async { ext::highscore::check $pl->ob; $pl->quit_character; }; } }); }; cf::object->attach ( type => cf::SAVEBED, on_apply => sub { my ($bed, $ob) = @_; return cf::override 0 unless $ob->type == cf::PLAYER; my $pl = $ob->contr; # update respawn position $pl->savebed ($bed->map->path, $bed->x, $bed->y); cf::async { $pl->save }; my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; ext::highscore::check $ob; $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { if ($_[0] !~ /^[yY]/) { $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); $pl->deactivate; $pl->ns->destroy; } else { cf::async { $pl->save }; } }); }, ); cf::player->attach ( on_login => sub { my ($pl) = @_; my $name = $pl->ob->name; $_->ob->message ("$name has entered the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; }, on_logout => sub { my ($pl, $cleanly) = @_; my $name = $pl->ob->name; if ($cleanly) { $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; } else { $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; $pl->{unclean_save} = $cf::RUNTIME unless safe_spot $pl; } }, );