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.112 by elmex, Mon May 10 15:54:40 2010 UTC vs.
Revision 1.120 by root, Thu Nov 15 06:00:16 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);
58 54
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;
59
60 # never happens normally, but helps when shell users make mistakes
61 $m->linkable
62 or return 1;
63 63
64# return 0;#d# 64# return 0;#d#
65# warn join ":", $m->at ($x, $y);#d# 65# warn join ":", $m->at ($x, $y);#d#
66# warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n"; 66# warn "FOO$m { ".scalar ($m->at ($x, $y))." }\n";
67# return 0; 67# return 0;
127 127
128 $ob->goto ($map, $x, $y); 128 $ob->goto ($map, $x, $y);
129} 129}
130 130
131sub encode_password($) { 131sub encode_password($) {
132# crypt $_[0],
133# join '',
134# ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[(cf::rndm 64), (cf::rndm 64)]
135 "!" . unpack "H*", $_[0] 132 "!" . unpack "H*", $_[0]
136} 133}
137 134
138sub compare_password($$) { 135sub compare_password($$) {
139 my ($pass, $token) = @_; 136 my ($pass, $token) = @_;
140 137
141 if ($token =~ /\!(.*)/) { 138 if ($token =~ /!!(.*)/) {
139 return +(substr $pass, 0, 8) eq pack "H*", $1;
140 } elsif ($token =~ /!(.*)/) {
142 return $pass eq pack "H*", $1; 141 return $pass eq pack "H*", $1;
143 } else { 142 } else {
144 return $token eq crypt $pass, $token; 143 return $token eq crypt $pass, $token;
145 } 144 }
146} 145}
152 my $lock = cf::lock_acquire "ext::login::nuke_playerdir"; 151 my $lock = cf::lock_acquire "ext::login::nuke_playerdir";
153 152
154 my $temp = "$PLAYERDIR/~$Coro::current~deleting~"; 153 my $temp = "$PLAYERDIR/~$Coro::current~deleting~";
155 aio_rename "$PLAYERDIR/$user", $temp; 154 aio_rename "$PLAYERDIR/$user", $temp;
156 IO::AIO::aio_rmtree $temp; 155 IO::AIO::aio_rmtree $temp;
156}
157
158sub login_done {
159 my ($pl) = @_;
160
161 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) {
162 $pl->ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR);
163 }
164}
165
166sub chargen {
167 my ($ns, $user, $pass) = @_;
168
169 # the rest of this function is character creation
170 $Coro::current->{desc} = "addme($user) chargen";
171
172 # just to make sure nothing is left over
173 # normally, nothing is there.
174 nuke_playerdir $user;
175
176 my $pl = cf::player::new $user;
177 $pl->password (encode_password $pass);
178 $pl->connect ($ns);
179 my $ob = $pl->ob;
180
181 $ob->goto ($pl->maplevel, $ob->x, $ob->y);
182
183 while () {
184 $ob->update_stats;
185 $pl->save_stats;
186
187 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
188 "[y] to roll new stats [n] to use stats\n[1-7] [1-7] to swap stats.\nRoll again (y/n/1-7)?";
189
190 if ($res =~ /^[Nn]/) {
191 last;
192 } elsif ($res > 0 && $res <= 7) {
193 my $swap = query $ns, cf::CS_QUERY_SINGLECHAR, "Swap stat with (will not roll new stats) [1-7]?";
194
195 if ($swap > 0 && $swap <= 7) {
196 $ob->swap_stats ($res - 1, $swap - 1);
197 }
198 } else {
199 $ob->roll_stats;
200 }
201
202 Coro::Timer::sleep 0.05;
203 }
204
205 $ob->set_animation (2);
206 $ob->add_statbonus;
207
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 # create the playerdir, if necessary, as chargen_race_done did it before
224 # presumably because of unique maps
225 aio_mkdir playerdir $pl, 0770;
226 $pl->chargen_race_done;
227
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
242 $ob->reply (undef, "Welcome to Deliantra!");
243
244 # XXX: Workaround for delayed client ext protocol handshake
245 $pl->esrv_new_player;
246
247 delete $pl->{deny_save};
157} 248}
158 249
159cf::client->attach (on_addme => sub { 250cf::client->attach (on_addme => sub {
160 my ($ns) = @_; 251 my ($ns) = @_;
161 252
242 aio_stat $pl->path and next; 333 aio_stat $pl->path and next;
243 my $mtime = (stat _)[9]; 334 my $mtime = (stat _)[9];
244 my $token = $pl->password; 335 my $token = $pl->password;
245 336
246 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) { 337 if ($cf::CFG{ext_login_nocheck} or compare_password $pass, $token) {
338 # player exists and passwords match - we can proceed
339
247 $pl->password (encode_password $pass); # make sure we store the new encoding #d# 340 $pl->password (encode_password $pass); # make sure we store the new encoding #d#
248 nuke_str $pass;
249 # password matches, wonderful 341 # password matches, wonderful
250 my $pl = cf::player::find $user or next; 342 my $pl = cf::player::find $user or next;
251 $pl->connect ($ns); 343 $pl->connect ($ns);
252 enter_map $pl; 344 enter_map $pl;
345 login_done $pl;
253 last; 346 return;
254 } elsif (can_cleanup $pl, $mtime) { 347 } elsif (can_cleanup $pl, $mtime) {
255 Coro::Timer::sleep 1; 348 Coro::Timer::sleep 1;
256 349
257 $ns->send_drawinfo ( 350 $ns->send_drawinfo (
258 "Player exists, but password does not match. If this is your account, " 351 "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 " 352 . "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.", 353 . "because it has not been in-use for some time.",
261 cf::NDI_RED 354 cf::NDI_RED
262 ); 355 );
263 356
264 #TODO: nuke_str
265 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 357 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
266 or next; 358 or next;
267 359
268 # check if the file hasn't changed 360 # check if the file hasn't changed
269 aio_stat cf::player::path $user and next; 361 aio_stat cf::player::path $user and next;
271 363
272 $pl->quit_character; 364 $pl->quit_character;
273 365
274 # fall through to creation 366 # fall through to creation
275 } else { 367 } else {
276 nuke_str $pass;
277
278 Coro::Timer::sleep 1; 368 Coro::Timer::sleep 1;
279 369
280 $ns->send_drawinfo ( 370 $ns->send_drawinfo (
281 "Wrong username or password. Please try again " 371 "Wrong username or password. Please try again "
282 . "(check for Numlock and other semi-obvious error sources).", 372 . "(check for Numlock and other semi-obvious error sources).",
284 ); 374 );
285 next; 375 next;
286 } 376 }
287 } else { 377 } else {
288 # unable to load the playerfile: 378 # unable to load the playerfile:
289 # check wether the player dir exists, which means the file is corrupted or 379 # check whether the player dir exists, which means the file is corrupted or
290 # something very similar. 380 # something very similar.
291 if (!aio_stat cf::player::playerdir $user) { 381 if (!aio_stat cf::player::playerdir $user) {
292 $ns->send_drawinfo ( 382 $ns->send_drawinfo (
293 "Unable to retrieve this player. It might be a locked or broken account. " 383 "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. " 384 . "If this is your account, ask a dungeon master for assistance. "
297 ); 387 );
298 next; 388 next;
299 } 389 }
300 } 390 }
301 391
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."; 392 my $pass2 = query $ns, cf::CS_QUERY_HIDEINPUT, "Please type your password again.";
310 393
311 if ($pass2 ne $pass) { 394 if ($pass2 ne $pass) {
312 nuke_str $pass;
313 nuke_str $pass2;
314 $ns->send_drawinfo ( 395 $ns->send_drawinfo (
315 "The passwords do not match, please try again.", 396 "The passwords do not match, please try again.",
316 cf::NDI_RED 397 cf::NDI_RED
317 ); 398 );
318 Coro::Timer::sleep 0.5; 399 Coro::Timer::sleep 0.5;
319 next; 400 next;
320 } 401 }
321 402
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; 403 last;
399 } 404 }
400 405
401 if (0 < Coro::AIO::aio_load "$cf::CONFDIR/motd", my $motd) { 406 # lock again, too layz to make this nicer
402 $ns->send_msg ("c/motd" => $motd, cf::NDI_CLEAR); 407 local $cf::LOGIN_LOCK{$user} = 1;
403 } 408
409 chargen $ns, $user, $pass;
410 login_done $ns->pl;
404 }); 411 });
405}); 412});
406 413
407cf::register_command password => sub { 414cf::register_command password => sub {
408 my ($pl, $arg) = @_; 415 my ($pl, $arg) = @_;
491 498
492 $pl->save; 499 $pl->save;
493 500
494 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY); 501 $ob->send_msg ($cf::SAY_CHANNEL => "In the future, you will wake up here when you die.", cf::NDI_DEF | cf::NDI_REPLY);
495 502
503 my $ns = $pl->ns
504 or return;
505
496 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 506 $ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
497 if ($_[0] !~ /^[yY]/) { 507 if ($_[0] !~ /^[yY]/) {
498 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 508 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
499 $pl->deactivate; 509 $pl->deactivate;
500 $pl->ns->destroy; 510 $pl->ns->destroy;
501 } 511 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines