1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | use Data::Dumper; |
3 | sub do_help { |
4 | use Carp; |
|
|
5 | cf::register_script_function "board::help" => sub { |
|
|
6 | # eval { Carp::confess; }; print "[[$@$!]]\n"; require Data::Dumper; |
|
|
7 | my ($board, $who, $npc) = @_; |
4 | my ($board, $who, $npc) = @_; |
8 | $who->reply ($npc, |
5 | $who->reply ($npc, |
9 | "Help for $board\n" |
6 | "Help for $board\n" |
10 | ."List of commands:\n\n" |
7 | ."List of commands:\n\n" |
11 | ."- list\n" |
8 | ."- list\n" |
12 | ."- write <message>\n" |
9 | ."- write <message>\n" |
13 | ."- remove <id>\n" |
10 | ."- remove <id>\n" |
14 | ); |
11 | ); |
15 | }; |
12 | } |
16 | |
13 | |
17 | cf::register_script_function "board::write" => sub { |
14 | sub do_write { |
18 | my ($board, $msg, $who, $npc) = @_; |
15 | my ($board, $msg, $who, $npc) = @_; |
19 | if ($msg =~ /\S/) { |
16 | if ($msg =~ /\S/) { |
20 | CFBoard::write ($board, $who->name, $msg); |
17 | CFBoard::write ($board, $who->name, $msg); |
21 | $who->reply ($npc, "Added to $board"); |
18 | $who->reply ($npc, "Added entry."); |
22 | } else { |
19 | } else { |
23 | $who->reply ($npc, "Usage: write <message>\n"); |
20 | $who->reply ($npc, "Usage: write <message>\n"); |
24 | } |
21 | } |
25 | 1 |
22 | 1 |
26 | }; |
23 | } |
27 | |
24 | |
28 | cf::register_script_function "board::list" => sub { |
25 | sub do_list { |
29 | my ($board, $who, $npc) = @_; |
26 | my ($board, $who, $npc) = @_; |
30 | my $cont = CFBoard::get ($board); |
27 | my $cont = CFBoard::get ($board); |
31 | if (@$cont) { |
28 | if (@{$cont || []}) { |
32 | $who->reply ($npc, "Content of $board:"); |
29 | $who->message ("$board content:", cf::NDI_BROWN | cf::NDI_UNIQUE); |
33 | my $idx = 0; |
30 | my $idx = 0; |
34 | for (@$cont) { |
31 | for (@$cont) { |
35 | $who->reply ($npc, "<$idx> $_->[0]: $_->[1]"); |
32 | $who->message ("<$idx> $_->[0]: $_->[1]", cf::NDI_BROWN | cf::NDI_UNIQUE); |
36 | $idx++; |
33 | $idx++; |
37 | } |
34 | } |
38 | } else { |
35 | } else { |
39 | $who->reply ($npc, "$board is empty."); |
36 | $who->message ("I'm empty.", cf::NDI_BROWN | cf::NDI_UNIQUE); |
40 | } |
37 | } |
41 | 1 |
38 | 1 |
42 | }; |
39 | } |
43 | |
40 | |
44 | cf::register_script_function "board::remove" => sub { |
41 | sub do_remove { |
45 | my ($board, $idx, $who, $npc) = @_; |
42 | my ($board, $idx, $who, $npc) = @_; |
46 | |
43 | |
47 | my $entry = CFBoard::get_idx ($board, $idx); |
44 | my $entry = CFBoard::get_idx ($board, $idx); |
48 | unless (defined $entry) { |
45 | unless (defined $entry) { |
49 | $who->reply ($npc, "No such entry."); |
46 | $who->reply ($npc, "No such entry."); |
50 | return 1; |
47 | return 1; |
51 | } |
48 | } |
52 | |
49 | |
53 | if ($entry->[0] eq $who->name or $who->flag (cf::FLAG_WIZ)) { |
50 | if ($entry->[0] eq $who->name or $who->flag (cf::FLAG_WIZ)) { |
54 | my $e = CFBoard::remove_idx ($board, $idx); |
51 | my $e = CFBoard::remove_idx ($board, $idx); |
55 | $who->reply ($npc, "Removed entry $idx ($e->[0]: $e->[1]) from $board."); |
52 | $who->reply ($npc, "Removed entry $idx ($e->[0]: $e->[1])."); |
56 | } else { |
53 | } else { |
57 | $who->reply ($npc, "Access denied."); |
54 | $who->reply ($npc, "Access denied."); |
58 | } |
55 | } |
|
|
56 | |
|
|
57 | 1 |
|
|
58 | } |
|
|
59 | |
|
|
60 | # this is the main command interface for the IPO NPC |
|
|
61 | cf::register_script_function "board::command" => sub { |
|
|
62 | my ($who, $msg, $npc) = @_; |
|
|
63 | my $board = $npc->name; |
|
|
64 | |
|
|
65 | if ($msg =~ /^list$/i) { |
|
|
66 | do_list ($board, $who, $npc); |
|
|
67 | } elsif ($msg =~ /^write (.+)$/i) { |
|
|
68 | do_write ($board, $1, $who, $npc); |
|
|
69 | } elsif ($msg =~ /^remove (\d+)$/i) { |
|
|
70 | do_remove ($board, $1, $who, $npc); |
|
|
71 | } else { |
|
|
72 | do_help ($board, $who, $npc); |
|
|
73 | } |
|
|
74 | |
59 | 1 |
75 | 1 |
60 | }; |
76 | }; |
61 | |
77 | |
|
|
78 | sub on_apply { |
|
|
79 | my ($ev, $npc, $who) = @_; |
|
|
80 | $who->reply ($npc, "Hello, i'm a talking board, 'say help' to get help"); |
|
|
81 | do_list ($npc->name, $who, $npc); |
|
|
82 | 1; |
|
|
83 | } |
62 | |
84 | |
63 | package CFBoard; |
85 | package CFBoard; |
64 | use POSIX qw/strftime/; |
86 | use POSIX qw/strftime/; |
65 | use CFDB; |
87 | use CFDB; |
66 | |
88 | |