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