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.33 by root, Mon Apr 2 18:14:24 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;
7use List::Util qw(min max);
7 8
8my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; 9my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir;
9 10
10# paranoia function to overwrite a string-in-place 11# paranoia function to overwrite a string-in-place
11sub nuke_str { 12sub nuke_str {
21 22
22 $current 23 $current
23} 24}
24 25
25sub can_cleanup { 26sub can_cleanup {
26 my ($playerfile, $mtime) = @_; 27 my ($pl, $mtime) = @_;
27 28
28 my $age = time - $mtime; 29 my $age = time - $mtime;
29 my $level = $playerfile =~ /^level (\d+)$/m ? $1 : return; 30 my $level = $pl->ob->level;
30 31
31 ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3 32 ($level <= 3 && $age > 7 * 86400) # 7 days for level 0..3
32 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9 33 || ($level <= 9 && $age > 90 * 86400) # 3 months for level 4..9
33 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20 34 || ($level <= 20 && $age > 180 * 86400) # 6 months for level 10..20
34 || $age > 700 * 86400 # 2 years for everybody else 35 || $age > 700 * 86400 # 2 years for everybody else
55} 56}
56 57
57sub check_clean_save { 58sub check_clean_save {
58 my ($pl) = @_; 59 my ($pl) = @_;
59 60
60 unless (delete $pl->{clean_save}) { 61 if (my $time = delete $pl->{unclean_save}) {
62 $pl->ns->send_drawinfo (
63 "You didn't use a savebed to leave this realm. This is very dangerous, "
64 . "as lots of things could happen when you leave by other means, such as cave-ins, "
65 . "or monsters suddenly snapping your body. Better use a savebed next time.",
66 cf::NDI_RED
67 );
61 #d#TODO 68 #d#TODO
62 } 69 }
63} 70}
64 71
65# delete a player directory, be non-blocking AND synchronous... 72# delete a player directory, be non-blocking AND synchronous...
71 system "cd \Q$PLAYERDIR\E " 78 system "cd \Q$PLAYERDIR\E "
72 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " 79 . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null "
73 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; 80 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
74} 81}
75 82
83sub setup {
84 my ($ns, $args) = @_;
85
86 # run through the cmds of setup
87 # syntax is setup <cmdname1> <parameter> <cmdname2> <parameter> ...
88 #
89 # we send the status of the cmd back, or a FALSE is the cmd is the server unknown
90 # The client then must sort this out
91
92 my %setup = split / +/, $args;
93 while (my ($k, $v) = each %setup) {
94
95 if ($k eq "sound") {
96 $ns->sound ($v);
97
98 } elsif ($k eq "exp64") {
99 $setup{$k} = 1;
100
101 } elsif ($k eq "spellmon") {
102 $ns->monitor_spells ($v);
103
104 } elsif ($k eq "darkness") {
105 $ns->darkness ($v);
106
107 } elsif ($k eq "map1cmd") {
108 $ns->mapmode (cf::Map1Cmd) if $v > 0;
109
110 } elsif ($k eq "map1acmd") {
111 $ns->mapmode (cf::Map1aCmd) if $v > 0;
112
113 } elsif ($k eq "map2cmd") {
114 # gcfclient bug, map1acmd is sent too late
115 $ns->mapmode (cf::Map1aCmd);
116 $setup{$k} = "FALSE";
117
118 } elsif ($k eq "newmapcmd") {
119 $ns->newmapcmd ($v);
120
121 } elsif ($k eq "mapinfocmd") {
122 $ns->mapinfocmd ($v);
123
124 } elsif ($k eq "extcmd") {
125 $ns->extcmd ($v > 0);
126
127 } elsif ($k eq "extmap") {
128 $ns->extmap ($v);
129
130 } elsif ($k eq "facecache") {
131 $ns->facecache ($v);
132
133 } elsif ($k eq "faceset") {
134 $ns->faceset (0);
135 $setup{$k} = 0;
136 # $ns->image2 (1)
137
138 } elsif ($k eq "itemcmd") {
139 # Version of the item protocol command to use. Currently,
140 # only supported versions are 1 and 2. Using a numeric
141 # value will make it very easy to extend this in the future.
142 $ns->itemcmd ($v) if $v >= 1 && $v <= 2;
143
144 $setup{$k} = $ns->itemcmd;
145
146 } elsif ($k eq "mapsize") {
147 my ($x, $y) = split /x/, $v;
148
149 $ns->mapx ($x = max 9, min cf::MAP_CLIENT_X, $x);
150 $ns->mapy ($y = max 9, min cf::MAP_CLIENT_Y, $y);
151
152 $setup{$k} = "${x}x${y}";
153
154 } elsif ($k eq "extendedMapInfos") {
155 $ns->ext_mapinfos ($v);
156
157 } elsif ($k eq "extendedTextInfos") {
158 $ns->has_readable_type ($v);
159
160 } else {
161 # other commands:
162 # sexp: no idea, probably for oudated servers
163 # tick: more stupidity, server should sned a tick per tick
164
165 $setup{$k} = "FALSE";
166 }
167 }
168
169 $ns->send_packet (join " ", setup => %setup);
170
171 cf::datalog setup =>
172 request => $args,
173 reply => \%setup,
174 client => $ns->version,
175 ;
176}
177
76sub addme { 178sub addme {
77 my ($ns) = @_; 179 my ($ns) = @_;
78 180
79 $ns->destroy if $ns->pl; 181 if (!$ns->facecache)
182 {
183 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
184
185
186***
187*** WARNING:
188*** Your client does not support face/image caching,
189*** or it has been disabled. Face caching is mandatory
190*** so please enable it or use a newer client.
191***
192*** Look at your client preferences:
193***
194*** CFPlus: all known versions automatically enable the facecache.
195*** cfclient: use the -cache commandline option.
196*** cfclient: map will not redraw automatically (bug).
197*** gcfclient: use -cache commandline option, or enable
198*** gcfclient: Client=>Configure=>Map & Image=>Cache Images.
199*** jcrossclient: your client is broken, use CFPlus or gcfclient.
200***
201***
202EOF
203 if ($ns->version =~ /jcrossclient/) {
204 # let them, for now
205 } else {
206 $ns->flush;
207 return $ns->destroy;
208 }
209
210 # $ns->facecache = true;
211 }
212
213 if ($ns->mapmode < cf::Map1aCmd) {
214 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
215
216
217***
218*** WARNING:
219*** Your client is too old. Please upgrade to a newer version.
220EOF
221
222 $ns->flush;
223 return $ns->destroy;
224 }
225
226 $ns->pl and return $ns->destroy;
80 227
81 $ns->async (sub { 228 $ns->async (sub {
82 my ($user, $pass); 229 my ($user, $pass);
83 230
84 $ns->send_packet ("addme_success"); 231 $ns->send_packet ("addme_success");
99 $ns->send_drawinfo ( 246 $ns->send_drawinfo (
100 "That username is currently used in another login session. " 247 "That username is currently used in another login session. "
101 . "Chose another, or wait till the other session has ended.", 248 . "Chose another, or wait till the other session has ended.",
102 cf::NDI_RED 249 cf::NDI_RED
103 ); 250 );
104 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { 251 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) {
105 last; 252 last;
106 } else { 253 } else {
107 $ns->send_drawinfo ( 254 $ns->send_drawinfo (
108 "Your username contains illegal characters " 255 "Your username contains illegal characters "
109 . "(only a-z, A-Z and 0-9 are allowed), " 256 . "(only a-z, A-Z and 0-9 are allowed), "
145 local $cf::LOGIN_LOCK{$user} = 1; 292 local $cf::LOGIN_LOCK{$user} = 1;
146 293
147 check_playing $ns, $user and next; 294 check_playing $ns, $user and next;
148 295
149 # try to read the user file and check the password 296 # try to read the user file and check the password
150 if (my $fh = aio_open cf::player::path $user, O_RDONLY, 0) { 297 if (my $pl = cf::player::find $user) {
298 aio_stat $pl->path and next;
151 my $mtime = (stat $fh)[9]; 299 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; 300 my $hash = $pl->password;
156 301
157 if ($hash eq crypt $pass, $hash) { 302 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
158 nuke_str $pass; 303 nuke_str $pass;
159 # password matches, wonderful 304 # password matches, wonderful
160 my $pl = cf::player::find $user or next; 305 my $pl = cf::player::find $user or next;
161 $pl->connect ($ns); 306 $pl->connect ($ns);
162 check_clean_save $pl; 307 check_clean_save $pl;
163 last; 308 last;
164 } elsif (can_cleanup $buf, $mtime) { 309 } elsif (can_cleanup $pl, $mtime) {
165 Coro::Timer::sleep 1; 310 Coro::Timer::sleep 1;
166 311
167 $ns->send_drawinfo ( 312 $ns->send_drawinfo (
168 "Player exists, but password does not match. If this is your account, " 313 "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 " 314 . "please try again. If not, you can now decide to take over this account "
177 322
178 # check if the file hasn't changed 323 # check if the file hasn't changed
179 aio_stat cf::player::path $user and next; 324 aio_stat cf::player::path $user and next;
180 $mtime == (stat _)[9] or next; 325 $mtime == (stat _)[9] or next;
181 326
182 nuke_playerdir $user; 327 $pl->quit_character;
183 328
184 # fall through to creation 329 # fall through to creation
185 } else { 330 } else {
186 nuke_str $pass; 331 nuke_str $pass;
187 332
255 last; 400 last;
256 } 401 }
257 }); 402 });
258} 403}
259 404
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." 405cf::register_command quit => sub {
261# "Quitting will delete your character.\nAre you sure you want to quit (y/n):" 406 my ($ob, $arg) = @_;
262# quit, quit_character, save 407
408 $ob->reply (undef,
409 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
410 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
411 cf::NDI_UNIQUE | cf::NDI_RED);
412};
413
414cf::register_command quit_character => sub {
415 my ($ob, $arg) = @_;
416
417 my $pl = $ob->contr;
418
419 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
420 if ($_[0] !~ /^[yY]/) {
421 $ob->reply (undef,
422 "Ok, not not quitting then.",
423 cf::NDI_UNIQUE | cf::NDI_RED);
424 } else {
425 $ob->reply (undef,
426 "Ok, quitting, hope to see you again.",
427 cf::NDI_UNIQUE | cf::NDI_RED);
428 $pl->ns->flush;
429 cf::async { $pl->quit_character };
430 }
431 });
432};
263 433
264cf::object->attach ( 434cf::object->attach (
265 type => cf::SAVEBED, 435 type => cf::SAVEBED,
266 on_apply => sub { 436 on_apply => sub {
267 my ($bed, $ob) = @_; 437 my ($bed, $ob) = @_;
268 438
269 return cf::override 0 unless $ob->type == cf::PLAYER; 439 return cf::override 0 unless $ob->type == cf::PLAYER;
270 440
271 my $pl = $ob->pl; 441 my $pl = $ob->contr;
272 442
273 # update respawn position 443 # update respawn position
274 $pl->savebed ($bed->map->path, $bed->x, $bed->y); 444 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
445 cf::async { $pl->save };
275 446
276 $pl->killer ("left"); 447 $pl->killer ("left");
277 $ob->check_score; 448 $ob->check_score;
278 449
279 $ob->reply (undef, "In the future, you will wake up here when you die."); 450 $ob->reply (undef, "In the future, you will wake up here when you die.");
282 if ($_[0] !~ /^[yY]/) { 453 if ($_[0] !~ /^[yY]/) {
283 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 454 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
284 $pl->deactivate; 455 $pl->deactivate;
285 $pl->ns->destroy; 456 $pl->ns->destroy;
286 } else { 457 } else {
287 cf::async { 458 cf::async { $pl->save };
288 $pl->{clean_save} = 1;
289 $pl->save;
290 };
291 } 459 }
292 }); 460 });
293 }, 461 },
294); 462);
295 463
306 474
307 if ($cleanly) { 475 if ($cleanly) {
308 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 476 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
309 } else { 477 } else {
310 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 478 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
479 $pl->{unclean_save} = $cf::RUNTIME;
311 } 480 }
312 }, 481 },
313); 482);
314 483
315cf::client->attach ( 484cf::client->attach (
316 on_addme => \&addme, 485 on_addme => \&addme,
486 on_setup => \&setup,
317); 487);
318 488
319############################################################################# 489#############################################################################
320 490
321our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs 491our $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 492our $SAVE_TIMEOUT = 20; # save players every n seconds
324our $SAVE_INTERVAL = 0.1; # save at max. one player every $SAVE_INTERVAL
325 493
326our $SCHEDULER = cf::async_ext { 494our $SCHEDULER = cf::async_ext {
495 my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL);
327 while () { 496 while () {
328 Coro::Timer::sleep $SCHEDULE_INTERVAL; 497 $schedule_interval->next;
329 498
330 # this weird form of iteration over values is used because 499 # this weird form of iteration over values is used because
331 # the hash changes underneath us frequently, and for 500 # the hash changes underneath us frequently, and for
332 # keeps a direct reference to the value without (in 5.8 perls) 501 # keeps a direct reference to the value without (in 5.8 perls)
333 # keeping a reference, so this is prone to crashes or worse. 502 # keeping a reference, so this is prone to crashes or worse.
337 or next; 506 or next;
338 $pl->valid or next; 507 $pl->valid or next;
339 508
340 eval { 509 eval {
341 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { 510 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
511 $cf::WAIT_FOR_TICK_ONE->wait;
342 $pl->save; 512 $pl->save;
343 Coro::Timer::sleep $SAVE_INTERVAL; 513
514 unless ($pl->active) {
515 # check refcounts, this is tricky and needs to be adjusted to fit server internals
516 my $ob = $pl->ob;
517 Scalar::Util::weaken $pl;
518 Scalar::Util::weaken $ob;
519 my $a_ = $pl->refcnt;#d#
520 my $b_ = $ob->refcnt;#d#
521 my $pl_ref = $pl->refcnt_cnt;
522 my $ob_ref = $ob->refcnt_cnt;
523
524 ## pl_ref == one from object + one from cf::PLAYER
525 ## ob_ref == one from simply being an object
526 if ($pl_ref == 2 && $ob_ref == 1) {
527 warn "player-scheduler destroy ", $ob->name;#d#
528
529 # remove from sight and get fresh "copies"
530 $pl = delete $cf::PLAYER{$ob->name};
531 $ob = $pl->ob;
532
533 $ob->destroy;
534 $pl->destroy;
535 } else {
536 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
537 }
538 }
344 } 539 }
345 }; 540 };
346 warn $@ if $@; 541 warn $@ if $@;
347 Coro::cede; 542 Coro::cede;
348 }; 543 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines