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.5 by root, Sun May 7 08:10:23 2006 UTC vs.
Revision 1.42 by root, Tue Dec 12 16:59:34 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
10sub clean_timeouts($) {
11 my ($player) = @_;
12 my $NOW = time;
13
14 for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) {
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 }
24 }
25}
26
27cf::attach_to_players
28 prio => -1000,
29 on_login => sub {
30 my ($pl) = @_;
31
32 clean_timeouts $pl->ob;
33 },
34;
35
6cf::register_command chat => 0, sub 36cf::register_command listen => sub {
7{
8 my ($who, $msg) = @_; 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 => sub {
54 my ($who, $msg) = @_;
55
56 utf8::decode $msg;
57
58 return if $who->contr->invoke (cf::EVENT_PLAYER_SAY, $msg);
9 59
10 if ($msg) { 60 if ($msg) {
11 my $name = $who->name; 61 my $name = $who->name;
12 62
63 utf8::encode $msg; # ->message not yet utf8-ified
13 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) 64 $_->ob->message ("$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE)
14 for grep !$player->ob->{ext_ignore_shout}, cf::player::list; 65 for grep $who->on_same_map_as ($_->ob), cf::player::list;
66 utf8::decode $msg;
15 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
16 } else { 108 } else {
17 $who->message ("Chat what?", cf::NDI_UNIQUE); 109 $who->message ("What do you want to say?", cf::NDI_UNIQUE);
18 } 110 }
19}; 111};
20 112
21cf::register_command shout => 0, sub 113cf::register_command chat => sub {
22{
23 my ($who, $msg) = @_; 114 my ($who, $msg) = @_;
115
116 utf8::decode $msg;
117
118 return if $who->contr->invoke (cf::EVENT_PLAYER_CHAT, $msg);
24 119
25 if ($msg) { 120 if ($msg) {
26 my $name = $who->name; 121 my $name = $who->name;
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
128 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE)
129 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
130
131 } else {
132 $who->message ("Chat what?", cf::NDI_UNIQUE);
133 }
134};
135
136cf::register_command shout => sub {
137 my ($who, $msg) = @_;
138
139 utf8::decode $msg;
140
141 return if $who->contr->invoke (cf::EVENT_PLAYER_SHOUT, $msg);
142
143 if ($msg) {
144 my $NOW = time;
145 my $name = $who->name;
146
147 cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg;
148 ext::schmorp_irc::do_notice (sprintf "\007\0034{%s} %s\n", $name, $msg);
149
150 utf8::encode $msg; # ->message not yet utf8-ified
28 $_->ob->message ("$name shouts: $msg", cf::NDI_RED) 151 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
29 for grep !$player->ob->{ext_ignore_shout}, cf::player::list; 152 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
30 153
31 } else { 154 } else {
32 $who->message ("Shout what?", cf::NDI_UNIQUE); 155 $who->message ("Shout what?", cf::NDI_UNIQUE);
33 } 156 }
34 157
35}; 158};
36 159
37cf::register_command tell => 0, sub { 160cf::register_command tell => sub {
38 my ($who, $args) = @_; 161 my ($who, $args) = @_;
39 my ($target, $msg) = split /\s+/, $args, 2; 162 my ($target, $msg) = split /\s+/, $args, 2;
40 163
164 utf8::decode $msg;
165
166 return if $who->contr->invoke (cf::EVENT_PLAYER_TELL, $target, $msg);
167
41 my $name = $who->name; 168 my $name = $who->name;
42 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);
43 if (my $other = cf::player::find $target) { 174 } elsif (my $other = cf::player::find $target) {
44 175
45 $other->ob->{ext_ignore_tell}{$name} >= time 176 if ($msg) {
46 or delete $other->ob->{ext_ignore_tell}{$name};
47
48 if ($target ne $name and !$other->ob->{ext_ignore_tell}{$name}) {
49 $who->message ("You tell $target: $msg");
50 $other->ob->message ("$name tells you: $msg");
51 $other->ob->{ext_last_tell} = $name;
52 } elsif ($target eq $name) { 177 if ($target eq $name) {
53 $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);
179 } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) {
180 $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE);
54 } else { 181 } else {
55 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE); 182 utf8::encode $msg; # ->message not yet utf8-ified
183 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg;
184
185 $who->message ("You tell $target: $msg");
186 $other->ob->message ("$name tells you: $msg");
187 $other->ob->{ext_last_tell} = $name;
56 } 188 }
57
58 } else { 189 } else {
190 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE);
191 }
192
193 } else {
59 $who->message ("No such player or ambiguous name. Your message: $msg", cf::NDI_UNIQUE); 194 $who->message ("No such player. Your message: $msg", cf::NDI_UNIQUE);
60 } 195 }
61}; 196};
62 197
63cf::register_command reply => 0, sub { 198cf::register_command reply => sub {
64 my ($who, $args) = @_; 199 my ($who, $args) = @_;
65 my $name = $who->name; 200 my $name = $who->name;
66 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);
67 if (my $other = cf::player::find $who->{ext_last_tell}) { 210 } elsif (my $other = cf::player::find $who->{ext_last_tell}) {
68 211
212 if ($args) {
69 $other->ob->{ext_ignore_tell}{$name} >= time 213 $other->ob->{ext_ignore_tell}{$name} >= time
70 or delete $other->ob->{ext_ignore_tell}{$name}; 214 or delete $other->ob->{ext_ignore_tell}{$name};
71 215
72 if (!$other->ob->{ext_ignore_tell}{$name}) { 216 if ($other->ob->{ext_ignore_tell}{$name} < time) {
217 utf8::encode $args; # ->message not yet utf8-ified
218 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args;
219
73 $who->message ("You tell " . $other->ob->name . ": $args"); 220 $who->message ("You tell " . $other->ob->name . ": $args");
74 $other->ob->message ("$name tells you: $args"); 221 $other->ob->message ("$name tells you: $args");
75 $who->{ext_last_tell} = $other->ob->name; 222 $who->{ext_last_tell} = $other->ob->name;
76 } else { 223 } else {
77 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE); 224 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
78 } 225 }
79
80 } else { 226 } else {
227 $who->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE);
228 }
229
230 } else {
81 $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);
82 } 232 }
83}; 233};
84 234
85cf::register_command ignore => 0, sub { 235cf::register_command ignore => sub {
86 my ($who, $args) = @_; 236 my ($who, $args) = @_;
87 my ($target, $type, $timeout) = split /\s+/, $args; 237 my ($target, $type, $timeout) = split /\s+/, $args;
88 238
89 if ($args eq "list") { 239 if ($args eq "list") {
240 clean_timeouts $who;
241
90 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}}) 242 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}})
91 + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) { 243 + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) {
92 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); 244 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
93 $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE); 245 $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE);
94 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); 246 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
99 } 251 }
100 252
101 } elsif ($target && $type) { 253 } elsif ($target && $type) {
102 254
103 $timeout ne "" or $timeout = 24; 255 $timeout ne "" or $timeout = 24;
104
105 my $absolute_timeout = time + $timeout * 3600; 256 my $absolute_timeout = time + $timeout * 3600;
106 257
107 if (my $other = cf::player::find $target) { 258 if (cf::player::exists $target) {
108 if ($type eq "tell") { 259 if ($type eq "tell") {
109 $who->message ("Now ignoring private messages from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 260 $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE);
110 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 261 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
111 } elsif ($type eq "shout") { 262 } elsif ($type eq "shout") {
112 $who->message ("Now ignoring shouts from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 263 $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE);
113 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 264 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
114 } elsif ($type eq "all") { 265 } elsif ($type eq "all") {
115 $who->message ("Now ignoring everything from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 266 $who->message ("Now ignoring everything from $target for $timeout hours.", cf::NDI_UNIQUE);
116 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 267 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
117 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 268 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
118 } else { 269 } else {
119 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); 270 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
120 } 271 }
121 } else { 272 } else {
122 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); 273 $who->message ("No such player: $target", cf::NDI_UNIQUE);
123 } 274 }
124 275
125 } else { 276 } else {
126 $who->message ("Usage:", cf::NDI_UNIQUE);
127 $who->message ("ignore <player> <tell | shout | all> <timeout>", cf::NDI_UNIQUE); 277 $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n"
128 $who->message ("will ignore a player for <timeout> hours.", cf::NDI_UNIQUE); 278 . "will ignore a player for <timeout> hours.\n"
129 $who->message ("ignore list", cf::NDI_UNIQUE); 279 . "Usage: ignore list\n"
130 $who->message ("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);
131 } 281 }
132}; 282};
133 283
134cf::register_command unignore => 0, sub { 284cf::register_command unignore => sub {
135 my ($who, $args) = @_; 285 my ($who, $args) = @_;
136 my ($target, $type) = split /\s+/, $args; 286 my ($target, $type) = split /\s+/, $args;
137 287
138 if ($args eq "") { 288 if ($args eq "") {
139 if ($who->{ ext_ignore_tell }) { 289 if ($who->{ext_ignore_tell}) {
140 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); 290 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
141 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE); 291 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE);
142 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); 292 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
143 $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);
144 } else { 294 } else {
145 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); 295 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
146 } 296 }
147 } else { 297 } else {
148
149 if (my $other = cf::player::find $target) { 298 if (cf::player::exists $target) {
150 if ($type eq "tell") { 299 if ($type eq "tell") {
151 $who->message ("Not ignoring private messages from " . $other->ob->name . " anymore.", cf::NDI_UNIQUE); 300 $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE);
152 delete $who->{ext_ignore_tell}{$other->ob->name}; 301 delete $who->{ext_ignore_tell} {$target};
153 } elsif ($type eq "shout") { 302 } elsif ($type eq "shout") {
154 $who->message ("Not ignoring shouts from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 303 $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE);
155 delete $who->{ext_ignore_shout}{$other->ob->name}; 304 delete $who->{ext_ignore_shout}{$target};
156 } elsif ($type eq "all") { 305 } elsif ($type eq "all") {
157 $who->message ("Not ignoring anything from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 306 $who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE);
158 delete $who->{ext_ignore_tell} {$other->ob->name}; 307 delete $who->{ext_ignore_tell} {$target};
159 delete $who->{ext_ignore_shout}{$other->ob->name}; 308 delete $who->{ext_ignore_shout}{$target};
160 } else { 309 } else {
161 $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);
162 } 311 }
163 } else { 312 } else {
164 $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);
165 } 314 }
166
167 } 315 }
168}; 316};
169 317
318cf::register_command seen => 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