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.95 by root, Wed Dec 17 16:43:38 2008 UTC vs.
Revision 1.122 by root, Thu Nov 15 07:08:15 2012 UTC

1#! perl # mandatory 1#! perl # mandatory depends=highscore
2 2
3# login handling 3# login handling
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7use Deliantra::Util ();
7 8
8our $MAX_DISCONNECT_TIME = $cf::CFG{max_disconnect_time} || 3600; 9CONF MAX_DISCONNECT_TIME = 3600;
9
10# paranoia function to overwrite a string-in-place
11sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13}
14 10
15sub query { 11sub query {
16 my ($ns, $flags, $text) = @_; 12 my ($ns, $flags, $text) = @_;
17 13
18 $ns->query ($flags, $text, Coro::rouse_cb); 14 $ns->query ($flags, $text, Coro::rouse_cb);
59 my $m = $ob->map 55 my $m = $ob->map
60 or return; 56 or return;
61 my $x = $ob->x; 57 my $x = $ob->x;
62 my $y = $ob->y; 58 my $y = $ob->y;
63 59
64# return 0;#d# 60 # never happens normally, but helps when shell users make mistakes
65# warn join ":", $m->at ($x, $y);#d# 61 $m->linkable
66# warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; 62 or return 1;
67# return 0;
68 63
69 scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y) 64 scalar grep $_->type == cf::SAVEBED, $m->at ($x, $y)
70} 65}
71 66
72sub enter_map { 67sub enter_map {
73 my ($pl) = @_; 68 my ($pl) = @_;
74 69
75 warn $pl->ob->name, ": enter map 1\n";#d#
76 my $ob = $pl->ob; 70 my $ob = $pl->ob;
77 71
78 my ($map, $x, $y) 72 my ($map, $x, $y)
79 = $ob->{_link_pos} 73 = $ob->{_link_pos}
80 ? @{delete $ob->{_link_pos}} 74 ? @{delete $ob->{_link_pos}}
81 : ($pl->maplevel, $ob->x, $ob->y); 75 : ($pl->maplevel, $ob->x, $ob->y);
82 76
83 warn $pl->ob->name, ": enter map 2\n";#d#
84 $ob->enter_link; 77 $ob->enter_link;
85 warn $pl->ob->name, ": enter map 3\n";#d#
86 78
87 my $m = cf::map::find $map; 79 my $m = cf::map::find $map;
88 my $time = delete $pl->{unclean_save}; 80 my $time = delete $pl->{unclean_save};
89 warn $pl->ob->name, ": enter map 4\n";#d#
90 81
91 if ($time && $m) { 82 if ($time && $m) {
92 if ($time < $m->{instantiate_time}) { 83 if ($time < $m->{instantiate_time}) {
93 # the map was reset in the meantime 84 # the map was reset in the meantime
94 my $age = $cf::RUNTIME - $time; 85 my $age = $cf::RUNTIME - $time;
95 86
96 warn $ob->name, " map reset after logout, logout age $age (>= $MAX_DISCONNECT_TIME)\n";#d# 87 cf::info $ob->name, " map reset after logout, logout age $age (>= $MAX_DISCONNECT_TIME)\n";#d#
97 88
98 if ($age >= $MAX_DISCONNECT_TIME) { 89 if ($age >= $MAX_DISCONNECT_TIME) {
99 $ob->message ( 90 $ob->message (
100 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " 91 "You didn't use a bed to reality to leave this realm, leaving your body in great danger. "
101 . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " 92 . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. "
127 cf::NDI_RED 118 cf::NDI_RED
128 ); 119 );
129 } 120 }
130 } 121 }
131 122
132 warn $pl->ob->name, ": enter map 5\n";#d#
133 #$ob->goto ($map, $x, $y); 123 $ob->goto ($map, $x, $y);
134 $ob->goto ($map, $x, $y, sub { 124}
135 warn $pl->ob->name, ": enter map check\n";#d# 125
136 $_[0] 126sub encode_password($) {
137 }, sub { 127 "!" . unpack "H*", $_[0]
138 warn $pl->ob->name, ": enter map done\n";#d# 128}
129
130sub compare_password($$) {
131 my ($pass, $token) = @_;
132
133 if ($token =~ /!!(.*)/) {
134 return +(substr $pass, 0, 8) eq pack "H*", $1;
135 } elsif ($token =~ /!(.*)/) {
136 return $pass eq pack "H*", $1;
137 } else {
138 return $token eq crypt $pass, $token;
139 }); 139 }
140 warn $pl->ob->name, ": enter map 6\n";#d#
141
142} 140}
143 141
144# delete a player directory, be non-blocking AND synchronous... 142# delete a player directory
145# (thats hard, so we crap out and fork).
146sub nuke_playerdir { 143sub nuke_playerdir {
147 my ($user) = @_; 144 my ($user) = @_;
148 145
146 my $lock = cf::lock_acquire "ext::login::nuke_playerdir";
147
149 my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; 148 my $temp = "$PLAYERDIR/~$Coro::current~deleting~";
150
151 cf::fork_call {
152 rename "$PLAYERDIR/$user", $temp; 149 aio_rename "$PLAYERDIR/$user", $temp;
153 system "rm", "-rf", $temp; 150 IO::AIO::aio_rmtree $temp;
151}
152
153sub login_done {
154 my ($pl) = @_;
155
156 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) {
157 $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR);
154 }; 158 }
159}
160
161sub chargen {
162 my ($ns, $user, $pass) = @_;
163
164 # the rest of this function is character creation
165 $Coro::current->{desc} = "addme($user) chargen";
166
167 # just to make sure nothing is left over
168 # normally, nothing is there.
169 nuke_playerdir $user;
170
171 my $pl = cf::player::new $user;
172 $pl->password (encode_password $pass);
173 $pl->connect ($ns);
174 my $ob = $pl->ob;
175
176 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
177
178 while () {
179 $ob->update_stats;
180 $pl->save_stats;
181
182 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
183 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
184
185 if ($res =~ /^[Nn]/) {
186 last;
187 } elsif ($res > 0 && $res <= 7) {
188 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
189
190 if ($swap > 0 && $swap <= 7) {
191 $ob->swap_stats ($res - 1, $swap - 1);
192 }
193 } else {
194 $ob->roll_stats;
195 }
196
197 Coro::Timer::sleep 0.05;
198 }
199
200 $ob->set_animation (2);
201 $ob->add_statbonus;
202
203 while () {
204 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
205 my $msg = $ob->msg;
206 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
207 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
208
209 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
210 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
211
212 last if $res =~ /[dD]/;
213
214 $pl->chargen_race_next;
215 Coro::Timer::sleep 0.05;
216 }
217
218 # create the playerdir, if necessary, as chargen_race_done did it before
219 # presumably because of unique maps
220 aio_mkdir playerdir $pl, 0770;
221 $pl->chargen_race_done;
222
223 while () {
224 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
225 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
226
227 if ($res =~ /^[fF]/) {
228 $pl->gender (1);
229 last;
230 } elsif ($res =~ /^[mM]/) {
231 $pl->gender (0);
232 last;
233 }
234 Coro::Timer::sleep 0.05;
235 }
236
237 $ob->reply (undef, "Welcome to Deliantra!");
238
239 # XXX: Workaround for delayed client ext protocol handshake
240 $pl->esrv_new_player;
241
242 delete $pl->{deny_save};
155} 243}
156 244
157cf::client->attach (on_addme => sub { 245cf::client->attach (on_addme => sub {
158 my ($ns) = @_; 246 my ($ns) = @_;
159
160 if (!$ns->facecache)
161 {
162 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
163
164
165***
166*** WARNING:
167*** Your client does not support face/image caching,
168*** or it has been disabled. Face caching is mandatory
169*** so please enable it or use a newer client.
170***
171*** Look at your client preferences:
172***
173*** deliantra: all known versions automatically enable the facecache.
174*** cfclient: use the -cache commandline option.
175*** cfclient: map will not redraw automatically (bug).
176*** gcfclient: use -cache commandline option, or enable
177*** gcfclient: Client => Configure => Map & Image => Cache Images.
178*** jcrossclient: your client is broken, use deliantra or gcfclient.
179***
180***
181EOF
182 if ($ns->version =~ /jcrossclient/) {
183 # let them, for now
184 } else {
185 $ns->flush;
186 return $ns->destroy;
187 }
188
189 # $ns->facecache = true;
190 }
191
192 if ($ns->mapmode < cf::Map1aCmd) {
193 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
194
195
196***
197*** WARNING:
198*** Your client is too old. Please upgrade to a newer version.
199EOF
200
201 $ns->flush;
202 return $ns->destroy;
203 }
204 247
205 $ns->pl and return $ns->destroy; 248 $ns->pl and return $ns->destroy;
206 249
207 $ns->async (sub { 250 $ns->async (sub {
208 $Coro::current->{desc} = "addme init"; 251 $Coro::current->{desc} = "addme init";
282 325
283 # try to read the user file and check the password 326 # try to read the user file and check the password
284 if (my $pl = cf::player::find $user) { 327 if (my $pl = cf::player::find $user) {
285 aio_stat $pl->path and next; 328 aio_stat $pl->path and next;
286 my $mtime = (stat _)[9]; 329 my $mtime = (stat _)[9];
287 my $hash = $pl->password; 330 my $token = $pl->password;
288 331
289 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { 332 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) {
290 nuke_str $pass; 333 # player exists and passwords match - we can proceed
334
291 # password matches, wonderful 335 # password matches, wonderful
292 my $pl = cf::player::find $user or next; 336 my $pl = cf::player::find $user or next;
293 $pl->connect ($ns); 337 $pl->connect ($ns);
294 $pl->ob->flag (cf::FLAG_DEBUG, 1);#d# temp
295 enter_map $pl; 338 enter_map $pl;
339 login_done $pl;
296 last; 340 return;
297 } elsif (can_cleanup $pl, $mtime) { 341 } elsif (can_cleanup $pl, $mtime) {
298 Coro::Timer::sleep 1; 342 Coro::Timer::sleep 1;
299 343
300 $ns->send_drawinfo ( 344 $ns->send_drawinfo (
301 "Player exists, but password does not match. If this is your account, " 345 "Player exists, but password does not match. If this is your account, "
302 . "please try again. If not, you can now decide to take over this account " 346 . "please try again. If not, you can now decide to take over this account "
303 . "because it has not been in-use for some time.", 347 . "because it has not been in-use for some time.",
304 cf::NDI_RED 348 cf::NDI_RED
305 ); 349 );
306 350
307 #TODO: nuke_str
308 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 351 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
309 or next; 352 or next;
310 353
311 # check if the file hasn't changed 354 # check if the file hasn't changed
312 aio_stat cf::player::path $user and next; 355 aio_stat cf::player::path $user and next;
314 357
315 $pl->quit_character; 358 $pl->quit_character;
316 359
317 # fall through to creation 360 # fall through to creation
318 } else { 361 } else {
319 nuke_str $pass;
320
321 Coro::Timer::sleep 1; 362 Coro::Timer::sleep 1;
322 363
323 $ns->send_drawinfo ( 364 $ns->send_drawinfo (
324 "Wrong username or password. Please try again " 365 "Wrong username or password. Please try again "
325 . "(check for Numlock and other semi-obvious error sources).", 366 . "(check for Numlock and other semi-obvious error sources).",
327 ); 368 );
328 next; 369 next;
329 } 370 }
330 } else { 371 } else {
331 # unable to load the playerfile: 372 # unable to load the playerfile:
332 # check wether the player dir exists, which means the file is corrupted or 373 # check whether the player dir exists, which means the file is corrupted or
333 # something very similar. 374 # something very similar.
334 if (!aio_stat cf::player::playerdir $user) { 375 if (!aio_stat cf::player::playerdir $user) {
335 $ns->send_drawinfo ( 376 $ns->send_drawinfo (
336 "Unable to retrieve this player. It might be a locked or broken account. " 377 "Unable to retrieve this player. It might be a locked or broken account. "
337 . "If this is your account, ask a dungeon master for assistance. " 378 . "If this is your account, ask a dungeon master for assistance. "
340 ); 381 );
341 next; 382 next;
342 } 383 }
343 } 384 }
344 385
345 # the rest of this function is character creation
346 $Coro::current->{desc} = "addme($user) chargen";
347
348 # just to make sure nothing is left over
349 nuke_playerdir $user;
350
351 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; 386 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
352 387
353 if ($pass2 ne $pass) { 388 if ($pass2 ne $pass) {
354 nuke_str $pass;
355 nuke_str $pass2;
356 $ns->send_drawinfo ( 389 $ns->send_drawinfo (
357 "The passwords do not match, please try again.", 390 "The passwords do not match, please try again.",
358 cf::NDI_RED 391 cf::NDI_RED
359 ); 392 );
360 Coro::Timer::sleep 0.5; 393 Coro::Timer::sleep 0.5;
361 next; 394 next;
362 } 395 }
363 396
364 nuke_str $pass2;
365
366 my $pl = cf::player::new $user;
367 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
368 nuke_str $pass;
369 $pl->connect ($ns);
370 my $ob = $pl->ob;
371
372 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
373
374 while () {
375 $ob->update_stats;
376 $pl->save_stats;
377
378 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
379 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
380
381 if ($res =~ /^[Nn]/) {
382 last;
383 } elsif ($res > 0 && $res <= 7) {
384 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
385
386 if ($swap > 0 && $swap <= 7) {
387 $ob->swap_stats ($res - 1, $swap - 1);
388 }
389 } else {
390 $ob->roll_stats;
391 }
392
393 Coro::Timer::sleep 0.05;
394 }
395
396 $ob->set_animation (2);
397 $ob->add_statbonus;
398
399 while () {
400 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
401 my $msg = $ob->msg;
402 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
403 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
404
405 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
406 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
407
408 last if $res =~ /[dD]/;
409
410 $pl->chargen_race_next;
411 Coro::Timer::sleep 0.2;
412 }
413
414 # create the playerdir, if necessary, as chargen_race_done did it before
415 # presumably because of unique maps
416 aio_mkdir playerdir $pl, 0770;
417 $pl->chargen_race_done;
418
419 while () {
420 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
421 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
422
423 if ($res =~ /^[fF]/) {
424 $pl->gender (1);
425 last;
426 } elsif ($res =~ /^[mM]/) {
427 $pl->gender (0);
428 last;
429 }
430 Coro::Timer::sleep 0.2;
431 }
432
433 $ob->reply (undef, "Welcome to Deliantra!");
434
435 # XXX: Workaround for delayed client ext protocol handshake
436 $pl->esrv_new_player;
437
438 delete $pl->{deny_save};
439
440 last; 397 last;
441 } 398 }
399
400 # lock again, too layz to make this nicer
401 local $cf::LOGIN_LOCK{$user} = 1;
402
403 chargen $ns, $user, $pass;
404 login_done $ns->pl;
442 }); 405 });
443}); 406});
407
408cf::client->attach (
409 on_version => sub {
410 my ($ns, $arg) = @_;
411
412 $ns->ext_msg (nonces => map { join "", map { chr rand 256 } 0..63 } 1..2);
413 },
414);
415
416#cf::register_async_exticmd create_login => sub {
417# my ($ns, $reply, $user, $pass) = @_;
418#
419# $ns->pl and return $ns->destroy;
420#};
421
422cf::register_async_exticmd login => sub {
423 my ($ns, $reply, $user, $hash) = @_;
424
425 $ns->pl and return $ns->destroy;
426};
427
428cf::register_command password => sub {
429 my ($pl, $arg) = @_;
430
431 unless ($pl->flag (cf::FLAG_WIZ)) {
432 $pl->message (
433 "The password can currently only changed by a DM.",
434 cf::NDI_UNIQUE | cf::NDI_REPLY);
435 return;
436 }
437
438 my (@args) = split /\s+/, $arg;
439 my ($player, $new_pw) = @args;
440
441 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') {
442 $pl->message (
443 "Usage: password <player> [<new password>]",
444 cf::NDI_UNIQUE | cf::NDI_REPLY);
445 return;
446 }
447
448 if ($new_pw eq '') {
449 $new_pw =
450 join '',
451 map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] }
452 1..9;
453 }
454
455 cf::async {
456 my $plc = cf::player::find $player;
457 if ($plc) {
458 $plc->password (encode_password $new_pw);
459 $pl->message (
460 "Ok, changed password of '$player' to '$new_pw'!",
461 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
462 } else {
463 $pl->message (
464 "Fail! Couldn't set password for '$player', "
465 . "he doesn't seem to exist!",
466 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
467 }
468 };
469};
444 470
445cf::register_command quit => sub { 471cf::register_command quit => sub {
446 my ($ob, $arg) = @_; 472 my ($ob, $arg) = @_;
447 473
448 $ob->send_msg (undef, 474 $ob->send_msg (undef,
459 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub { 485 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
460 if ($_[0] !~ /^[yY]/) { 486 if ($_[0] !~ /^[yY]/) {
461 $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 487 $ob->send_msg (undef, "Ok, not not quitting then.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
462 } else { 488 } else {
463 $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 489 $ob->send_msg (undef, "Ok, quitting, hope to see you again.", cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
464 $pl->ns->flush; 490 cf::async {
465 cf::async { $pl->quit_character }; 491 $pl->quit_character;
492 };
466 } 493 }
467 }); 494 });
468}; 495};
469 496
470cf::object->attach ( 497cf::object->attach (
476 503
477 my $pl = $ob->contr; 504 my $pl = $ob->contr;
478 505
479 # update respawn position 506 # update respawn position
480 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 507 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
481 cf::async { $pl->save };
482 508
509 cf::async {
483 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy; 510 my $killer = cf::arch::get "killer_logout"; $pl->killer ($killer); $killer->destroy;
484 $ob->check_score; 511 ext::highscore::check $ob;
485 512
513 $pl->save;
514
486 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); 515 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY);
487 516
517 my $ns = $pl->ns
518 or return;
519
488 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 520 $ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
489 if ($_[0] !~ /^[yY]/) { 521 if ($_[0] !~ /^[yY]/) {
490 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 522 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
491 $pl->deactivate; 523 $pl->deactivate;
492 $pl->ns->destroy; 524 $pl->ns->destroy;
493 } else { 525 }
494 cf::async { $pl->save };
495 } 526 });
496 }); 527 };
497 }, 528 },
498); 529);
499 530
500cf::player->attach ( 531cf::player->attach (
501 on_login => sub { 532 on_login => sub {
516 unless safe_spot $pl; 547 unless safe_spot $pl;
517 } 548 }
518 }, 549 },
519); 550);
520 551
521

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines