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.6 by pippijn, Sun May 7 08:24:50 2006 UTC vs.
Revision 1.35 by root, Tue Aug 29 07:58:35 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
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
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, $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};
5 112
6cf::register_command chat => 0, sub { 113cf::register_command chat => 0, sub {
7 my ($who, $msg) = @_; 114 my ($who, $msg) = @_;
8 115
116 utf8::decode $msg;
117
118 return if $who->contr->invoke (cf::EVENT_PLAYER_CHAT, $msg);
119
9 if ($msg) { 120 if ($msg) {
10 my $name = $who->name; 121 my $name = $who->name;
122 my $NOW = time;
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);
11 127
12 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) 128 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE)
13 for grep !$player->ob->{ext_ignore_shout}, cf::player::list; 129 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
14 130
15 } else { 131 } else {
16 $who->message ("Chat what?", cf::NDI_UNIQUE); 132 $who->message ("Chat what?", cf::NDI_UNIQUE);
17 } 133 }
18}; 134};
19 135
20cf::register_command shout => 0, sub { 136cf::register_command shout => 0, sub {
21 my ($who, $msg) = @_; 137 my ($who, $msg) = @_;
22 138
139 utf8::decode $msg;
140
141 return if $who->contr->invoke (cf::EVENT_PLAYER_SHOUT, $msg);
142
23 if ($msg) { 143 if ($msg) {
144 my $NOW = time;
24 my $name = $who->name; 145 my $name = $who->name;
25 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
26 $_->ob->message ("$name shouts: $msg", cf::NDI_RED) 151 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
27 for grep !$player->ob->{ext_ignore_shout}, cf::player::list; 152 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
28 153
29 } else { 154 } else {
30 $who->message ("Shout what?", cf::NDI_UNIQUE); 155 $who->message ("Shout what?", cf::NDI_UNIQUE);
31 } 156 }
32 157
34 159
35cf::register_command tell => 0, sub { 160cf::register_command tell => 0, sub {
36 my ($who, $args) = @_; 161 my ($who, $args) = @_;
37 my ($target, $msg) = split /\s+/, $args, 2; 162 my ($target, $msg) = split /\s+/, $args, 2;
38 163
164 utf8::decode $msg;
165
166 return if $who->contr->invoke (cf::EVENT_PLAYER_TELL, $target, $msg);
167
39 my $name = $who->name; 168 my $name = $who->name;
40 169
41 if (my $other = cf::player::find $target) { 170 if (my $other = cf::player::find $target) {
42 171
43 $other->ob->{ext_ignore_tell}{$name} >= time 172 if ($msg) {
44 or delete $other->ob->{ext_ignore_tell}{$name};
45
46 if ($target ne $name and !$other->ob->{ext_ignore_tell}{$name}) {
47 $who->message ("You tell $target: $msg");
48 $other->ob->message ("$name tells you: $msg");
49 $other->ob->{ext_last_tell} = $name;
50 } elsif ($target eq $name) { 173 if ($target eq $name) {
51 $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE); 174 $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE);
175 } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) {
176 $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE);
52 } else { 177 } else {
53 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE); 178 utf8::encode $msg; # ->message not yet utf8-ified
179 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg;
180
181 $who->message ("You tell $target: $msg");
182 $other->ob->message ("$name tells you: $msg");
183 $other->ob->{ext_last_tell} = $name;
54 } 184 }
55
56 } else { 185 } else {
186 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE);
187 }
188
189 } else {
57 $who->message ("No such player or ambiguous name. Your message: $msg", cf::NDI_UNIQUE); 190 $who->message ("No such player. Your message: $msg", cf::NDI_UNIQUE);
58 } 191 }
59}; 192};
60 193
61cf::register_command reply => 0, sub { 194cf::register_command reply => 0, sub {
62 my ($who, $args) = @_; 195 my ($who, $args) = @_;
63 my $name = $who->name; 196 my $name = $who->name;
64 197
198 utf8::decode $args;
199
200 return if $who->contr->invoke (cf::EVENT_PLAYER_TELL, $who->{ext_last_tell}, $args);
201
65 if (my $other = cf::player::find $who->{ext_last_tell}) { 202 if (my $other = cf::player::find $who->{ext_last_tell}) {
66 203
204 if ($args) {
67 $other->ob->{ext_ignore_tell}{$name} >= time 205 $other->ob->{ext_ignore_tell}{$name} >= time
68 or delete $other->ob->{ext_ignore_tell}{$name}; 206 or delete $other->ob->{ext_ignore_tell}{$name};
69 207
70 if (!$other->ob->{ext_ignore_tell}{$name}) { 208 if ($other->ob->{ext_ignore_tell}{$name} < time) {
209 utf8::encode $args; # ->message not yet utf8-ified
210 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args;
211
71 $who->message ("You tell " . $other->ob->name . ": $args"); 212 $who->message ("You tell " . $other->ob->name . ": $args");
72 $other->ob->message ("$name tells you: $args"); 213 $other->ob->message ("$name tells you: $args");
73 $who->{ext_last_tell} = $other->ob->name; 214 $who->{ext_last_tell} = $other->ob->name;
74 } else { 215 } else {
75 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE); 216 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
217 }
218 } else {
219 $who->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE);
76 } 220 }
77 221
78 } else { 222 } else {
79 $who->message ("Can't reply, player left. Your message: $args", cf::NDI_UNIQUE); 223 $who->message ("Can't reply, player left. Your message: $args", cf::NDI_UNIQUE);
80 } 224 }
83cf::register_command ignore => 0, sub { 227cf::register_command ignore => 0, sub {
84 my ($who, $args) = @_; 228 my ($who, $args) = @_;
85 my ($target, $type, $timeout) = split /\s+/, $args; 229 my ($target, $type, $timeout) = split /\s+/, $args;
86 230
87 if ($args eq "list") { 231 if ($args eq "list") {
232 clean_timeouts $who;
233
88 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}}) 234 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}})
89 + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) { 235 + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) {
90 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); 236 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
91 $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE); 237 $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE);
92 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); 238 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
97 } 243 }
98 244
99 } elsif ($target && $type) { 245 } elsif ($target && $type) {
100 246
101 $timeout ne "" or $timeout = 24; 247 $timeout ne "" or $timeout = 24;
102
103 my $absolute_timeout = time + $timeout * 3600; 248 my $absolute_timeout = time + $timeout * 3600;
104 249
105 if (my $other = cf::player::find $target) { 250 if (cf::player::exists $target) {
106 if ($type eq "tell") { 251 if ($type eq "tell") {
107 $who->message ("Now ignoring private messages from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 252 $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE);
108 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 253 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
109 } elsif ($type eq "shout") { 254 } elsif ($type eq "shout") {
110 $who->message ("Now ignoring shouts from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 255 $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE);
111 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 256 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
112 } elsif ($type eq "all") { 257 } elsif ($type eq "all") {
113 $who->message ("Now ignoring everything from " . $other->ob->name . " for $timeout hours.", cf::NDI_UNIQUE); 258 $who->message ("Now ignoring everything from $target for $timeout hours.", cf::NDI_UNIQUE);
114 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 259 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
115 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 260 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
116 } else { 261 } else {
117 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); 262 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
118 } 263 }
119 } else { 264 } else {
120 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); 265 $who->message ("No such player: $target", cf::NDI_UNIQUE);
121 } 266 }
122 267
123 } else { 268 } else {
124 $who->message ("Usage:", cf::NDI_UNIQUE);
125 $who->message ("ignore <player> <tell | shout | all> <timeout>", cf::NDI_UNIQUE); 269 $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n"
126 $who->message ("will ignore a player for <timeout> hours.", cf::NDI_UNIQUE); 270 . "will ignore a player for <timeout> hours.\n"
127 $who->message ("ignore list", cf::NDI_UNIQUE); 271 . "Usage: ignore list\n"
128 $who->message ("will show you a list of players currently ignored.", cf::NDI_UNIQUE); 272 . "will show you a list of players currently ignored.", cf::NDI_UNIQUE);
129 } 273 }
130}; 274};
131 275
132cf::register_command unignore => 0, sub { 276cf::register_command unignore => 0, sub {
133 my ($who, $args) = @_; 277 my ($who, $args) = @_;
134 my ($target, $type) = split /\s+/, $args; 278 my ($target, $type) = split /\s+/, $args;
135 279
136 if ($args eq "") { 280 if ($args eq "") {
137 if ($who->{ ext_ignore_tell }) { 281 if ($who->{ext_ignore_tell}) {
138 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); 282 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
139 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE); 283 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE);
140 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); 284 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
141 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE); 285 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE);
142 } else { 286 } else {
143 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); 287 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
144 } 288 }
145 } else { 289 } else {
146
147 if (my $other = cf::player::find $target) { 290 if (cf::player::exists $target) {
148 if ($type eq "tell") { 291 if ($type eq "tell") {
149 $who->message ("Not ignoring private messages from " . $other->ob->name . " anymore.", cf::NDI_UNIQUE); 292 $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE);
150 delete $who->{ext_ignore_tell}{$other->ob->name}; 293 delete $who->{ext_ignore_tell} {$target};
151 } elsif ($type eq "shout") { 294 } elsif ($type eq "shout") {
152 $who->message ("Not ignoring shouts from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 295 $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE);
153 delete $who->{ext_ignore_shout}{$other->ob->name}; 296 delete $who->{ext_ignore_shout}{$target};
154 } elsif ($type eq "all") { 297 } elsif ($type eq "all") {
155 $who->message ("Not ignoring anything from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 298 $who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE);
156 delete $who->{ext_ignore_tell} {$other->ob->name}; 299 delete $who->{ext_ignore_tell} {$target};
157 delete $who->{ext_ignore_shout}{$other->ob->name}; 300 delete $who->{ext_ignore_shout}{$target};
158 } else { 301 } else {
159 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); 302 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
160 } 303 }
161 } else { 304 } else {
162 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); 305 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE);
163 } 306 }
164
165 } 307 }
166}; 308};
167 309
310cf::register_command seen => 0, sub {
311 my ($who, $args) = @_;
312
313 if (my ($login) = $args =~ /(\S+)/) {
314 if ($login eq $who->name) {
315 $who->message ("Very funny, $login. Ha. Ha.", cf::NDI_UNIQUE);
316 } elsif (cf::player::find $login) {
317 $who->message ("$login is right here on this server!", cf::NDI_UNIQUE);
318 } elsif (cf::player::exists $login
319 and stat sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($login) x 2) {
320 my $time = (stat _)[9];
321
322 $who->message ("$login was last seen here "
323 . (POSIX::strftime "%Y-%m-%d %H:%M:%S +0000", gmtime $time)
324 . " which was " . (int +(time - $time) / 3600) . " hours ago.", cf::NDI_UNIQUE);
325 } else {
326 $who->message ("No player named $login is known to me.", cf::NDI_UNIQUE);
327 }
328 } else {
329 $who->message ("Usage: seen <player>", cf::NDI_UNIQUE);
330 }
331};
332

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines