ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.282 by elmex, Tue Jun 19 18:15:34 2007 UTC vs.
Revision 1.287 by root, Mon Jun 25 05:43:45 2007 UTC

49our %COMMAND = (); 49our %COMMAND = ();
50our %COMMAND_TIME = (); 50our %COMMAND_TIME = ();
51 51
52our @EXTS = (); # list of extension package names 52our @EXTS = (); # list of extension package names
53our %EXTCMD = (); 53our %EXTCMD = ();
54our %EXTICMD = ();
54our %EXT_CORO = (); # coroutines bound to extensions 55our %EXT_CORO = (); # coroutines bound to extensions
55our %EXT_MAP = (); # pluggable maps 56our %EXT_MAP = (); # pluggable maps
56 57
57our $RELOAD; # number of reloads so far 58our $RELOAD; # number of reloads so far
58our @EVENT; 59our @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
241use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working 242use JSON::XS ();
242 243
243=item $ref = cf::from_json $json 244=item $ref = cf::from_json $json
244 245
245Converts a JSON string into the corresponding perl data structure. 246Converts 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
249Converts a perl data structure into its JSON representation. 250Converts a perl data structure into its JSON representation.
251
252=cut
253
254our $json_coder = JSON::XS->new->convert_blessed->utf8;
255
256sub to_json ($) { $json_coder->encode ($_[0]) }
257sub from_json ($) { $json_coder->decode ($_[0]) }
250 258
251=item cf::lock_wait $string 259=item cf::lock_wait $string
252 260
253Wait until the given lock is available. See cf::lock_acquire. 261Wait 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
1034Register a callback fro execution when the client sends an extcmd packet. 1042Register a callback for execution when the client sends an (synchronous)
1043extcmd packet. Ext commands will be processed in the order they are
1044received by the server, like other user commands. The first argument is
1045the logged-in player. Ext commands can only be processed after a player
1046has logged in successfully.
1035 1047
1036If the callback returns something, it is sent back as if reply was being 1048If the callback returns something, it is sent back as if reply was being
1037called. 1049called.
1038 1050
1051=item cf::register_exticmd $name => \&callback($ns,$packet);
1052
1053Register a callback for execution when the client sends an (asynchronous)
1054exticmd packet. Exti commands are processed by the server as soon as they
1055are received, i.e. out of order w.r.t. other commands. The first argument
1056is a client socket. Exti commands can be received anytime, even before
1057log-in.
1058
1059If the callback returns something, it is sent back as if reply was being
1060called.
1061
1039=cut 1062=cut
1040 1063
1041sub register_extcmd { 1064sub register_extcmd {
1042 my ($name, $cb) = @_; 1065 my ($name, $cb) = @_;
1043 1066
1044 $EXTCMD{$name} = $cb; 1067 $EXTCMD{$name} = $cb;
1068}
1069
1070sub register_exticmd {
1071 my ($name, $cb) = @_;
1072
1073 $EXTICMD{$name} = $cb;
1045} 1074}
1046 1075
1047cf::player->attach ( 1076cf::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
1383Expand crossfire pod fragments into protocol xml.
1384
1385=cut
1386
1387sub expand_cfpod {
1388 ((my $self), (local $_)) = @_;
1389
1390 # escape & and <
1391 s/&/&amp;/g;
1392 s/(?<![BIUGH])</&lt;/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
1354Sends an ext reply to the player. 1416Sends an ext reply to the player.
1355 1417
1356=cut 1418=cut
1357 1419
1358sub ext_reply($$%) { 1420sub 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
2052 my $pl = $self->contr; 2113 my $pl = $self->contr;
2053 2114
2054 if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) { 2115 if ($pl->{npc_dialog} && $pl->{npc_dialog}->{id}) {
2055 my $diag = $pl->{npc_dialog}; 2116 my $diag = $pl->{npc_dialog};
2056 $diag->{pl}->ext_reply ( 2117 $diag->{pl}->ext_reply (
2057 $diag->{id}, msgtype => "reply", msg => $msg, add_topics => [] 2118 $diag->{id},
2119 msgtype => "reply",
2120 msg => $diag->{pl}->expand_cfpod ($msg),
2121 add_topics => []
2058 ); 2122 );
2059 2123
2060 } else { 2124 } else {
2061 $msg = $npc->name . " says: $msg" if $npc; 2125 $msg = $npc->name . " says: $msg" if $npc;
2062 $self->message ($msg, $flags); 2126 $self->message ($msg, $flags);
2313 2377
2314 utf8::encode $text; 2378 utf8::encode $text;
2315 $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);
2316} 2380}
2317 2381
2382=item $client->send_msg ($color, $type, $msg, [extra...])
2383
2384Send a drawinfo or msg packet to the client, formatting the msg for the
2385client if neccessary. C<$type> should be a string identifying the type of
2386the message, with C<log> being the default. If C<$color> is negative, suppress
2387the message unless the client supports the msg packet.
2388
2389=cut
2390
2391sub 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
2318=item $client->ext_event ($type, %msg) 2420=item $client->ext_event ($type, %msg)
2319 2421
2320Sends an exti event to the client. 2422Sends an ext event to the client.
2321 2423
2322=cut 2424=cut
2323 2425
2324sub cf::client::ext_event($$%) { 2426sub cf::client::ext_event($$%) {
2325 my ($self, $type, %msg) = @_; 2427 my ($self, $type, %msg) = @_;
2332 2434
2333Queues a query to the client, calling the given callback with 2435Queues a query to the client, calling the given callback with
2334the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, 2436the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>,
2335C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. 2437C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>.
2336 2438
2337Queries can fail, so check the return code. Or don't, as queries will become 2439Queries can fail, so check the return code. Or don't, as queries will
2338reliable at some point in the future. 2440become reliable at some point in the future.
2339 2441
2340=cut 2442=cut
2341 2443
2342sub cf::client::query { 2444sub cf::client::query {
2343 my ($self, $flags, $text, $cb) = @_; 2445 my ($self, $flags, $text, $cb) = @_;
2351 utf8::encode $text; 2453 utf8::encode $text;
2352 push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; 2454 push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb];
2353 2455
2354 $self->send_packet ($self->{query_queue}[0][0]) 2456 $self->send_packet ($self->{query_queue}[0][0])
2355 if @{ $self->{query_queue} } == 1; 2457 if @{ $self->{query_queue} } == 1;
2458
2459 1
2356} 2460}
2357 2461
2358cf::client->attach ( 2462cf::client->attach (
2359 on_reply => sub { 2463 on_reply => sub {
2360 my ($ns, $msg) = @_; 2464 my ($ns, $msg) = @_;
2374 $ns->send_packet ($ns->{query_queue}[0][0]); 2478 $ns->send_packet ($ns->{query_queue}[0][0]);
2375 } else { 2479 } else {
2376 $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; 2480 $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM;
2377 } 2481 }
2378 } 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;
2379 }, 2501 },
2380); 2502);
2381 2503
2382=item $client->async (\&cb) 2504=item $client->async (\&cb)
2383 2505
2790 %EXT_CORO = (); 2912 %EXT_CORO = ();
2791 2913
2792 warn "removing commands"; 2914 warn "removing commands";
2793 %COMMAND = (); 2915 %COMMAND = ();
2794 2916
2795 warn "removing ext commands"; 2917 warn "removing ext/exti commands";
2796 %EXTCMD = (); 2918 %EXTCMD = ();
2919 %EXTICMD = ();
2797 2920
2798 warn "unloading/nuking all extensions"; 2921 warn "unloading/nuking all extensions";
2799 for my $pkg (@EXTS) { 2922 for my $pkg (@EXTS) {
2800 warn "... unloading $pkg"; 2923 warn "... unloading $pkg";
2801 2924

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines