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