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

Comparing deliantra/server/ext/chat.ext (file contents):
Revision 1.15 by root, Fri Apr 27 00:49:58 2007 UTC vs.
Revision 1.28 by root, Tue Jul 24 22:49:40 2007 UTC

1#! perl 1#! perl # depends=irc
2#CONVERSION: PARTIAL 2#CONVERSION: PARTIAL
3 3
4# 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
5# adds ignore/unignore functionality 5# adds ignore/unignore functionality
6 6
7use NPC_Dialogue; 7use NPC_Dialogue;
8use POSIX (); # for strftime only 8use POSIX (); # for strftime only
9
10our $SAY_CHANNEL = {
11 id => "say",
12 title => "Map",
13 reply => "say ",
14 tooltip => "Things said to and replied from npcs near you and other players on the same map only.",
15};
16
17our $CHAT_CHANNEL = {
18 id => "chat",
19 title => "Chat",
20 reply => "chat ",
21 tooltip => "Player chat and shouts, global to the server.",
22};
9 23
10sub clean_timeouts($) { 24sub clean_timeouts($) {
11 my ($player) = @_; 25 my ($player) = @_;
12 my $NOW = time; 26 my $NOW = time;
13 27
22 } 36 }
23 } 37 }
24 } 38 }
25} 39}
26 40
41# send_irc ($format, @args, $msg)
42# make sure the last argument is the message!
43sub send_irc {
44 my ($format, @args) = @_;
45 my $msg = pop @args;
46 for (split /\n/, $msg) {
47 ext::irc::do_notice (sprintf $format, @args, $_)
48 }
49}
50
27cf::player->attach ( 51cf::player->attach (
28 prio => -1000, 52 prio => -1000,
29 on_login => sub { 53 on_login => sub {
30 my ($pl) = @_; 54 my ($pl) = @_;
31 55
32 clean_timeouts $pl->ob; 56 clean_timeouts $pl->ob;
57 $pl->ns->send_msg ($SAY_CHANNEL);
58 $pl->ns->send_msg ($CHAT_CHANNEL);
33 }, 59 },
34); 60);
35 61
36cf::register_command listen => sub { 62cf::register_command listen => sub {
37 my ($pl, $msg) = @_; 63 my ($pl, $msg) = @_;
41 $msg = 10 if $msg > 10; 67 $msg = 10 if $msg > 10;
42 68
43 my $prev_listen = $player->listening; 69 my $prev_listen = $player->listening;
44 $player->listening ($msg); 70 $player->listening ($msg);
45 if ($prev_listen == $player->listening) { 71 if ($prev_listen == $player->listening) {
46 $pl->message ("Your verbose level stayed $prev_listen.", cf::NDI_UNIQUE); 72 $pl->message ("Your verbose level stays at $prev_listen.", cf::NDI_UNIQUE);
47 } else { 73 } else {
48 $pl->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE); 74 $pl->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE);
49 } 75 }
50 } else { 76 } else {
51 $pl->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE); 77 $pl->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE);
54 80
55cf::register_command cointoss => sub { 81cf::register_command cointoss => sub {
56 my ($pl, $msg) = @_; 82 my ($pl, $msg) = @_;
57 83
58 my $name = $pl->name; 84 my $name = $pl->name;
85 my $coin = int rand 2 ? "Heads" : "Tails";
59 86
60 if (int rand 2) {
61 for my $other ( grep { $pl->on_same_map_as ($_->ob) } cf::player::list ) { 87 for my $other ( grep { $pl->on_same_map_as ($_->ob) } cf::player::list ) {
62 next 88 next
63 if $other->ob == $pl; 89 if $other->ob == $pl;
64 $other->ob->message ("$name flips a coin.... Heads!", cf::NDI_GREY | cf::NDI_UNIQUE);
65 }
66
67 $pl->message ("You flip a coin.... Heads!", cf::NDI_GREY | cf::NDI_UNIQUE);
68 } else {
69 for my $other ( grep { $pl->on_same_map_as ($_->ob) } cf::player::list ) {
70 next
71 if $other->ob == $pl;
72 $other->ob->message ("$name flips a coin.... Tails!", cf::NDI_GREY | cf::NDI_UNIQUE); 90 $other->ob->message ("$name flips a coin.... $coin!", cf::NDI_GREY | cf::NDI_UNIQUE);
73 }
74
75 $pl->message ("You flip a coin.... Tails!", cf::NDI_GREY | cf::NDI_UNIQUE);
76 } 91 }
92
93 $pl->message ("You flip a coin.... $coin!", cf::NDI_GREY | cf::NDI_UNIQUE);
77}; 94};
78 95
79cf::register_command orcknuckle => sub { 96cf::register_command orcknuckle => sub {
80 my ($pl, $msg) = @_; 97 my ($pl, $msg) = @_;
81 my @orcknuckle = ("none", "beholder", "ghost", "knight", "princess", "dragon", "orc"); 98 my @orcknuckle = ("none", "beholder", "ghost", "knight", "princess", "dragon", "orc");
750cf::register_command me => sub { 767cf::register_command me => sub {
751 my ($pl, $msg) = @_; 768 my ($pl, $msg) = @_;
752 769
753 my $name = $pl->name; 770 my $name = $pl->name;
754 771
755 $_->ob->message ("* $name $msg", cf::NDI_GREY | cf::NDI_UNIQUE) 772 $_->ns->send_msg ($SAY_CHANNEL => "* $name $msg", cf::NDI_GREY, $_ == $pl ? (reply => 1) : ())
756 for grep $pl->on_same_map_as ($_->ob), cf::player::list; 773 for grep $pl->on_same_map_as ($_->ob), cf::player::list;
757}; 774};
758 775
759cf::register_command say => sub { 776cf::register_command say => sub {
760 my ($pl, $msg) = @_; 777 my ($ob, $msg) = @_;
761 778
762 utf8::decode $msg; 779 utf8::decode $msg;
763 780
764 return if $pl->contr->invoke (cf::EVENT_PLAYER_SAY, $msg); 781 return if $ob->contr->invoke (cf::EVENT_PLAYER_SAY, $msg);
765 782
766 if ($msg) { 783 if ($msg) {
767 my $name = $pl->name; 784 my $name = $ob->name;
768
769 utf8::encode $msg; # ->message not yet utf8-ified
770 $_->ob->message ("$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE)
771 for grep $pl->on_same_map_as ($_->ob), cf::player::list; 785 my @plonmap = grep $ob->on_same_map_as ($_->ob), cf::player::list;
772 utf8::decode $msg; 786
787 $_->ns->send_msg ($SAY_CHANNEL => "$name says: $msg", cf::NDI_GREY, $_ == $ob ? (reply => 1) : ())
788 for @plonmap;
773 789
774 # npcs, magic_ears etc. 790 # npcs, magic_ears etc.
775 # first find all objects and theirt-level inventories 791 # first find all objects and their first-level inventories
776 # within a 5x5 square # that have something resembling 792 # within a 5x5 square that have something resembling
777 # dialogue or support on_say. 793 # dialogue or support on_say.
778 my ($map, $x, $y) = ($pl->map, $pl->x - 2, $pl->y - 2); 794 my ($map, $x, $y) = ($ob->map, $ob->x - 2, $ob->y - 2);
779 795
780 for my $npc ( 796 for my $npc (
781 grep +($_->invoke (cf::EVENT_OBJECT_SAY, $pl->contr, $msg) && return) || NPC_Dialogue::has_dialogue $_, 797 grep +($_->invoke (cf::EVENT_OBJECT_SAY, $ob->contr, $msg) && return) || NPC_Dialogue::has_dialogue $_,
782 map +($_, $_->inv), 798 map +($_, $_->inv),
783 grep $_, 799 grep $_,
784 map $map->at ($x + $_ % 5, $y + (int $_ / 5)), 800 map $map->at ($x + $_ % 5, $y + (int $_ / 5)),
785 0..24 801 0..24
786 ) { 802 ) {
787 # if some listener teleported us somewhere else, stop right here 803 # if some listener teleported us somewhere else, stop right here
788 last unless $map->path == $pl->map->path; 804 last unless $map->path == $ob->map->path;
789 805
790 my $dialog = new NPC_Dialogue ob => $pl, npc => $npc; 806 my $dialog = new NPC_Dialogue pl => $ob->contr, npc => $npc;
791 my ($reply, @kw) = $dialog->tell ($msg); 807 my ($reply, @kw) = $dialog->tell ($msg);
792 808
793 if (defined $reply) { 809 if (defined $reply) {
794 if ($npc->type == cf::MAGIC_EAR) { 810 if ($npc->type == cf::MAGIC_EAR) {
795 if (length $reply) { 811 if (length $reply) {
796 $_->ob->message ($reply, cf::NDI_BROWN | cf::NDI_UNIQUE) 812 $_->ns->send_msg ($SAY_CHANNEL => $reply, cf::NDI_BROWN)
797 for grep $pl->on_same_map_as ($_->ob), cf::player::list; 813 for @plonmap;
798 } 814 }
799 $npc->use_trigger; 815 $npc->use_trigger;
800 } else { 816 } else {
801 if (length $reply) { 817 if (length $reply) {
802 $_->ob->message ($npc->name . " says: $reply", cf::NDI_BROWN | cf::NDI_UNIQUE) 818 $_->ns->send_msg ($SAY_CHANNEL => $npc->name . " says: $reply", cf::NDI_BROWN)
803 for grep $pl->on_same_map_as ($_->ob), cf::player::list; 819 for @plonmap;
804 } 820 }
805 } 821 }
806 } 822 }
807 823
808 if (@kw) { 824 if (@kw) {
809 $_->ob->message ("[further topics: " . (join ", ", @kw) . "]", cf::NDI_BROWN | cf::NDI_UNIQUE) 825 $_->ns->send_msg ($SAY_CHANNEL => "[further topics: " . (join ", ", @kw) . "]", cf::NDI_BROWN)
810 for grep $pl->on_same_map_as ($_->ob), cf::player::list; 826 for @plonmap;
811 } 827 }
812 } 828 }
813 829
814 } else { 830 } else {
815 $pl->message ("What do you want to say?", cf::NDI_UNIQUE); 831 $ob->contr->ns->send_msg ($SAY_CHANNEL => "What do you want to say?", 0, reply => 1);
816 } 832 }
817}; 833};
818 834
819cf::register_command chat => sub { 835cf::register_command chat => sub {
820 my ($pl, $msg) = @_; 836 my ($ob, $msg) = @_;
821 837
822 utf8::decode $msg; 838 utf8::decode $msg;
823 839
840 my $pl = $ob->contr;
841
824 return if $pl->contr->invoke (cf::EVENT_PLAYER_CHAT, $msg); 842 return if $pl->invoke (cf::EVENT_PLAYER_CHAT, $msg);
825 843
826 if ($msg) { 844 if ($msg) {
827 my $name = $pl->name; 845 my $name = $ob->name;
828 my $NOW = time; 846 my $NOW = time;
829 847
830 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg; 848 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg;
831 ext::schmorp_irc::do_notice (sprintf "[%s] %s", $name, $msg); 849 send_irc ("[%s] %s", $name, $msg);
832 850
833 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) 851 $_->ns->send_msg ($CHAT_CHANNEL => "$name chats: $msg", cf::NDI_BLUE, $_ == $pl ? (reply => 1) : ())
834 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; 852 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
835 853
836 } else { 854 } else {
837 $pl->message ("Chat what?", cf::NDI_UNIQUE); 855 $pl->ns->send_msg ($CHAT_CHANNEL => "Chat what?");
838 } 856 }
839}; 857};
840 858
841cf::register_command shout => sub { 859cf::register_command shout => sub {
842 my ($pl, $msg) = @_; 860 my ($ob, $msg) = @_;
843 861
844 utf8::decode $msg; 862 utf8::decode $msg;
845 863
864 my $pl = $ob->contr;
865
846 return if $pl->contr->invoke (cf::EVENT_PLAYER_SHOUT, $msg); 866 return if $pl->invoke (cf::EVENT_PLAYER_SHOUT, $msg);
847 867
848 if ($msg) { 868 if ($msg) {
849 my $NOW = time; 869 my $NOW = time;
850 my $name = $pl->name; 870 my $name = $ob->name;
851 871
852 cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg; 872 cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg;
853 ext::schmorp_irc::do_notice (sprintf "\007\0034{%s} %s\n", $name, $msg); 873 send_irc ("\007\0034{%s} %s\n", $name, $msg);
854 874
855 utf8::encode $msg; # ->message not yet utf8-ified 875 $_->ns->send_msg ($CHAT_CHANNEL => "$name shouts: $msg", cf::NDI_RED, $_ == $pl ? (reply => 1) : ())
856 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
857 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list; 876 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
858 877
859 } else { 878 } else {
860 $pl->message ("Shout what?", cf::NDI_UNIQUE); 879 $pl->ns->send_msg ($CHAT_CHANNEL => "Shout what?");
861 } 880 }
862}; 881};
863 882
864cf::register_command tell => sub { 883cf::register_command tell => sub {
865 my ($pl, $args) = @_; 884 my ($ob, $args) = @_;
866 my ($target, $msg) = split /\s+/, $args, 2; 885 my ($target, $msg) = split /\s+/, $args, 2;
867 886
868 utf8::decode $msg; 887 utf8::decode $msg;
869 888
889 my $pl = $ob->contr;
890 my $ns = $pl->ns
891 or return;
892 my $name = $ob->name;
893
870 return if $pl->contr->invoke (cf::EVENT_PLAYER_TELL, $target, $msg); 894 return if $pl->invoke (cf::EVENT_PLAYER_TELL, $target, $msg);
871 895
872 my $name = $pl->name; 896 my $pl_channel = {
897 id => "tell-$target",
898 title => "$target",
899 reply => "tell $target ",
900 tooltip => "Private messages from/to $target",
901 };
873 902
874 if ($target =~ /irc\//) { 903 if ($target =~ /irc\//) {
875 my (undef, $nick) = split /\//, $target, 2; 904 my (undef, $nick) = split /\//, $target, 2;
876 $pl->message ("You tell $target: $args"); 905 $ns->send_msg ($pl_channel => "You tell $target: $args", reply => 1);
877 ext::schmorp_irc::do_notice (sprintf "(%s) %s: %s\n", $name, $nick, $msg); 906 send_irc ("(%s) %s: %s\n", $name, $nick, $msg);
878 } elsif (my $other = cf::player::find_active $target) { 907 } elsif (my $other = cf::player::find_active $target) {
908 my $other_channel = {
909 id => "tell-$name",
910 title => "$name",
911 reply => "tell $name ",
912 tooltip => "Private messages from/to $name",
913 };
879 914
880 if ($msg) { 915 if ($msg) {
881 if ($target eq $name) { 916 if ($target eq $name) {
882 $pl->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE); 917 $ns->send_msg ($pl_channel => "You are talking to yourself, you freak!", reply => 1);
883 } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) { 918 } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) {
884 $pl->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE); 919 $ns->send_msg ($pl_channel => "$target ignores what you say. Give up on it.", reply => 1);
885 } else { 920 } else {
886 return if $other->invoke (cf::EVENT_PLAYER_TOLD, $pl->contr, $msg); 921 return if $other->invoke (cf::EVENT_PLAYER_TOLD, $pl, $msg);
887 utf8::encode $msg; # ->message not yet utf8-ified
888 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg; 922 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg;
889 923
890 $pl->message ("You tell $target: $msg"); 924 $ns->send_msg ($pl_channel => "You tell $target: $msg", reply => 1);
891 $other->ob->message ("$name tells you: $msg"); 925 $other->ns->send_msg ($other_channel => "$name tells you: $msg");
892 $other->ob->{ext_last_tell} = $name;
893 } 926 }
894 } else { 927 } else {
895 $pl->message ("What do you want to tell $target?", cf::NDI_UNIQUE); 928 $ns->send_msg ($pl_channel => "What do you want to tell $target?");
896 } 929 }
897 930
898 } else { 931 } else {
899 $pl->message ("No such player. Your message: $msg", cf::NDI_UNIQUE); 932 $ns->send_msg ($pl_channel => "No such player. Your message: $msg");
900 }
901};
902
903cf::register_command reply => sub {
904 my ($pl, $args) = @_;
905 my $name = $pl->name;
906
907 utf8::decode $args;
908
909 return if $pl->contr->invoke (cf::EVENT_PLAYER_TELL, $pl->{ext_last_tell}, $args);
910
911 if ($pl->{ext_last_tell} =~ /irc\//) {
912 my (undef, $nick) = split /\//, $pl->{ext_last_tell}, 2;
913 $pl->message ("You tell " . $pl->{ext_last_tell} . ": $args");
914 ext::schmorp_irc::do_notice (sprintf "(%s) %s: %s\n", $name, $nick, $args);
915 } elsif (my $other = cf::player::find_active $pl->{ext_last_tell}) {
916
917 if ($args) {
918 $other->ob->{ext_ignore_tell}{$name} >= time
919 or delete $other->ob->{ext_ignore_tell}{$name};
920
921 if ($other->ob->{ext_ignore_tell}{$name} < time) {
922 utf8::encode $args; # ->message not yet utf8-ified
923 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args;
924
925 $pl->message ("You tell " . $other->ob->name . ": $args");
926 $other->ob->message ("$name tells you: $args");
927 $pl->{ext_last_tell} = $other->ob->name;
928 } else {
929 $pl->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
930 }
931 } else {
932 $pl->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE);
933 }
934
935 } else {
936 $pl->message ("Can't reply, player left. Your message: $args".$pl->{ext_last_tell}, cf::NDI_UNIQUE);
937 } 933 }
938}; 934};
939 935
940cf::register_command ignore => sub { 936cf::register_command ignore => sub {
941 my ($pl, $args) = @_; 937 my ($pl, $args) = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines