… | |
… | |
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 | # HACK: remove when done |
|
|
15 | sub cf::client::send_drawinfo { |
|
|
16 | my ($self, $text, $flags) = @_; |
|
|
17 | |
|
|
18 | utf8::encode $text; |
|
|
19 | $self->send_packet (sprintf "msg %d log %s", $flags || cf::NDI_BLACK, $text); |
|
|
20 | } |
12 | |
21 | |
13 | sub query { |
22 | sub query { |
14 | my ($ns, $flags, $text) = @_; |
23 | my ($ns, $flags, $text) = @_; |
15 | |
24 | |
16 | $ns->query ($flags, $text, Coro::rouse_cb); |
25 | $ns->query ($flags, $text, Coro::rouse_cb); |
… | |
… | |
27 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
36 | || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 |
28 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
37 | || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 |
29 | || $age > 700 * 86400 # 2 years for everybody else |
38 | || $age > 700 * 86400 # 2 years for everybody else |
30 | } |
39 | } |
31 | |
40 | |
32 | sub check_playing { |
41 | # return a guard object for a lock on the given username, if available |
|
|
42 | sub login_guard { |
33 | my ($ns, $user) = @_; |
43 | my ($user) = @_; |
34 | |
44 | |
|
|
45 | exists $LOGIN_LOCK{$user} |
|
|
46 | and return undef; |
|
|
47 | |
35 | return unless cf::player::find_active $user; |
48 | cf::player::find_active $user |
|
|
49 | and return undef; |
36 | |
50 | |
37 | $ns->send_drawinfo ( |
51 | undef $LOGIN_LOCK{$user}; |
38 | "That player is already logged in on this server. " |
52 | 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 | } |
53 | } |
51 | |
54 | |
52 | sub safe_spot($) { |
55 | sub safe_spot($) { |
53 | my ($pl) = @_; |
56 | my ($pl) = @_; |
54 | |
57 | |
… | |
… | |
159 | # the rest of this function is character creation |
162 | # the rest of this function is character creation |
160 | |
163 | |
161 | my $ns = $pl->ns; |
164 | my $ns = $pl->ns; |
162 | my $ob = $pl->ob; |
165 | my $ob = $pl->ob; |
163 | |
166 | |
164 | $Coro::current->{desc} = "addme(" . $ob->name . ") login"; |
|
|
165 | |
|
|
166 | if ($pl->{chargen} eq "init") { |
167 | if ($pl->{chargen} eq "init") { |
167 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
168 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
168 | |
169 | |
169 | # create the playerdir, if necessary, as chargen_race_done did it before |
170 | # create the playerdir, if necessary, as chargen_race_done did it before |
170 | # presumably because of unique maps |
171 | # presumably because of unique maps |
… | |
… | |
258 | } |
259 | } |
259 | |
260 | |
260 | sub chargen { |
261 | sub chargen { |
261 | my ($ns, $user, $hash) = @_; |
262 | my ($ns, $user, $hash) = @_; |
262 | |
263 | |
263 | # lock again, too lazy to make this nicer |
|
|
264 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
265 | |
|
|
266 | # just to make sure nothing is left over |
264 | # just to make sure nothing is left over |
267 | # normally, nothing is there. |
265 | # normally, nothing is there. |
268 | nuke_playerdir $user; |
266 | nuke_playerdir $user; |
269 | |
267 | |
270 | my $pl = cf::player::new $user; |
268 | my $pl = cf::player::new $user; |
… | |
… | |
287 | my ($user, $pass); |
285 | my ($user, $pass); |
288 | |
286 | |
289 | $ns->send_packet ("addme_success"); |
287 | $ns->send_packet ("addme_success"); |
290 | |
288 | |
291 | for (;;) { |
289 | for (;;) { |
|
|
290 | delete $ns->{login_guard}; |
|
|
291 | |
292 | $ns->send_drawinfo ( |
292 | $ns->send_drawinfo ( |
293 | "Please enter your username now. If you are a new user, " |
293 | "Please enter your username now. If you are a new user, " |
294 | . "make one up that describes your character best. " |
294 | . "make one up that describes your character best. " |
295 | . "Only letters and digits are allowed, though.", |
295 | . "Only letters and digits are allowed, though.", |
296 | cf::NDI_BLUE |
296 | cf::NDI_BLUE |
… | |
… | |
298 | |
298 | |
299 | # read username |
299 | # read username |
300 | while () { |
300 | while () { |
301 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
301 | $user = query $ns, 0, "What is your name? (login names are case-sensitive)\n:"; |
302 | |
302 | |
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) { |
303 | if ($user =~ $VALID_LOGIN) { |
310 | last; |
304 | last; |
311 | } else { |
305 | } else { |
312 | $ns->send_drawinfo ( |
306 | $ns->send_drawinfo ( |
313 | "Your username contains illegal characters " |
307 | "Your username contains illegal characters " |
314 | . "(only a-z, A-Z and 0-9 are allowed), " |
308 | . "(only a-z, A-Z and 0-9 are allowed), " |
… | |
… | |
317 | ); |
311 | ); |
318 | } |
312 | } |
319 | Coro::Timer::sleep 0.4; |
313 | Coro::Timer::sleep 0.4; |
320 | } |
314 | } |
321 | |
315 | |
322 | check_playing $ns, $user and next; |
|
|
323 | |
|
|
324 | $Coro::current->{desc} = "addme($user) pass"; |
316 | $Coro::current->{desc} = "addme($user)"; |
325 | |
317 | |
326 | $ns->send_drawinfo ( |
318 | $ns->send_drawinfo ( |
327 | "Welcome $user, please enter your password now. " |
319 | "Welcome $user, please enter your password now. " |
328 | . "New users should now choose a password. " |
320 | . "New users should now choose a password. " |
329 | . "Anything your client lets you enter is fine.", |
321 | . "Anything your client lets you enter is fine.", |
… | |
… | |
340 | cf::NDI_RED |
332 | cf::NDI_RED |
341 | ); |
333 | ); |
342 | Coro::Timer::sleep 0.4; |
334 | Coro::Timer::sleep 0.4; |
343 | } |
335 | } |
344 | |
336 | |
345 | # lock this username for the remainder of this login session |
337 | $ns->{login_guard} = login_guard $user |
346 | if ($cf::LOGIN_LOCK{$user}) { |
338 | or do { |
347 | $ns->send_drawinfo ( |
339 | $ns->send_drawinfo ( |
348 | "That username is currently used in another login session. " |
340 | "That user is already logged in (or is logging in)." |
349 | . "Chose another, or wait till the other session has ended.", |
341 | . "Chose another, or wait till the other session has ended.", |
350 | cf::NDI_RED |
342 | cf::NDI_RED |
|
|
343 | ); |
|
|
344 | next; |
351 | ); |
345 | }; |
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 | |
346 | |
360 | # try to read the user file and check the password |
347 | # try to read the user file and check the password |
361 | if (my $pl = cf::player::find $user) { |
348 | if (my $pl = cf::player::find $user) { |
362 | aio_stat $pl->path and next; |
349 | aio_stat $pl->path and next; |
363 | my $mtime = (stat _)[9]; |
350 | my $mtime = (stat _)[9]; |
… | |
… | |
467 | or return $fail ( |
454 | or return $fail ( |
468 | "Your username contains illegal characters (only a-z, A-Z and 0-9 are allowed), " |
455 | "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." |
456 | . "or is not between 3 and 20 characters in length." |
470 | ); |
457 | ); |
471 | |
458 | |
|
|
459 | $ns->{login_guard} = login_guard $user |
|
|
460 | or return $fail->("User name '$user' is in use - try another login name."); |
|
|
461 | |
472 | cf::player::find $user |
462 | cf::player::find $user |
473 | and return $fail->("User '$user' already exists - choose another login name."); |
463 | and return $fail->("User name '$user' is already registered - choose another login name."); |
474 | |
464 | |
475 | $reply->(1, "Account Created"); |
465 | $reply->(1, "Account Created"); |
476 | |
466 | |
477 | chargen $ns, $user, $pass; |
467 | chargen $ns, $user, $pass; |
478 | }); |
468 | }); |
… | |
… | |
482 | my ($ns, $reply, $user, $hash) = @_; |
472 | my ($ns, $reply, $user, $hash) = @_; |
483 | |
473 | |
484 | $ns->{addme}++ and return $ns->destroy; |
474 | $ns->{addme}++ and return $ns->destroy; |
485 | |
475 | |
486 | $ns->async (sub { |
476 | $ns->async (sub { |
487 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
488 | |
|
|
489 | $Coro::current->{desc} = "login($user) check"; |
477 | $Coro::current->{desc} = "login($user)"; |
490 | |
478 | |
491 | my $fail = sub { |
479 | my $fail = sub { |
492 | $reply->(0, $_[0]); |
480 | $reply->(0, $_[0]); |
493 | $ns->flush; # does not ensure that the data reaches the client - TODO |
481 | $ns->flush; # does not ensure that the data reaches the client - TODO |
494 | # need to do this in another thread, as this one gets canceled |
482 | # need to do this in another thread, as this one gets canceled |
… | |
… | |
496 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
484 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
497 | $ns->destroy if $ns->valid; |
485 | $ns->destroy if $ns->valid; |
498 | }; |
486 | }; |
499 | Coro::schedule; # do the destroy, should not return |
487 | Coro::schedule; # do the destroy, should not return |
500 | }; |
488 | }; |
|
|
489 | |
|
|
490 | $ns->{login_guard} = login_guard $user |
|
|
491 | or return $fail->("User '$user' is currently playing or logging in in another session. If that is your " |
|
|
492 | . "user name, make sure you are not running two clients. When in doubt, reboot."); |
501 | |
493 | |
502 | # try to read the user file and check the password |
494 | # try to read the user file and check the password |
503 | my $pl = cf::player::find $user |
495 | my $pl = cf::player::find $user |
504 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
496 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
505 | |
497 | |