ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/chat.ext
Revision: 1.37
Committed: Sun Sep 17 23:57:20 2006 UTC (17 years, 8 months ago) by pippijn
Branch: MAIN
Changes since 1.36: +15 -3 lines
Log Message:
Implemented !tell fantasy commands and changed in-game tell and reply to
print to IRC.

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.35 return if $who->contr->invoke (cf::EVENT_PLAYER_SAY, $msg);
59    
60 root 1.20 if ($msg) {
61     my $name = $who->name;
62    
63 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
64 root 1.21 $_->ob->message ("$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE)
65     for grep $who->on_same_map_as ($_->ob), cf::player::list;
66 root 1.27 utf8::decode $msg;
67 root 1.20
68     # npcs, magic_ears etc.
69 root 1.35 # first find all objects and theirt-level inventories
70     # within a 5x5 square # that have something resembling
71     # dialogue or support on_say.
72 root 1.20 my ($map, $x, $y) = ($who->map, $who->x - 2, $who->y - 2);
73    
74     for my $npc (
75 root 1.36 grep +($_->invoke (cf::EVENT_OBJECT_SAY, $who->contr, $msg) && return) || NPC_Dialogue::has_dialogue $_,
76 root 1.20 map +($_, $_->inv),
77     grep $_,
78     map $map->at ($x + $_ % 5, $y + (int $_ / 5)),
79     0..24
80     ) {
81     # if some listener teleported us somewhere else, stop right here
82     last unless $map->path == $who->map->path;
83    
84     my $dialog = new NPC_Dialogue ob => $who, npc => $npc;
85 root 1.26 my ($reply, @kw) = $dialog->tell ($msg);
86 root 1.20
87     if (defined $reply) {
88     if ($npc->type == cf::MAGIC_EAR) {
89 root 1.21 if (length $reply) {
90     $_->ob->message ($reply, cf::NDI_BROWN | cf::NDI_UNIQUE)
91     for grep $who->on_same_map_as ($_->ob), cf::player::list;
92     }
93 root 1.20 $npc->use_trigger;
94     } else {
95 root 1.21 if (length $reply) {
96     $_->ob->message ($npc->name . " says: $reply", cf::NDI_BROWN | cf::NDI_UNIQUE)
97     for grep $who->on_same_map_as ($_->ob), cf::player::list;
98     }
99 root 1.20 }
100     }
101 root 1.26
102     if (@kw) {
103     $_->ob->message ("[further topics: " . (join ", ", @kw) . "]", cf::NDI_BROWN | cf::NDI_UNIQUE)
104     for grep $who->on_same_map_as ($_->ob), cf::player::list;
105     }
106 root 1.20 }
107    
108     } else {
109     $who->message ("What do you want to say?", cf::NDI_UNIQUE);
110     }
111     };
112 pippijn 1.15
113 pippijn 1.6 cf::register_command chat => 0, sub {
114 pippijn 1.1 my ($who, $msg) = @_;
115    
116 root 1.27 utf8::decode $msg;
117    
118 root 1.35 return if $who->contr->invoke (cf::EVENT_PLAYER_CHAT, $msg);
119    
120 pippijn 1.1 if ($msg) {
121     my $name = $who->name;
122 root 1.8 my $NOW = time;
123 pippijn 1.1
124 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
125 root 1.30 # cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg;
126 root 1.34 ext::schmorp_irc::do_notice (sprintf "[%s] %s", $name, $msg);
127 root 1.19
128 root 1.4 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE)
129 pippijn 1.15 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
130 root 1.4
131 pippijn 1.1 } else {
132     $who->message ("Chat what?", cf::NDI_UNIQUE);
133     }
134     };
135    
136 pippijn 1.6 cf::register_command shout => 0, sub {
137 pippijn 1.1 my ($who, $msg) = @_;
138    
139 root 1.27 utf8::decode $msg;
140    
141 root 1.35 return if $who->contr->invoke (cf::EVENT_PLAYER_SHOUT, $msg);
142    
143 pippijn 1.1 if ($msg) {
144 root 1.8 my $NOW = time;
145 pippijn 1.1 my $name = $who->name;
146    
147 root 1.30 # cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg;
148 root 1.34 ext::schmorp_irc::do_notice (sprintf "{%s} %s\n", $name, $msg);
149 root 1.19
150 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
151 root 1.4 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
152 pippijn 1.15 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
153 root 1.4
154 pippijn 1.1 } else {
155     $who->message ("Shout what?", cf::NDI_UNIQUE);
156     }
157    
158     };
159 root 1.5
160     cf::register_command tell => 0, sub {
161     my ($who, $args) = @_;
162     my ($target, $msg) = split /\s+/, $args, 2;
163    
164 root 1.27 utf8::decode $msg;
165    
166 root 1.35 return if $who->contr->invoke (cf::EVENT_PLAYER_TELL, $target, $msg);
167    
168 root 1.5 my $name = $who->name;
169    
170 pippijn 1.37 if ($target =~ /irc\//) {
171     my (undef, $nick) = split /\//, $target, 2;
172     $who->message ("You tell $target: $args");
173     ext::schmorp_irc::do_notice (sprintf "(%s) %s: %s\n", $name, $nick, $msg);
174     }
175    
176     elsif (my $other = cf::player::find $target) {
177 root 1.12
178 pippijn 1.11 if ($msg) {
179 root 1.12 if ($target eq $name) {
180     $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE);
181     } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) {
182     $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE);
183     } else {
184 root 1.27 utf8::encode $msg; # ->message not yet utf8-ified
185 root 1.19 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg;
186    
187 pippijn 1.9 $who->message ("You tell $target: $msg");
188     $other->ob->message ("$name tells you: $msg");
189     $other->ob->{ext_last_tell} = $name;
190     }
191 root 1.5 } else {
192 pippijn 1.11 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE);
193 root 1.5 }
194 root 1.12
195 root 1.5 } else {
196 root 1.12 $who->message ("No such player. Your message: $msg", cf::NDI_UNIQUE);
197 root 1.5 }
198     };
199    
200     cf::register_command reply => 0, sub {
201     my ($who, $args) = @_;
202     my $name = $who->name;
203    
204 root 1.27 utf8::decode $args;
205    
206 root 1.35 return if $who->contr->invoke (cf::EVENT_PLAYER_TELL, $who->{ext_last_tell}, $args);
207    
208 pippijn 1.37 if ($who->{ext_last_tell} =~ /irc\//) {
209     my (undef, $nick) = split /\//, $who->{ext_last_tell}, 2;
210     $who->message ("You tell " . $who->{ext_last_tell} . ": $args");
211     ext::schmorp_irc::do_notice (sprintf "(%s) %s: %s\n", $name, $nick, $args);
212     }
213    
214     elsif (my $other = cf::player::find $who->{ext_last_tell}) {
215 root 1.35
216 pippijn 1.11 if ($args) {
217 pippijn 1.9 $other->ob->{ext_ignore_tell}{$name} >= time
218     or delete $other->ob->{ext_ignore_tell}{$name};
219    
220     if ($other->ob->{ext_ignore_tell}{$name} < time) {
221 root 1.28 utf8::encode $args; # ->message not yet utf8-ified
222 root 1.19 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args;
223    
224 pippijn 1.9 $who->message ("You tell " . $other->ob->name . ": $args");
225     $other->ob->message ("$name tells you: $args");
226     $who->{ext_last_tell} = $other->ob->name;
227     } else {
228     $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
229     }
230 root 1.5 } else {
231 pippijn 1.11 $who->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE);
232 root 1.5 }
233 pippijn 1.11
234 root 1.5 } else {
235 pippijn 1.37 $who->message ("Can't reply, player left. Your message: $args".$who->{ext_last_tell}, cf::NDI_UNIQUE);
236 root 1.5 }
237     };
238    
239     cf::register_command ignore => 0, sub {
240     my ($who, $args) = @_;
241     my ($target, $type, $timeout) = split /\s+/, $args;
242    
243     if ($args eq "list") {
244 root 1.8 clean_timeouts $who;
245    
246 root 1.5 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}})
247     + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) {
248     $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
249     $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE);
250     $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
251     $who->message ((join ", ", @ignored_shout), cf::NDI_UNIQUE);
252     $who->message ("To stop ignoring one, use unignore.", cf::NDI_UNIQUE);
253     } else {
254     $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
255     }
256    
257     } elsif ($target && $type) {
258    
259     $timeout ne "" or $timeout = 24;
260     my $absolute_timeout = time + $timeout * 3600;
261    
262 root 1.22 if (cf::player::exists $target) {
263 root 1.5 if ($type eq "tell") {
264 root 1.12 $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE);
265     $who->{ext_ignore_tell}{$target} = $absolute_timeout;
266 root 1.5 } elsif ($type eq "shout") {
267 root 1.12 $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE);
268     $who->{ext_ignore_shout}{$target} = $absolute_timeout;
269 root 1.5 } elsif ($type eq "all") {
270 root 1.12 $who->message ("Now ignoring everything from $target for $timeout hours.", cf::NDI_UNIQUE);
271     $who->{ext_ignore_tell}{$target} = $absolute_timeout;
272     $who->{ext_ignore_shout}{$target} = $absolute_timeout;
273 root 1.5 } else {
274     $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
275     }
276     } else {
277 root 1.12 $who->message ("No such player: $target", cf::NDI_UNIQUE);
278 root 1.5 }
279    
280     } else {
281 root 1.23 $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n"
282 root 1.12 . "will ignore a player for <timeout> hours.\n"
283     . "Usage: ignore list\n"
284     . "will show you a list of players currently ignored.", cf::NDI_UNIQUE);
285 root 1.5 }
286     };
287    
288     cf::register_command unignore => 0, sub {
289     my ($who, $args) = @_;
290     my ($target, $type) = split /\s+/, $args;
291    
292     if ($args eq "") {
293 root 1.12 if ($who->{ext_ignore_tell}) {
294 root 1.5 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
295     $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE);
296     $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
297     $who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE);
298     } else {
299     $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
300     }
301     } else {
302 root 1.22 if (cf::player::exists $target) {
303 root 1.5 if ($type eq "tell") {
304 root 1.12 $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE);
305     delete $who->{ext_ignore_tell} {$target};
306 root 1.5 } elsif ($type eq "shout") {
307 root 1.12 $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE);
308     delete $who->{ext_ignore_shout}{$target};
309 root 1.5 } elsif ($type eq "all") {
310 root 1.12 $who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE);
311     delete $who->{ext_ignore_tell} {$target};
312     delete $who->{ext_ignore_shout}{$target};
313 root 1.5 } else {
314     $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
315     }
316     } else {
317     $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE);
318     }
319 root 1.23 }
320     };
321    
322     cf::register_command seen => 0, sub {
323     my ($who, $args) = @_;
324 root 1.5
325 root 1.23 if (my ($login) = $args =~ /(\S+)/) {
326 root 1.24 if ($login eq $who->name) {
327     $who->message ("Very funny, $login. Ha. Ha.", cf::NDI_UNIQUE);
328     } elsif (cf::player::find $login) {
329 root 1.23 $who->message ("$login is right here on this server!", cf::NDI_UNIQUE);
330     } elsif (cf::player::exists $login
331     and stat sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($login) x 2) {
332     my $time = (stat _)[9];
333    
334 root 1.25 $who->message ("$login was last seen here "
335 root 1.23 . (POSIX::strftime "%Y-%m-%d %H:%M:%S +0000", gmtime $time)
336     . " which was " . (int +(time - $time) / 3600) . " hours ago.", cf::NDI_UNIQUE);
337     } else {
338     $who->message ("No player named $login is known to me.", cf::NDI_UNIQUE);
339     }
340     } else {
341     $who->message ("Usage: seen <player>", cf::NDI_UNIQUE);
342 root 1.5 }
343     };
344