ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/chat.ext
Revision: 1.34
Committed: Fri Aug 25 15:24:54 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.33: +2 -2 lines
Log Message:
rename ext:: to safe:: and cf::ext:: to ext::

File Contents

# User Rev Content
1 pippijn 1.1 #! perl
2 root 1.33 #CONVERSION: PARTIAL
3 root 1.5
4 pippijn 1.15 # implement a replacement for the built-in say/chat/shout/tell/reply commands
5 root 1.5 # adds ignore/unignore functionality
6    
7 root 1.20 use NPC_Dialogue;
8 root 1.23 use POSIX (); # for strftime only
9 root 1.20
10 root 1.12 sub clean_timeouts($) {
11 root 1.8 my ($player) = @_;
12     my $NOW = time;
13    
14     for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) {
15 root 1.12 while (my ($k, $v) = each %$hash) {
16     if ($v < $NOW) {
17 root 1.20 $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE);
18 root 1.12 delete $hash->{$k};
19 root 1.22 } elsif (!cf::player::exists $k) {
20 root 1.20 $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE);
21 root 1.12 delete $hash->{$k};
22     }
23     }
24 root 1.8 }
25     }
26    
27 root 1.32 cf::attach_to_players
28     prio => -1000,
29     on_login => sub {
30     my ($pl) = @_;
31    
32     clean_timeouts $pl->ob;
33     },
34     ;
35 root 1.8
36 pippijn 1.15 cf::register_command listen => 0, sub {
37     my ($who, $msg) = @_;
38     my $player = cf::player::find $who->name;
39    
40 pippijn 1.17 if ($msg ne "") {
41 pippijn 1.15 my $prev_listen = $player->listening;
42     $player->listening ($msg);
43     if ($prev_listen == $player->listening) {
44 root 1.20 $who->message ("Your verbose level stayed $prev_listen.", cf::NDI_UNIQUE);
45 pippijn 1.15 } else {
46 root 1.20 $who->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE);
47 pippijn 1.15 }
48     } else {
49 root 1.20 $who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE);
50 pippijn 1.15 }
51     };
52    
53 root 1.21 cf::register_command say => 0, sub {
54 root 1.20 my ($who, $msg) = @_;
55    
56 root 1.27 utf8::decode $msg;
57    
58 root 1.20 if ($msg) {
59     my $name = $who->name;
60    
61 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
62 root 1.21 $_->ob->message ("$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE)
63     for grep $who->on_same_map_as ($_->ob), cf::player::list;
64 root 1.27 utf8::decode $msg;
65 root 1.20
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 root 1.26 my ($reply, @kw) = $dialog->tell ($msg);
83 root 1.20
84     if (defined $reply) {
85     if ($npc->type == cf::MAGIC_EAR) {
86 root 1.21 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 root 1.20 $npc->use_trigger;
91     } else {
92 root 1.21 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 root 1.20 }
97     }
98 root 1.26
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 root 1.20 }
104    
105     } else {
106     $who->message ("What do you want to say?", cf::NDI_UNIQUE);
107     }
108     };
109 pippijn 1.15
110 pippijn 1.6 cf::register_command chat => 0, sub {
111 pippijn 1.1 my ($who, $msg) = @_;
112    
113 root 1.27 utf8::decode $msg;
114    
115 pippijn 1.1 if ($msg) {
116     my $name = $who->name;
117 root 1.8 my $NOW = time;
118 pippijn 1.1
119 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
120 root 1.30 # cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg;
121 root 1.34 ext::schmorp_irc::do_notice (sprintf "[%s] %s", $name, $msg);
122 root 1.19
123 root 1.4 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE)
124 pippijn 1.15 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
125 root 1.4
126 pippijn 1.1 } else {
127     $who->message ("Chat what?", cf::NDI_UNIQUE);
128     }
129     };
130    
131 pippijn 1.6 cf::register_command shout => 0, sub {
132 pippijn 1.1 my ($who, $msg) = @_;
133    
134 root 1.27 utf8::decode $msg;
135    
136 pippijn 1.1 if ($msg) {
137 root 1.8 my $NOW = time;
138 pippijn 1.1 my $name = $who->name;
139    
140 root 1.30 # cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg;
141 root 1.34 ext::schmorp_irc::do_notice (sprintf "{%s} %s\n", $name, $msg);
142 root 1.19
143 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
144 root 1.4 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
145 pippijn 1.15 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
146 root 1.4
147 pippijn 1.1 } else {
148     $who->message ("Shout what?", cf::NDI_UNIQUE);
149     }
150    
151     };
152 root 1.5
153     cf::register_command tell => 0, sub {
154     my ($who, $args) = @_;
155     my ($target, $msg) = split /\s+/, $args, 2;
156    
157 root 1.27 utf8::decode $msg;
158    
159 root 1.5 my $name = $who->name;
160    
161 pippijn 1.11 if (my $other = cf::player::find $target) {
162 root 1.12
163 pippijn 1.11 if ($msg) {
164 root 1.12 if ($target eq $name) {
165     $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE);
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 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
170 root 1.19 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg;
171    
172 pippijn 1.9 $who->message ("You tell $target: $msg");
173     $other->ob->message ("$name tells you: $msg");
174     $other->ob->{ext_last_tell} = $name;
175     }
176 root 1.5 } else {
177 pippijn 1.11 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE);
178 root 1.5 }
179 root 1.12
180 root 1.5 } else {
181 root 1.12 $who->message ("No such player. Your message: $msg", cf::NDI_UNIQUE);
182 root 1.5 }
183     };
184    
185     cf::register_command reply => 0, sub {
186     my ($who, $args) = @_;
187     my $name = $who->name;
188    
189 root 1.27 utf8::decode $args;
190    
191 pippijn 1.11 if (my $other = cf::player::find $who->{ext_last_tell}) {
192     if ($args) {
193 root 1.5
194 pippijn 1.9 $other->ob->{ext_ignore_tell}{$name} >= time
195     or delete $other->ob->{ext_ignore_tell}{$name};
196    
197     if ($other->ob->{ext_ignore_tell}{$name} < time) {
198 root 1.28 utf8::encode $args; # ->message not yet utf8-ified
199 root 1.19 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args;
200    
201 pippijn 1.9 $who->message ("You tell " . $other->ob->name . ": $args");
202     $other->ob->message ("$name tells you: $args");
203     $who->{ext_last_tell} = $other->ob->name;
204     } else {
205     $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
206     }
207 root 1.5 } else {
208 pippijn 1.11 $who->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE);
209 root 1.5 }
210 pippijn 1.11
211 root 1.5 } else {
212 pippijn 1.11 $who->message ("Can't reply, player left. Your message: $args", cf::NDI_UNIQUE);
213 root 1.5 }
214     };
215    
216     cf::register_command ignore => 0, sub {
217     my ($who, $args) = @_;
218     my ($target, $type, $timeout) = split /\s+/, $args;
219    
220     if ($args eq "list") {
221 root 1.8 clean_timeouts $who;
222    
223 root 1.5 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}})
224     + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) {
225     $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
226     $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE);
227     $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
228     $who->message ((join ", ", @ignored_shout), cf::NDI_UNIQUE);
229     $who->message ("To stop ignoring one, use unignore.", cf::NDI_UNIQUE);
230     } else {
231     $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
232     }
233    
234     } elsif ($target && $type) {
235    
236     $timeout ne "" or $timeout = 24;
237     my $absolute_timeout = time + $timeout * 3600;
238    
239 root 1.22 if (cf::player::exists $target) {
240 root 1.5 if ($type eq "tell") {
241 root 1.12 $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE);
242     $who->{ext_ignore_tell}{$target} = $absolute_timeout;
243 root 1.5 } elsif ($type eq "shout") {
244 root 1.12 $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE);
245     $who->{ext_ignore_shout}{$target} = $absolute_timeout;
246 root 1.5 } elsif ($type eq "all") {
247 root 1.12 $who->message ("Now ignoring everything from $target for $timeout hours.", cf::NDI_UNIQUE);
248     $who->{ext_ignore_tell}{$target} = $absolute_timeout;
249     $who->{ext_ignore_shout}{$target} = $absolute_timeout;
250 root 1.5 } else {
251     $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
252     }
253     } else {
254 root 1.12 $who->message ("No such player: $target", cf::NDI_UNIQUE);
255 root 1.5 }
256    
257     } else {
258 root 1.23 $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n"
259 root 1.12 . "will ignore a player for <timeout> hours.\n"
260     . "Usage: ignore list\n"
261     . "will show you a list of players currently ignored.", cf::NDI_UNIQUE);
262 root 1.5 }
263     };
264    
265     cf::register_command unignore => 0, sub {
266     my ($who, $args) = @_;
267     my ($target, $type) = split /\s+/, $args;
268    
269     if ($args eq "") {
270 root 1.12 if ($who->{ext_ignore_tell}) {
271 root 1.5 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
272     $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE);
273     $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
274     $who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE);
275     } else {
276     $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
277     }
278     } else {
279 root 1.22 if (cf::player::exists $target) {
280 root 1.5 if ($type eq "tell") {
281 root 1.12 $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE);
282     delete $who->{ext_ignore_tell} {$target};
283 root 1.5 } elsif ($type eq "shout") {
284 root 1.12 $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE);
285     delete $who->{ext_ignore_shout}{$target};
286 root 1.5 } elsif ($type eq "all") {
287 root 1.12 $who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE);
288     delete $who->{ext_ignore_tell} {$target};
289     delete $who->{ext_ignore_shout}{$target};
290 root 1.5 } else {
291     $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
292     }
293     } else {
294     $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE);
295     }
296 root 1.23 }
297     };
298    
299     cf::register_command seen => 0, sub {
300     my ($who, $args) = @_;
301 root 1.5
302 root 1.23 if (my ($login) = $args =~ /(\S+)/) {
303 root 1.24 if ($login eq $who->name) {
304     $who->message ("Very funny, $login. Ha. Ha.", cf::NDI_UNIQUE);
305     } elsif (cf::player::find $login) {
306 root 1.23 $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 root 1.25 $who->message ("$login was last seen here "
312 root 1.23 . (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 root 1.5 }
320     };
321