1 | #! perl |
1 | #! perl |
|
|
2 | #CONVERSION: PARTIAL |
2 | |
3 | |
3 | # implement a replacement for the built-in say/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 | |
6 | use NPC_Dialogue; |
7 | use NPC_Dialogue; |
7 | |
8 | use POSIX (); # for strftime only |
8 | sub valid_user($) { |
|
|
9 | cf::player::find $_[0] |
|
|
10 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
|
|
11 | } |
|
|
12 | |
9 | |
13 | sub clean_timeouts($) { |
10 | sub clean_timeouts($) { |
14 | my ($player) = @_; |
11 | my ($player) = @_; |
15 | my $NOW = time; |
12 | my $NOW = time; |
16 | |
13 | |
17 | for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) { |
14 | for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) { |
18 | while (my ($k, $v) = each %$hash) { |
15 | while (my ($k, $v) = each %$hash) { |
19 | if ($v < $NOW) { |
16 | if ($v < $NOW) { |
20 | $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE); |
17 | $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE); |
21 | delete $hash->{$k}; |
18 | delete $hash->{$k}; |
22 | } elsif (!valid_user $k) { |
19 | } elsif (!cf::player::exists $k) { |
23 | $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE); |
20 | $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE); |
24 | delete $hash->{$k}; |
21 | delete $hash->{$k}; |
25 | } |
22 | } |
26 | } |
23 | } |
27 | } |
24 | } |
28 | } |
25 | } |
29 | |
26 | |
30 | sub on_logout { |
27 | cf::attach_to_players |
|
|
28 | prio => -1000, |
|
|
29 | on_login => sub { |
31 | my ($pl, $host) = @_; |
30 | my ($pl) = @_; |
32 | |
31 | |
33 | clean_timeouts $pl->ob; |
32 | clean_timeouts $pl->ob; |
34 | } |
33 | }, |
|
|
34 | ; |
35 | |
35 | |
36 | cf::register_command listen => 0, sub { |
36 | cf::register_command listen => 0, sub { |
37 | my ($who, $msg) = @_; |
37 | my ($who, $msg) = @_; |
38 | my $player = cf::player::find $who->name; |
38 | my $player = cf::player::find $who->name; |
39 | |
39 | |
… | |
… | |
48 | } else { |
48 | } else { |
49 | $who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE); |
49 | $who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE); |
50 | } |
50 | } |
51 | }; |
51 | }; |
52 | |
52 | |
53 | cf::register_command xsay => 0, sub { |
53 | cf::register_command say => 0, sub { |
54 | my ($who, $msg) = @_; |
54 | my ($who, $msg) = @_; |
|
|
55 | |
|
|
56 | utf8::decode $msg; |
55 | |
57 | |
56 | if ($msg) { |
58 | if ($msg) { |
57 | my $name = $who->name; |
59 | my $name = $who->name; |
58 | |
60 | |
|
|
61 | utf8::encode $msg; # ->message not yet utf8-ified |
59 | my @messages = (["$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE]); |
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; |
60 | |
65 | |
61 | # npcs, magic_ears etc. |
66 | # npcs, magic_ears etc. |
62 | # first find all objects and their inventories within a 5x5 square |
67 | # first find all objects and their inventories within a 5x5 square |
63 | # that have something resembling dialogue |
68 | # that have something resembling dialogue |
64 | my ($map, $x, $y) = ($who->map, $who->x - 2, $who->y - 2); |
69 | my ($map, $x, $y) = ($who->map, $who->x - 2, $who->y - 2); |
… | |
… | |
72 | ) { |
77 | ) { |
73 | # if some listener teleported us somewhere else, stop right here |
78 | # if some listener teleported us somewhere else, stop right here |
74 | last unless $map->path == $who->map->path; |
79 | last unless $map->path == $who->map->path; |
75 | |
80 | |
76 | my $dialog = new NPC_Dialogue ob => $who, npc => $npc; |
81 | my $dialog = new NPC_Dialogue ob => $who, npc => $npc; |
77 | my $reply = $dialog->tell ($msg); |
82 | my ($reply, @kw) = $dialog->tell ($msg); |
78 | |
83 | |
79 | if (defined $reply) { |
84 | if (defined $reply) { |
80 | if ($npc->type == cf::MAGIC_EAR) { |
85 | if ($npc->type == cf::MAGIC_EAR) { |
81 | push @messages, [$reply, cf::NDI_BROWN | cf::NDI_UNIQUE] if length $reply; |
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 | } |
82 | $npc->use_trigger; |
90 | $npc->use_trigger; |
83 | } else { |
91 | } else { |
|
|
92 | if (length $reply) { |
84 | push @messages, [$npc->name . " says: $reply", cf::NDI_BROWN | cf::NDI_UNIQUE] 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 | } |
85 | } |
96 | } |
86 | } |
97 | } |
87 | } |
|
|
88 | |
98 | |
89 | # send replies to all players on this map |
99 | if (@kw) { |
90 | for (@messages) { |
100 | $_->ob->message ("[further topics: " . (join ", ", @kw) . "]", cf::NDI_BROWN | cf::NDI_UNIQUE) |
91 | my ($msg, $flags) = @$_; |
|
|
92 | |
|
|
93 | $_->ob->message ($msg, $flags) |
|
|
94 | for grep $who->on_same_map_as ($_->ob), cf::player::list; |
101 | for grep $who->on_same_map_as ($_->ob), cf::player::list; |
|
|
102 | } |
95 | } |
103 | } |
96 | |
104 | |
97 | } else { |
105 | } else { |
98 | $who->message ("What do you want to say?", cf::NDI_UNIQUE); |
106 | $who->message ("What do you want to say?", cf::NDI_UNIQUE); |
99 | } |
107 | } |
100 | }; |
108 | }; |
101 | |
109 | |
102 | cf::register_command chat => 0, sub { |
110 | cf::register_command chat => 0, sub { |
103 | my ($who, $msg) = @_; |
111 | my ($who, $msg) = @_; |
|
|
112 | |
|
|
113 | utf8::decode $msg; |
104 | |
114 | |
105 | if ($msg) { |
115 | if ($msg) { |
106 | my $name = $who->name; |
116 | my $name = $who->name; |
107 | my $NOW = time; |
117 | my $NOW = time; |
108 | |
118 | |
|
|
119 | utf8::encode $msg; # ->message not yet utf8-ified |
109 | cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg; |
120 | # cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg; |
|
|
121 | ext::schmorp_irc::do_notice (sprintf "[%s] %s", $name, $msg); |
110 | |
122 | |
111 | $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) |
123 | $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) |
112 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
124 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
113 | |
125 | |
114 | } else { |
126 | } else { |
… | |
… | |
116 | } |
128 | } |
117 | }; |
129 | }; |
118 | |
130 | |
119 | cf::register_command shout => 0, sub { |
131 | cf::register_command shout => 0, sub { |
120 | my ($who, $msg) = @_; |
132 | my ($who, $msg) = @_; |
|
|
133 | |
|
|
134 | utf8::decode $msg; |
121 | |
135 | |
122 | if ($msg) { |
136 | if ($msg) { |
123 | my $NOW = time; |
137 | my $NOW = time; |
124 | my $name = $who->name; |
138 | my $name = $who->name; |
125 | |
139 | |
126 | cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg; |
140 | # cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg; |
|
|
141 | ext::schmorp_irc::do_notice (sprintf "{%s} %s\n", $name, $msg); |
127 | |
142 | |
|
|
143 | utf8::encode $msg; # ->message not yet utf8-ified |
128 | $_->ob->message ("$name shouts: $msg", cf::NDI_RED) |
144 | $_->ob->message ("$name shouts: $msg", cf::NDI_RED) |
129 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list; |
145 | for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list; |
130 | |
146 | |
131 | } else { |
147 | } else { |
132 | $who->message ("Shout what?", cf::NDI_UNIQUE); |
148 | $who->message ("Shout what?", cf::NDI_UNIQUE); |
… | |
… | |
135 | }; |
151 | }; |
136 | |
152 | |
137 | cf::register_command tell => 0, sub { |
153 | cf::register_command tell => 0, sub { |
138 | my ($who, $args) = @_; |
154 | my ($who, $args) = @_; |
139 | my ($target, $msg) = split /\s+/, $args, 2; |
155 | my ($target, $msg) = split /\s+/, $args, 2; |
|
|
156 | |
|
|
157 | utf8::decode $msg; |
140 | |
158 | |
141 | my $name = $who->name; |
159 | my $name = $who->name; |
142 | |
160 | |
143 | if (my $other = cf::player::find $target) { |
161 | if (my $other = cf::player::find $target) { |
144 | |
162 | |
… | |
… | |
146 | if ($target eq $name) { |
164 | if ($target eq $name) { |
147 | $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE); |
165 | $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE); |
148 | } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) { |
166 | } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) { |
149 | $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE); |
167 | $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE); |
150 | } else { |
168 | } else { |
|
|
169 | utf8::encode $msg; # ->message not yet utf8-ified |
151 | cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg; |
170 | cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg; |
152 | |
171 | |
153 | $who->message ("You tell $target: $msg"); |
172 | $who->message ("You tell $target: $msg"); |
154 | $other->ob->message ("$name tells you: $msg"); |
173 | $other->ob->message ("$name tells you: $msg"); |
155 | $other->ob->{ext_last_tell} = $name; |
174 | $other->ob->{ext_last_tell} = $name; |
… | |
… | |
165 | |
184 | |
166 | cf::register_command reply => 0, sub { |
185 | cf::register_command reply => 0, sub { |
167 | my ($who, $args) = @_; |
186 | my ($who, $args) = @_; |
168 | my $name = $who->name; |
187 | my $name = $who->name; |
169 | |
188 | |
|
|
189 | utf8::decode $args; |
|
|
190 | |
170 | if (my $other = cf::player::find $who->{ext_last_tell}) { |
191 | if (my $other = cf::player::find $who->{ext_last_tell}) { |
171 | if ($args) { |
192 | if ($args) { |
172 | |
193 | |
173 | $other->ob->{ext_ignore_tell}{$name} >= time |
194 | $other->ob->{ext_ignore_tell}{$name} >= time |
174 | or delete $other->ob->{ext_ignore_tell}{$name}; |
195 | or delete $other->ob->{ext_ignore_tell}{$name}; |
175 | |
196 | |
176 | 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 |
177 | cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args; |
199 | cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args; |
178 | |
200 | |
179 | $who->message ("You tell " . $other->ob->name . ": $args"); |
201 | $who->message ("You tell " . $other->ob->name . ": $args"); |
180 | $other->ob->message ("$name tells you: $args"); |
202 | $other->ob->message ("$name tells you: $args"); |
181 | $who->{ext_last_tell} = $other->ob->name; |
203 | $who->{ext_last_tell} = $other->ob->name; |
… | |
… | |
212 | } elsif ($target && $type) { |
234 | } elsif ($target && $type) { |
213 | |
235 | |
214 | $timeout ne "" or $timeout = 24; |
236 | $timeout ne "" or $timeout = 24; |
215 | my $absolute_timeout = time + $timeout * 3600; |
237 | my $absolute_timeout = time + $timeout * 3600; |
216 | |
238 | |
217 | if (valid_user $target) { |
239 | if (cf::player::exists $target) { |
218 | if ($type eq "tell") { |
240 | if ($type eq "tell") { |
219 | $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE); |
241 | $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE); |
220 | $who->{ext_ignore_tell}{$target} = $absolute_timeout; |
242 | $who->{ext_ignore_tell}{$target} = $absolute_timeout; |
221 | } elsif ($type eq "shout") { |
243 | } elsif ($type eq "shout") { |
222 | $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE); |
244 | $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE); |
… | |
… | |
231 | } else { |
253 | } else { |
232 | $who->message ("No such player: $target", cf::NDI_UNIQUE); |
254 | $who->message ("No such player: $target", cf::NDI_UNIQUE); |
233 | } |
255 | } |
234 | |
256 | |
235 | } else { |
257 | } else { |
236 | $who->message ("Usage: ignore <player> <tell | shout | all> <timeout>\n" |
258 | $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n" |
237 | . "will ignore a player for <timeout> hours.\n" |
259 | . "will ignore a player for <timeout> hours.\n" |
238 | . "Usage: ignore list\n" |
260 | . "Usage: ignore list\n" |
239 | . "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); |
240 | } |
262 | } |
241 | }; |
263 | }; |
… | |
… | |
252 | $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); |
253 | } else { |
275 | } else { |
254 | $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); |
276 | $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); |
255 | } |
277 | } |
256 | } else { |
278 | } else { |
257 | |
279 | if (cf::player::exists $target) { |
258 | if (valid_user $target) { |
|
|
259 | if ($type eq "tell") { |
280 | if ($type eq "tell") { |
260 | $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE); |
281 | $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE); |
261 | delete $who->{ext_ignore_tell} {$target}; |
282 | delete $who->{ext_ignore_tell} {$target}; |
262 | } elsif ($type eq "shout") { |
283 | } elsif ($type eq "shout") { |
263 | $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE); |
284 | $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE); |
… | |
… | |
270 | $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); |
271 | } |
292 | } |
272 | } else { |
293 | } else { |
273 | $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); |
274 | } |
295 | } |
275 | |
|
|
276 | } |
296 | } |
277 | }; |
297 | }; |
278 | |
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 | |