ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/chat.ext
(Generate patch)

Comparing deliantra/maps/perl/chat.ext (file contents):
Revision 1.20 by root, Wed Jul 19 08:52:57 2006 UTC vs.
Revision 1.38 by pippijn, Mon Sep 18 00:14:48 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines