… | |
… | |
49 | our %COMMAND = (); |
49 | our %COMMAND = (); |
50 | our %COMMAND_TIME = (); |
50 | our %COMMAND_TIME = (); |
51 | |
51 | |
52 | our @EXTS = (); # list of extension package names |
52 | our @EXTS = (); # list of extension package names |
53 | our %EXTCMD = (); |
53 | our %EXTCMD = (); |
|
|
54 | our %EXTICMD = (); |
54 | our %EXT_CORO = (); # coroutines bound to extensions |
55 | our %EXT_CORO = (); # coroutines bound to extensions |
55 | our %EXT_MAP = (); # pluggable maps |
56 | our %EXT_MAP = (); # pluggable maps |
56 | |
57 | |
57 | our $RELOAD; # number of reloads so far |
58 | our $RELOAD; # number of reloads so far |
58 | our @EVENT; |
59 | our @EVENT; |
… | |
… | |
236 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
237 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
237 | $d |
238 | $d |
238 | } || "[unable to dump $_[0]: '$@']"; |
239 | } || "[unable to dump $_[0]: '$@']"; |
239 | } |
240 | } |
240 | |
241 | |
241 | use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working |
242 | use JSON::XS (); |
242 | |
243 | |
243 | =item $ref = cf::from_json $json |
244 | =item $ref = cf::from_json $json |
244 | |
245 | |
245 | Converts a JSON string into the corresponding perl data structure. |
246 | Converts a JSON string into the corresponding perl data structure. |
246 | |
247 | |
247 | =item $json = cf::to_json $ref |
248 | =item $json = cf::to_json $ref |
248 | |
249 | |
249 | Converts a perl data structure into its JSON representation. |
250 | Converts a perl data structure into its JSON representation. |
|
|
251 | |
|
|
252 | =cut |
|
|
253 | |
|
|
254 | our $json_coder = JSON::XS->new->convert_blessed->utf8; |
|
|
255 | |
|
|
256 | sub to_json ($) { $json_coder->encode ($_[0]) } |
|
|
257 | sub from_json ($) { $json_coder->decode ($_[0]) } |
250 | |
258 | |
251 | =item cf::lock_wait $string |
259 | =item cf::lock_wait $string |
252 | |
260 | |
253 | Wait until the given lock is available. See cf::lock_acquire. |
261 | Wait until the given lock is available. See cf::lock_acquire. |
254 | |
262 | |
… | |
… | |
1029 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
1037 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
1030 | } |
1038 | } |
1031 | |
1039 | |
1032 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
1040 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
1033 | |
1041 | |
1034 | Register a callback fro execution when the client sends an extcmd packet. |
1042 | Register a callback for execution when the client sends an (synchronous) |
|
|
1043 | extcmd packet. Ext commands will be processed in the order they are |
|
|
1044 | received by the server, like other user commands. The first argument is |
|
|
1045 | the logged-in player. Ext commands can only be processed after a player |
|
|
1046 | has logged in successfully. |
1035 | |
1047 | |
1036 | If the callback returns something, it is sent back as if reply was being |
1048 | If the callback returns something, it is sent back as if reply was being |
1037 | called. |
1049 | called. |
1038 | |
1050 | |
|
|
1051 | =item cf::register_exticmd $name => \&callback($ns,$packet); |
|
|
1052 | |
|
|
1053 | Register a callback for execution when the client sends an (asynchronous) |
|
|
1054 | exticmd packet. Exti commands are processed by the server as soon as they |
|
|
1055 | are received, i.e. out of order w.r.t. other commands. The first argument |
|
|
1056 | is a client socket. Exti commands can be received anytime, even before |
|
|
1057 | log-in. |
|
|
1058 | |
|
|
1059 | If the callback returns something, it is sent back as if reply was being |
|
|
1060 | called. |
|
|
1061 | |
1039 | =cut |
1062 | =cut |
1040 | |
1063 | |
1041 | sub register_extcmd { |
1064 | sub register_extcmd { |
1042 | my ($name, $cb) = @_; |
1065 | my ($name, $cb) = @_; |
1043 | |
1066 | |
1044 | $EXTCMD{$name} = $cb; |
1067 | $EXTCMD{$name} = $cb; |
|
|
1068 | } |
|
|
1069 | |
|
|
1070 | sub register_exticmd { |
|
|
1071 | my ($name, $cb) = @_; |
|
|
1072 | |
|
|
1073 | $EXTICMD{$name} = $cb; |
1045 | } |
1074 | } |
1046 | |
1075 | |
1047 | cf::player->attach ( |
1076 | cf::player->attach ( |
1048 | on_command => sub { |
1077 | on_command => sub { |
1049 | my ($pl, $name, $params) = @_; |
1078 | my ($pl, $name, $params) = @_; |
… | |
… | |
1347 | } |
1376 | } |
1348 | |
1377 | |
1349 | \@paths |
1378 | \@paths |
1350 | } |
1379 | } |
1351 | |
1380 | |
|
|
1381 | =item $protocol_xml = $player->expand_cfpod ($crossfire_pod) |
|
|
1382 | |
|
|
1383 | Expand crossfire pod fragments into protocol xml. |
|
|
1384 | |
|
|
1385 | =cut |
|
|
1386 | |
|
|
1387 | sub expand_cfpod { |
|
|
1388 | ((my $self), (local $_)) = @_; |
|
|
1389 | |
|
|
1390 | # escape & and < |
|
|
1391 | s/&/&/g; |
|
|
1392 | s/(?<![BIUGH])</</g; |
|
|
1393 | |
|
|
1394 | # this is buggy, it needs to properly take care of nested <'s |
|
|
1395 | |
|
|
1396 | 1 while |
|
|
1397 | # replace B<>, I<>, U<> etc. |
|
|
1398 | s/B<([^\>]*)>/<b>$1<\/b>/ |
|
|
1399 | || s/I<([^\>]*)>/<i>$1<\/i>/ |
|
|
1400 | || s/U<([^\>]*)>/<u>$1<\/u>/ |
|
|
1401 | # replace G<male|female> tags |
|
|
1402 | || s{G<([^>|]*)\|([^>]*)>}{ |
|
|
1403 | $self->gender ? $2 : $1 |
|
|
1404 | }ge |
|
|
1405 | # replace H<hint text> |
|
|
1406 | || s/H<([^\>]*)>/<fg name="lightblue">[$1]<\/fg>/g; |
|
|
1407 | |
|
|
1408 | # create single paragraphs (very hackish) |
|
|
1409 | s/(?<=\S)\n(?=\w)/ /g; |
|
|
1410 | |
|
|
1411 | $_ |
|
|
1412 | } |
|
|
1413 | |
1352 | =item $player->ext_reply ($msgid, %msg) |
1414 | =item $player->ext_reply ($msgid, %msg) |
1353 | |
1415 | |
1354 | Sends an ext reply to the player. |
1416 | Sends an ext reply to the player. |
1355 | |
1417 | |
1356 | =cut |
1418 | =cut |
1357 | |
1419 | |
1358 | sub ext_reply($$%) { |
1420 | sub ext_reply($$%) { |
1359 | my ($self, $id, %msg) = @_; |
1421 | my ($self, $id, %msg) = @_; |
1360 | |
1422 | |
1361 | $msg{msgid} = $id; |
1423 | $msg{msgid} = $id; |
1362 | |
|
|
1363 | $self->send ("ext " . cf::to_json \%msg); |
1424 | $self->send ("ext " . cf::to_json \%msg); |
1364 | } |
1425 | } |
1365 | |
1426 | |
1366 | =item $player->ext_event ($type, %msg) |
1427 | =item $player->ext_event ($type, %msg) |
1367 | |
1428 | |
… | |
… | |
2045 | |
2106 | |
2046 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
2107 | $flags = cf::NDI_BROWN | cf::NDI_UNIQUE unless @_ >= 4; |
2047 | |
2108 | |
2048 | if ($self->{record_replies}) { |
2109 | if ($self->{record_replies}) { |
2049 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
2110 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
|
|
2111 | |
2050 | } else { |
2112 | } else { |
|
|
2113 | my $pl = $self->contr; |
|
|
2114 | |
|
|
2115 | if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) { |
|
|
2116 | my $diag = $pl->{npc_dialog}; |
|
|
2117 | $diag->{pl}->ext_reply ( |
|
|
2118 | $diag->{id}, |
|
|
2119 | msgtype => "reply", |
|
|
2120 | msg => $diag->{pl}->expand_cfpod ($msg), |
|
|
2121 | add_topics => [] |
|
|
2122 | ); |
|
|
2123 | |
|
|
2124 | } else { |
2051 | $msg = $npc->name . " says: $msg" if $npc; |
2125 | $msg = $npc->name . " says: $msg" if $npc; |
2052 | $self->message ($msg, $flags); |
2126 | $self->message ($msg, $flags); |
|
|
2127 | } |
2053 | } |
2128 | } |
2054 | } |
2129 | } |
2055 | |
2130 | |
2056 | =item $player_object->may ("access") |
2131 | =item $player_object->may ("access") |
2057 | |
2132 | |
… | |
… | |
2302 | |
2377 | |
2303 | utf8::encode $text; |
2378 | utf8::encode $text; |
2304 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2379 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2305 | } |
2380 | } |
2306 | |
2381 | |
|
|
2382 | =item $client->send_msg ($color, $type, $msg, [extra...]) |
|
|
2383 | |
|
|
2384 | Send a drawinfo or msg packet to the client, formatting the msg for the |
|
|
2385 | client if neccessary. C<$type> should be a string identifying the type of |
|
|
2386 | the message, with C<log> being the default. If C<$color> is negative, suppress |
|
|
2387 | the message unless the client supports the msg packet. |
|
|
2388 | |
|
|
2389 | =cut |
|
|
2390 | |
|
|
2391 | sub cf::client::send_msg { |
|
|
2392 | my ($self, $color, $type, $msg, @extra) = @_; |
|
|
2393 | |
|
|
2394 | $msg = $self->pl->expand_cfpod ($msg); |
|
|
2395 | |
|
|
2396 | if ($self->can_msg) { |
|
|
2397 | $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); |
|
|
2398 | } else { |
|
|
2399 | # replace some tags by gcfclient-compatible ones |
|
|
2400 | for ($msg) { |
|
|
2401 | 1 while |
|
|
2402 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
|
|
2403 | || s/<i>([^<]*)<\/i>/[i]${1}[\/i]/ |
|
|
2404 | || s/<u>([^<]*)<\/u>/[ul]${1}[\/ul]/ |
|
|
2405 | || s/<tt>([^<]*)<\/tt>/[fixed]${1}[\/fixed]/ |
|
|
2406 | || s/<fg name=\"([^"]+)\">([^<]*)<\/fg>/[color=$1]${2}[\/color]/; |
|
|
2407 | } |
|
|
2408 | |
|
|
2409 | if ($color >= 0) { |
|
|
2410 | if (0 && $msg =~ /\[/) { |
|
|
2411 | $self->send_packet ("drawextinfo $color 4 0 $msg") |
|
|
2412 | } else { |
|
|
2413 | $msg =~ s/\[\/?(?:b|i|u|fixed|color)[^\]]*\]//g; |
|
|
2414 | $self->send_packet ("drawinfo $color $msg") |
|
|
2415 | } |
|
|
2416 | } |
|
|
2417 | } |
|
|
2418 | } |
|
|
2419 | |
2307 | =item $client->ext_event ($type, %msg) |
2420 | =item $client->ext_event ($type, %msg) |
2308 | |
2421 | |
2309 | Sends an exti event to the client. |
2422 | Sends an ext event to the client. |
2310 | |
2423 | |
2311 | =cut |
2424 | =cut |
2312 | |
2425 | |
2313 | sub cf::client::ext_event($$%) { |
2426 | sub cf::client::ext_event($$%) { |
2314 | my ($self, $type, %msg) = @_; |
2427 | my ($self, $type, %msg) = @_; |
… | |
… | |
2321 | |
2434 | |
2322 | Queues a query to the client, calling the given callback with |
2435 | Queues a query to the client, calling the given callback with |
2323 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2436 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2324 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
2437 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
2325 | |
2438 | |
2326 | Queries can fail, so check the return code. Or don't, as queries will become |
2439 | Queries can fail, so check the return code. Or don't, as queries will |
2327 | reliable at some point in the future. |
2440 | become reliable at some point in the future. |
2328 | |
2441 | |
2329 | =cut |
2442 | =cut |
2330 | |
2443 | |
2331 | sub cf::client::query { |
2444 | sub cf::client::query { |
2332 | my ($self, $flags, $text, $cb) = @_; |
2445 | my ($self, $flags, $text, $cb) = @_; |
… | |
… | |
2340 | utf8::encode $text; |
2453 | utf8::encode $text; |
2341 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
2454 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
2342 | |
2455 | |
2343 | $self->send_packet ($self->{query_queue}[0][0]) |
2456 | $self->send_packet ($self->{query_queue}[0][0]) |
2344 | if @{ $self->{query_queue} } == 1; |
2457 | if @{ $self->{query_queue} } == 1; |
|
|
2458 | |
|
|
2459 | 1 |
2345 | } |
2460 | } |
2346 | |
2461 | |
2347 | cf::client->attach ( |
2462 | cf::client->attach ( |
2348 | on_reply => sub { |
2463 | on_reply => sub { |
2349 | my ($ns, $msg) = @_; |
2464 | my ($ns, $msg) = @_; |
… | |
… | |
2363 | $ns->send_packet ($ns->{query_queue}[0][0]); |
2478 | $ns->send_packet ($ns->{query_queue}[0][0]); |
2364 | } else { |
2479 | } else { |
2365 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
2480 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
2366 | } |
2481 | } |
2367 | } |
2482 | } |
|
|
2483 | }, |
|
|
2484 | on_exticmd => sub { |
|
|
2485 | my ($ns, $buf) = @_; |
|
|
2486 | |
|
|
2487 | my $msg = eval { from_json $buf }; |
|
|
2488 | |
|
|
2489 | if (ref $msg) { |
|
|
2490 | if (my $cb = $EXTICMD{$msg->{msgtype}}) { |
|
|
2491 | if (my %reply = $cb->($ns, $msg)) { |
|
|
2492 | $reply{msgid} = $msg->{msgid}; |
|
|
2493 | $ns->send ("ext " . cf::to_json \%reply); |
|
|
2494 | } |
|
|
2495 | } |
|
|
2496 | } else { |
|
|
2497 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
|
|
2498 | } |
|
|
2499 | |
|
|
2500 | cf::override; |
2368 | }, |
2501 | }, |
2369 | ); |
2502 | ); |
2370 | |
2503 | |
2371 | =item $client->async (\&cb) |
2504 | =item $client->async (\&cb) |
2372 | |
2505 | |
… | |
… | |
2779 | %EXT_CORO = (); |
2912 | %EXT_CORO = (); |
2780 | |
2913 | |
2781 | warn "removing commands"; |
2914 | warn "removing commands"; |
2782 | %COMMAND = (); |
2915 | %COMMAND = (); |
2783 | |
2916 | |
2784 | warn "removing ext commands"; |
2917 | warn "removing ext/exti commands"; |
2785 | %EXTCMD = (); |
2918 | %EXTCMD = (); |
|
|
2919 | %EXTICMD = (); |
2786 | |
2920 | |
2787 | warn "unloading/nuking all extensions"; |
2921 | warn "unloading/nuking all extensions"; |
2788 | for my $pkg (@EXTS) { |
2922 | for my $pkg (@EXTS) { |
2789 | warn "... unloading $pkg"; |
2923 | warn "... unloading $pkg"; |
2790 | |
2924 | |