1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | # implement a replacement for the built-in say/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 | sub valid_user($) { |
6 | use NPC_Dialogue; |
7 | cf::player::find $_[0] |
7 | use POSIX (); # for strftime only |
8 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
9 | } |
|
|
10 | |
8 | |
11 | sub clean_timeouts($) { |
9 | sub clean_timeouts($) { |
12 | my ($player) = @_; |
10 | my ($player) = @_; |
13 | my $NOW = time; |
11 | my $NOW = time; |
14 | |
12 | |
15 | for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) { |
13 | for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) { |
16 | while (my ($k, $v) = each %$hash) { |
14 | while (my ($k, $v) = each %$hash) { |
17 | if ($v < $NOW) { |
15 | if ($v < $NOW) { |
18 | $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN); |
16 | $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE); |
19 | delete $hash->{$k}; |
17 | delete $hash->{$k}; |
20 | } elsif (!valid_user $k) { |
18 | } elsif (!cf::player::exists $k) { |
21 | $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN); |
19 | $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE); |
22 | delete $hash->{$k}; |
20 | delete $hash->{$k}; |
23 | } |
21 | } |
24 | } |
22 | } |
25 | } |
23 | } |
26 | } |
24 | } |
27 | |
25 | |
28 | sub on_logout { |
26 | cf::attach_to_players |
|
|
27 | prio => -1000, |
|
|
28 | on_login => sub { |
29 | my ($pl, $host) = @_; |
29 | my ($pl) = @_; |
30 | |
30 | |
31 | clean_timeouts $pl->ob; |
31 | clean_timeouts $pl->ob; |
32 | } |
32 | }, |
|
|
33 | ; |
33 | |
34 | |
34 | cf::register_command listen => 0, sub { |
35 | cf::register_command listen => 0, sub { |
35 | my ($who, $msg) = @_; |
36 | my ($who, $msg) = @_; |
36 | my $player = cf::player::find $who->name; |
37 | my $player = cf::player::find $who->name; |
37 | |
38 | |
38 | if ($msg ne "") { |
39 | if ($msg ne "") { |
39 | my $prev_listen = $player->listening; |
40 | my $prev_listen = $player->listening; |
40 | $player->listening ($msg); |
41 | $player->listening ($msg); |
41 | if ($prev_listen == $player->listening) { |
42 | if ($prev_listen == $player->listening) { |
42 | $who->message ("Your verbose level stayed ".$prev_listen.".", cf::NDI_UNIQUE); |
43 | $who->message ("Your verbose level stayed $prev_listen.", cf::NDI_UNIQUE); |
43 | } else { |
44 | } else { |
44 | $who->message ("Your verbose level is now ".$player->listening.". (previously: ".$prev_listen.")", cf::NDI_UNIQUE); |
45 | $who->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE); |
45 | } |
46 | } |
46 | } else { |
47 | } else { |
47 | $who->message ("Your verbose level is ".$player->listening.".", cf::NDI_UNIQUE); |
48 | $who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE); |
48 | } |
49 | } |
49 | }; |
50 | }; |
50 | |
51 | |
51 | #cf::register_command say => 0, sub { |
52 | cf::register_command say => 0, sub { |
52 | # my ($who, $msg) = @_; |
53 | my ($who, $msg) = @_; |
53 | # |
54 | |
|
|
55 | utf8::decode $msg; |
|
|
56 | |
54 | # if ($msg) { |
57 | if ($msg) { |
55 | # my $name = $who->name; |
58 | my $name = $who->name; |
|
|
59 | |
|
|
60 | utf8::encode $msg; # ->message not yet utf8-ified |
56 | # $_->ob->message ("$name says: $msg", cf::NDI_WHITE) |
61 | $_->ob->message ("$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE) |
57 | # for grep $who->on_same_map_as ($_->ob), cf::player::list; |
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 | |
58 | # } else { |
104 | } else { |
59 | # $who->message ("What do you want to say?", cf::NDI_UNIQUE); |
105 | $who->message ("What do you want to say?", cf::NDI_UNIQUE); |
60 | # } |
106 | } |
61 | #}; |
107 | }; |
62 | |
108 | |
63 | cf::register_command chat => 0, sub { |
109 | cf::register_command chat => 0, sub { |
64 | my ($who, $msg) = @_; |
110 | my ($who, $msg) = @_; |
|
|
111 | |
|
|
112 | utf8::decode $msg; |
65 | |
113 | |
66 | if ($msg) { |
114 | if ($msg) { |
67 | my $name = $who->name; |
115 | my $name = $who->name; |
68 | my $NOW = time; |
116 | my $NOW = time; |
69 | |
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 | |
70 | $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) |
122 | $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) |
71 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
123 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
72 | |
124 | |
73 | } else { |
125 | } else { |
74 | $who->message ("Chat what?", cf::NDI_UNIQUE); |
126 | $who->message ("Chat what?", cf::NDI_UNIQUE); |
75 | } |
127 | } |
76 | }; |
128 | }; |
77 | |
129 | |
78 | cf::register_command shout => 0, sub { |
130 | cf::register_command shout => 0, sub { |
79 | my ($who, $msg) = @_; |
131 | my ($who, $msg) = @_; |
|
|
132 | |
|
|
133 | utf8::decode $msg; |
80 | |
134 | |
81 | if ($msg) { |
135 | if ($msg) { |
82 | my $NOW = time; |
136 | my $NOW = time; |
83 | my $name = $who->name; |
137 | my $name = $who->name; |
84 | |
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 |
85 | $_->ob->message ("$name shouts: $msg", cf::NDI_RED) |
143 | $_->ob->message ("$name shouts: $msg", cf::NDI_RED) |
86 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list; |
144 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list; |
87 | |
145 | |
88 | } else { |
146 | } else { |
89 | $who->message ("Shout what?", cf::NDI_UNIQUE); |
147 | $who->message ("Shout what?", cf::NDI_UNIQUE); |
… | |
… | |
92 | }; |
150 | }; |
93 | |
151 | |
94 | cf::register_command tell => 0, sub { |
152 | cf::register_command tell => 0, sub { |
95 | my ($who, $args) = @_; |
153 | my ($who, $args) = @_; |
96 | my ($target, $msg) = split /\s+/, $args, 2; |
154 | my ($target, $msg) = split /\s+/, $args, 2; |
|
|
155 | |
|
|
156 | utf8::decode $msg; |
97 | |
157 | |
98 | my $name = $who->name; |
158 | my $name = $who->name; |
99 | |
159 | |
100 | if (my $other = cf::player::find $target) { |
160 | if (my $other = cf::player::find $target) { |
101 | |
161 | |
… | |
… | |
103 | if ($target eq $name) { |
163 | if ($target eq $name) { |
104 | $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE); |
164 | $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE); |
105 | } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) { |
165 | } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) { |
106 | $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE); |
166 | $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE); |
107 | } else { |
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 | |
108 | $who->message ("You tell $target: $msg"); |
171 | $who->message ("You tell $target: $msg"); |
109 | $other->ob->message ("$name tells you: $msg"); |
172 | $other->ob->message ("$name tells you: $msg"); |
110 | $other->ob->{ext_last_tell} = $name; |
173 | $other->ob->{ext_last_tell} = $name; |
111 | } |
174 | } |
112 | } else { |
175 | } else { |
… | |
… | |
120 | |
183 | |
121 | cf::register_command reply => 0, sub { |
184 | cf::register_command reply => 0, sub { |
122 | my ($who, $args) = @_; |
185 | my ($who, $args) = @_; |
123 | my $name = $who->name; |
186 | my $name = $who->name; |
124 | |
187 | |
|
|
188 | utf8::decode $args; |
|
|
189 | |
125 | if (my $other = cf::player::find $who->{ext_last_tell}) { |
190 | if (my $other = cf::player::find $who->{ext_last_tell}) { |
126 | if ($args) { |
191 | if ($args) { |
127 | |
192 | |
128 | $other->ob->{ext_ignore_tell}{$name} >= time |
193 | $other->ob->{ext_ignore_tell}{$name} >= time |
129 | or delete $other->ob->{ext_ignore_tell}{$name}; |
194 | or delete $other->ob->{ext_ignore_tell}{$name}; |
130 | |
195 | |
131 | 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 | |
132 | $who->message ("You tell " . $other->ob->name . ": $args"); |
200 | $who->message ("You tell " . $other->ob->name . ": $args"); |
133 | $other->ob->message ("$name tells you: $args"); |
201 | $other->ob->message ("$name tells you: $args"); |
134 | $who->{ext_last_tell} = $other->ob->name; |
202 | $who->{ext_last_tell} = $other->ob->name; |
135 | } else { |
203 | } else { |
136 | $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); |
… | |
… | |
165 | } elsif ($target && $type) { |
233 | } elsif ($target && $type) { |
166 | |
234 | |
167 | $timeout ne "" or $timeout = 24; |
235 | $timeout ne "" or $timeout = 24; |
168 | my $absolute_timeout = time + $timeout * 3600; |
236 | my $absolute_timeout = time + $timeout * 3600; |
169 | |
237 | |
170 | if (valid_user $target) { |
238 | if (cf::player::exists $target) { |
171 | if ($type eq "tell") { |
239 | if ($type eq "tell") { |
172 | $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE); |
240 | $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE); |
173 | $who->{ext_ignore_tell}{$target} = $absolute_timeout; |
241 | $who->{ext_ignore_tell}{$target} = $absolute_timeout; |
174 | } elsif ($type eq "shout") { |
242 | } elsif ($type eq "shout") { |
175 | $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE); |
243 | $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE); |
… | |
… | |
184 | } else { |
252 | } else { |
185 | $who->message ("No such player: $target", cf::NDI_UNIQUE); |
253 | $who->message ("No such player: $target", cf::NDI_UNIQUE); |
186 | } |
254 | } |
187 | |
255 | |
188 | } else { |
256 | } else { |
189 | $who->message ("Usage: ignore <player> <tell | shout | all> <timeout>\n" |
257 | $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n" |
190 | . "will ignore a player for <timeout> hours.\n" |
258 | . "will ignore a player for <timeout> hours.\n" |
191 | . "Usage: ignore list\n" |
259 | . "Usage: ignore list\n" |
192 | . "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); |
193 | } |
261 | } |
194 | }; |
262 | }; |
… | |
… | |
205 | $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); |
206 | } else { |
274 | } else { |
207 | $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); |
275 | $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); |
208 | } |
276 | } |
209 | } else { |
277 | } else { |
210 | |
278 | if (cf::player::exists $target) { |
211 | if (valid_user $target) { |
|
|
212 | if ($type eq "tell") { |
279 | if ($type eq "tell") { |
213 | $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE); |
280 | $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE); |
214 | delete $who->{ext_ignore_tell} {$target}; |
281 | delete $who->{ext_ignore_tell} {$target}; |
215 | } elsif ($type eq "shout") { |
282 | } elsif ($type eq "shout") { |
216 | $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE); |
283 | $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE); |
… | |
… | |
223 | $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); |
224 | } |
291 | } |
225 | } else { |
292 | } else { |
226 | $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); |
227 | } |
294 | } |
228 | |
|
|
229 | } |
295 | } |
230 | }; |
296 | }; |
231 | |
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 | |