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.11 by root, Sun Jan 7 02:39:14 2007 UTC vs.
Revision 1.42 by root, Wed Apr 18 17:32:06 2007 UTC

1#! perl 1#! perl # MANDATORY
2 2
3# login handling 3# login handling
4 4
5use Fcntl; 5use Fcntl;
6use Coro::AIO; 6use Coro::AIO;
7 7use List::Util qw(min max);
8my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir;
9 8
10# paranoia function to overwrite a string-in-place 9# paranoia function to overwrite a string-in-place
11sub nuke_str { 10sub nuke_str {
12 substr $_[0], 0, (length $_[0]), "x" x length $_[0] 11 substr $_[0], 0, (length $_[0]), "x" x length $_[0]
13} 12}
14 13
15sub query { 14sub query {
16 my ($ns, $flags, $text) = @_; 15 my ($ns, $flags, $text) = @_;
17 16
18 my $current = $Coro::current; 17 my $current = $Coro::current;
19 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); 18 $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] });
20 Coro::schedule while ref $current; 19 Coro::schedule while ref $current;
21 20
22 $current 21 $current
23} 22}
24 23
25sub can_cleanup { 24sub can_cleanup {
26 my ($playerfile, $mtime) = @_; 25 my ($pl, $mtime) = @_;
27 26
28 my $age = time - $mtime; 27 my $age = time - $mtime;
29 my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; 28 my $level = $pl->ob->level;
30 29
31 ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 30 ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3
32 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 31 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9
33 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 32 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20
34 || $age > 700 * 86400 # 2 years for everybody else 33 || $age > 700 * 86400 # 2 years for everybody else
55} 54}
56 55
57sub check_clean_save { 56sub check_clean_save {
58 my ($pl) = @_; 57 my ($pl) = @_;
59 58
60 unless (delete $pl->{clean_save}) { 59 if (my $time = delete $pl->{unclean_save}) {
60 $pl->ns->send_drawinfo (
61 "You didn't use a savebed to leave this realm. This is very dangerous, "
62 . "as lots of things could happen when you leave by other means, such as cave-ins, "
63 . "or monsters suddenly snapping your body. Better use a savebed next time.",
64 cf::NDI_RED
65 );
61 #d#TODO 66 #d#TODO
62 } 67 }
63} 68}
64 69
65# delete a player directory, be non-blocking AND synchronous... 70# delete a player directory, be non-blocking AND synchronous...
71 system "cd \Q$PLAYERDIR\E " 76 system "cd \Q$PLAYERDIR\E "
72 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " 77 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
73 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; 78 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
74} 79}
75 80
81sub send_capabilities {
82 my ($ns) = @_;
83
84 return unless $ns->extcmd;
85
86 $ns->ext_event (capabilities =>
87 # id, name, flags (1 == 2d), edge length
88 tileset => [[1, "default 64x64 faceset", 1, 64], [0, "default 32x32 faceset", 1, 32]],
89 );
90}
91
92sub setup {
93 my ($ns, $args) = @_;
94
95 # run through the cmds of setup
96 # syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ...
97 #
98 # we send the status of the cmd back, or a FALSE is the cmd is the server unknown
99 # The client then must sort this out
100
101 my %setup = split / +/, $args;
102 while (my ($k, $v) = each %setup) {
103 if ($k eq "sound") {
104 $ns->sound ($v);
105
106 } elsif ($k eq "exp64") {
107 $setup{$k} = 1;
108
109 } elsif ($k eq "spellmon") {
110 $ns->monitor_spells ($v);
111
112 } elsif ($k eq "darkness") {
113 $ns->darkness ($v);
114
115 } elsif ($k eq "map1cmd") {
116 $ns->mapmode (cf::Map1Cmd) if $v > 0;
117
118 } elsif ($k eq "map1acmd") {
119 $ns->mapmode (cf::Map1aCmd) if $v > 0;
120
121 } elsif ($k eq "map2cmd") {
122 # gcfclient bug, map1acmd is sent too late
123 $ns->mapmode (cf::Map1aCmd);
124 $setup{$k} = "FALSE";
125
126 } elsif ($k eq "newmapcmd") {
127 $ns->newmapcmd ($v);
128
129 } elsif ($k eq "mapinfocmd") {
130 $ns->mapinfocmd ($v);
131
132 } elsif ($k eq "extcmd") {
133 $ns->extcmd ($v > 0);
134 send_capabilities $ns;
135
136 } elsif ($k eq "extmap") {
137 $ns->extmap ($v);
138
139 } elsif ($k eq "facecache") {
140 if (!$v) {
141 $v = 1;
142 $setup{$k} = $v;
143 $ns->send_drawinfo ("(trying to forcefully enable facecaching)", cf::NDI_RED);
144 }
145
146 $ns->facecache ($v);
147
148 } elsif ($k eq "faceset") {
149 $ns->faceset (0);
150 $setup{$k} = 0;
151 # $ns->image2 (1)
152
153 } elsif ($k eq "tileset") {
154 $setup{$k} = $ns->faceset ($v & 1);
155
156 } elsif ($k eq "itemcmd") {
157 # Version of the item protocol command to use. Currently,
158 # only supported versions are 1 and 2. Using a numeric
159 # value will make it very easy to extend this in the future.
160 $ns->itemcmd ($v) if $v >= 1 && $v <= 2;
161
162 $setup{$k} = $ns->itemcmd;
163
164 } elsif ($k eq "mapsize") {
165 my ($x, $y) = split /x/, $v;
166
167 $ns->mapx ($x = max 9, min cf::MAP_CLIENT_X, ($x - 1) | 1);
168 $ns->mapy ($y = max 9, min cf::MAP_CLIENT_Y, ($y - 1) | 1);
169
170 $setup{$k} = "${x}x${y}";
171
172 } elsif ($k eq "extendedMapInfos") {
173 $ns->ext_mapinfos ($v);
174
175 } elsif ($k eq "extendedTextInfos") {
176 $ns->has_readable_type ($v);
177
178 } elsif ($k eq "smoothing") { # cfplus-style smoothing
179 $ns->smoothing ($v);
180
181 } elsif ($k eq "fxix") {
182 $ns->fxix ($v);
183
184 } else {
185 # other commands:
186 # sexp: no idea, probably for oudated servers
187 # tick: more stupidity, server should sned a tick per tick
188
189 $setup{$k} = "FALSE";
190 }
191 }
192
193 $ns->send_packet (join " ", setup => %setup);
194
195 cf::datalog setup =>
196 request => $args,
197 reply => \%setup,
198 client => $ns->version,
199 ;
200}
201
76sub addme { 202sub addme {
77 my ($ns) = @_; 203 my ($ns) = @_;
78 204
79 $ns->destroy if $ns->pl; 205 if (!$ns->facecache)
206 {
207 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
208
209
210***
211*** WARNING:
212*** Your client does not support face/image caching,
213*** or it has been disabled. Face caching is mandatory
214*** so please enable it or use a newer client.
215***
216*** Look at your client preferences:
217***
218*** CFPlus: all known versions automatically enable the facecache.
219*** cfclient: use the -cache commandline option.
220*** cfclient: map will not redraw automatically (bug).
221*** gcfclient: use -cache commandline option, or enable
222*** gcfclient: Client=>Configure=>Map & Image=>Cache Images.
223*** jcrossclient: your client is broken, use CFPlus or gcfclient.
224***
225***
226EOF
227 if ($ns->version =~ /jcrossclient/) {
228 # let them, for now
229 } else {
230 $ns->flush;
231 return $ns->destroy;
232 }
233
234 # $ns->facecache = true;
235 }
236
237 if ($ns->mapmode < cf::Map1aCmd) {
238 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
239
240
241***
242*** WARNING:
243*** Your client is too old. Please upgrade to a newer version.
244EOF
245
246 $ns->flush;
247 return $ns->destroy;
248 }
249
250 $ns->pl and return $ns->destroy;
80 251
81 $ns->async (sub { 252 $ns->async (sub {
82 my ($user, $pass); 253 my ($user, $pass);
83 254
84 $ns->send_packet ("addme_success"); 255 $ns->send_packet ("addme_success");
99 $ns->send_drawinfo ( 270 $ns->send_drawinfo (
100 "That username is currently used in another login session. " 271 "That username is currently used in another login session. "
101 . "Chose another, or wait till the other session has ended.", 272 . "Chose another, or wait till the other session has ended.",
102 cf::NDI_RED 273 cf::NDI_RED
103 ); 274 );
104 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { 275 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) {
105 last; 276 last;
106 } else { 277 } else {
107 $ns->send_drawinfo ( 278 $ns->send_drawinfo (
108 "Your username contains illegal characters " 279 "Your username contains illegal characters "
109 . "(only a-z, A-Z and 0-9 are allowed), " 280 . "(only a-z, A-Z and 0-9 are allowed), "
145 local $cf::LOGIN_LOCK{$user} = 1; 316 local $cf::LOGIN_LOCK{$user} = 1;
146 317
147 check_playing $ns, $user and next; 318 check_playing $ns, $user and next;
148 319
149 # try to read the user file and check the password 320 # try to read the user file and check the password
150 if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) { 321 if (my $pl = cf::player::find $user) {
322 aio_stat $pl->path and next;
151 my $mtime = (stat $fh)[9]; 323 my $mtime = (stat _)[9];
152
153 0 < aio_read $fh, 0, 16384, my $buf, 0 or next;
154 $buf =~ /^password (\S+)$/m or next;
155 my $hash = $1; 324 my $hash = $pl->password;
156 325
157 if ($hash eq crypt $pass, $hash) { 326 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
158 nuke_str $pass; 327 nuke_str $pass;
159 # password matches, wonderful 328 # password matches, wonderful
160 my $pl = cf::player::find $user or next; 329 my $pl = cf::player::find $user or next;
161 $pl->connect ($ns); 330 $pl->connect ($ns);
162 check_clean_save $pl; 331 check_clean_save $pl;
163 last; 332 last;
164 } elsif (can_cleanup $buf, $mtime) { 333 } elsif (can_cleanup $pl, $mtime) {
165 Coro::Timer::sleep 1; 334 Coro::Timer::sleep 1;
166 335
167 $ns->send_drawinfo ( 336 $ns->send_drawinfo (
168 "Player exists, but password does not match. If this is your account, " 337 "Player exists, but password does not match. If this is your account, "
169 . "please try again. If not, you can now decide to take over this account " 338 . "please try again. If not, you can now decide to take over this account "
177 346
178 # check if the file hasn't changed 347 # check if the file hasn't changed
179 aio_stat cf::player::path $user and next; 348 aio_stat cf::player::path $user and next;
180 $mtime == (stat _)[9] or next; 349 $mtime == (stat _)[9] or next;
181 350
182 nuke_playerdir $user; 351 $pl->quit_character;
183 352
184 # fall through to creation 353 # fall through to creation
185 } else { 354 } else {
186 nuke_str $pass; 355 nuke_str $pass;
187 356
188 Coro::Timer::sleep 1; 357 Coro::Timer::sleep 1;
189 358
190 $ns->send_drawinfo ( 359 $ns->send_drawinfo (
191 "Wrong username or password. Please try again " 360 "Wrong username or password. Please try again "
192 . "(check for Numlock and other semi-obvious error sources).", 361 . "(check for Numlock and other semi-obvious error sources).",
362 cf::NDI_RED
363 );
364 next;
365 }
366 } else {
367 # unable to load the playerfile:
368 # check wether the player dir exists, which means the file is corrupted or
369 # something very similar.
370 if (!aio_stat cf::player::playerdir $user) {
371 $ns->send_drawinfo (
372 "Unable to retrieve this player. It might be a locked or broken account. "
373 . "If this is your account, ask a dungeon master for assistance. "
374 . "Otherwise choose a different login name.",
193 cf::NDI_RED 375 cf::NDI_RED
194 ); 376 );
195 next; 377 next;
196 } 378 }
197 } 379 }
255 last; 437 last;
256 } 438 }
257 }); 439 });
258} 440}
259 441
260# "Quitting will delete your character PERMANENTLY. If you are sure you want to do this, then use the quit_character command instead of quit." 442cf::register_command quit => sub {
261# "Quitting will delete your character.\nAre you sure you want to quit (y/n):" 443 my ($ob, $arg) = @_;
262# quit, quit_character, save 444
445 $ob->reply (undef,
446 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
447 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
448 cf::NDI_UNIQUE | cf::NDI_RED);
449};
450
451cf::register_command quit_character => sub {
452 my ($ob, $arg) = @_;
453
454 my $pl = $ob->contr;
455
456 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
457 if ($_[0] !~ /^[yY]/) {
458 $ob->reply (undef,
459 "Ok, not not quitting then.",
460 cf::NDI_UNIQUE | cf::NDI_RED);
461 } else {
462 $ob->reply (undef,
463 "Ok, quitting, hope to see you again.",
464 cf::NDI_UNIQUE | cf::NDI_RED);
465 $pl->ns->flush;
466 cf::async { $pl->quit_character };
467 }
468 });
469};
263 470
264cf::object->attach ( 471cf::object->attach (
265 type => cf::SAVEBED, 472 type => cf::SAVEBED,
266 on_apply => sub { 473 on_apply => sub {
267 my ($bed, $ob) = @_; 474 my ($bed, $ob) = @_;
268 475
269 return cf::override 0 unless $ob->type == cf::PLAYER; 476 return cf::override 0 unless $ob->type == cf::PLAYER;
270 477
271 my $pl = $ob->pl; 478 my $pl = $ob->contr;
272 479
273 # update respawn position 480 # update respawn position
274 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 481 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
482 cf::async { $pl->save };
275 483
276 $pl->killer ("left"); 484 $pl->killer ("left");
277 $ob->check_score; 485 $ob->check_score;
278 486
279 $ob->reply (undef, "In the future, you will wake up here when you die."); 487 $ob->reply (undef, "In the future, you will wake up here when you die.");
282 if ($_[0] !~ /^[yY]/) { 490 if ($_[0] !~ /^[yY]/) {
283 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 491 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
284 $pl->deactivate; 492 $pl->deactivate;
285 $pl->ns->destroy; 493 $pl->ns->destroy;
286 } else { 494 } else {
287 cf::async { 495 cf::async { $pl->save };
288 $pl->{clean_save} = 1;
289 $pl->save;
290 };
291 } 496 }
292 }); 497 });
293 }, 498 },
294); 499);
295 500
306 511
307 if ($cleanly) { 512 if ($cleanly) {
308 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 513 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
309 } else { 514 } else {
310 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 515 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
516 $pl->{unclean_save} = $cf::RUNTIME;
311 } 517 }
312 }, 518 },
313); 519);
314 520
315cf::client->attach ( 521cf::client->attach (
316 on_addme => \&addme, 522 on_addme => \&addme,
523 on_setup => \&setup,
317); 524);
318 525
319############################################################################# 526#############################################################################
320 527
321our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs 528our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
322our $SWAP_TIMEOUT = 30; # time after which an unused player is evicted form memory
323our $SAVE_TIMEOUT = 20; # save players every n seconds 529our $SAVE_TIMEOUT = 20; # save players every n seconds
324our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
325 530
326our $SCHEDULER = cf::async_ext { 531our $SCHEDULER = cf::async_ext {
532 my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL);
327 while () { 533 while () {
328 Coro::Timer::sleep $SCHEDULE_INTERVAL; 534 $schedule_interval->next;
329 535
330 # this weird form of iteration over values is used because 536 # this weird form of iteration over values is used because
331 # the hash changes underneath us frequently, and for 537 # the hash changes underneath us frequently, and for
332 # keeps a direct reference to the value without (in 5.8 perls) 538 # keeps a direct reference to the value without (in 5.8 perls)
333 # keeping a reference, so this is prone to crashes or worse. 539 # keeping a reference, so this is prone to crashes or worse.
337 or next; 543 or next;
338 $pl->valid or next; 544 $pl->valid or next;
339 545
340 eval { 546 eval {
341 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { 547 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
548 cf::wait_for_tick_begin;
342 $pl->save; 549 $pl->save;
343 Coro::Timer::sleep $SAVE_INTERVAL; 550
551 unless ($pl->active) {
552 # check refcounts, this is tricky and needs to be adjusted to fit server internals
553 my $ob = $pl->ob;
554 Scalar::Util::weaken $pl;
555 Scalar::Util::weaken $ob;
556 my $a_ = $pl->refcnt;#d#
557 my $b_ = $ob->refcnt;#d#
558 my $pl_ref = $pl->refcnt_cnt;
559 my $ob_ref = $ob->refcnt_cnt;
560
561 ## pl_ref == one from object + one from cf::PLAYER
562 ## ob_ref == one from simply being an object
563 if ($pl_ref == 2 && $ob_ref == 1) {
564 warn "player-scheduler destroy ", $ob->name;#d#
565
566 # remove from sight and get fresh "copies"
567 $pl = delete $cf::PLAYER{$ob->name};
568 $ob = $pl->ob;
569
570 $ob->destroy;
571 $pl->destroy;
572 } else {
573 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
574 }
575 }
344 } 576 }
345 }; 577 };
346 warn $@ if $@; 578 warn $@ if $@;
347 Coro::cede; 579 Coro::cede;
348 }; 580 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines