… | |
… | |
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; |
12 | |
13 | |
13 | sub query { |
14 | sub query { |
14 | my ($ns, $flags, $text) = @_; |
15 | my ($ns, $flags, $text) = @_; |
15 | |
16 | |
16 | $ns->query ($flags, $text, Coro::rouse_cb); |
17 | $ns->query ($flags, $text, Coro::rouse_cb); |
… | |
… | |
27 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
28 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
28 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
29 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
29 | || $age > 700 * 86400 # 2 years for everybody else |
30 | || $age > 700 * 86400 # 2 years for everybody else |
30 | } |
31 | } |
31 | |
32 | |
32 | sub check_playing { |
33 | # return a guard object for a lock on the given username, if available |
|
|
34 | sub login_guard { |
33 | my ($ns, $user) = @_; |
35 | my ($user) = @_; |
34 | |
36 | |
|
|
37 | exists $LOGIN_LOCK{$user} |
|
|
38 | and return undef; |
|
|
39 | |
35 | return unless cf::player::find_active $user; |
40 | cf::player::find_active $user |
|
|
41 | and return undef; |
36 | |
42 | |
37 | $ns->send_drawinfo ( |
43 | undef $LOGIN_LOCK{$user}; |
38 | "That player is already logged in on this server. " |
44 | 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 | } |
45 | } |
51 | |
46 | |
52 | sub safe_spot($) { |
47 | sub safe_spot($) { |
53 | my ($pl) = @_; |
48 | my ($pl) = @_; |
54 | |
49 | |
… | |
… | |
92 | $ob->message ( |
87 | $ob->message ( |
93 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
88 | "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. " |
89 | . "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... " |
90 | . "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.>", |
91 | . "H<You disconnected too long without having used a savebed.>", |
97 | cf::NDI_RED |
92 | cf::NDI_RED | cf::NDI_REPLY |
98 | ); |
93 | ); |
99 | # kill them. |
94 | # kill them. |
100 | # reminds me of the famous badness 10000 syndrome... |
95 | # reminds me of the famous badness 10000 syndrome... |
101 | $ob->stats->hp (-10000); #] if they survive this they deserved to live |
96 | $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; |
97 | my $killer = cf::arch::get "killer_login"; $pl->killer ($killer); $killer->destroy; |
… | |
… | |
106 | $ob->message ( |
101 | $ob->message ( |
107 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
102 | "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. " |
103 | . "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... " |
104 | . "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.>", |
105 | . "H<You disconnected without having used a savebed. When you do that for too long, you might die.>", |
111 | cf::NDI_RED |
106 | cf::NDI_RED | cf::NDI_REPLY |
112 | ); |
107 | ); |
113 | } |
108 | } |
114 | } else { |
109 | } else { |
115 | $ob->message ( |
110 | $ob->message ( |
116 | "You didn't use a bed to reality to leave this realm. This is very dangerous, " |
111 | "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, " |
112 | . "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. " |
113 | . "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.>", |
114 | . "H<Always apply a bed of reality to disconnect from the server.>", |
120 | cf::NDI_RED |
115 | cf::NDI_RED | cf::NDI_REPLY |
121 | ); |
116 | ); |
122 | } |
117 | } |
123 | } |
118 | } |
124 | |
119 | |
125 | $ob->goto ($map, $x, $y); |
120 | $ob->goto ($map, $x, $y); |
126 | } |
121 | } |
127 | |
122 | |
128 | sub encode_password($) { |
123 | sub encode_password($) { |
129 | "!" . unpack "H*", $_[0] |
124 | unpack "H*", Deliantra::Util::hash_pw $_[0] |
130 | } |
125 | } |
131 | |
126 | |
132 | sub compare_password($$) { |
127 | sub compare_password($$) { |
133 | my ($pass, $token) = @_; |
128 | my ($pass, $token) = @_; |
134 | |
129 | |
… | |
… | |
159 | # the rest of this function is character creation |
154 | # the rest of this function is character creation |
160 | |
155 | |
161 | my $ns = $pl->ns; |
156 | my $ns = $pl->ns; |
162 | my $ob = $pl->ob; |
157 | my $ob = $pl->ob; |
163 | |
158 | |
164 | $Coro::current->{desc} = "addme(" . $ob->name . ") login"; |
|
|
165 | |
|
|
166 | delete $pl->{deny_save}; # set by new |
|
|
167 | |
|
|
168 | if ($pl->{chargen} eq "init") { |
159 | if ($pl->{chargen} eq "init") { |
|
|
160 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
|
|
161 | |
169 | # create the playerdir, if necessary, as chargen_race_done did it before |
162 | # create the playerdir, if necessary, as chargen_race_done did it before |
170 | # presumably because of unique maps |
163 | # presumably because of unique maps |
171 | aio_mkdir playerdir $pl, 0770; |
164 | aio_mkdir playerdir $pl, 0770; |
|
|
165 | delete $pl->{deny_save}; # set by new |
172 | $pl->save; |
166 | $pl->save; |
173 | |
|
|
174 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
|
|
175 | |
167 | |
176 | $pl->{chargen} = "stats"; |
168 | $pl->{chargen} = "stats"; |
177 | } |
169 | } |
178 | |
170 | |
179 | if ($pl->{chargen} eq "stats") { |
171 | if ($pl->{chargen} eq "stats") { |
… | |
… | |
257 | $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
249 | $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
258 | } |
250 | } |
259 | } |
251 | } |
260 | |
252 | |
261 | sub chargen { |
253 | sub chargen { |
262 | my ($ns, $user, $hasah) = @_; |
254 | my ($ns, $user, $hash) = @_; |
263 | |
|
|
264 | # lock again, too lazy to make this nicer |
|
|
265 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
266 | |
255 | |
267 | # just to make sure nothing is left over |
256 | # just to make sure nothing is left over |
268 | # normally, nothing is there. |
257 | # normally, nothing is there. |
269 | nuke_playerdir $user; |
258 | nuke_playerdir $user; |
270 | |
259 | |
271 | my $pl = cf::player::new $user; |
260 | my $pl = cf::player::new $user; |
272 | $pl->password (encode_password $pass); |
261 | $pl->password (unpack "H*", $hash); |
273 | $pl->connect ($ns); |
262 | $pl->connect ($ns); |
274 | |
263 | |
275 | $pl->{chargen} = "init"; |
264 | $pl->{chargen} = "init"; |
276 | |
265 | |
277 | login $pl; |
266 | login $pl; |
… | |
… | |
288 | my ($user, $pass); |
277 | my ($user, $pass); |
289 | |
278 | |
290 | $ns->send_packet ("addme_success"); |
279 | $ns->send_packet ("addme_success"); |
291 | |
280 | |
292 | for (;;) { |
281 | for (;;) { |
293 | $ns->send_drawinfo ( |
282 | delete $ns->{login_guard}; |
|
|
283 | |
|
|
284 | $ns->send_msg (log => |
294 | "Please enter your username now. If you are a new user, " |
285 | "Please enter your username now. If you are a new user, " |
295 | . "make one up that describes your character best. " |
286 | . "make one up that describes your character best. " |
296 | . "Only letters and digits are allowed, though.", |
287 | . "Only letters and digits are allowed, though.", |
297 | cf::NDI_BLUE |
288 | cf::NDI_BLUE | cf::NDI_REPLY |
298 | ); |
289 | ); |
299 | |
290 | |
300 | # read username |
291 | # read username |
301 | while () { |
292 | while () { |
302 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
293 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
303 | |
294 | |
304 | if ($cf::LOGIN_LOCK{$user}) { |
|
|
305 | $ns->send_drawinfo ( |
|
|
306 | "That username is currently used in another login session. " |
|
|
307 | . "Chose another, or wait till the other session has ended.", |
|
|
308 | cf::NDI_RED |
|
|
309 | ); |
|
|
310 | } elsif ($user =~ $VALID_LOGIN) { |
295 | if ($user =~ $VALID_LOGIN) { |
311 | last; |
296 | last; |
312 | } else { |
297 | } else { |
313 | $ns->send_drawinfo ( |
298 | $ns->send_msg (log => |
314 | "Your username contains illegal characters " |
299 | "Your username contains illegal characters " |
315 | . "(only a-z, A-Z and 0-9 are allowed), " |
300 | . "(only a-z, A-Z and 0-9 are allowed), " |
316 | . "or is not between 3 and 20 characters in length.", |
301 | . "or is not between 3 and 20 characters in length.", |
317 | cf::NDI_RED |
302 | cf::NDI_RED | cf::NDI_REPLY |
318 | ); |
303 | ); |
319 | } |
304 | } |
320 | Coro::Timer::sleep 0.4; |
305 | Coro::Timer::sleep 0.4; |
321 | } |
306 | } |
322 | |
307 | |
323 | check_playing $ns, $user and next; |
|
|
324 | |
|
|
325 | $Coro::current->{desc} = "addme($user) pass"; |
308 | $Coro::current->{desc} = "addme($user)"; |
326 | |
309 | |
327 | $ns->send_drawinfo ( |
310 | $ns->send_msg (log => |
328 | "Welcome $user, please enter your password now. " |
311 | "Welcome $user, please enter your password now. " |
329 | . "New users should now choose a password. " |
312 | . "New users should now choose a password. " |
330 | . "Anything your client lets you enter is fine.", |
313 | . "Anything your client lets you enter is fine.", |
331 | cf::NDI_BLUE |
314 | cf::NDI_BLUE | cf::NDI_REPLY |
332 | ); |
315 | ); |
333 | |
316 | |
334 | # read password |
317 | # read password |
335 | while () { |
318 | while () { |
336 | $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; |
319 | $pass = query $ns, cf::CS_QUERY_HIDEINPUT, "What is your password?\n:"; |
337 | last if $pass =~ /.../; |
320 | last if $pass =~ /.../; |
338 | $ns->send_drawinfo ( |
321 | $ns->send_msg (log => |
339 | "Try to use at least three characters as your password please, " |
322 | "Try to use at least three characters as your password please, " |
340 | . "that cannot be too much to ask for :)", |
323 | . "that cannot be too much to ask for :)", |
341 | cf::NDI_RED |
324 | cf::NDI_RED | cf::NDI_REPLY |
342 | ); |
325 | ); |
343 | Coro::Timer::sleep 0.4; |
326 | Coro::Timer::sleep 0.4; |
344 | } |
327 | } |
345 | |
328 | |
346 | # lock this username for the remainder of this login session |
329 | $ns->{login_guard} = login_guard $user |
347 | if ($cf::LOGIN_LOCK{$user}) { |
330 | or do { |
348 | $ns->send_drawinfo ( |
331 | $ns->send_msg (log => |
349 | "That username is currently used in another login session. " |
332 | "That user is already logged in (or is logging in)." |
350 | . "Chose another, or wait till the other session has ended.", |
333 | . "Chose another, or wait till the other session has ended.", |
351 | cf::NDI_RED |
334 | cf::NDI_RED | cf::NDI_REPLY |
|
|
335 | ); |
|
|
336 | next; |
352 | ); |
337 | }; |
353 | next; |
|
|
354 | } |
|
|
355 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
356 | |
|
|
357 | check_playing $ns, $user and next; |
|
|
358 | |
|
|
359 | $Coro::current->{desc} = "addme($user) check"; |
|
|
360 | |
338 | |
361 | # try to read the user file and check the password |
339 | # try to read the user file and check the password |
362 | if (my $pl = cf::player::find $user) { |
340 | if (my $pl = cf::player::find $user) { |
363 | aio_stat $pl->path and next; |
341 | aio_stat $pl->path and next; |
364 | my $mtime = (stat _)[9]; |
342 | my $mtime = (stat _)[9]; |
… | |
… | |
374 | login $pl; |
352 | login $pl; |
375 | return; |
353 | return; |
376 | } elsif (can_cleanup $pl, $mtime) { |
354 | } elsif (can_cleanup $pl, $mtime) { |
377 | Coro::Timer::sleep 1; |
355 | Coro::Timer::sleep 1; |
378 | |
356 | |
379 | $ns->send_drawinfo ( |
357 | $ns->send_msg (log => |
380 | "Player exists, but password does not match. If this is your account, " |
358 | "Player exists, but password does not match. If this is your account, " |
381 | . "please try again. If not, you can now decide to take over this account " |
359 | . "please try again. If not, you can now decide to take over this account " |
382 | . "because it has not been in-use for some time.", |
360 | . "because it has not been in-use for some time.", |
383 | cf::NDI_RED |
361 | cf::NDI_RED | cf::NDI_REPLY |
384 | ); |
362 | ); |
385 | |
363 | |
386 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
364 | (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ |
387 | or next; |
365 | or next; |
388 | |
366 | |
… | |
… | |
394 | |
372 | |
395 | # fall through to creation |
373 | # fall through to creation |
396 | } else { |
374 | } else { |
397 | Coro::Timer::sleep 1; |
375 | Coro::Timer::sleep 1; |
398 | |
376 | |
399 | $ns->send_drawinfo ( |
377 | $ns->send_msg (log => |
400 | "Wrong username or password. Please try again " |
378 | "Wrong username or password. Please try again " |
401 | . "(check for Numlock and other semi-obvious error sources).", |
379 | . "(check for Numlock and other semi-obvious error sources).", |
402 | cf::NDI_RED |
380 | cf::NDI_RED | cf::NDI_REPLY |
403 | ); |
381 | ); |
404 | next; |
382 | next; |
405 | } |
383 | } |
406 | } else { |
384 | } else { |
407 | # unable to load the playerfile: |
385 | # unable to load the playerfile: |
408 | # check whether the player dir exists, which means the file is corrupted or |
386 | # check whether the player dir exists, which means the file is corrupted or |
409 | # something very similar. |
387 | # something very similar. |
410 | if (!aio_stat cf::player::playerdir $user) { |
388 | if (!aio_stat cf::player::playerdir $user) { |
411 | $ns->send_drawinfo ( |
389 | $ns->send_msg ( log => |
412 | "Unable to retrieve this player. It might be a locked or broken account. " |
390 | "Unable to retrieve this player. It might be a locked or broken account. " |
413 | . "If this is your account, ask a dungeon master for assistance. " |
391 | . "If this is your account, ask a dungeon master for assistance. " |
414 | . "Otherwise choose a different login name.", |
392 | . "Otherwise choose a different login name.", |
415 | cf::NDI_RED |
393 | cf::NDI_RED | cf::NDI_REPLY |
416 | ); |
394 | ); |
417 | next; |
395 | next; |
418 | } |
396 | } |
419 | } |
397 | } |
420 | |
398 | |
421 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
399 | my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again."; |
422 | |
400 | |
423 | if ($pass2 ne $pass) { |
401 | if ($pass2 ne $pass) { |
424 | $ns->send_drawinfo ( |
402 | $ns->send_msg (log => |
425 | "The passwords do not match, please try again.", |
403 | "The passwords do not match, please try again.", |
426 | cf::NDI_RED |
404 | cf::NDI_RED | cf::NDI_REPLY |
427 | ); |
405 | ); |
428 | Coro::Timer::sleep 0.5; |
406 | Coro::Timer::sleep 0.5; |
429 | next; |
407 | next; |
430 | } |
408 | } |
431 | |
409 | |
… | |
… | |
457 | $reply->(0, $_[0]); |
435 | $reply->(0, $_[0]); |
458 | $ns->flush; # does not ensure that the data reaches the client - TODO |
436 | $ns->flush; # does not ensure that the data reaches the client - TODO |
459 | # need to do this in another thread, as this one gets canceled |
437 | # need to do this in another thread, as this one gets canceled |
460 | Coro::async_pool { |
438 | Coro::async_pool { |
461 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
439 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
462 | $ns->destroy; |
440 | $ns->destroy if $ns->valid; |
463 | }; |
441 | }; |
464 | Coro::schedule; # do the destroy, should not return |
442 | Coro::schedule; # do the destroy, should not return |
465 | }; |
443 | }; |
466 | |
444 | |
467 | $user =~ $VALID_LOGIN |
445 | $user =~ $VALID_LOGIN |
468 | or return $fail ( |
446 | or return $fail ( |
469 | "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), " |
447 | "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), " |
470 | . "or is not between 3 and 20 characters in length." |
448 | . "or is not between 3 and 20 characters in length." |
471 | ); |
449 | ); |
472 | |
450 | |
|
|
451 | $ns->{login_guard} = login_guard $user |
|
|
452 | or return $fail->("User name '$user' is in use - try another login name."); |
|
|
453 | |
473 | cf::player::find $user |
454 | cf::player::find $user |
474 | and return $fail->("User '$user' already exists - choose another login name."); |
455 | and return $fail->("User name '$user' is already registered - choose another login name."); |
475 | |
456 | |
476 | chargen $ns, $user, Deliantra::Util::hash_pw $pass; |
457 | $reply->(1, "Account Created"); |
|
|
458 | |
|
|
459 | chargen $ns, $user, $pass; |
477 | }); |
460 | }); |
478 | }; |
461 | }; |
479 | |
462 | |
480 | cf::register_async_exticmd login => sub { |
463 | cf::register_async_exticmd login => sub { |
481 | my ($ns, $reply, $user, $hash) = @_; |
464 | my ($ns, $reply, $user, $hash) = @_; |
482 | |
465 | |
483 | $ns->{addme}++ and return $ns->destroy; |
466 | $ns->{addme}++ and return $ns->destroy; |
484 | |
467 | |
485 | $ns->async (sub { |
468 | $ns->async (sub { |
486 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
487 | |
|
|
488 | $Coro::current->{desc} = "login($user) check"; |
469 | $Coro::current->{desc} = "login($user)"; |
489 | |
470 | |
490 | my $fail = sub { |
471 | my $fail = sub { |
491 | $reply->(0, $_[0]); |
472 | $reply->(0, $_[0]); |
492 | $ns->flush; # does not ensure that the data reaches the client - TODO |
473 | $ns->flush; # does not ensure that the data reaches the client - TODO |
493 | # need to do this in another thread, as this one gets canceled |
474 | # need to do this in another thread, as this one gets canceled |
494 | Coro::async_pool { |
475 | Coro::async_pool { |
495 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
476 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
496 | $ns->destroy; |
477 | $ns->destroy if $ns->valid; |
497 | }; |
478 | }; |
498 | Coro::schedule; # do the destroy, should not return |
479 | Coro::schedule; # do the destroy, should not return |
499 | }; |
480 | }; |
|
|
481 | |
|
|
482 | $ns->{login_guard} = login_guard $user |
|
|
483 | or return $fail->("User '$user' is currently playing or logging in in another session. If that is your " |
|
|
484 | . "user name, make sure you are not running two clients. When in doubt, reboot."); |
500 | |
485 | |
501 | # try to read the user file and check the password |
486 | # try to read the user file and check the password |
502 | my $pl = cf::player::find $user |
487 | my $pl = cf::player::find $user |
503 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
488 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
504 | |
489 | |