… | |
… | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
7 | use Deliantra::Util (); |
7 | use Deliantra::Util (); |
8 | |
8 | |
9 | CONF MAX_DISCONNECT_TIME = 3600; |
9 | CONF MAX_DISCONNECT_TIME = 3600; |
|
|
10 | |
|
|
11 | our $VALID_LOGIN = qr<^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z>; |
10 | |
12 | |
11 | sub query { |
13 | sub query { |
12 | my ($ns, $flags, $text) = @_; |
14 | my ($ns, $flags, $text) = @_; |
13 | |
15 | |
14 | $ns->query ($flags, $text, Coro::rouse_cb); |
16 | $ns->query ($flags, $text, Coro::rouse_cb); |
… | |
… | |
122 | |
124 | |
123 | $ob->goto ($map, $x, $y); |
125 | $ob->goto ($map, $x, $y); |
124 | } |
126 | } |
125 | |
127 | |
126 | sub encode_password($) { |
128 | sub encode_password($) { |
127 | "!" . unpack "H*", $_[0] |
129 | unpack "H*", Deliantra::Util::hash_pw $_[0] |
128 | } |
130 | } |
129 | |
131 | |
130 | sub compare_password($$) { |
132 | sub compare_password($$) { |
131 | my ($pass, $token) = @_; |
133 | my ($pass, $token) = @_; |
132 | |
134 | |
… | |
… | |
148 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
150 | my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; |
149 | aio_rename "$PLAYERDIR/$user", $temp; |
151 | aio_rename "$PLAYERDIR/$user", $temp; |
150 | IO::AIO::aio_rmtree $temp; |
152 | IO::AIO::aio_rmtree $temp; |
151 | } |
153 | } |
152 | |
154 | |
153 | sub login_done { |
155 | sub login { |
154 | my ($pl) = @_; |
156 | my ($pl) = @_; |
|
|
157 | |
|
|
158 | # handle character creation, if neccessary |
|
|
159 | # the rest of this function is character creation |
|
|
160 | |
|
|
161 | my $ns = $pl->ns; |
|
|
162 | my $ob = $pl->ob; |
|
|
163 | |
|
|
164 | $Coro::current->{desc} = "addme(" . $ob->name . ") login"; |
|
|
165 | |
|
|
166 | if ($pl->{chargen} eq "init") { |
|
|
167 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
|
|
168 | |
|
|
169 | # create the playerdir, if necessary, as chargen_race_done did it before |
|
|
170 | # presumably because of unique maps |
|
|
171 | aio_mkdir playerdir $pl, 0770; |
|
|
172 | delete $pl->{deny_save}; # set by new |
|
|
173 | $pl->save; |
|
|
174 | |
|
|
175 | $pl->{chargen} = "stats"; |
|
|
176 | } |
|
|
177 | |
|
|
178 | if ($pl->{chargen} eq "stats") { |
|
|
179 | while () { |
|
|
180 | $ob->update_stats; |
|
|
181 | $pl->save_stats; |
|
|
182 | |
|
|
183 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
184 | "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?"; |
|
|
185 | |
|
|
186 | if ($res =~ /^[Nn]/) { |
|
|
187 | last; |
|
|
188 | } elsif ($res > 0 && $res <= 7) { |
|
|
189 | my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?"; |
|
|
190 | |
|
|
191 | if ($swap > 0 && $swap <= 7) { |
|
|
192 | $ob->swap_stats ($res - 1, $swap - 1); |
|
|
193 | } |
|
|
194 | } else { |
|
|
195 | $ob->roll_stats; |
|
|
196 | } |
|
|
197 | |
|
|
198 | Coro::Timer::sleep 0.05; |
|
|
199 | } |
|
|
200 | |
|
|
201 | $ob->set_animation (2); |
|
|
202 | $ob->add_statbonus; |
|
|
203 | |
|
|
204 | $pl->{chargen} = "race"; |
|
|
205 | } |
|
|
206 | |
|
|
207 | if ($pl->{chargen} eq "race") { |
|
|
208 | while () { |
|
|
209 | $ns->send_msg ("chargen-race-title", ucfirst $pl->title, -1); |
|
|
210 | my $msg = $ob->msg; |
|
|
211 | $msg =~ s/(?<=\S)\n(?=\S)/ /g; |
|
|
212 | $ns->send_msg ("chargen-race-description", $msg, cf::NDI_BLUE); |
|
|
213 | |
|
|
214 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
215 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; |
|
|
216 | |
|
|
217 | last if $res =~ /[dD]/; |
|
|
218 | |
|
|
219 | $pl->chargen_race_next; |
|
|
220 | Coro::Timer::sleep 0.05; |
|
|
221 | } |
|
|
222 | |
|
|
223 | $pl->chargen_race_done; |
|
|
224 | $pl->{chargen} = "gender"; |
|
|
225 | } |
|
|
226 | |
|
|
227 | if ($pl->{chargen} eq "race") { |
|
|
228 | while () { |
|
|
229 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
230 | "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; |
|
|
231 | |
|
|
232 | if ($res =~ /^[fF]/) { |
|
|
233 | $pl->gender (1); |
|
|
234 | last; |
|
|
235 | } elsif ($res =~ /^[mM]/) { |
|
|
236 | $pl->gender (0); |
|
|
237 | last; |
|
|
238 | } |
|
|
239 | Coro::Timer::sleep 0.05; |
|
|
240 | } |
|
|
241 | $pl->{chargen} = "done"; |
|
|
242 | } |
|
|
243 | |
|
|
244 | $ns->state (cf::ST_PLAYING); |
|
|
245 | |
|
|
246 | if ($pl->{chargen} eq "done") { |
|
|
247 | # XXX: Workaround for delayed client ext protocol handshake |
|
|
248 | $pl->esrv_new_player; |
|
|
249 | |
|
|
250 | $pl->{chargen} = "done"; |
|
|
251 | } |
|
|
252 | |
|
|
253 | $ob->reply (undef, "Welcome to Deliantra!"); |
155 | |
254 | |
156 | if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { |
255 | if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { |
157 | $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
256 | $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); |
158 | } |
257 | } |
159 | } |
258 | } |
160 | |
259 | |
161 | sub chargen { |
260 | sub chargen { |
162 | my ($ns, $user, $pass) = @_; |
261 | my ($ns, $user, $hash) = @_; |
163 | |
262 | |
164 | # the rest of this function is character creation |
263 | # lock again, too lazy to make this nicer |
165 | $Coro::current->{desc} = "addme($user) chargen"; |
264 | local $cf::LOGIN_LOCK{$user} = 1; |
166 | |
265 | |
167 | # just to make sure nothing is left over |
266 | # just to make sure nothing is left over |
168 | # normally, nothing is there. |
267 | # normally, nothing is there. |
169 | nuke_playerdir $user; |
268 | nuke_playerdir $user; |
170 | |
269 | |
171 | my $pl = cf::player::new $user; |
270 | my $pl = cf::player::new $user; |
172 | $pl->password (encode_password $pass); |
271 | $pl->password (unpack "H*", $hash); |
173 | $pl->connect ($ns); |
272 | $pl->connect ($ns); |
174 | my $ob = $pl->ob; |
|
|
175 | |
273 | |
176 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
274 | $pl->{chargen} = "init"; |
177 | |
275 | |
178 | while () { |
276 | login $pl; |
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}; |
|
|
243 | } |
277 | } |
244 | |
278 | |
245 | cf::client->attach (on_addme => sub { |
279 | cf::client->attach (on_addme => sub { |
246 | my ($ns) = @_; |
280 | my ($ns) = @_; |
247 | |
281 | |
248 | $ns->pl and return $ns->destroy; |
282 | $ns->{addme}++ and return $ns->destroy; |
249 | |
283 | |
250 | $ns->async (sub { |
284 | $ns->async (sub { |
251 | $Coro::current->{desc} = "addme init"; |
285 | $Coro::current->{desc} = "addme init"; |
252 | |
286 | |
253 | my ($user, $pass); |
287 | my ($user, $pass); |
… | |
… | |
270 | $ns->send_drawinfo ( |
304 | $ns->send_drawinfo ( |
271 | "That username is currently used in another login session. " |
305 | "That username is currently used in another login session. " |
272 | . "Chose another, or wait till the other session has ended.", |
306 | . "Chose another, or wait till the other session has ended.", |
273 | cf::NDI_RED |
307 | cf::NDI_RED |
274 | ); |
308 | ); |
275 | } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,19}\z/) { |
309 | } elsif ($user =~ $VALID_LOGIN) { |
276 | last; |
310 | last; |
277 | } else { |
311 | } else { |
278 | $ns->send_drawinfo ( |
312 | $ns->send_drawinfo ( |
279 | "Your username contains illegal characters " |
313 | "Your username contains illegal characters " |
280 | . "(only a-z, A-Z and 0-9 are allowed), " |
314 | . "(only a-z, A-Z and 0-9 are allowed), " |
… | |
… | |
334 | |
368 | |
335 | # password matches, wonderful |
369 | # password matches, wonderful |
336 | my $pl = cf::player::find $user or next; |
370 | my $pl = cf::player::find $user or next; |
337 | $pl->connect ($ns); |
371 | $pl->connect ($ns); |
338 | enter_map $pl; |
372 | enter_map $pl; |
339 | login_done $pl; |
373 | login $pl; |
340 | return; |
374 | return; |
341 | } elsif (can_cleanup $pl, $mtime) { |
375 | } elsif (can_cleanup $pl, $mtime) { |
342 | Coro::Timer::sleep 1; |
376 | Coro::Timer::sleep 1; |
343 | |
377 | |
344 | $ns->send_drawinfo ( |
378 | $ns->send_drawinfo ( |
… | |
… | |
395 | } |
429 | } |
396 | |
430 | |
397 | last; |
431 | last; |
398 | } |
432 | } |
399 | |
433 | |
400 | # lock again, too layz to make this nicer |
434 | chargen $ns, $user, Deliantra::Util::hash_pw $pass; |
401 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
402 | |
|
|
403 | chargen $ns, $user, $pass; |
|
|
404 | login_done $ns->pl; |
|
|
405 | }); |
435 | }); |
406 | }); |
436 | }); |
407 | |
437 | |
408 | cf::client->attach ( |
438 | cf::client->attach ( |
409 | on_version => sub { |
439 | on_version => sub { |
410 | my ($ns, $arg) = @_; |
440 | my ($ns, $arg) = @_; |
411 | |
441 | |
|
|
442 | # perl probably uses lrand48, which is not secure at all |
|
|
443 | # maybe require linux and use /dev/urandom. |
412 | $ns->ext_msg (nonces => map { join "", map { chr rand 256 } 0..63 } 1..2); |
444 | $ns->{nonces} = [map { join "", map { chr rand 256 } 0..63 } 1..2]; |
|
|
445 | $ns->ext_msg (nonces => @{ $ns->{nonces} }); |
413 | }, |
446 | }, |
414 | ); |
447 | ); |
415 | |
448 | |
416 | #cf::register_async_exticmd create_login => sub { |
449 | cf::register_async_exticmd create_login => sub { |
417 | # my ($ns, $reply, $user, $pass) = @_; |
450 | my ($ns, $reply, $user, $pass) = @_; |
418 | # |
451 | |
419 | # $ns->pl and return $ns->destroy; |
452 | $ns->{addme}++ and return $ns->destroy; |
|
|
453 | |
|
|
454 | $ns->async (sub { |
|
|
455 | my $fail = sub { |
|
|
456 | $reply->(0, $_[0]); |
|
|
457 | $ns->flush; # does not ensure that the data reaches the client - TODO |
|
|
458 | # need to do this in another thread, as this one gets canceled |
|
|
459 | Coro::async_pool { |
|
|
460 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
|
|
461 | $ns->destroy if $ns->valid; |
|
|
462 | }; |
|
|
463 | Coro::schedule; # do the destroy, should not return |
|
|
464 | }; |
|
|
465 | |
|
|
466 | $user =~ $VALID_LOGIN |
|
|
467 | or return $fail ( |
|
|
468 | "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." |
|
|
470 | ); |
|
|
471 | |
|
|
472 | cf::player::find $user |
|
|
473 | and return $fail->("User '$user' already exists - choose another login name."); |
|
|
474 | |
|
|
475 | chargen $ns, $user, $pass; |
|
|
476 | }); |
420 | #}; |
477 | }; |
421 | |
478 | |
422 | cf::register_async_exticmd login => sub { |
479 | cf::register_async_exticmd login => sub { |
423 | my ($ns, $reply, $user, $hash) = @_; |
480 | my ($ns, $reply, $user, $hash) = @_; |
424 | |
481 | |
425 | $ns->pl and return $ns->destroy; |
482 | $ns->{addme}++ and return $ns->destroy; |
|
|
483 | |
|
|
484 | $ns->async (sub { |
|
|
485 | local $cf::LOGIN_LOCK{$user} = 1; |
|
|
486 | |
|
|
487 | $Coro::current->{desc} = "login($user) check"; |
|
|
488 | |
|
|
489 | my $fail = sub { |
|
|
490 | $reply->(0, $_[0]); |
|
|
491 | $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 |
|
|
493 | Coro::async_pool { |
|
|
494 | Coro::AnyEvent::sleep 0.1; # TODO, see above, extra hack |
|
|
495 | $ns->destroy if $ns->valid; |
|
|
496 | }; |
|
|
497 | Coro::schedule; # do the destroy, should not return |
|
|
498 | }; |
|
|
499 | |
|
|
500 | # try to read the user file and check the password |
|
|
501 | my $pl = cf::player::find $user |
|
|
502 | or return $fail->("User '$user' does not exist - wrong spelling?"); |
|
|
503 | |
|
|
504 | aio_stat $pl->path |
|
|
505 | and return $ns->destroy; |
|
|
506 | |
|
|
507 | my $mtime = (stat _)[9]; |
|
|
508 | my $token = $pl->password; |
|
|
509 | |
|
|
510 | $token = $token =~ /^!/ |
|
|
511 | ? Deliantra::Util::hash_pw pack "H*", substr $token, 1 |
|
|
512 | : pack "H*", $token; |
|
|
513 | |
|
|
514 | $token = Deliantra::Util::auth_pw $token, $ns->{nonces}[0], $ns->{nonces}[1]; |
|
|
515 | |
|
|
516 | $token eq $hash |
|
|
517 | or $cf::CFG{ext_login_nocheck} |
|
|
518 | or return $fail->("User exists, but the password doesn't match - check your spelling, NumLock/CapsLock etc."); |
|
|
519 | |
|
|
520 | # player exists and passwords match - we can proceed |
|
|
521 | |
|
|
522 | $reply->(1, "Success"); |
|
|
523 | |
|
|
524 | $pl->connect ($ns); |
|
|
525 | enter_map $pl; |
|
|
526 | login $pl; |
|
|
527 | }); |
426 | }; |
528 | }; |
427 | |
529 | |
428 | cf::register_command password => sub { |
530 | cf::register_command password => sub { |
429 | my ($pl, $arg) = @_; |
531 | my ($pl, $arg) = @_; |
430 | |
532 | |
… | |
… | |
432 | $pl->message ( |
534 | $pl->message ( |
433 | "The password can currently only changed by a DM.", |
535 | "The password can currently only changed by a DM.", |
434 | cf::NDI_UNIQUE | cf::NDI_REPLY); |
536 | cf::NDI_UNIQUE | cf::NDI_REPLY); |
435 | return; |
537 | return; |
436 | } |
538 | } |
|
|
539 | |
|
|
540 | $pl->message (#d# |
|
|
541 | "Passwords cannot currently be changed.",#d# |
|
|
542 | cf::NDI_UNIQUE | cf::NDI_REPLY);#d# |
|
|
543 | return;#d# |
437 | |
544 | |
438 | my (@args) = split /\s+/, $arg; |
545 | my (@args) = split /\s+/, $arg; |
439 | my ($player, $new_pw) = @args; |
546 | my ($player, $new_pw) = @args; |
440 | |
547 | |
441 | if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { |
548 | if ($pl->flag (cf::FLAG_WIZ) && $player eq '') { |