… | |
… | |
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 | use List::Util qw(min max); |
7 | |
8 | |
8 | my $PLAYERDIR = sprintf "%s/%s", cf::localdir, cf::playerdir; |
9 | my $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 |
11 | sub nuke_str { |
12 | sub nuke_str { |
… | |
… | |
55 | } |
56 | } |
56 | |
57 | |
57 | sub check_clean_save { |
58 | sub 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 | |
|
|
83 | sub 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 | } elsif ($k eq "fxix") { |
|
|
161 | $ns->fxix ($v); |
|
|
162 | |
|
|
163 | } else { |
|
|
164 | # other commands: |
|
|
165 | # sexp: no idea, probably for oudated servers |
|
|
166 | # tick: more stupidity, server should sned a tick per tick |
|
|
167 | |
|
|
168 | $setup{$k} = "FALSE"; |
|
|
169 | } |
|
|
170 | } |
|
|
171 | |
|
|
172 | $ns->send_packet (join " ", setup => %setup); |
|
|
173 | |
|
|
174 | cf::datalog setup => |
|
|
175 | request => $args, |
|
|
176 | reply => \%setup, |
|
|
177 | client => $ns->version, |
|
|
178 | ; |
|
|
179 | } |
|
|
180 | |
76 | sub addme { |
181 | sub addme { |
77 | my ($ns) = @_; |
182 | my ($ns) = @_; |
78 | |
183 | |
79 | $ns->destroy if $ns->pl; |
184 | if (!$ns->facecache) |
|
|
185 | { |
|
|
186 | $ns->send_drawinfo (<<EOF, cf::NDI_RED); |
|
|
187 | |
|
|
188 | |
|
|
189 | *** |
|
|
190 | *** WARNING: |
|
|
191 | *** Your client does not support face/image caching, |
|
|
192 | *** or it has been disabled. Face caching is mandatory |
|
|
193 | *** so please enable it or use a newer client. |
|
|
194 | *** |
|
|
195 | *** Look at your client preferences: |
|
|
196 | *** |
|
|
197 | *** CFPlus: all known versions automatically enable the facecache. |
|
|
198 | *** cfclient: use the -cache commandline option. |
|
|
199 | *** cfclient: map will not redraw automatically (bug). |
|
|
200 | *** gcfclient: use -cache commandline option, or enable |
|
|
201 | *** gcfclient: Client=>Configure=>Map & Image=>Cache Images. |
|
|
202 | *** jcrossclient: your client is broken, use CFPlus or gcfclient. |
|
|
203 | *** |
|
|
204 | *** |
|
|
205 | EOF |
|
|
206 | if ($ns->version =~ /jcrossclient/) { |
|
|
207 | # let them, for now |
|
|
208 | } else { |
|
|
209 | $ns->flush; |
|
|
210 | return $ns->destroy; |
|
|
211 | } |
|
|
212 | |
|
|
213 | # $ns->facecache = true; |
|
|
214 | } |
|
|
215 | |
|
|
216 | if ($ns->mapmode < cf::Map1aCmd) { |
|
|
217 | $ns->send_drawinfo (<<EOF, cf::NDI_RED); |
|
|
218 | |
|
|
219 | |
|
|
220 | *** |
|
|
221 | *** WARNING: |
|
|
222 | *** Your client is too old. Please upgrade to a newer version. |
|
|
223 | EOF |
|
|
224 | |
|
|
225 | $ns->flush; |
|
|
226 | return $ns->destroy; |
|
|
227 | } |
|
|
228 | |
|
|
229 | $ns->pl and return $ns->destroy; |
80 | |
230 | |
81 | $ns->async (sub { |
231 | $ns->async (sub { |
82 | my ($user, $pass); |
232 | my ($user, $pass); |
83 | |
233 | |
84 | $ns->send_packet ("addme_success"); |
234 | $ns->send_packet ("addme_success"); |
… | |
… | |
99 | $ns->send_drawinfo ( |
249 | $ns->send_drawinfo ( |
100 | "That username is currently used in another login session. " |
250 | "That username is currently used in another login session. " |
101 | . "Chose another, or wait till the other session has ended.", |
251 | . "Chose another, or wait till the other session has ended.", |
102 | cf::NDI_RED |
252 | cf::NDI_RED |
103 | ); |
253 | ); |
104 | } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}$/) { |
254 | } elsif ($user =~ /^[a-zA-Z0-9][a-zA-Z0-9\-_]{2,17}\z/) { |
105 | last; |
255 | last; |
106 | } else { |
256 | } else { |
107 | $ns->send_drawinfo ( |
257 | $ns->send_drawinfo ( |
108 | "Your username contains illegal characters " |
258 | "Your username contains illegal characters " |
109 | . "(only a-z, A-Z and 0-9 are allowed), " |
259 | . "(only a-z, A-Z and 0-9 are allowed), " |
… | |
… | |
156 | nuke_str $pass; |
306 | nuke_str $pass; |
157 | # password matches, wonderful |
307 | # password matches, wonderful |
158 | my $pl = cf::player::find $user or next; |
308 | my $pl = cf::player::find $user or next; |
159 | $pl->connect ($ns); |
309 | $pl->connect ($ns); |
160 | check_clean_save $pl; |
310 | check_clean_save $pl; |
161 | $pl->{clean_save} = 1; |
|
|
162 | last; |
311 | last; |
163 | } elsif (can_cleanup $pl, $mtime) { |
312 | } elsif (can_cleanup $pl, $mtime) { |
164 | Coro::Timer::sleep 1; |
313 | Coro::Timer::sleep 1; |
165 | |
314 | |
166 | $ns->send_drawinfo ( |
315 | $ns->send_drawinfo ( |
… | |
… | |
278 | } else { |
427 | } else { |
279 | $ob->reply (undef, |
428 | $ob->reply (undef, |
280 | "Ok, quitting, hope to see you again.", |
429 | "Ok, quitting, hope to see you again.", |
281 | cf::NDI_UNIQUE | cf::NDI_RED); |
430 | cf::NDI_UNIQUE | cf::NDI_RED); |
282 | $pl->ns->flush; |
431 | $pl->ns->flush; |
283 | $pl->quit_character; |
432 | cf::async { $pl->quit_character }; |
284 | } |
433 | } |
285 | }); |
434 | }); |
286 | }; |
435 | }; |
287 | |
436 | |
288 | cf::object->attach ( |
437 | cf::object->attach ( |
… | |
… | |
328 | |
477 | |
329 | if ($cleanly) { |
478 | if ($cleanly) { |
330 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
479 | $_->ob->message ("$name left the game.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
331 | } else { |
480 | } else { |
332 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
481 | $_->ob->message ("$name uncerimoniously disconnected.", cf::NDI_DK_ORANGE | cf::NDI_UNIQUE) for cf::player::list; |
333 | delete $pl->{clean_save}; |
482 | $pl->{unclean_save} = $cf::RUNTIME; |
334 | } |
483 | } |
335 | }, |
484 | }, |
336 | ); |
485 | ); |
337 | |
486 | |
338 | cf::client->attach ( |
487 | cf::client->attach ( |
339 | on_addme => \&addme, |
488 | on_addme => \&addme, |
|
|
489 | on_setup => \&setup, |
340 | ); |
490 | ); |
341 | |
491 | |
342 | ############################################################################# |
492 | ############################################################################# |
343 | |
493 | |
344 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
494 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
345 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
495 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
346 | |
496 | |
347 | our $SCHEDULER = cf::async_ext { |
497 | our $SCHEDULER = cf::async_ext { |
|
|
498 | my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL); |
348 | while () { |
499 | while () { |
349 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
500 | $schedule_interval->next; |
350 | |
501 | |
351 | # this weird form of iteration over values is used because |
502 | # this weird form of iteration over values is used because |
352 | # the hash changes underneath us frequently, and for |
503 | # the hash changes underneath us frequently, and for |
353 | # keeps a direct reference to the value without (in 5.8 perls) |
504 | # keeps a direct reference to the value without (in 5.8 perls) |
354 | # keeping a reference, so this is prone to crashes or worse. |
505 | # keeping a reference, so this is prone to crashes or worse. |