1 | #! perl # MANDATORY |
1 | #! perl # mandatory |
2 | |
2 | |
3 | # login handling |
3 | # login handling |
4 | |
4 | |
5 | use Fcntl; |
5 | use Fcntl; |
6 | use Coro::AIO; |
6 | use Coro::AIO; |
7 | |
|
|
8 | my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; |
|
|
9 | |
7 | |
10 | # paranoia function to overwrite a string-in-place |
8 | # paranoia function to overwrite a string-in-place |
11 | sub nuke_str { |
9 | sub nuke_str { |
12 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
10 | substr $_[0], 0, (length $_[0]), "x" x length $_[0] |
13 | } |
11 | } |
14 | |
12 | |
15 | sub query { |
13 | sub query { |
16 | my ($ns, $flags, $text) = @_; |
14 | my ($ns, $flags, $text) = @_; |
17 | |
15 | |
18 | my $current = $Coro::current; |
16 | my $current = $Coro::current; |
19 | $ns->query ($flags, $text, sub { $current->ready; $current = $_[0]; }); |
17 | $ns->query ($flags, $text, sub { $current->ready; $current = $_[0] }); |
20 | Coro::schedule while ref $current; |
18 | Coro::schedule while ref $current; |
21 | |
19 | |
22 | $current |
20 | $current |
23 | } |
21 | } |
24 | |
22 | |
… | |
… | |
40 | return unless cf::player::find_active $user; |
38 | return unless cf::player::find_active $user; |
41 | |
39 | |
42 | $ns->send_drawinfo ( |
40 | $ns->send_drawinfo ( |
43 | "That player is already logged in on this server. " |
41 | "That player is already logged in on this server. " |
44 | . "If you want to create a new player, choose another name. " |
42 | . "If you want to create a new player, choose another name. " |
45 | . "If you are already a registered player, make sure nobody " |
43 | . "If you have already a registered, make sure nobody " |
46 | . "else is using your account at this time. If you lost your conenction " |
44 | . "else is using your account at this time. If you lost your connection " |
47 | . "then the server will likely timeout within a minute. If you still " |
45 | . "then the server will likely timeout within a minute. If you still " |
48 | . "cannot log-in after a minute, you are still logged in. Make sure " |
46 | . "cannot log-in after a minute, you are still logged in. Make sure " |
49 | . "you do not have another client running. If you use windows, reboot, " |
47 | . "you do not have another client running. If you use windows, reboot, " |
50 | . "this will fix anything.", |
48 | . "this will fix anything.", |
51 | cf::NDI_RED |
49 | cf::NDI_RED |
52 | ); |
50 | ); |
53 | |
51 | |
54 | 1 |
52 | 1 |
55 | } |
53 | } |
56 | |
54 | |
57 | sub check_clean_save { |
55 | sub enter_map { |
58 | my ($pl) = @_; |
56 | my ($pl) = @_; |
59 | |
57 | |
|
|
58 | my ($map, $x, $y) |
|
|
59 | = $pl->ob->{_link_pos} |
|
|
60 | ? @{delete $pl->ob->{_link_pos}} |
|
|
61 | : ($pl->maplevel, $pl->ob->x, $pl->ob->y); |
|
|
62 | |
|
|
63 | $pl->ob->enter_link; |
|
|
64 | |
60 | if (my $time = delete $pl->{unclean_save}) { |
65 | if (my $time = delete $pl->{unclean_save}) { |
|
|
66 | if (my $m = cf::map::find $map) { |
|
|
67 | if ($time < $m->{instantiate_time}) { |
|
|
68 | # the map was reset in the meantime |
|
|
69 | my $age = $cf::RUNTIME - $time; |
|
|
70 | warn $pl->ob->name, " map reset after logout, logout age $age\n";#d# |
|
|
71 | |
|
|
72 | # for now, just go back to savebed |
|
|
73 | ($map, $x, $y) = $pl->savebed; |
|
|
74 | |
61 | $pl->ns->send_drawinfo ( |
75 | $pl->ns->send_drawinfo ( |
|
|
76 | "You didn't use a bed to reality to leave this realm, leaving your body in great danger. " |
|
|
77 | . "Fortunately, some friendly dwellers found you, checked your passport, and brought you to safety. " |
|
|
78 | . "Better use a savebed next time, much worse things could have happened...", |
|
|
79 | cf::NDI_RED |
|
|
80 | ); |
|
|
81 | } else { |
|
|
82 | $pl->ns->send_drawinfo ( |
62 | "You didn't use a savebed to leave this realm. This is very dangerous, " |
83 | "You didn't use a bed to reality to leave this realm. This is very dangerous, " |
63 | . "as lots of things could happen when you leave by other means, such as cave-ins, " |
84 | . "as lots of things could happen when you leave by other means, such as cave-ins, " |
64 | . "or monsters suddenly snapping your body. Better use a savebed next time.", |
85 | . "or monsters suddenly snapping your body. Better use a savebed next time.", |
65 | cf::NDI_RED |
86 | cf::NDI_RED |
|
|
87 | ); |
|
|
88 | } |
66 | ); |
89 | } |
67 | #d#TODO |
|
|
68 | } |
90 | } |
|
|
91 | |
|
|
92 | $pl->ob->goto ($map, $x, $y); |
69 | } |
93 | } |
70 | |
94 | |
71 | # delete a player directory, be non-blocking AND synchronous... |
95 | # delete a player directory, be non-blocking AND synchronous... |
72 | # (thats hard, so we crap out and fork). |
96 | # (thats hard, so we crap out and fork). |
73 | sub nuke_playerdir { |
97 | sub nuke_playerdir { |
… | |
… | |
77 | system "cd \Q$PLAYERDIR\E " |
101 | system "cd \Q$PLAYERDIR\E " |
78 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
102 | . "&& mv \Q$user\E ~\Q$Coro::current\E~deleting~ 2>/dev/null " |
79 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
103 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
80 | } |
104 | } |
81 | |
105 | |
82 | sub addme { |
106 | cf::client->attach (on_addme => sub { |
83 | my ($ns) = @_; |
107 | my ($ns) = @_; |
84 | |
108 | |
85 | $ns->destroy if $ns->pl; |
109 | if (!$ns->facecache) |
|
|
110 | { |
|
|
111 | $ns->send_drawinfo (<<EOF, cf::NDI_RED); |
|
|
112 | |
|
|
113 | |
|
|
114 | *** |
|
|
115 | *** WARNING: |
|
|
116 | *** Your client does not support face/image caching, |
|
|
117 | *** or it has been disabled. Face caching is mandatory |
|
|
118 | *** so please enable it or use a newer client. |
|
|
119 | *** |
|
|
120 | *** Look at your client preferences: |
|
|
121 | *** |
|
|
122 | *** CFPlus: all known versions automatically enable the facecache. |
|
|
123 | *** cfclient: use the -cache commandline option. |
|
|
124 | *** cfclient: map will not redraw automatically (bug). |
|
|
125 | *** gcfclient: use -cache commandline option, or enable |
|
|
126 | *** gcfclient: Client=>Configure=>Map & Image=>Cache Images. |
|
|
127 | *** jcrossclient: your client is broken, use CFPlus or gcfclient. |
|
|
128 | *** |
|
|
129 | *** |
|
|
130 | EOF |
|
|
131 | if ($ns->version =~ /jcrossclient/) { |
|
|
132 | # let them, for now |
|
|
133 | } else { |
|
|
134 | $ns->flush; |
|
|
135 | return $ns->destroy; |
|
|
136 | } |
|
|
137 | |
|
|
138 | # $ns->facecache = true; |
|
|
139 | } |
|
|
140 | |
|
|
141 | if ($ns->mapmode < cf::Map1aCmd) { |
|
|
142 | $ns->send_drawinfo (<<EOF, cf::NDI_RED); |
|
|
143 | |
|
|
144 | |
|
|
145 | *** |
|
|
146 | *** WARNING: |
|
|
147 | *** Your client is too old. Please upgrade to a newer version. |
|
|
148 | EOF |
|
|
149 | |
|
|
150 | $ns->flush; |
|
|
151 | return $ns->destroy; |
|
|
152 | } |
|
|
153 | |
|
|
154 | $ns->pl and return $ns->destroy; |
86 | |
155 | |
87 | $ns->async (sub { |
156 | $ns->async (sub { |
88 | my ($user, $pass); |
157 | my ($user, $pass); |
89 | |
158 | |
90 | $ns->send_packet ("addme_success"); |
159 | $ns->send_packet ("addme_success"); |
… | |
… | |
105 | $ns->send_drawinfo ( |
174 | $ns->send_drawinfo ( |
106 | "That username is currently used in another login session. " |
175 | "That username is currently used in another login session. " |
107 | . "Chose another, or wait till the other session has ended.", |
176 | . "Chose another, or wait till the other session has ended.", |
108 | cf::NDI_RED |
177 | cf::NDI_RED |
109 | ); |
178 | ); |
110 | } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { |
179 | } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) { |
111 | last; |
180 | last; |
112 | } else { |
181 | } else { |
113 | $ns->send_drawinfo ( |
182 | $ns->send_drawinfo ( |
114 | "Your username contains illegal characters " |
183 | "Your username contains illegal characters " |
115 | . "(only a-z, A-Z and 0-9 are allowed), " |
184 | . "(only a-z, A-Z and 0-9 are allowed), " |
… | |
… | |
161 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
230 | if ($cf::CFG{ext_login_nocheck} or $hash eq crypt $pass, $hash) { |
162 | nuke_str $pass; |
231 | nuke_str $pass; |
163 | # password matches, wonderful |
232 | # password matches, wonderful |
164 | my $pl = cf::player::find $user or next; |
233 | my $pl = cf::player::find $user or next; |
165 | $pl->connect ($ns); |
234 | $pl->connect ($ns); |
166 | check_clean_save $pl; |
235 | enter_map $pl; |
167 | last; |
236 | last; |
168 | } elsif (can_cleanup $pl, $mtime) { |
237 | } elsif (can_cleanup $pl, $mtime) { |
169 | Coro::Timer::sleep 1; |
238 | Coro::Timer::sleep 1; |
170 | |
239 | |
171 | $ns->send_drawinfo ( |
240 | $ns->send_drawinfo ( |
… | |
… | |
192 | Coro::Timer::sleep 1; |
261 | Coro::Timer::sleep 1; |
193 | |
262 | |
194 | $ns->send_drawinfo ( |
263 | $ns->send_drawinfo ( |
195 | "Wrong username or password. Please try again " |
264 | "Wrong username or password. Please try again " |
196 | . "(check for Numlock and other semi-obvious error sources).", |
265 | . "(check for Numlock and other semi-obvious error sources).", |
|
|
266 | cf::NDI_RED |
|
|
267 | ); |
|
|
268 | next; |
|
|
269 | } |
|
|
270 | } else { |
|
|
271 | # unable to load the playerfile: |
|
|
272 | # check wether the player dir exists, which means the file is corrupted or |
|
|
273 | # something very similar. |
|
|
274 | if (!aio_stat cf::player::playerdir $user) { |
|
|
275 | $ns->send_drawinfo ( |
|
|
276 | "Unable to retrieve this player. It might be a locked or broken account. " |
|
|
277 | . "If this is your account, ask a dungeon master for assistance. " |
|
|
278 | . "Otherwise choose a different login name.", |
197 | cf::NDI_RED |
279 | cf::NDI_RED |
198 | ); |
280 | ); |
199 | next; |
281 | next; |
200 | } |
282 | } |
201 | } |
283 | } |
… | |
… | |
221 | |
303 | |
222 | my $pl = cf::player::new $user; |
304 | my $pl = cf::player::new $user; |
223 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
305 | $pl->password (crypt $pass, join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]); |
224 | nuke_str $pass; |
306 | nuke_str $pass; |
225 | $pl->connect ($ns); |
307 | $pl->connect ($ns); |
226 | |
|
|
227 | my $ob = $pl->ob; |
308 | my $ob = $pl->ob; |
|
|
309 | |
|
|
310 | $ob->goto ($pl->maplevel, $ob->x, $ob->y); |
228 | |
311 | |
229 | while () { |
312 | while () { |
230 | $ob->update_stats; |
313 | $ob->update_stats; |
231 | $pl->save_stats; |
314 | $pl->save_stats; |
232 | |
315 | |
… | |
… | |
247 | } |
330 | } |
248 | |
331 | |
249 | $ob->set_animation (2); |
332 | $ob->set_animation (2); |
250 | $ob->add_statbonus; |
333 | $ob->add_statbonus; |
251 | |
334 | |
252 | $ns->send_drawinfo ($ob->msg, cf::NDI_BLUE); |
335 | while () { |
253 | $ns->send_packet (sprintf "query %d %s", cf::CS_QUERY_SINGLECHAR, |
336 | $ns->send_msg (-1, "chargen-race-title", ucfirst $pl->title); |
|
|
337 | my $msg = $ob->msg; |
|
|
338 | $msg =~ s/(?<=\S)\n(?=\S)/ /g; |
|
|
339 | $ns->send_msg (cf::NDI_BLUE, "chargen-race-description", $msg); |
|
|
340 | |
|
|
341 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
254 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"); |
342 | "Now choose a character.\nPress any key to change outlook.\nPress `d' when you're pleased.\n"; |
255 | |
343 | |
256 | $ns->state (cf::ST_CHANGE_CLASS); |
344 | last if $res =~ /[dD]/; |
|
|
345 | |
|
|
346 | $pl->chargen_race_next; |
|
|
347 | } |
|
|
348 | |
|
|
349 | $pl->chargen_race_done; |
|
|
350 | |
|
|
351 | while () { |
|
|
352 | my $res = query $ns, cf::CS_QUERY_SINGLECHAR, |
|
|
353 | "Now choose a gender.\nPress 'f' to become female, and 'm' to become male.\n"; |
|
|
354 | |
|
|
355 | if ($res =~ /^[fF]/) { |
|
|
356 | $pl->gender (1); |
|
|
357 | last; |
|
|
358 | } elsif ($res =~ /^[mM]/) { |
|
|
359 | $pl->gender (0); |
|
|
360 | last; |
|
|
361 | } |
|
|
362 | } |
|
|
363 | |
|
|
364 | $ob->reply (undef, "Welcome to Crossfire!"); |
|
|
365 | |
257 | delete $pl->{deny_save};#d# too early |
366 | delete $pl->{deny_save}; |
258 | |
367 | |
259 | last; |
368 | last; |
260 | } |
369 | } |
261 | }); |
370 | }); |
262 | } |
371 | }); |
263 | |
372 | |
264 | cf::register_command quit => sub { |
373 | cf::register_command quit => sub { |
265 | my ($ob, $arg) = @_; |
374 | my ($ob, $arg) = @_; |
266 | |
375 | |
267 | $ob->reply (undef, |
376 | $ob->reply (undef, |
… | |
… | |
283 | } else { |
392 | } else { |
284 | $ob->reply (undef, |
393 | $ob->reply (undef, |
285 | "Ok, quitting, hope to see you again.", |
394 | "Ok, quitting, hope to see you again.", |
286 | cf::NDI_UNIQUE | cf::NDI_RED); |
395 | cf::NDI_UNIQUE | cf::NDI_RED); |
287 | $pl->ns->flush; |
396 | $pl->ns->flush; |
288 | $pl->quit_character; |
397 | cf::async { $pl->quit_character }; |
289 | } |
398 | } |
290 | }); |
399 | }); |
291 | }; |
400 | }; |
292 | |
401 | |
293 | cf::object->attach ( |
402 | cf::object->attach ( |
… | |
… | |
336 | } else { |
445 | } else { |
337 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
446 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
338 | $pl->{unclean_save} = $cf::RUNTIME; |
447 | $pl->{unclean_save} = $cf::RUNTIME; |
339 | } |
448 | } |
340 | }, |
449 | }, |
341 | ); |
|
|
342 | |
|
|
343 | cf::client->attach ( |
|
|
344 | on_addme => \&addme, |
|
|
345 | ); |
450 | ); |
346 | |
451 | |
347 | ############################################################################# |
452 | ############################################################################# |
348 | |
453 | |
349 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
454 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
… | |
… | |
364 | or next; |
469 | or next; |
365 | $pl->valid or next; |
470 | $pl->valid or next; |
366 | |
471 | |
367 | eval { |
472 | eval { |
368 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
473 | if ($pl->{last_save} + $SAVE_TIMEOUT <= $cf::RUNTIME) { |
369 | $cf::WAIT_FOR_TICK_ONE->wait; |
474 | cf::wait_for_tick_begin; |
370 | $pl->save; |
475 | $pl->save; |
371 | |
476 | |
372 | unless ($pl->active) { |
477 | unless ($pl->active || $pl->ns) { |
373 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
478 | # check refcounts, this is tricky and needs to be adjusted to fit server internals |
374 | my $ob = $pl->ob; |
479 | my $ob = $pl->ob; |
375 | Scalar::Util::weaken $pl; |
480 | |
376 | Scalar::Util::weaken $ob; |
|
|
377 | my $a_ = $pl->refcnt;#d# |
|
|
378 | my $b_ = $ob->refcnt;#d# |
|
|
379 | my $pl_ref = $pl->refcnt_cnt; |
481 | my $pl_ref = $pl->refcnt_cnt; |
380 | my $ob_ref = $ob->refcnt_cnt; |
482 | my $ob_ref = $ob->refcnt_cnt; |
381 | |
483 | |
382 | ## pl_ref == one from object + one from cf::PLAYER |
484 | ## pl_ref == $pl + ob->contr + %cf::PLAYER |
383 | ## ob_ref == one from simply being an object |
485 | ## ob_ref == $ob + pl->observe + simply being an object |
384 | #TODO: the above should be correct, understand the 1/0, respectively |
|
|
385 | if ($pl_ref == 1 && $ob_ref == 0) { |
486 | if ($pl_ref == 3 && $ob_ref == 3) { |
386 | warn "player-scheduler destroy ", $ob->name;#d# |
487 | warn "player-scheduler destroy ", $ob->name;#d# |
387 | |
488 | |
388 | # remove from sight and get fresh "copies" |
489 | # remove from sight and get fresh "copies" |
389 | $pl = delete $cf::PLAYER{$ob->name}; |
490 | $pl = delete $cf::PLAYER{$ob->name}; |
390 | $ob = $pl->ob; |
491 | $ob = $pl->ob; |
391 | |
492 | |
392 | $ob->destroy; |
|
|
393 | $pl->destroy; |
493 | $pl->destroy; # destroys $ob |
394 | } else { |
494 | } else { |
|
|
495 | my $a_ = $pl->refcnt;#d# |
|
|
496 | my $b_ = $ob->refcnt;#d# |
|
|
497 | |
395 | warn "player-scheduler refcnt ", $ob->name, " $pl_ref,$a_ $ob_ref,$b_\n";#d# |
498 | warn "player-scheduler refcnt ", $ob->name, " pl $pl_ref ob $ob_ref (C pl $a_ ob $b_)\n";#d# |
396 | } |
499 | } |
397 | } |
500 | } |
398 | } |
501 | } |
399 | }; |
502 | }; |
400 | warn $@ if $@; |
503 | warn $@ if $@; |