1 |
#! perl |
2 |
|
3 |
sub do_help { |
4 |
my ($board, $who, $npc) = @_; |
5 |
$who->reply ($npc, |
6 |
"Help for $board\n" |
7 |
."List of commands:\n\n" |
8 |
."- list\n" |
9 |
."- write <message>\n" |
10 |
."- remove <id>\n" |
11 |
); |
12 |
} |
13 |
|
14 |
sub do_write { |
15 |
my ($board, $msg, $who, $npc) = @_; |
16 |
if ($msg =~ /\S/) { |
17 |
CFBoard::write ($board, $who->name, $msg); |
18 |
$who->reply ($npc, "Added entry."); |
19 |
} else { |
20 |
$who->reply ($npc, "Usage: write <message>\n"); |
21 |
} |
22 |
1 |
23 |
} |
24 |
|
25 |
sub do_list { |
26 |
my ($board, $who, $npc) = @_; |
27 |
my $cont = CFBoard::get ($board); |
28 |
if (@{$cont || []}) { |
29 |
$who->message ("$board content:", cf::NDI_BROWN | cf::NDI_UNIQUE); |
30 |
my $idx = 0; |
31 |
for (@$cont) { |
32 |
$who->message ("<$idx> $_->[0]: $_->[1]", cf::NDI_BROWN | cf::NDI_UNIQUE); |
33 |
$idx++; |
34 |
} |
35 |
} else { |
36 |
$who->message ("I'm empty.", cf::NDI_BROWN | cf::NDI_UNIQUE); |
37 |
} |
38 |
1 |
39 |
} |
40 |
|
41 |
sub do_remove { |
42 |
my ($board, $idx, $who, $npc) = @_; |
43 |
|
44 |
my $entry = CFBoard::get_idx ($board, $idx); |
45 |
unless (defined $entry) { |
46 |
$who->reply ($npc, "No such entry."); |
47 |
return 1; |
48 |
} |
49 |
|
50 |
if ($entry->[0] eq $who->name or $who->flag (cf::FLAG_WIZ)) { |
51 |
my $e = CFBoard::remove_idx ($board, $idx); |
52 |
$who->reply ($npc, "Removed entry $idx ($e->[0]: $e->[1])."); |
53 |
} else { |
54 |
$who->reply ($npc, "Access denied."); |
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 |
|
75 |
1 |
76 |
}; |
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 |
} |
84 |
|
85 |
package CFBoard; |
86 |
use POSIX qw/strftime/; |
87 |
use CFDB; |
88 |
|
89 |
my $BOARDDB = CFDB->new (db_file => cf::localdir . "/crossfireboard.perl"); |
90 |
|
91 |
sub get { |
92 |
my ($board) = @_; |
93 |
$BOARDDB->get ($board); |
94 |
} |
95 |
|
96 |
sub get_idx { |
97 |
my ($board, $idx) = @_; |
98 |
$board = $BOARDDB->get ($board); |
99 |
return $board->[$idx]; |
100 |
} |
101 |
|
102 |
sub remove_idx { |
103 |
my ($boardname, $idx) = @_; |
104 |
my $board = $BOARDDB->get ($boardname); |
105 |
my $e = splice @$board, $idx, 1; |
106 |
$BOARDDB->set ($boardname, $board); |
107 |
$e |
108 |
} |
109 |
|
110 |
sub clear { |
111 |
my ($board) = @_; |
112 |
$BOARDDB->clear ($board); |
113 |
} |
114 |
|
115 |
sub write { |
116 |
my ($board, $from, $message) = @_; |
117 |
my $boardentrys = $BOARDDB->get ($board); |
118 |
push @$boardentrys, [$from, $message]; |
119 |
$BOARDDB->set ($board, $boardentrys); |
120 |
} |
121 |
|
122 |
1; |