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.101 by root, Thu Oct 15 21:56:50 2009 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 {
87 if ($time && $m) { 82 if ($time && $m) {
88 if ($time < $m->{instantiate_time}) { 83 if ($time < $m->{instantiate_time}) {
89 # the map was reset in the meantime 84 # the map was reset in the meantime
90 my $age = $cf::RUNTIME - $time; 85 my $age = $cf::RUNTIME - $time;
91 86
92 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#
93 88
94 if ($age >= $MAX_DISCONNECT_TIME) { 89 if ($age >= $MAX_DISCONNECT_TIME) {
95 $ob->message ( 90 $ob->message (
96 "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. "
97 . "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. "
126 } 121 }
127 122
128 $ob->goto ($map, $x, $y); 123 $ob->goto ($map, $x, $y);
129} 124}
130 125
131# delete a player directory, be non-blocking AND synchronous... 126sub encode_password($) {
132# (that's hard, so we crap out and fork). 127 "!" . unpack "H*", $_[0]
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 }
140}
141
142# delete a player directory
133sub nuke_playerdir { 143sub nuke_playerdir {
134 my ($user) = @_; 144 my ($user) = @_;
135 145
146 my $lock = cf::lock_acquire "ext::login::nuke_playerdir";
147
136 my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; 148 my $temp = "$PLAYERDIR/~$Coro::current~deleting~";
137
138 cf::fork_call {
139 rename "$PLAYERDIR/$user", $temp; 149 aio_rename "$PLAYERDIR/$user", $temp;
140 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);
141 }; 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};
142} 243}
143 244
144cf::client->attach (on_addme => sub { 245cf::client->attach (on_addme => sub {
145 my ($ns) = @_; 246 my ($ns) = @_;
146 247
224 325
225 # try to read the user file and check the password 326 # try to read the user file and check the password
226 if (my $pl = cf::player::find $user) { 327 if (my $pl = cf::player::find $user) {
227 aio_stat $pl->path and next; 328 aio_stat $pl->path and next;
228 my $mtime = (stat _)[9]; 329 my $mtime = (stat _)[9];
229 my $hash = $pl->password; 330 my $token = $pl->password;
230 331
231 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { 332 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) {
232 nuke_str $pass; 333 # player exists and passwords match - we can proceed
334
233 # password matches, wonderful 335 # password matches, wonderful
234 my $pl = cf::player::find $user or next; 336 my $pl = cf::player::find $user or next;
235 $pl->connect ($ns); 337 $pl->connect ($ns);
236 enter_map $pl; 338 enter_map $pl;
339 login_done $pl;
237 last; 340 return;
238 } elsif (can_cleanup $pl, $mtime) { 341 } elsif (can_cleanup $pl, $mtime) {
239 Coro::Timer::sleep 1; 342 Coro::Timer::sleep 1;
240 343
241 $ns->send_drawinfo ( 344 $ns->send_drawinfo (
242 "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, "
243 . "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 "
244 . "because it has not been in-use for some time.", 347 . "because it has not been in-use for some time.",
245 cf::NDI_RED 348 cf::NDI_RED
246 ); 349 );
247 350
248 #TODO: nuke_str
249 (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]/
250 or next; 352 or next;
251 353
252 # check if the file hasn't changed 354 # check if the file hasn't changed
253 aio_stat cf::player::path $user and next; 355 aio_stat cf::player::path $user and next;
255 357
256 $pl->quit_character; 358 $pl->quit_character;
257 359
258 # fall through to creation 360 # fall through to creation
259 } else { 361 } else {
260 nuke_str $pass;
261
262 Coro::Timer::sleep 1; 362 Coro::Timer::sleep 1;
263 363
264 $ns->send_drawinfo ( 364 $ns->send_drawinfo (
265 "Wrong username or password. Please try again " 365 "Wrong username or password. Please try again "
266 . "(check for Numlock and other semi-obvious error sources).", 366 . "(check for Numlock and other semi-obvious error sources).",
268 ); 368 );
269 next; 369 next;
270 } 370 }
271 } else { 371 } else {
272 # unable to load the playerfile: 372 # unable to load the playerfile:
273 # 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
274 # something very similar. 374 # something very similar.
275 if (!aio_stat cf::player::playerdir $user) { 375 if (!aio_stat cf::player::playerdir $user) {
276 $ns->send_drawinfo ( 376 $ns->send_drawinfo (
277 "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. "
278 . "If this is your account, ask a dungeon master for assistance. " 378 . "If this is your account, ask a dungeon master for assistance. "
281 ); 381 );
282 next; 382 next;
283 } 383 }
284 } 384 }
285 385
286 # the rest of this function is character creation
287 $Coro::current->{desc} = "addme($user) chargen";
288
289 # just to make sure nothing is left over
290 nuke_playerdir $user;
291
292 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.";
293 387
294 if ($pass2 ne $pass) { 388 if ($pass2 ne $pass) {
295 nuke_str $pass;
296 nuke_str $pass2;
297 $ns->send_drawinfo ( 389 $ns->send_drawinfo (
298 "The passwords do not match, please try again.", 390 "The passwords do not match, please try again.",
299 cf::NDI_RED 391 cf::NDI_RED
300 ); 392 );
301 Coro::Timer::sleep 0.5; 393 Coro::Timer::sleep 0.5;
302 next; 394 next;
303 } 395 }
304 396
305 nuke_str $pass2;
306
307 my $pl = cf::player::new $user;
308 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
309 nuke_str $pass;
310 $pl->connect ($ns);
311 my $ob = $pl->ob;
312
313 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
314
315 while () {
316 $ob->update_stats;
317 $pl->save_stats;
318
319 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
320 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
321
322 if ($res =~ /^[Nn]/) {
323 last;
324 } elsif ($res > 0 && $res <= 7) {
325 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
326
327 if ($swap > 0 && $swap <= 7) {
328 $ob->swap_stats ($res - 1, $swap - 1);
329 }
330 } else {
331 $ob->roll_stats;
332 }
333
334 Coro::Timer::sleep 0.05;
335 }
336
337 $ob->set_animation (2);
338 $ob->add_statbonus;
339
340 while () {
341 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
342 my $msg = $ob->msg;
343 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
344 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
345
346 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
347 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
348
349 last if $res =~ /[dD]/;
350
351 $pl->chargen_race_next;
352 Coro::Timer::sleep 0.2;
353 }
354
355 # create the playerdir, if necessary, as chargen_race_done did it before
356 # presumably because of unique maps
357 aio_mkdir playerdir $pl, 0770;
358 $pl->chargen_race_done;
359
360 while () {
361 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
362 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
363
364 if ($res =~ /^[fF]/) {
365 $pl->gender (1);
366 last;
367 } elsif ($res =~ /^[mM]/) {
368 $pl->gender (0);
369 last;
370 }
371 Coro::Timer::sleep 0.2;
372 }
373
374 $ob->reply (undef, "Welcome to Deliantra!");
375
376 # XXX: Workaround for delayed client ext protocol handshake
377 $pl->esrv_new_player;
378
379 delete $pl->{deny_save};
380
381 last; 397 last;
382 } 398 }
383 399
384 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { 400 # lock again, too layz to make this nicer
385 $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); 401 local $cf::LOGIN_LOCK{$user} = 1;
386 } 402
403 chargen $ns, $user, $pass;
404 login_done $ns->pl;
387 }); 405 });
388}); 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};
389 470
390cf::register_command quit => sub { 471cf::register_command quit => sub {
391 my ($ob, $arg) = @_; 472 my ($ob, $arg) = @_;
392 473
393 $ob->send_msg (undef, 474 $ob->send_msg (undef,
404 $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 {
405 if ($_[0] !~ /^[yY]/) { 486 if ($_[0] !~ /^[yY]/) {
406 $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);
407 } else { 488 } else {
408 $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);
409 $pl->ns->flush; 490 cf::async {
410 cf::async { $pl->quit_character }; 491 $pl->quit_character;
492 };
411 } 493 }
412 }); 494 });
413}; 495};
414 496
415cf::object->attach ( 497cf::object->attach (
421 503
422 my $pl = $ob->contr; 504 my $pl = $ob->contr;
423 505
424 # update respawn position 506 # update respawn position
425 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 507 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
426 cf::async { $pl->save };
427 508
509 cf::async {
428 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;
429 $ob->check_score; 511 ext::highscore::check $ob;
430 512
513 $pl->save;
514
431 $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);
432 516
517 my $ns = $pl->ns
518 or return;
519
433 $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 {
434 if ($_[0] !~ /^[yY]/) { 521 if ($_[0] !~ /^[yY]/) {
435 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 522 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
436 $pl->deactivate; 523 $pl->deactivate;
437 $pl->ns->destroy; 524 $pl->ns->destroy;
438 } else { 525 }
439 cf::async { $pl->save };
440 } 526 });
441 }); 527 };
442 }, 528 },
443); 529);
444 530
445cf::player->attach ( 531cf::player->attach (
446 on_login => sub { 532 on_login => sub {
461 unless safe_spot $pl; 547 unless safe_spot $pl;
462 } 548 }
463 }, 549 },
464); 550);
465 551
466

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines