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.9 by root, Thu Jan 4 16:19:31 2007 UTC vs.
Revision 1.45 by root, Tue Apr 24 00:42:02 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
35} 34}
36 35
37sub check_playing { 36sub check_playing {
38 my ($ns, $user) = @_; 37 my ($ns, $user) = @_;
39 38
40 return unless cf::player::find $user; 39 return unless cf::player::find_active $user;
41 40
42 $ns->send_drawinfo ( 41 $ns->send_drawinfo (
43 "That player is already logged in on this server. " 42 "That player is already logged in on this server. "
44 . "If you want to create a new player, choose another name. " 43 . "If you want to create a new player, choose another name. "
45 . "If you are already a registered player, make sure nobody " 44 . "If you are already a registered player, make sure nobody "
52 ); 51 );
53 52
54 1 53 1
55} 54}
56 55
56sub check_clean_save {
57 my ($pl) = @_;
58
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 );
66 #d#TODO
67 }
68}
69
57# delete a player directory, be non-blocking AND synchronous... 70# delete a player directory, be non-blocking AND synchronous...
58# (thats hard, so we crap out and fork). 71# (thats hard, so we crap out and fork).
59sub nuke_playerdir { 72sub nuke_playerdir {
60 my ($user) = @_; 73 my ($user) = @_;
61 74
63 system "cd \Q$PLAYERDIR\E " 76 system "cd \Q$PLAYERDIR\E "
64 . "&& 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 "
65 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; 78 . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)";
66} 79}
67 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 } elsif ($k eq "msg") {
185 $ns->can_msg ($v);
186
187 } elsif ($k eq "excmd") {
188 # we support it
189
190 } else {
191 # other commands:
192 # sexp: no idea, probably for oudated servers
193 # tick: more stupidity, server should send a tick per tick
194
195 $setup{$k} = "FALSE";
196 }
197 }
198
199 $ns->send_packet (join " ", setup => %setup);
200
201 cf::datalog setup =>
202 request => $args,
203 reply => \%setup,
204 client => $ns->version,
205 ;
206}
207
68sub addme { 208sub addme {
69 my ($ns) = @_; 209 my ($ns) = @_;
70 210
71 $ns->destroy if $ns->pl; 211 if (!$ns->facecache)
212 {
213 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
72 214
215
216***
217*** WARNING:
218*** Your client does not support face/image caching,
219*** or it has been disabled. Face caching is mandatory
220*** so please enable it or use a newer client.
221***
222*** Look at your client preferences:
223***
224*** CFPlus: all known versions automatically enable the facecache.
225*** cfclient: use the -cache commandline option.
226*** cfclient: map will not redraw automatically (bug).
227*** gcfclient: use -cache commandline option, or enable
228*** gcfclient: Client=>Configure=>Map & Image=>Cache Images.
229*** jcrossclient: your client is broken, use CFPlus or gcfclient.
230***
231***
232EOF
233 if ($ns->version =~ /jcrossclient/) {
234 # let them, for now
235 } else {
236 $ns->flush;
237 return $ns->destroy;
238 }
239
240 # $ns->facecache = true;
241 }
242
243 if ($ns->mapmode < cf::Map1aCmd) {
244 $ns->send_drawinfo (<<EOF, cf::NDI_RED);
245
246
247***
248*** WARNING:
249*** Your client is too old. Please upgrade to a newer version.
250EOF
251
252 $ns->flush;
253 return $ns->destroy;
254 }
255
256 $ns->pl and return $ns->destroy;
257
73 $ns->coro (sub { 258 $ns->async (sub {
74 my ($user, $pass); 259 my ($user, $pass);
75 260
76 $ns->send_packet ("addme_success"); 261 $ns->send_packet ("addme_success");
77 262
78 for (;;) { 263 for (;;) {
91 $ns->send_drawinfo ( 276 $ns->send_drawinfo (
92 "That username is currently used in another login session. " 277 "That username is currently used in another login session. "
93 . "Chose another, or wait till the other session has ended.", 278 . "Chose another, or wait till the other session has ended.",
94 cf::NDI_RED 279 cf::NDI_RED
95 ); 280 );
96 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { 281 } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) {
97 last; 282 last;
98 } else { 283 } else {
99 $ns->send_drawinfo ( 284 $ns->send_drawinfo (
100 "Your username contains illegal characters " 285 "Your username contains illegal characters "
101 . "(only a-z, A-Z and 0-9 are allowed), " 286 . "(only a-z, A-Z and 0-9 are allowed), "
123 . "that cannot be too much to ask for :)", 308 . "that cannot be too much to ask for :)",
124 cf::NDI_RED 309 cf::NDI_RED
125 ); 310 );
126 } 311 }
127 312
128 my $dir = "$PLAYERDIR/$user";
129 my $plfile = "$dir/$user.pl";
130
131 # lock this username for the remainder of this login session 313 # lock this username for the remainder of this login session
132 if ($cf::LOGIN_LOCK{$user}) { 314 if ($cf::LOGIN_LOCK{$user}) {
133 $ns->send_drawinfo ( 315 $ns->send_drawinfo (
134 "That username is currently used in another login session. " 316 "That username is currently used in another login session. "
135 . "Chose another, or wait till the other session has ended.", 317 . "Chose another, or wait till the other session has ended.",
140 local $cf::LOGIN_LOCK{$user} = 1; 322 local $cf::LOGIN_LOCK{$user} = 1;
141 323
142 check_playing $ns, $user and next; 324 check_playing $ns, $user and next;
143 325
144 # try to read the user file and check the password 326 # try to read the user file and check the password
145 if (my $fh = aio_open $plfile, O_RDONLY, 0) { 327 if (my $pl = cf::player::find $user) {
328 aio_stat $pl->path and next;
146 my $mtime = (stat $fh)[9]; 329 my $mtime = (stat _)[9];
147
148 0 < aio_read $fh, 0, 16384, my $buf, 0 or next;
149 $buf =~ /^password (\S+)$/m or next;
150 my $hash = $1; 330 my $hash = $pl->password;
151 331
152 if ($hash eq crypt $pass, $hash) { 332 if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) {
153 nuke_str $pass; 333 nuke_str $pass;
154 # password matches, wonderful 334 # password matches, wonderful
155 my $pl = cf::player::load $plfile or next; 335 my $pl = cf::player::find $user or next;
156 $pl->enable_save (1);
157 $pl->connect ($ns); 336 $pl->connect ($ns);
337 check_clean_save $pl;
158 last; 338 last;
159 } elsif (can_cleanup $buf, $mtime) { 339 } elsif (can_cleanup $pl, $mtime) {
160 Coro::Timer::sleep 1; 340 Coro::Timer::sleep 1;
161 341
162 $ns->send_drawinfo ( 342 $ns->send_drawinfo (
163 "Player exists, but password does not match. If this is your account, " 343 "Player exists, but password does not match. If this is your account, "
164 . "please try again. If not, you can now decide to take over this account " 344 . "please try again. If not, you can now decide to take over this account "
169 #TODO: nuke_str 349 #TODO: nuke_str
170 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/ 350 (query $ns, cf::CS_QUERY_SINGLECHAR, "Delete existing account and create a new one (Y/N)?") =~ /^[yY]/
171 or next; 351 or next;
172 352
173 # check if the file hasn't changed 353 # check if the file hasn't changed
174 aio_stat $plfile and next; 354 aio_stat cf::player::path $user and next;
175 $mtime == (stat _)[9] or next; 355 $mtime == (stat _)[9] or next;
176 356
177 nuke_playerdir $user; 357 $pl->quit_character;
178 358
179 # fall through to creation 359 # fall through to creation
180 } else { 360 } else {
181 nuke_str $pass; 361 nuke_str $pass;
182 362
183 Coro::Timer::sleep 1; 363 Coro::Timer::sleep 1;
184 364
185 $ns->send_drawinfo ( 365 $ns->send_drawinfo (
186 "Wrong username or password. Please try again " 366 "Wrong username or password. Please try again "
187 . "(check for Numlock and other semi-obvious error sources).", 367 . "(check for Numlock and other semi-obvious error sources).",
368 cf::NDI_RED
369 );
370 next;
371 }
372 } else {
373 # unable to load the playerfile:
374 # check wether the player dir exists, which means the file is corrupted or
375 # something very similar.
376 if (!aio_stat cf::player::playerdir $user) {
377 $ns->send_drawinfo (
378 "Unable to retrieve this player. It might be a locked or broken account. "
379 . "If this is your account, ask a dungeon master for assistance. "
380 . "Otherwise choose a different login name.",
188 cf::NDI_RED 381 cf::NDI_RED
189 ); 382 );
190 next; 383 next;
191 } 384 }
192 } 385 }
208 next; 401 next;
209 } 402 }
210 403
211 nuke_str $pass2; 404 nuke_str $pass2;
212 405
213 my $pl = cf::player::create; 406 my $pl = cf::player::new $user;
214 $pl->ob->name ($user);
215 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); 407 $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]);
216 nuke_str $pass; 408 nuke_str $pass;
217 $pl->connect ($ns); 409 $pl->connect ($ns);
218 410
219 my $ob = $pl->ob; 411 my $ob = $pl->ob;
239 } 431 }
240 432
241 $ob->set_animation (2); 433 $ob->set_animation (2);
242 $ob->add_statbonus; 434 $ob->add_statbonus;
243 435
244 $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); 436 while () {
245 $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, 437 $ns->send_msg (-1, "chargen-race-title", ucfirst $pl->title);
438 my $msg = $ob->msg;
439 $msg =~ s/\b\n\b/ /g;
440 $ns->send_msg (cf::NDI_BLUE, "chargen-race-description", $msg);
441
442 my $res = query $ns, cf::CS_QUERY_SINGLECHAR,
246 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); 443 "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n";
247 444
248 $ns->state (cf::ST_CHANGE_CLASS); 445 last if $res =~ /[dD]/;
249 $pl->enable_save (1);#d# too early 446
447 $pl->chargen_race_next;
448 }
449
450 $pl->chargen_race_done;
451
452 delete $pl->{deny_save};
250 453
251 last; 454 last;
252 } 455 }
253 }); 456 });
254} 457}
458
459cf::register_command quit => sub {
460 my ($ob, $arg) = @_;
461
462 $ob->reply (undef,
463 "Quitting will delete your character PERMANENTLY: It will be gone forever and any progress will be lost. "
464 . "If you are sure you want to do this, then use the quit_character command instead of quit.",
465 cf::NDI_UNIQUE | cf::NDI_RED);
466};
467
468cf::register_command quit_character => sub {
469 my ($ob, $arg) = @_;
470
471 my $pl = $ob->contr;
472
473 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to PERMANENTLY delete your character and all associated data (y/n)?", sub {
474 if ($_[0] !~ /^[yY]/) {
475 $ob->reply (undef,
476 "Ok, not not quitting then.",
477 cf::NDI_UNIQUE | cf::NDI_RED);
478 } else {
479 $ob->reply (undef,
480 "Ok, quitting, hope to see you again.",
481 cf::NDI_UNIQUE | cf::NDI_RED);
482 $pl->ns->flush;
483 cf::async { $pl->quit_character };
484 }
485 });
486};
255 487
256cf::object->attach ( 488cf::object->attach (
257 type => cf::SAVEBED, 489 type => cf::SAVEBED,
258 on_apply => sub { 490 on_apply => sub {
259 my ($bed, $ob) = @_; 491 my ($bed, $ob) = @_;
260 492
261 return cf::override 0 unless $ob->type == cf::PLAYER; 493 return cf::override 0 unless $ob->type == cf::PLAYER;
262 494
495 my $pl = $ob->contr;
496
263 # update respawn position 497 # update respawn position
264 $ob->contr->savebed ($bed->map->path, $bed->x, $bed->y); 498 $pl->savebed ($bed->map->path, $bed->x, $bed->y);
499 cf::async { $pl->save };
265 500
266 $ob->contr->killer ("left"); 501 $pl->killer ("left");
267 $ob->check_score; 502 $ob->check_score;
268 503
269 $ob->reply (undef, "In the future, you will wake up here when you die."); 504 $ob->reply (undef, "In the future, you will wake up here when you die.");
270 $ob->contr->save (1);
271 505
272 $ob->contr->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub { 506 $pl->ns->query (cf::CS_QUERY_SINGLECHAR, "Do you want to continue playing (y/n)?", sub {
273 if ($_[0] !~ /^[yY]/) { 507 if ($_[0] !~ /^[yY]/) {
274 $ob->contr->invoke (cf::EVENT_PLAYER_LOGOUT, 1); 508 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1);
509 $pl->deactivate;
275 $ob->contr->ns->destroy; 510 $pl->ns->destroy;
276 } else { 511 } else {
277 $ob->contr->enable_save (1); 512 cf::async { $pl->save };
278 } 513 }
279 }); 514 });
280 }, 515 },
281); 516);
282 517
293 528
294 if ($cleanly) { 529 if ($cleanly) {
295 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 530 $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
296 } else { 531 } else {
297 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; 532 $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list;
533 $pl->{unclean_save} = $cf::RUNTIME;
298 } 534 }
299 }, 535 },
300); 536);
301 537
302cf::client->attach ( 538cf::client->attach (
303 on_addme => \&addme, 539 on_addme => \&addme,
540 on_setup => \&setup,
304); 541);
305 542
543#############################################################################
544
545our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs
546our $SAVE_TIMEOUT = 20; # save players every n seconds
547
548our $SCHEDULER = cf::async_ext {
549 my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL);
550 while () {
551 $schedule_interval->next;
552
553 # this weird form of iteration over values is used because
554 # the hash changes underneath us frequently, and for
555 # keeps a direct reference to the value without (in 5.8 perls)
556 # keeping a reference, so this is prone to crashes or worse.
557 my @players = keys %cf::PLAYER;
558 for (@players) {
559 my $pl = $cf::PLAYER{$_}
560 or next;
561 $pl->valid or next;
562
563 eval {
564 if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) {
565 cf::wait_for_tick_begin;
566 $pl->save;
567
568 unless ($pl->active) {
569 # check refcounts, this is tricky and needs to be adjusted to fit server internals
570 my $ob = $pl->ob;
571 Scalar::Util::weaken $pl;
572 Scalar::Util::weaken $ob;
573 my $a_ = $pl->refcnt;#d#
574 my $b_ = $ob->refcnt;#d#
575 my $pl_ref = $pl->refcnt_cnt;
576 my $ob_ref = $ob->refcnt_cnt;
577
578 ## pl_ref == one from object + one from cf::PLAYER
579 ## ob_ref == one from simply being an object
580 if ($pl_ref == 2 && $ob_ref == 1) {
581 warn "player-scheduler destroy ", $ob->name;#d#
582
583 # remove from sight and get fresh "copies"
584 $pl = delete $cf::PLAYER{$ob->name};
585 $ob = $pl->ob;
586
587 $ob->destroy;
588 $pl->destroy;
589 } else {
590 warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d#
591 }
592 }
593 }
594 };
595 warn $@ if $@;
596 Coro::cede;
597 };
598 }
599};
600
601$SCHEDULER->prio (1);
602

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines