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.9 by pippijn, Sun May 7 10:46:07 2006 UTC vs.
Revision 1.37 by pippijn, Sun Sep 17 23:57:20 2006 UTC

1#! perl 1#! perl
2#CONVERSION: PARTIAL
2 3
3# implement a replacement for the built-in 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
7use NPC_Dialogue;
8use POSIX (); # for strftime only
9
6sub clean_timeouts { 10sub clean_timeouts($) {
7 my ($player) = @_; 11 my ($player) = @_;
8 my $NOW = time; 12 my $NOW = time;
9 13
10 for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) { 14 for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) {
11 delete $hash->{$_} for grep $hash->{$_} < $NOW, keys %$hash; 15 while (my ($k, $v) = each %$hash) {
16 if ($v < $NOW) {
17 $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE);
18 delete $hash->{$k};
19 } elsif (!cf::player::exists $k) {
20 $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE);
21 delete $hash->{$k};
22 }
23 }
12 } 24 }
13} 25}
14 26
15sub on_logout { 27cf::attach_to_players
28 prio => -1000,
29 on_login => sub {
16 my ($pl, $host) = @_; 30 my ($pl) = @_;
17 31
18 clean_timeouts $pl->ob; 32 clean_timeouts $pl->ob;
19} 33 },
34;
35
36cf::register_command listen => 0, sub {
37 my ($who, $msg) = @_;
38 my $player = cf::player::find $who->name;
39
40 if ($msg ne "") {
41 my $prev_listen = $player->listening;
42 $player->listening ($msg);
43 if ($prev_listen == $player->listening) {
44 $who->message ("Your verbose level stayed $prev_listen.", cf::NDI_UNIQUE);
45 } else {
46 $who->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE);
47 }
48 } else {
49 $who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE);
50 }
51};
52
53cf::register_command say => 0, sub {
54 my ($who, $msg) = @_;
55
56 utf8::decode $msg;
57
58 return if $who->contr->invoke (cf::EVENT_PLAYER_SAY, $msg);
59
60 if ($msg) {
61 my $name = $who->name;
62
63 utf8::encode $msg; # ->message not yet utf8-ified
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;
67
68 # npcs, magic_ears etc.
69 # first find all objects and theirt-level inventories
70 # within a 5x5 square # that have something resembling
71 # dialogue or support on_say.
72 my ($map, $x, $y) = ($who->map, $who->x - 2, $who->y - 2);
73
74 for my $npc (
75 grep +($_->invoke (cf::EVENT_OBJECT_SAY, $who->contr, $msg) && return) || NPC_Dialogue::has_dialogue $_,
76 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 my ($reply, @kw) = $dialog->tell ($msg);
86
87 if (defined $reply) {
88 if ($npc->type == cf::MAGIC_EAR) {
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 }
93 $npc->use_trigger;
94 } else {
95 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 }
100 }
101
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 }
107
108 } else {
109 $who->message ("What do you want to say?", cf::NDI_UNIQUE);
110 }
111};
20 112
21cf::register_command chat => 0, sub { 113cf::register_command chat => 0, sub {
22 my ($who, $msg) = @_; 114 my ($who, $msg) = @_;
115
116 utf8::decode $msg;
117
118 return if $who->contr->invoke (cf::EVENT_PLAYER_CHAT, $msg);
23 119
24 if ($msg) { 120 if ($msg) {
25 my $name = $who->name; 121 my $name = $who->name;
26 my $NOW = time; 122 my $NOW = time;
27 123
124 utf8::encode $msg; # ->message not yet utf8-ified
125# cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg;
126 ext::schmorp_irc::do_notice (sprintf "[%s] %s", $name, $msg);
127
28 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) 128 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE)
29 for grep $_->ob->{ext_ignore_shout}{$name} < $NOW, cf::player::list; 129 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
30 130
31 } else { 131 } else {
32 $who->message ("Chat what?", cf::NDI_UNIQUE); 132 $who->message ("Chat what?", cf::NDI_UNIQUE);
33 } 133 }
34}; 134};
35 135
36cf::register_command shout => 0, sub { 136cf::register_command shout => 0, sub {
37 my ($who, $msg) = @_; 137 my ($who, $msg) = @_;
138
139 utf8::decode $msg;
140
141 return if $who->contr->invoke (cf::EVENT_PLAYER_SHOUT, $msg);
38 142
39 if ($msg) { 143 if ($msg) {
40 my $NOW = time; 144 my $NOW = time;
41 my $name = $who->name; 145 my $name = $who->name;
42 146
147# cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg;
148 ext::schmorp_irc::do_notice (sprintf "{%s} %s\n", $name, $msg);
149
150 utf8::encode $msg; # ->message not yet utf8-ified
43 $_->ob->message ("$name shouts: $msg", cf::NDI_RED) 151 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
44 for grep $_->ob->{ext_ignore_shout}{$name} < $NOW, cf::player::list; 152 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
45 153
46 } else { 154 } else {
47 $who->message ("Shout what?", cf::NDI_UNIQUE); 155 $who->message ("Shout what?", cf::NDI_UNIQUE);
48 } 156 }
49 157
51 159
52cf::register_command tell => 0, sub { 160cf::register_command tell => 0, sub {
53 my ($who, $args) = @_; 161 my ($who, $args) = @_;
54 my ($target, $msg) = split /\s+/, $args, 2; 162 my ($target, $msg) = split /\s+/, $args, 2;
55 163
164 utf8::decode $msg;
165
166 return if $who->contr->invoke (cf::EVENT_PLAYER_TELL, $target, $msg);
167
56 my $name = $who->name; 168 my $name = $who->name;
57 169
58 if ($msg) { 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);
174 }
175
59 if (my $other = cf::player::find $target) { 176 elsif (my $other = cf::player::find $target) {
60 177
178 if ($msg) {
179 if ($target eq $name) {
180 $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE);
61 if ($target ne $name and $other->ob->{ext_ignore_tell}{$name} < time) { 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 utf8::encode $msg; # ->message not yet utf8-ified
185 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg;
186
62 $who->message ("You tell $target: $msg"); 187 $who->message ("You tell $target: $msg");
63 $other->ob->message ("$name tells you: $msg"); 188 $other->ob->message ("$name tells you: $msg");
64 $other->ob->{ext_last_tell} = $name; 189 $other->ob->{ext_last_tell} = $name;
65 } elsif ($target eq $name) { 190 }
66 $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE);
67 } else { 191 } else {
68 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
69 }
70
71 } else {
72 $who->message ("No such player or ambiguous name. Your message: $msg", cf::NDI_UNIQUE);
73 }
74 } else {
75 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE); 192 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE);
193 }
194
195 } else {
196 $who->message ("No such player. Your message: $msg", cf::NDI_UNIQUE);
76 } 197 }
77}; 198};
78 199
79cf::register_command reply => 0, sub { 200cf::register_command reply => 0, sub {
80 my ($who, $args) = @_; 201 my ($who, $args) = @_;
81 my $name = $who->name; 202 my $name = $who->name;
82 203
83 if ($args) { 204 utf8::decode $args;
205
206 return if $who->contr->invoke (cf::EVENT_PLAYER_TELL, $who->{ext_last_tell}, $args);
207
208 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
84 if (my $other = cf::player::find $who->{ext_last_tell}) { 214 elsif (my $other = cf::player::find $who->{ext_last_tell}) {
85 215
216 if ($args) {
86 $other->ob->{ext_ignore_tell}{$name} >= time 217 $other->ob->{ext_ignore_tell}{$name} >= time
87 or delete $other->ob->{ext_ignore_tell}{$name}; 218 or delete $other->ob->{ext_ignore_tell}{$name};
88 219
89 if ($other->ob->{ext_ignore_tell}{$name} < time) { 220 if ($other->ob->{ext_ignore_tell}{$name} < time) {
221 utf8::encode $args; # ->message not yet utf8-ified
222 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args;
223
90 $who->message ("You tell " . $other->ob->name . ": $args"); 224 $who->message ("You tell " . $other->ob->name . ": $args");
91 $other->ob->message ("$name tells you: $args"); 225 $other->ob->message ("$name tells you: $args");
92 $who->{ext_last_tell} = $other->ob->name; 226 $who->{ext_last_tell} = $other->ob->name;
93 } else { 227 } else {
94 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE); 228 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
95 } 229 }
96
97 } else {
98 $who->message ("Can't reply, player left. Your message: $args", cf::NDI_UNIQUE);
99 }
100 } else { 230 } else {
101 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE); 231 $who->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE);
232 }
233
234 } else {
235 $who->message ("Can't reply, player left. Your message: $args".$who->{ext_last_tell}, cf::NDI_UNIQUE);
102 } 236 }
103}; 237};
104 238
105cf::register_command ignore => 0, sub { 239cf::register_command ignore => 0, sub {
106 my ($who, $args) = @_; 240 my ($who, $args) = @_;
121 } 255 }
122 256
123 } elsif ($target && $type) { 257 } elsif ($target && $type) {
124 258
125 $timeout ne "" or $timeout = 24; 259 $timeout ne "" or $timeout = 24;
126
127 my $absolute_timeout = time + $timeout * 3600; 260 my $absolute_timeout = time + $timeout * 3600;
128 261
129 if (my $other = cf::player::find $target) { 262 if (cf::player::exists $target) {
130 if ($type eq "tell") { 263 if ($type eq "tell") {
131 $who->message ("Now ignoring private messages from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 264 $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE);
132 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 265 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
133 } elsif ($type eq "shout") { 266 } elsif ($type eq "shout") {
134 $who->message ("Now ignoring shouts from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 267 $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE);
135 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 268 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
136 } elsif ($type eq "all") { 269 } elsif ($type eq "all") {
137 $who->message ("Now ignoring everything from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 270 $who->message ("Now ignoring everything from $target for $timeout hours.", cf::NDI_UNIQUE);
138 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 271 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
139 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 272 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
140 } else { 273 } else {
141 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); 274 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
142 } 275 }
143 } else { 276 } else {
144 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); 277 $who->message ("No such player: $target", cf::NDI_UNIQUE);
145 } 278 }
146 279
147 } else { 280 } else {
148 $who->message ("Usage:", cf::NDI_UNIQUE);
149 $who->message ("ignore <player> <tell | shout | all> <timeout>", cf::NDI_UNIQUE); 281 $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n"
150 $who->message ("will ignore a player for <timeout> hours.", cf::NDI_UNIQUE); 282 . "will ignore a player for <timeout> hours.\n"
151 $who->message ("ignore list", cf::NDI_UNIQUE); 283 . "Usage: ignore list\n"
152 $who->message ("will show you a list of players currently ignored.", cf::NDI_UNIQUE); 284 . "will show you a list of players currently ignored.", cf::NDI_UNIQUE);
153 } 285 }
154}; 286};
155 287
156cf::register_command unignore => 0, sub { 288cf::register_command unignore => 0, sub {
157 my ($who, $args) = @_; 289 my ($who, $args) = @_;
158 my ($target, $type) = split /\s+/, $args; 290 my ($target, $type) = split /\s+/, $args;
159 291
160 if ($args eq "") { 292 if ($args eq "") {
161 if ($who->{ ext_ignore_tell }) { 293 if ($who->{ext_ignore_tell}) {
162 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); 294 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
163 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE); 295 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE);
164 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); 296 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
165 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE); 297 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE);
166 } else { 298 } else {
167 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); 299 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
168 } 300 }
169 } else { 301 } else {
170
171 if (my $other = cf::player::find $target) { 302 if (cf::player::exists $target) {
172 if ($type eq "tell") { 303 if ($type eq "tell") {
173 $who->message ("Not ignoring private messages from " . $other->ob->name . " anymore.", cf::NDI_UNIQUE); 304 $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE);
174 delete $who->{ext_ignore_tell}{$other->ob->name}; 305 delete $who->{ext_ignore_tell} {$target};
175 } elsif ($type eq "shout") { 306 } elsif ($type eq "shout") {
176 $who->message ("Not ignoring shouts from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 307 $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE);
177 delete $who->{ext_ignore_shout}{$other->ob->name}; 308 delete $who->{ext_ignore_shout}{$target};
178 } elsif ($type eq "all") { 309 } elsif ($type eq "all") {
179 $who->message ("Not ignoring anything from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 310 $who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE);
180 delete $who->{ext_ignore_tell} {$other->ob->name}; 311 delete $who->{ext_ignore_tell} {$target};
181 delete $who->{ext_ignore_shout}{$other->ob->name}; 312 delete $who->{ext_ignore_shout}{$target};
182 } else { 313 } else {
183 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); 314 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
184 } 315 }
185 } else { 316 } else {
186 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); 317 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE);
187 } 318 }
188
189 } 319 }
190}; 320};
191 321
322cf::register_command seen => 0, sub {
323 my ($who, $args) = @_;
324
325 if (my ($login) = $args =~ /(\S+)/) {
326 if ($login eq $who->name) {
327 $who->message ("Very funny, $login. Ha. Ha.", cf::NDI_UNIQUE);
328 } elsif (cf::player::find $login) {
329 $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 $who->message ("$login was last seen here "
335 . (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 }
343};
344

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines