1 |
#! perl |
2 |
#CONVERSION: NONE |
3 |
|
4 |
sub do_help { |
5 |
my ($board, $who, $npc) = @_; |
6 |
$who->reply ($npc, |
7 |
"Help for $board\n" |
8 |
."List of commands:\n\n" |
9 |
."- list\n" |
10 |
."- write <message>\n" |
11 |
."- remove <id>\n" |
12 |
); |
13 |
} |
14 |
|
15 |
sub do_write { |
16 |
my ($board, $msg, $who, $npc) = @_; |
17 |
if ($msg =~ /\S/) { |
18 |
CFBoard::write ($board, $who->name, $msg); |
19 |
$who->reply ($npc, "Added entry."); |
20 |
} else { |
21 |
$who->reply ($npc, "Usage: write <message>\n"); |
22 |
} |
23 |
1 |
24 |
} |
25 |
|
26 |
sub do_list { |
27 |
my ($board, $who, $npc) = @_; |
28 |
my $cont = CFBoard::get ($board); |
29 |
if (@{$cont || []}) { |
30 |
$who->message ("$board content:", cf::NDI_BROWN | cf::NDI_UNIQUE); |
31 |
my $idx = 0; |
32 |
for (@$cont) { |
33 |
$who->message ("<$idx> $_->[0]: $_->[1]", cf::NDI_BROWN | cf::NDI_UNIQUE); |
34 |
$idx++; |
35 |
} |
36 |
} else { |
37 |
$who->message ("$board is empty.", cf::NDI_BROWN | cf::NDI_UNIQUE); |
38 |
} |
39 |
1 |
40 |
} |
41 |
|
42 |
sub do_remove { |
43 |
my ($board, $idx, $who, $npc) = @_; |
44 |
|
45 |
my $entry = CFBoard::get_idx ($board, $idx); |
46 |
unless (defined $entry) { |
47 |
$who->reply ($npc, "No such entry."); |
48 |
return 1; |
49 |
} |
50 |
|
51 |
if ($entry->[0] eq $who->name or $who->flag (cf::FLAG_WIZ)) { |
52 |
my $e = CFBoard::remove_idx ($board, $idx); |
53 |
$who->reply ($npc, "Removed entry $idx ($e->[0]: $e->[1])."); |
54 |
} else { |
55 |
$who->reply ($npc, "Access denied."); |
56 |
} |
57 |
|
58 |
1 |
59 |
} |
60 |
|
61 |
# this is the main command interface for the IPO NPC |
62 |
cf::register_script_function "board::command" => sub { |
63 |
my ($who, $msg, $npc) = @_; |
64 |
my $board = $npc->name; |
65 |
|
66 |
if ($msg =~ /^list$/i) { |
67 |
do_list ($board, $who, $npc); |
68 |
} elsif ($msg =~ /^write (.+)$/i) { |
69 |
do_write ($board, $1, $who, $npc); |
70 |
} elsif ($msg =~ /^remove (\d+)$/i) { |
71 |
do_remove ($board, $1, $who, $npc); |
72 |
} else { |
73 |
do_help ($board, $who, $npc); |
74 |
} |
75 |
|
76 |
1 |
77 |
}; |
78 |
|
79 |
sub on_apply { |
80 |
my ($ev, $npc, $who) = @_; |
81 |
$who->reply ($npc, "Hello, i'm a talking board, 'say help' to get help"); |
82 |
do_list ($npc->name, $who, $npc); |
83 |
1; |
84 |
} |
85 |
|
86 |
package CFBoard; |
87 |
use POSIX qw/strftime/; |
88 |
use CFDB; |
89 |
|
90 |
my $BOARDDB = CFDB->new (db_file => cf::localdir . "/crossfireboard.perl"); |
91 |
|
92 |
sub get { |
93 |
my ($board) = @_; |
94 |
$BOARDDB->get ($board); |
95 |
} |
96 |
|
97 |
sub get_idx { |
98 |
my ($board, $idx) = @_; |
99 |
$board = $BOARDDB->get ($board); |
100 |
return $board->[$idx]; |
101 |
} |
102 |
|
103 |
sub remove_idx { |
104 |
my ($boardname, $idx) = @_; |
105 |
my $board = $BOARDDB->get ($boardname); |
106 |
my $e = splice @$board, $idx, 1; |
107 |
$BOARDDB->set ($boardname, $board); |
108 |
$e |
109 |
} |
110 |
|
111 |
sub clear { |
112 |
my ($board) = @_; |
113 |
$BOARDDB->clear ($board); |
114 |
} |
115 |
|
116 |
sub write { |
117 |
my ($board, $from, $message) = @_; |
118 |
my $boardentrys = $BOARDDB->get ($board); |
119 |
push @$boardentrys, [$from, $message]; |
120 |
$BOARDDB->set ($board, $boardentrys); |
121 |
} |
122 |
|
123 |
1; |