--- deliantra/server/ext/login.ext 2007/07/10 06:12:13 1.61 +++ deliantra/server/ext/login.ext 2010/05/04 21:45:42 1.109 @@ -1,10 +1,12 @@ -#! perl # mandatory +#! 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] @@ -13,11 +15,8 @@ sub query { my ($ns, $flags, $text) = @_; - my $current = $Coro::current; - $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] }); - Coro::schedule while ref $current; - - $current + $ns->query ($flags, $text, Coro::rouse_cb); + Coro::rouse_wait } sub can_cleanup { @@ -52,108 +51,110 @@ 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) - = $pl->ob->{_link_pos} - ? @{delete $pl->ob->{_link_pos}} - : ($pl->maplevel, $pl->ob->x, $pl->ob->y); - - $pl->ob->enter_link; - - if (my $time = delete $pl->{unclean_save}) { - if (my $m = cf::map::find $map) { - if ($time < $m->{instantiate_time}) { - # the map was reset in the meantime - my $age = $cf::RUNTIME - $time; - warn $pl->ob->name, " map reset after logout, logout age $age\n";#d# + = $ob->{_link_pos} + ? @{delete $ob->{_link_pos}} + : ($pl->maplevel, $ob->x, $ob->y); - # for now, just go back to savebed - ($map, $x, $y) = $pl->savebed; + $ob->enter_link; - $pl->ns->send_drawinfo ( + 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; + + cf::info $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. " - . "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...", + . "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 { - $pl->ns->send_drawinfo ( - "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.", + ($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 + ); } } - $pl->ob->goto ($map, $x, $y); + $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... -# (thats hard, so we crap out and fork). +# (that's hard, so we crap out and fork). sub nuke_playerdir { my ($user) = @_; - aio_stat "$PLAYERDIR/$user"; - system "cd \Q$PLAYERDIR\E " - . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " - . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; + 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) = @_; - if (!$ns->facecache) - { - $ns->send_drawinfo (<Configure=>Map & Image=>Cache Images. -*** jcrossclient: your client is broken, use CFPlus or gcfclient. -*** -*** -EOF - if ($ns->version =~ /jcrossclient/) { - # let them, for now - } else { - $ns->flush; - return $ns->destroy; - } - - # $ns->facecache = true; - } - - if ($ns->mapmode < cf::Map1aCmd) { - $ns->send_drawinfo (<flush; - return $ns->destroy; - } - $ns->pl and return $ns->destroy; $ns->async (sub { + $Coro::current->{desc} = "addme init"; + my ($user, $pass); $ns->send_packet ("addme_success"); @@ -168,7 +169,7 @@ # read username while () { - $user = query $ns, 0, "What is your name?\n:"; + $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; if ($cf::LOGIN_LOCK{$user}) { $ns->send_drawinfo ( @@ -176,13 +177,13 @@ . "Chose another, or wait till the other session has ended.", cf::NDI_RED ); - } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) { + } 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 18 characters in length.", + . "or is not between 3 and 20 characters in length.", cf::NDI_RED ); } @@ -191,6 +192,8 @@ 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. " @@ -223,6 +226,8 @@ 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; @@ -285,6 +290,7 @@ } # 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; @@ -305,7 +311,7 @@ nuke_str $pass2; my $pl = cf::player::new $user; - $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); + $pl->password (encode_password $pass); nuke_str $pass; $pl->connect ($ns); my $ob = $pl->ob; @@ -331,17 +337,17 @@ $ob->roll_stats; } - Coro::Timer::sleep 0.2; + Coro::Timer::sleep 0.05; } $ob->set_animation (2); $ob->add_statbonus; while () { - $ns->send_msg (-1, "chargen-race-title", ucfirst $pl->title); + $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1); my $msg = $ob->msg; $msg =~ s/(?<=\S)\n(?=\S)/ /g; - $ns->send_msg (cf::NDI_BLUE, "chargen-race-description", $msg); + $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"; @@ -349,9 +355,12 @@ last if $res =~ /[dD]/; $pl->chargen_race_next; - Coro::Timer::sleep 0.2; + 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 () { @@ -365,25 +374,98 @@ $pl->gender (0); last; } - Coro::Timer::sleep 0.2; + Coro::Timer::sleep 0.05; } - $ob->reply (undef, "Welcome to Crossfire!"); + $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}; + + 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->reply (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); + $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 { @@ -393,15 +475,14 @@ $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->reply (undef, - "Ok, not not quitting then.", - cf::NDI_UNIQUE | cf::NDI_RED); + $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); } else { - $ob->reply (undef, - "Ok, quitting, hope to see you again.", - cf::NDI_UNIQUE | cf::NDI_RED); + $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 { $pl->quit_character }; + cf::async { + ext::highscore::check $pl->ob; + $pl->quit_character; + }; } }); }; @@ -419,10 +500,10 @@ $pl->savebed ($bed->map->path, $bed->x, $bed->y); cf::async { $pl->save }; - $pl->killer ("left"); - $ob->check_score; + my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; + ext::highscore::check $ob; - $ob->reply (undef, "In the future, you will wake up here when you die."); + $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]/) { @@ -451,67 +532,9 @@ $_->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; + $pl->{unclean_save} = $cf::RUNTIME + unless safe_spot $pl; } }, ); -############################################################################# - -our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs -our $SAVE_TIMEOUT = 20; # save players every n seconds - -our $SCHEDULER = cf::async_ext { - my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL); - while () { - $schedule_interval->next; - - # this weird form of iteration over values is used because - # the hash changes underneath us frequently, and for - # keeps a direct reference to the value without (in 5.8 perls) - # keeping a reference, so this is prone to crashes or worse. - my @players = keys %cf::PLAYER; - for (@players) { - my $pl = $cf::PLAYER{$_} - or next; - $pl->valid or next; - - eval { - if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { - cf::wait_for_tick_begin; - $pl->save; - - unless ($pl->active || $pl->ns) { - # check refcounts, this is tricky and needs to be adjusted to fit server internals - my $ob = $pl->ob; - - my $pl_ref = $pl->refcnt_cnt; - my $ob_ref = $ob->refcnt_cnt; - - ## pl_ref == $pl + ob->contr + %cf::PLAYER - ## ob_ref == $ob + pl->observe + simply being an object - if ($pl_ref == 3 && $ob_ref == 3) { - warn "player-scheduler destroy ", $ob->name;#d# - - # remove from sight and get fresh "copies" - $pl = delete $cf::PLAYER{$ob->name}; - $ob = $pl->ob; - - $pl->destroy; # destroys $ob - } else { - my $a_ = $pl->refcnt;#d# - my $b_ = $ob->refcnt;#d# - - warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref ob $ob_ref (C pl $a_ ob $b_)\n";#d# - } - } - } - }; - warn $@ if $@; - Coro::cede; - }; - } -}; - -$SCHEDULER->prio (1); -