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