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.111 by root, Sun May 9 22:51:13 2010 UTC vs.
Revision 1.122 by root, Thu Nov 15 07:08:15 2012 UTC

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 {
127 122
128 $ob->goto ($map, $x, $y); 123 $ob->goto ($map, $x, $y);
129} 124}
130 125
131sub encode_password($) { 126sub encode_password($) {
132# crypt $_[0],
133# join '',
134# ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)]
135 "!" . unpack "H*", $_[0] 127 "!" . unpack "H*", $_[0]
136} 128}
137 129
138sub compare_password($$) { 130sub compare_password($$) {
139 my ($pass, $token) = @_; 131 my ($pass, $token) = @_;
140 132
141 if ($token =~ /\!(.*)/) { 133 if ($token =~ /!!(.*)/) {
134 return +(substr $pass, 0, 8) eq pack "H*", $1;
135 } elsif ($token =~ /!(.*)/) {
142 return $pass eq pack "H*", $1; 136 return $pass eq pack "H*", $1;
143 } else { 137 } else {
144 return $token eq crypt $pass, $token; 138 return $token eq crypt $pass, $token;
145 } 139 }
146} 140}
152 my $lock = cf::lock_acquire "ext::login::nuke_playerdir"; 146 my $lock = cf::lock_acquire "ext::login::nuke_playerdir";
153 147
154 my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; 148 my $temp = "$PLAYERDIR/~$Coro::current~deleting~";
155 aio_rename "$PLAYERDIR/$user", $temp; 149 aio_rename "$PLAYERDIR/$user", $temp;
156 IO::AIO::aio_rmtree $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);
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};
157} 243}
158 244
159cf::client->attach (on_addme => sub { 245cf::client->attach (on_addme => sub {
160 my ($ns) = @_; 246 my ($ns) = @_;
161 247
242 aio_stat $pl->path and next; 328 aio_stat $pl->path and next;
243 my $mtime = (stat _)[9]; 329 my $mtime = (stat _)[9];
244 my $token = $pl->password; 330 my $token = $pl->password;
245 331
246 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) { 332 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) {
247 $pl->password (encode_password $pass); # make sure we store the new encoding #d# 333 # player exists and passwords match - we can proceed
248 nuke_str $pass; 334
249 # password matches, wonderful 335 # password matches, wonderful
250 my $pl = cf::player::find $user or next; 336 my $pl = cf::player::find $user or next;
251 $pl->connect ($ns); 337 $pl->connect ($ns);
252 enter_map $pl; 338 enter_map $pl;
339 login_done $pl;
253 last; 340 return;
254 } elsif (can_cleanup $pl, $mtime) { 341 } elsif (can_cleanup $pl, $mtime) {
255 Coro::Timer::sleep 1; 342 Coro::Timer::sleep 1;
256 343
257 $ns->send_drawinfo ( 344 $ns->send_drawinfo (
258 "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, "
259 . "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 "
260 . "because it has not been in-use for some time.", 347 . "because it has not been in-use for some time.",
261 cf::NDI_RED 348 cf::NDI_RED
262 ); 349 );
263 350
264 #TODO: nuke_str
265 (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]/
266 or next; 352 or next;
267 353
268 # check if the file hasn't changed 354 # check if the file hasn't changed
269 aio_stat cf::player::path $user and next; 355 aio_stat cf::player::path $user and next;
271 357
272 $pl->quit_character; 358 $pl->quit_character;
273 359
274 # fall through to creation 360 # fall through to creation
275 } else { 361 } else {
276 nuke_str $pass;
277
278 Coro::Timer::sleep 1; 362 Coro::Timer::sleep 1;
279 363
280 $ns->send_drawinfo ( 364 $ns->send_drawinfo (
281 "Wrong username or password. Please try again " 365 "Wrong username or password. Please try again "
282 . "(check for Numlock and other semi-obvious error sources).", 366 . "(check for Numlock and other semi-obvious error sources).",
284 ); 368 );
285 next; 369 next;
286 } 370 }
287 } else { 371 } else {
288 # unable to load the playerfile: 372 # unable to load the playerfile:
289 # 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
290 # something very similar. 374 # something very similar.
291 if (!aio_stat cf::player::playerdir $user) { 375 if (!aio_stat cf::player::playerdir $user) {
292 $ns->send_drawinfo ( 376 $ns->send_drawinfo (
293 "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. "
294 . "If this is your account, ask a dungeon master for assistance. " 378 . "If this is your account, ask a dungeon master for assistance. "
297 ); 381 );
298 next; 382 next;
299 } 383 }
300 } 384 }
301 385
302 # the rest of this function is character creation
303 $Coro::current->{desc} = "addme($user) chargen";
304
305 # just to make sure nothing is left over
306 # normally, nothing is there.
307 nuke_playerdir $user;
308
309 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.";
310 387
311 if ($pass2 ne $pass) { 388 if ($pass2 ne $pass) {
312 nuke_str $pass;
313 nuke_str $pass2;
314 $ns->send_drawinfo ( 389 $ns->send_drawinfo (
315 "The passwords do not match, please try again.", 390 "The passwords do not match, please try again.",
316 cf::NDI_RED 391 cf::NDI_RED
317 ); 392 );
318 Coro::Timer::sleep 0.5; 393 Coro::Timer::sleep 0.5;
319 next; 394 next;
320 } 395 }
321 396
322 nuke_str $pass2;
323
324 my $pl = cf::player::new $user;
325 $pl->password (encode_password $pass);
326 nuke_str $pass;
327 $pl->connect ($ns);
328 my $ob = $pl->ob;
329
330 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
331
332 while () {
333 $ob->update_stats;
334 $pl->save_stats;
335
336 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
337 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
338
339 if ($res =~ /^[Nn]/) {
340 last;
341 } elsif ($res > 0 && $res <= 7) {
342 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
343
344 if ($swap > 0 && $swap <= 7) {
345 $ob->swap_stats ($res - 1, $swap - 1);
346 }
347 } else {
348 $ob->roll_stats;
349 }
350
351 Coro::Timer::sleep 0.05;
352 }
353
354 $ob->set_animation (2);
355 $ob->add_statbonus;
356
357 while () {
358 $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1);
359 my $msg = $ob->msg;
360 $msg =~ s/(?<=\S)\n(?=\S)/ /g;
361 $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE);
362
363 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
364 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
365
366 last if $res =~ /[dD]/;
367
368 $pl->chargen_race_next;
369 Coro::Timer::sleep 0.05;
370 }
371
372 # create the playerdir, if necessary, as chargen_race_done did it before
373 # presumably because of unique maps
374 aio_mkdir playerdir $pl, 0770;
375 $pl->chargen_race_done;
376
377 while () {
378 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
379 "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n";
380
381 if ($res =~ /^[fF]/) {
382 $pl->gender (1);
383 last;
384 } elsif ($res =~ /^[mM]/) {
385 $pl->gender (0);
386 last;
387 }
388 Coro::Timer::sleep 0.05;
389 }
390
391 $ob->reply (undef, "Welcome to Deliantra!");
392
393 # XXX: Workaround for delayed client ext protocol handshake
394 $pl->esrv_new_player;
395
396 delete $pl->{deny_save};
397
398 last; 397 last;
399 } 398 }
400 399
401 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { 400 # lock again, too layz to make this nicer
402 $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); 401 local $cf::LOGIN_LOCK{$user} = 1;
403 } 402
403 chargen $ns, $user, $pass;
404 login_done $ns->pl;
404 }); 405 });
405}); 406});
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
407cf::register_command password => sub { 428cf::register_command password => sub {
408 my ($pl, $arg) = @_; 429 my ($pl, $arg) = @_;
409 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
410 my (@args) = split /\s+/, $arg; 438 my (@args) = split /\s+/, $arg;
411
412 my ($new_pw, $player);
413
414 if ($pl->flag (cf::FLAG_WIZ)) {
415 ($player, $new_pw) = @args; 439 my ($player, $new_pw) = @args;
416 } else {
417 $new_pw = $args[0];
418 }
419 440
420 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { 441 if ($pl->flag (cf::FLAG_WIZ) && $player eq '') {
421 $pl->message ( 442 $pl->message (
422 "Usage: password <player> [<new password>]", 443 "Usage: password <player> [<new password>]",
423 cf::NDI_UNIQUE | cf::NDI_REPLY); 444 cf::NDI_UNIQUE | cf::NDI_REPLY);
424 return; 445 return;
425 } elsif (!$pl->flag (cf::FLAG_WIZ) && $new_pw eq '') {
426 $pl->message (
427 "Usage: password <new password>",
428 cf::NDI_UNIQUE | cf::NDI_REPLY);
429 return;
430 } 446 }
431 447
432 if ($player ne '' && $pl->flag (cf::FLAG_WIZ)) {
433 unless ($new_pw ne '') { 448 if ($new_pw eq '') {
434 $new_pw = 449 $new_pw =
435 join '', 450 join '',
436 map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] } 451 map { ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64)] }
437 1..9; 452 1..9;
438 } 453 }
439 454
440 cf::async { 455 cf::async {
441 my $plc = cf::player::find $player; 456 my $plc = cf::player::find $player;
442 if ($plc) { 457 if ($plc) {
443 $plc->password (encode_password $new_pw); 458 $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 } else {
455 my $change = delete $pl->{password_change};
456
457 if ($change && (time - $change->[0]) < 60) {
458 $pl->message ( 459 $pl->message (
459 "Ok, changed your password!", 460 "Ok, changed password of '$player' to '$new_pw'!",
460 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 461 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
461 $pl->contr->password (encode_password $new_pw);
462
463 } else { 462 } else {
464 $pl->message ( 463 $pl->message (
465 "Ok, please confirm your new password by sending " 464 "Fail! Couldn't set password for '$player', "
466 . "the command again within one minute!", 465 . "he doesn't seem to exist!",
467 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY); 466 cf::NDI_UNIQUE | cf::NDI_RED | cf::NDI_REPLY);
468 $pl->{password_change} = [time, $new_pw];
469 } 467 }
470 } 468 };
471}; 469};
472 470
473cf::register_command quit => sub { 471cf::register_command quit => sub {
474 my ($ob, $arg) = @_; 472 my ($ob, $arg) = @_;
475 473
514 512
515 $pl->save; 513 $pl->save;
516 514
517 $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);
518 516
517 my $ns = $pl->ns
518 or return;
519
519 $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 {
520 if ($_[0] !~ /^[yY]/) { 521 if ($_[0] !~ /^[yY]/) {
521 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 522 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
522 $pl->deactivate; 523 $pl->deactivate;
523 $pl->ns->destroy; 524 $pl->ns->destroy;
524 } 525 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines