… | |
… | |
7 | use Deliantra::Util (); |
7 | use Deliantra::Util (); |
8 | |
8 | |
9 | CONF MAX_DISCONNECT_TIME = 3600; |
9 | CONF MAX_DISCONNECT_TIME = 3600; |
10 | |
10 | |
11 | our $VALID_LOGIN = qr<^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z>; |
11 | our $VALID_LOGIN = qr<^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z>; |
|
|
12 | our %LOGIN_LOCK; |
|
|
13 | |
|
|
14 | # utility function to send messages to the client before |
|
|
15 | # we have a player object to format them for. does not |
|
|
16 | # escape anything. |
|
|
17 | sub send_log ($$$) { |
|
|
18 | $_[0]->send_packet ("msg $_[2] log $_[1]"); |
|
|
19 | } |
12 | |
20 | |
13 | sub query { |
21 | sub query { |
14 | my ($ns, $flags, $text) = @_; |
22 | my ($ns, $flags, $text) = @_; |
15 | |
23 | |
16 | $ns->query ($flags, $text, Coro::rouse_cb); |
24 | $ns->query ($flags, $text, Coro::rouse_cb); |
… | |
… | |
27 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
35 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
28 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
36 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
29 | || $age > 700 * 86400 # 2 years for everybody else |
37 | || $age > 700 * 86400 # 2 years for everybody else |
30 | } |
38 | } |
31 | |
39 | |
32 | sub check_playing { |
40 | # return a guard object for a lock on the given username, if available |
|
|
41 | sub login_guard { |
33 | my ($ns, $user) = @_; |
42 | my ($user) = @_; |
34 | |
43 | |
|
|
44 | exists $LOGIN_LOCK{$user} |
|
|
45 | and return undef; |
|
|
46 | |
35 | return unless cf::player::find_active $user; |
47 | cf::player::find_active $user |
|
|
48 | and return undef; |
36 | |
49 | |
37 | $ns->send_drawinfo ( |
50 | undef $LOGIN_LOCK{$user}; |
38 | "That player is already logged in on this server. " |
51 | Guard::guard { delete $LOGIN_LOCK{$user} } |
39 | . "If you want to create a new player, choose another name. " |
|
|
40 | . "If you have already a registered, make sure nobody " |
|
|
41 | . "else is using your account at this time. If you lost your connection " |
|
|
42 | . "then the server will likely timeout within a minute. If you still " |
|
|
43 | . "cannot log-in after a minute, you are still logged in. Make sure " |
|
|
44 | . "you do not have another client running. If you use windows, reboot, " |
|
|
45 | . "this will fix anything.", |
|
|
46 | cf::NDI_RED |
|
|
47 | ); |
|
|
48 | |
|
|
49 | 1 |
|
|
50 | } |
52 | } |
51 | |
53 | |
52 | sub safe_spot($) { |
54 | sub safe_spot($) { |
53 | my ($pl) = @_; |
55 | my ($pl) = @_; |
54 | |
56 | |
… | |
… | |
92 | $ob->message ( |
94 | $ob->message ( |
93 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
95 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
94 | . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " |
96 | . "Unfortunately, nobody was near to help you when the monsters arrived to eat you. " |
95 | . "Maybe you can find comfort in the thought that your body was quite satisfying in taste... " |
97 | . "Maybe you can find comfort in the thought that your body was quite satisfying in taste... " |
96 | . "H<You disconnected too long without having used a savebed.>", |
98 | . "H<You disconnected too long without having used a savebed.>", |
97 | cf::NDI_RED |
99 | cf::NDI_RED | cf::NDI_REPLY |
98 | ); |
100 | ); |
99 | # kill them. |
101 | # kill them. |
100 | # reminds me of the famous badness 10000 syndrome... |
102 | # reminds me of the famous badness 10000 syndrome... |
101 | $ob->stats->hp (-10000); #] if they survive this they deserved to live |
103 | $ob->stats->hp (-10000); #] if they survive this they deserved to live |
102 | my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy; |
104 | my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy; |
… | |
… | |
106 | $ob->message ( |
108 | $ob->message ( |
107 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
109 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
108 | . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. " |
110 | . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. " |
109 | . "Better use a savebed next time, much worse things could have happened... " |
111 | . "Better use a savebed next time, much worse things could have happened... " |
110 | . "H<You disconnected without having used a savebed. When you do that for too long, you might die.>", |
112 | . "H<You disconnected without having used a savebed. When you do that for too long, you might die.>", |
111 | cf::NDI_RED |
113 | cf::NDI_RED | cf::NDI_REPLY |
112 | ); |
114 | ); |
113 | } |
115 | } |
114 | } else { |
116 | } else { |
115 | $ob->message ( |
117 | $ob->message ( |
116 | "You didn't use a bed to reality to leave this realm. This is very dangerous, " |
118 | "You didn't use a bed to reality to leave this realm. This is very dangerous, " |
117 | . "as lots of things could happen when you leave by other means, such as cave-ins, " |
119 | . "as lots of things could happen when you leave by other means, such as cave-ins, " |
118 | . "or monsters suddenly snapping your body. Better use a savebed next time. " |
120 | . "or monsters suddenly snapping your body. Better use a savebed next time. " |
119 | . "H<Always apply a bed of reality to disconnect from the server.>", |
121 | . "H<Always apply a bed of reality to disconnect from the server.>", |
120 | cf::NDI_RED |
122 | cf::NDI_RED | cf::NDI_REPLY |
121 | ); |
123 | ); |
122 | } |
124 | } |
123 | } |
125 | } |
124 | |
126 | |
125 | $ob->goto ($map, $x, $y); |
127 | $ob->goto ($map, $x, $y); |
… | |
… | |
158 | # handle character creation, if neccessary |
160 | # handle character creation, if neccessary |
159 | # the rest of this function is character creation |
161 | # the rest of this function is character creation |
160 | |
162 | |
161 | my $ns = $pl->ns; |
163 | my $ns = $pl->ns; |
162 | my $ob = $pl->ob; |
164 | my $ob = $pl->ob; |
163 | |
|
|
164 | $Coro::current->{desc} = "addme(" . $ob->name . ") login"; |
|
|
165 | |
165 | |
166 | if ($pl->{chargen} eq "init") { |
166 | if ($pl->{chargen} eq "init") { |
167 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
167 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
168 | |
168 | |
169 | # create the playerdir, if necessary, as chargen_race_done did it before |
169 | # create the playerdir, if necessary, as chargen_race_done did it before |
… | |
… | |
248 | $pl->esrv_new_player; |
248 | $pl->esrv_new_player; |
249 | |
249 | |
250 | $pl->{chargen} = "done"; |
250 | $pl->{chargen} = "done"; |
251 | } |
251 | } |
252 | |
252 | |
|
|
253 | $ns->update_command_faces; |
|
|
254 | |
253 | $ob->reply (undef, "Welcome to Deliantra!"); |
255 | $ob->reply (undef, "Welcome to Deliantra!"); |
254 | |
256 | |
255 | if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { |
257 | if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { |
256 | $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
258 | $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
257 | } |
259 | } |
258 | } |
260 | } |
259 | |
261 | |
260 | sub chargen { |
262 | sub chargen { |
261 | my ($ns, $user, $hash) = @_; |
263 | my ($ns, $user, $hash) = @_; |
262 | |
|
|
263 | # lock again, too lazy to make this nicer |
|
|
264 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
265 | |
264 | |
266 | # just to make sure nothing is left over |
265 | # just to make sure nothing is left over |
267 | # normally, nothing is there. |
266 | # normally, nothing is there. |
268 | nuke_playerdir $user; |
267 | nuke_playerdir $user; |
269 | |
268 | |
… | |
… | |
287 | my ($user, $pass); |
286 | my ($user, $pass); |
288 | |
287 | |
289 | $ns->send_packet ("addme_success"); |
288 | $ns->send_packet ("addme_success"); |
290 | |
289 | |
291 | for (;;) { |
290 | for (;;) { |
292 | $ns->send_drawinfo ( |
291 | delete $ns->{login_guard}; |
|
|
292 | |
|
|
293 | send_log $ns, |
293 | "Please enter your username now. If you are a new user, " |
294 | "Please enter your username now. If you are a new user, " |
294 | . "make one up that describes your character best. " |
295 | . "make one up that describes your character best. " |
295 | . "Only letters and digits are allowed, though.", |
296 | . "Only letters and digits are allowed, though.", |
296 | cf::NDI_BLUE |
297 | cf::NDI_BLUE | cf::NDI_REPLY |
297 | ); |
298 | ; |
298 | |
299 | |
299 | # read username |
300 | # read username |
300 | while () { |
301 | while () { |
301 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
302 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
302 | |
303 | |
303 | if ($cf::LOGIN_LOCK{$user}) { |
|
|
304 | $ns->send_drawinfo ( |
|
|
305 | "That username is currently used in another login session. " |
|
|
306 | . "Chose another, or wait till the other session has ended.", |
|
|
307 | cf::NDI_RED |
|
|
308 | ); |
|
|
309 | } elsif ($user =~ $VALID_LOGIN) { |
304 | if ($user =~ $VALID_LOGIN) { |
310 | last; |
305 | last; |
311 | } else { |
306 | } else { |
312 | $ns->send_drawinfo ( |
307 | send_log $ns, |
313 | "Your username contains illegal characters " |
308 | "Your username contains illegal characters " |
314 | . "(only a-z, A-Z and 0-9 are allowed), " |
309 | . "(only a-z, A-Z and 0-9 are allowed), " |
315 | . "or is not between 3 and 20 characters in length.", |
310 | . "or is not between 3 and 20 characters in length.", |
316 | cf::NDI_RED |
311 | cf::NDI_RED | cf::NDI_REPLY |
317 | ); |
312 | ; |
318 | } |
313 | } |
319 | Coro::Timer::sleep 0.4; |
314 | Coro::Timer::sleep 0.4; |
320 | } |
315 | } |
321 | |
316 | |
322 | check_playing $ns, $user and next; |
|
|
323 | |
|
|
324 | $Coro::current->{desc} = "addme($user) pass"; |
317 | $Coro::current->{desc} = "addme($user)"; |
325 | |
318 | |
326 | $ns->send_drawinfo ( |
319 | send_log $ns, |
327 | "Welcome $user, please enter your password now. " |
320 | "Welcome $user, please enter your password now. " |
328 | . "New users should now choose a password. " |
321 | . "New users should now choose a password. " |
329 | . "Anything your client lets you enter is fine.", |
322 | . "Anything your client lets you enter is fine.", |
330 | cf::NDI_BLUE |
323 | cf::NDI_BLUE | cf::NDI_REPLY |
331 | ); |
324 | ; |
332 | |
325 | |
333 | # read password |
326 | # read password |
334 | while () { |
327 | while () { |
335 | $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; |
328 | $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; |
336 | last if $pass =~ /.../; |
329 | last if $pass =~ /.../; |
337 | $ns->send_drawinfo ( |
330 | send_log $ns, |
338 | "Try to use at least three characters as your password please, " |
331 | "Try to use at least three characters as your password please, " |
339 | . "that cannot be too much to ask for :)", |
332 | . "that cannot be too much to ask for :)", |
340 | cf::NDI_RED |
333 | cf::NDI_RED | cf::NDI_REPLY |
341 | ); |
334 | ; |
342 | Coro::Timer::sleep 0.4; |
335 | Coro::Timer::sleep 0.4; |
343 | } |
336 | } |
344 | |
337 | |
345 | # lock this username for the remainder of this login session |
338 | $ns->{login_guard} = login_guard $user |
346 | if ($cf::LOGIN_LOCK{$user}) { |
339 | or do { |
347 | $ns->send_drawinfo ( |
340 | send_log $ns, |
348 | "That username is currently used in another login session. " |
341 | "That user is already logged in (or is logging in)." |
349 | . "Chose another, or wait till the other session has ended.", |
342 | . "Chose another, or wait till the other session has ended.", |
350 | cf::NDI_RED |
343 | cf::NDI_RED | cf::NDI_REPLY |
|
|
344 | ; |
|
|
345 | next; |
351 | ); |
346 | }; |
352 | next; |
|
|
353 | } |
|
|
354 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
355 | |
|
|
356 | check_playing $ns, $user and next; |
|
|
357 | |
|
|
358 | $Coro::current->{desc} = "addme($user) check"; |
|
|
359 | |
347 | |
360 | # try to read the user file and check the password |
348 | # try to read the user file and check the password |
361 | if (my $pl = cf::player::find $user) { |
349 | if (my $pl = cf::player::find $user) { |
362 | aio_stat $pl->path and next; |
350 | aio_stat $pl->path and next; |
363 | my $mtime = (stat _)[9]; |
351 | my $mtime = (stat _)[9]; |
… | |
… | |
373 | login $pl; |
361 | login $pl; |
374 | return; |
362 | return; |
375 | } elsif (can_cleanup $pl, $mtime) { |
363 | } elsif (can_cleanup $pl, $mtime) { |
376 | Coro::Timer::sleep 1; |
364 | Coro::Timer::sleep 1; |
377 | |
365 | |
378 | $ns->send_drawinfo ( |
366 | send_log $ns, |
379 | "Player exists, but password does not match. If this is your account, " |
367 | "Player exists, but password does not match. If this is your account, " |
380 | . "please try again. If not, you can now decide to take over this account " |
368 | . "please try again. If not, you can now decide to take over this account " |
381 | . "because it has not been in-use for some time.", |
369 | . "because it has not been in-use for some time.", |
382 | cf::NDI_RED |
370 | cf::NDI_RED | cf::NDI_REPLY |
383 | ); |
371 | ; |
384 | |
372 | |
385 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
373 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
386 | or next; |
374 | or next; |
387 | |
375 | |
388 | # check if the file hasn't changed |
376 | # check if the file hasn't changed |
… | |
… | |
393 | |
381 | |
394 | # fall through to creation |
382 | # fall through to creation |
395 | } else { |
383 | } else { |
396 | Coro::Timer::sleep 1; |
384 | Coro::Timer::sleep 1; |
397 | |
385 | |
398 | $ns->send_drawinfo ( |
386 | send_log $ns, |
399 | "Wrong username or password. Please try again " |
387 | "Wrong username or password. Please try again " |
400 | . "(check for Numlock and other semi-obvious error sources).", |
388 | . "(check for Numlock and other semi-obvious error sources).", |
401 | cf::NDI_RED |
389 | cf::NDI_RED | cf::NDI_REPLY |
402 | ); |
390 | ; |
403 | next; |
391 | next; |
404 | } |
392 | } |
405 | } else { |
393 | } else { |
406 | # unable to load the playerfile: |
394 | # unable to load the playerfile: |
407 | # check whether the player dir exists, which means the file is corrupted or |
395 | # check whether the player dir exists, which means the file is corrupted or |
408 | # something very similar. |
396 | # something very similar. |
409 | if (!aio_stat cf::player::playerdir $user) { |
397 | if (!aio_stat cf::player::playerdir $user) { |
410 | $ns->send_drawinfo ( |
398 | send_log $ns, |
411 | "Unable to retrieve this player. It might be a locked or broken account. " |
399 | "Unable to retrieve this player. It might be a locked or broken account. " |
412 | . "If this is your account, ask a dungeon master for assistance. " |
400 | . "If this is your account, ask a dungeon master for assistance. " |
413 | . "Otherwise choose a different login name.", |
401 | . "Otherwise choose a different login name.", |
414 | cf::NDI_RED |
402 | cf::NDI_RED | cf::NDI_REPLY |
415 | ); |
403 | ; |
416 | next; |
404 | next; |
417 | } |
405 | } |
418 | } |
406 | } |
419 | |
407 | |
420 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
408 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
421 | |
409 | |
422 | if ($pass2 ne $pass) { |
410 | if ($pass2 ne $pass) { |
423 | $ns->send_drawinfo ( |
411 | send_log $ns, |
424 | "The passwords do not match, please try again.", |
412 | "The passwords do not match, please try again.", |
425 | cf::NDI_RED |
413 | cf::NDI_RED | cf::NDI_REPLY |
426 | ); |
414 | ; |
427 | Coro::Timer::sleep 0.5; |
415 | Coro::Timer::sleep 0.5; |
428 | next; |
416 | next; |
429 | } |
417 | } |
430 | |
418 | |
431 | last; |
419 | last; |
… | |
… | |
467 | or return $fail ( |
455 | or return $fail ( |
468 | "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), " |
456 | "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), " |
469 | . "or is not between 3 and 20 characters in length." |
457 | . "or is not between 3 and 20 characters in length." |
470 | ); |
458 | ); |
471 | |
459 | |
|
|
460 | $ns->{login_guard} = login_guard $user |
|
|
461 | or return $fail->("User name '$user' is in use - try another login name."); |
|
|
462 | |
472 | cf::player::find $user |
463 | cf::player::find $user |
473 | and return $fail->("User '$user' already exists - choose another login name."); |
464 | and return $fail->("User name '$user' is already registered - choose another login name."); |
|
|
465 | |
|
|
466 | $reply->(1, "Account Created"); |
474 | |
467 | |
475 | chargen $ns, $user, $pass; |
468 | chargen $ns, $user, $pass; |
476 | }); |
469 | }); |
477 | }; |
470 | }; |
478 | |
471 | |
… | |
… | |
480 | my ($ns, $reply, $user, $hash) = @_; |
473 | my ($ns, $reply, $user, $hash) = @_; |
481 | |
474 | |
482 | $ns->{addme}++ and return $ns->destroy; |
475 | $ns->{addme}++ and return $ns->destroy; |
483 | |
476 | |
484 | $ns->async (sub { |
477 | $ns->async (sub { |
485 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
486 | |
|
|
487 | $Coro::current->{desc} = "login($user) check"; |
478 | $Coro::current->{desc} = "login($user)"; |
488 | |
479 | |
489 | my $fail = sub { |
480 | my $fail = sub { |
490 | $reply->(0, $_[0]); |
481 | $reply->(0, $_[0]); |
491 | $ns->flush; # does not ensure that the data reaches the client - TODO |
482 | $ns->flush; # does not ensure that the data reaches the client - TODO |
492 | # need to do this in another thread, as this one gets canceled |
483 | # need to do this in another thread, as this one gets canceled |
… | |
… | |
494 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
485 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
495 | $ns->destroy if $ns->valid; |
486 | $ns->destroy if $ns->valid; |
496 | }; |
487 | }; |
497 | Coro::schedule; # do the destroy, should not return |
488 | Coro::schedule; # do the destroy, should not return |
498 | }; |
489 | }; |
|
|
490 | |
|
|
491 | $ns->{login_guard} = login_guard $user |
|
|
492 | or return $fail->("User '$user' is currently playing or logging in in another session. If that is your " |
|
|
493 | . "user name, make sure you are not running two clients. When in doubt, reboot."); |
499 | |
494 | |
500 | # try to read the user file and check the password |
495 | # try to read the user file and check the password |
501 | my $pl = cf::player::find $user |
496 | my $pl = cf::player::find $user |
502 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
497 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
503 | |
498 | |