1 |
#! perl |
2 |
|
3 |
# implement a replacement for the built-in say/chat/shout/tell/reply commands |
4 |
# adds ignore/unignore functionality |
5 |
|
6 |
use NPC_Dialogue; |
7 |
use POSIX (); # for strftime only |
8 |
|
9 |
sub clean_timeouts($) { |
10 |
my ($player) = @_; |
11 |
my $NOW = time; |
12 |
|
13 |
for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) { |
14 |
while (my ($k, $v) = each %$hash) { |
15 |
if ($v < $NOW) { |
16 |
$player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE); |
17 |
delete $hash->{$k}; |
18 |
} elsif (!cf::player::exists $k) { |
19 |
$player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE); |
20 |
delete $hash->{$k}; |
21 |
} |
22 |
} |
23 |
} |
24 |
} |
25 |
|
26 |
sub on_logout { |
27 |
my ($pl, $host) = @_; |
28 |
|
29 |
clean_timeouts $pl->ob; |
30 |
} |
31 |
|
32 |
cf::register_command listen => 0, sub { |
33 |
my ($who, $msg) = @_; |
34 |
my $player = cf::player::find $who->name; |
35 |
|
36 |
if ($msg ne "") { |
37 |
my $prev_listen = $player->listening; |
38 |
$player->listening ($msg); |
39 |
if ($prev_listen == $player->listening) { |
40 |
$who->message ("Your verbose level stayed $prev_listen.", cf::NDI_UNIQUE); |
41 |
} else { |
42 |
$who->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE); |
43 |
} |
44 |
} else { |
45 |
$who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE); |
46 |
} |
47 |
}; |
48 |
|
49 |
cf::register_command say => 0, sub { |
50 |
my ($who, $msg) = @_; |
51 |
|
52 |
utf8::decode $msg; |
53 |
|
54 |
if ($msg) { |
55 |
my $name = $who->name; |
56 |
|
57 |
utf8::encode $msg; # ->message not yet utf8-ified |
58 |
$_->ob->message ("$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE) |
59 |
for grep $who->on_same_map_as ($_->ob), cf::player::list; |
60 |
utf8::decode $msg; |
61 |
|
62 |
# npcs, magic_ears etc. |
63 |
# first find all objects and their inventories within a 5x5 square |
64 |
# that have something resembling dialogue |
65 |
my ($map, $x, $y) = ($who->map, $who->x - 2, $who->y - 2); |
66 |
|
67 |
for my $npc ( |
68 |
grep NPC_Dialogue::has_dialogue $_, |
69 |
map +($_, $_->inv), |
70 |
grep $_, |
71 |
map $map->at ($x + $_ % 5, $y + (int $_ / 5)), |
72 |
0..24 |
73 |
) { |
74 |
# if some listener teleported us somewhere else, stop right here |
75 |
last unless $map->path == $who->map->path; |
76 |
|
77 |
my $dialog = new NPC_Dialogue ob => $who, npc => $npc; |
78 |
my ($reply, @kw) = $dialog->tell ($msg); |
79 |
|
80 |
if (defined $reply) { |
81 |
if ($npc->type == cf::MAGIC_EAR) { |
82 |
if (length $reply) { |
83 |
$_->ob->message ($reply, cf::NDI_BROWN | cf::NDI_UNIQUE) |
84 |
for grep $who->on_same_map_as ($_->ob), cf::player::list; |
85 |
} |
86 |
$npc->use_trigger; |
87 |
} else { |
88 |
if (length $reply) { |
89 |
$_->ob->message ($npc->name . " says: $reply", cf::NDI_BROWN | cf::NDI_UNIQUE) |
90 |
for grep $who->on_same_map_as ($_->ob), cf::player::list; |
91 |
} |
92 |
} |
93 |
} |
94 |
|
95 |
if (@kw) { |
96 |
$_->ob->message ("[further topics: " . (join ", ", @kw) . "]", cf::NDI_BROWN | cf::NDI_UNIQUE) |
97 |
for grep $who->on_same_map_as ($_->ob), cf::player::list; |
98 |
} |
99 |
} |
100 |
|
101 |
} else { |
102 |
$who->message ("What do you want to say?", cf::NDI_UNIQUE); |
103 |
} |
104 |
}; |
105 |
|
106 |
cf::register_command chat => 0, sub { |
107 |
my ($who, $msg) = @_; |
108 |
|
109 |
utf8::decode $msg; |
110 |
|
111 |
if ($msg) { |
112 |
my $name = $who->name; |
113 |
my $NOW = time; |
114 |
|
115 |
utf8::encode $msg; # ->message not yet utf8-ified |
116 |
cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg; |
117 |
|
118 |
$_->ob->message ("$name chats: $msg", cf::NDI_BLUE) |
119 |
for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
120 |
|
121 |
} else { |
122 |
$who->message ("Chat what?", cf::NDI_UNIQUE); |
123 |
} |
124 |
}; |
125 |
|
126 |
cf::register_command shout => 0, sub { |
127 |
my ($who, $msg) = @_; |
128 |
|
129 |
utf8::decode $msg; |
130 |
|
131 |
if ($msg) { |
132 |
my $NOW = time; |
133 |
my $name = $who->name; |
134 |
|
135 |
cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg; |
136 |
|
137 |
utf8::encode $msg; # ->message not yet utf8-ified |
138 |
$_->ob->message ("$name shouts: $msg", cf::NDI_RED) |
139 |
for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list; |
140 |
|
141 |
} else { |
142 |
$who->message ("Shout what?", cf::NDI_UNIQUE); |
143 |
} |
144 |
|
145 |
}; |
146 |
|
147 |
cf::register_command tell => 0, sub { |
148 |
my ($who, $args) = @_; |
149 |
my ($target, $msg) = split /\s+/, $args, 2; |
150 |
|
151 |
utf8::decode $msg; |
152 |
|
153 |
my $name = $who->name; |
154 |
|
155 |
if (my $other = cf::player::find $target) { |
156 |
|
157 |
if ($msg) { |
158 |
if ($target eq $name) { |
159 |
$who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE); |
160 |
} elsif ($other->ob->{ext_ignore_tell}{$name} >= time) { |
161 |
$who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE); |
162 |
} else { |
163 |
utf8::encode $msg; # ->message not yet utf8-ified |
164 |
cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg; |
165 |
|
166 |
$who->message ("You tell $target: $msg"); |
167 |
$other->ob->message ("$name tells you: $msg"); |
168 |
$other->ob->{ext_last_tell} = $name; |
169 |
} |
170 |
} else { |
171 |
$who->message ("What do you want to tell $target?", cf::NDI_UNIQUE); |
172 |
} |
173 |
|
174 |
} else { |
175 |
$who->message ("No such player. Your message: $msg", cf::NDI_UNIQUE); |
176 |
} |
177 |
}; |
178 |
|
179 |
cf::register_command reply => 0, sub { |
180 |
my ($who, $args) = @_; |
181 |
my $name = $who->name; |
182 |
|
183 |
utf8::decode $args; |
184 |
|
185 |
if (my $other = cf::player::find $who->{ext_last_tell}) { |
186 |
if ($args) { |
187 |
|
188 |
$other->ob->{ext_ignore_tell}{$name} >= time |
189 |
or delete $other->ob->{ext_ignore_tell}{$name}; |
190 |
|
191 |
if ($other->ob->{ext_ignore_tell}{$name} < time) { |
192 |
utf8::encode $msg; # ->message not yet utf8-ified |
193 |
cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args; |
194 |
|
195 |
$who->message ("You tell " . $other->ob->name . ": $args"); |
196 |
$other->ob->message ("$name tells you: $args"); |
197 |
$who->{ext_last_tell} = $other->ob->name; |
198 |
} else { |
199 |
$who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE); |
200 |
} |
201 |
} else { |
202 |
$who->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE); |
203 |
} |
204 |
|
205 |
} else { |
206 |
$who->message ("Can't reply, player left. Your message: $args", cf::NDI_UNIQUE); |
207 |
} |
208 |
}; |
209 |
|
210 |
cf::register_command ignore => 0, sub { |
211 |
my ($who, $args) = @_; |
212 |
my ($target, $type, $timeout) = split /\s+/, $args; |
213 |
|
214 |
if ($args eq "list") { |
215 |
clean_timeouts $who; |
216 |
|
217 |
if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}}) |
218 |
+ (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) { |
219 |
$who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); |
220 |
$who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE); |
221 |
$who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); |
222 |
$who->message ((join ", ", @ignored_shout), cf::NDI_UNIQUE); |
223 |
$who->message ("To stop ignoring one, use unignore.", cf::NDI_UNIQUE); |
224 |
} else { |
225 |
$who->message ("Not ignoring anyone", cf::NDI_UNIQUE); |
226 |
} |
227 |
|
228 |
} elsif ($target && $type) { |
229 |
|
230 |
$timeout ne "" or $timeout = 24; |
231 |
my $absolute_timeout = time + $timeout * 3600; |
232 |
|
233 |
if (cf::player::exists $target) { |
234 |
if ($type eq "tell") { |
235 |
$who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE); |
236 |
$who->{ext_ignore_tell}{$target} = $absolute_timeout; |
237 |
} elsif ($type eq "shout") { |
238 |
$who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE); |
239 |
$who->{ext_ignore_shout}{$target} = $absolute_timeout; |
240 |
} elsif ($type eq "all") { |
241 |
$who->message ("Now ignoring everything from $target for $timeout hours.", cf::NDI_UNIQUE); |
242 |
$who->{ext_ignore_tell}{$target} = $absolute_timeout; |
243 |
$who->{ext_ignore_shout}{$target} = $absolute_timeout; |
244 |
} else { |
245 |
$who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); |
246 |
} |
247 |
} else { |
248 |
$who->message ("No such player: $target", cf::NDI_UNIQUE); |
249 |
} |
250 |
|
251 |
} else { |
252 |
$who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n" |
253 |
. "will ignore a player for <timeout> hours.\n" |
254 |
. "Usage: ignore list\n" |
255 |
. "will show you a list of players currently ignored.", cf::NDI_UNIQUE); |
256 |
} |
257 |
}; |
258 |
|
259 |
cf::register_command unignore => 0, sub { |
260 |
my ($who, $args) = @_; |
261 |
my ($target, $type) = split /\s+/, $args; |
262 |
|
263 |
if ($args eq "") { |
264 |
if ($who->{ext_ignore_tell}) { |
265 |
$who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); |
266 |
$who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE); |
267 |
$who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); |
268 |
$who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE); |
269 |
} else { |
270 |
$who->message ("Not ignoring anyone", cf::NDI_UNIQUE); |
271 |
} |
272 |
} else { |
273 |
if (cf::player::exists $target) { |
274 |
if ($type eq "tell") { |
275 |
$who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE); |
276 |
delete $who->{ext_ignore_tell} {$target}; |
277 |
} elsif ($type eq "shout") { |
278 |
$who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE); |
279 |
delete $who->{ext_ignore_shout}{$target}; |
280 |
} elsif ($type eq "all") { |
281 |
$who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE); |
282 |
delete $who->{ext_ignore_tell} {$target}; |
283 |
delete $who->{ext_ignore_shout}{$target}; |
284 |
} else { |
285 |
$who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); |
286 |
} |
287 |
} else { |
288 |
$who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); |
289 |
} |
290 |
} |
291 |
}; |
292 |
|
293 |
cf::register_command seen => 0, sub { |
294 |
my ($who, $args) = @_; |
295 |
|
296 |
if (my ($login) = $args =~ /(\S+)/) { |
297 |
if ($login eq $who->name) { |
298 |
$who->message ("Very funny, $login. Ha. Ha.", cf::NDI_UNIQUE); |
299 |
} elsif (cf::player::find $login) { |
300 |
$who->message ("$login is right here on this server!", cf::NDI_UNIQUE); |
301 |
} elsif (cf::player::exists $login |
302 |
and stat sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($login) x 2) { |
303 |
my $time = (stat _)[9]; |
304 |
|
305 |
$who->message ("$login was last seen here " |
306 |
. (POSIX::strftime "%Y-%m-%d %H:%M:%S +0000", gmtime $time) |
307 |
. " which was " . (int +(time - $time) / 3600) . " hours ago.", cf::NDI_UNIQUE); |
308 |
} else { |
309 |
$who->message ("No player named $login is known to me.", cf::NDI_UNIQUE); |
310 |
} |
311 |
} else { |
312 |
$who->message ("Usage: seen <player>", cf::NDI_UNIQUE); |
313 |
} |
314 |
}; |
315 |
|