… | |
… | |
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 { |
… | |
… | |
77 | system "cd \Q$PLAYERDIR\E " |
78 | system "cd \Q$PLAYERDIR\E " |
78 | . "&& 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 " |
79 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
80 | . "&& (rm -rf ~\Q$Coro::current\E~deleting~ &)"; |
80 | } |
81 | } |
81 | |
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 | } 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 | |
82 | sub addme { |
178 | sub addme { |
83 | my ($ns) = @_; |
179 | my ($ns) = @_; |
84 | |
180 | |
85 | $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 | *** |
|
|
202 | EOF |
|
|
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. |
|
|
220 | EOF |
|
|
221 | |
|
|
222 | $ns->flush; |
|
|
223 | return $ns->destroy; |
|
|
224 | } |
|
|
225 | |
|
|
226 | $ns->pl and return $ns->destroy; |
86 | |
227 | |
87 | $ns->async (sub { |
228 | $ns->async (sub { |
88 | my ($user, $pass); |
229 | my ($user, $pass); |
89 | |
230 | |
90 | $ns->send_packet ("addme_success"); |
231 | $ns->send_packet ("addme_success"); |
… | |
… | |
105 | $ns->send_drawinfo ( |
246 | $ns->send_drawinfo ( |
106 | "That username is currently used in another login session. " |
247 | "That username is currently used in another login session. " |
107 | . "Chose another, or wait till the other session has ended.", |
248 | . "Chose another, or wait till the other session has ended.", |
108 | cf::NDI_RED |
249 | cf::NDI_RED |
109 | ); |
250 | ); |
110 | } 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/) { |
111 | last; |
252 | last; |
112 | } else { |
253 | } else { |
113 | $ns->send_drawinfo ( |
254 | $ns->send_drawinfo ( |
114 | "Your username contains illegal characters " |
255 | "Your username contains illegal characters " |
115 | . "(only a-z, A-Z and 0-9 are allowed), " |
256 | . "(only a-z, A-Z and 0-9 are allowed), " |
… | |
… | |
283 | } else { |
424 | } else { |
284 | $ob->reply (undef, |
425 | $ob->reply (undef, |
285 | "Ok, quitting, hope to see you again.", |
426 | "Ok, quitting, hope to see you again.", |
286 | cf::NDI_UNIQUE | cf::NDI_RED); |
427 | cf::NDI_UNIQUE | cf::NDI_RED); |
287 | $pl->ns->flush; |
428 | $pl->ns->flush; |
288 | $pl->quit_character; |
429 | cf::async { $pl->quit_character }; |
289 | } |
430 | } |
290 | }); |
431 | }); |
291 | }; |
432 | }; |
292 | |
433 | |
293 | cf::object->attach ( |
434 | cf::object->attach ( |
… | |
… | |
340 | }, |
481 | }, |
341 | ); |
482 | ); |
342 | |
483 | |
343 | cf::client->attach ( |
484 | cf::client->attach ( |
344 | on_addme => \&addme, |
485 | on_addme => \&addme, |
|
|
486 | on_setup => \&setup, |
345 | ); |
487 | ); |
346 | |
488 | |
347 | ############################################################################# |
489 | ############################################################################# |
348 | |
490 | |
349 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
491 | our $SCHEDULE_INTERVAL = 10; # time the player scheduler sleeps between runs |
350 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
492 | our $SAVE_TIMEOUT = 20; # save players every n seconds |
351 | |
493 | |
352 | our $SCHEDULER = cf::async_ext { |
494 | our $SCHEDULER = cf::async_ext { |
|
|
495 | my $schedule_interval = Coro::Event->timer (after => 1, interval => $SCHEDULE_INTERVAL); |
353 | while () { |
496 | while () { |
354 | Coro::Timer::sleep $SCHEDULE_INTERVAL; |
497 | $schedule_interval->next; |
355 | |
498 | |
356 | # this weird form of iteration over values is used because |
499 | # this weird form of iteration over values is used because |
357 | # the hash changes underneath us frequently, and for |
500 | # the hash changes underneath us frequently, and for |
358 | # 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) |
359 | # keeping a reference, so this is prone to crashes or worse. |
502 | # keeping a reference, so this is prone to crashes or worse. |