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

Comparing deliantra/server/ext/irc.ext (file contents):
Revision 1.1 by root, Fri May 18 13:56:50 2007 UTC vs.
Revision 1.17 by elmex, Fri Jun 26 16:02:15 2009 UTC

1#! perl 1#! perl
2 2
3use Time::HiRes; 3use Time::HiRes;
4use Net::IRC3::Client::Connection; 4use AnyEvent::IRC::Client;
5use AnyEvent::IRC::Util qw/filter_colors/;
5 6
6# requires: commands.ext 7# requires: commands.ext
7 8
8return unless exists $cf::CFG{irc_server}; 9return unless exists $cf::CFG{irc_server};
9 10
10my $BOTSERVER = $cf::CFG{irc_server}; 11my $BOTSERVER = $cf::CFG{irc_server} || "localhost";
11my $BOTPORT = $cf::CFG{irc_port}; 12my $BOTPORT = $cf::CFG{irc_port} || 6667;
12my $BOTNAME = $cf::CFG{irc_nick}; 13my $BOTNAME = $cf::CFG{irc_nick} || "server";
13my $BOTCHAN = $cf::CFG{irc_chan}; 14my $BOTCHAN = $cf::CFG{irc_chan} || "cf";
14 15
15my $CON; # the connection 16my $CON; # the connection
16 17
17sub unload { 18sub unload {
18 $CON->disconnect if $CON; 19 $CON->disconnect if $CON;
21 22
22sub do_notice { 23sub do_notice {
23 my ($msg) = @_; 24 my ($msg) = @_;
24 25
25 utf8::encode $msg; 26 utf8::encode $msg;
26 $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) 27 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, $msg)
27 if $CON; 28 if $CON;
28} 29}
29 30
30sub users { 31sub users {
31 $CON 32 $CON
34} 35}
35 36
36sub handle_fcmd { 37sub handle_fcmd {
37 my ($name, $me, $msg) = @_; 38 my ($name, $me, $msg) = @_;
38 39
39 utf8::decode $msg;
40
41 if ($msg eq "!who") { 40 if ($msg eq "!who") {
41 # clobbers irc, http is available
42 do_notice "see http://www.deliantra.net/userlist.crossfire.schmorp.de.html";
42 do_notice $_ 43# do_notice $_
43 for ext::commands::who_listing (); 44# for ext::commands::who_listing (0, ".");
44 45
45 } elsif ($msg =~ /^\!tell/) { 46 } elsif ($msg =~ /^\!tell/) {
46 my (undef, $target, $tmsg) = split / /, $msg, 3; 47 my (undef, $target, $tmsg) = split / /, $msg, 3;
47 48
48 if (my $other = cf::player::find_active $target) { 49 if (my $other = cf::player::find_active $target) {
49 50
50 if ($tmsg) { 51 if ($tmsg) {
51 if ($me eq $target) { 52 if ($me eq $target) {
52 $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); 53 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: You are talking to yourself, you freak!");
53 } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { 54 } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) {
54 $CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); 55 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: $target ignores what you say. Give up on it.");
55 } else { 56 } else {
56 cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; 57 cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg;
57 58
58 $other->ob->message ("$name/$me tells you: $tmsg"); 59 $other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF);
59 $other->ob->{ext_last_tell} = "$name/$me";
60 } 60 }
61 } else { 61 } else {
62 do_notice "$me: What do you want to tell $target?"; 62 do_notice "$me: What do you want to tell $target?";
63 } 63 }
64 64
67} 67}
68 68
69sub check_connection { 69sub check_connection {
70 return if $CON; 70 return if $CON;
71 71
72 $CON = Net::IRC3::Client::Connection->new; 72 $CON = AnyEvent::IRC::Client->new;
73 $CON->set_exception_cb (sub {
74 my ($exp, $ev) = @_;
75 warn "IRC: IRC EXCEPTION (event $ev): $exp\n";
76 });
73 $CON->connect ($BOTSERVER, $BOTPORT); 77 $CON->connect ($BOTSERVER, $BOTPORT, {
78 nick => $BOTNAME,
79 user => $BOTNAME,
80 real => 'deliantra server'
81 });
74 $CON->send_srv (JOIN => undef, $BOTCHAN); 82 $CON->send_srv (JOIN => undef, $BOTCHAN);
75 $CON->register ($BOTNAME, $BOTNAME, 'crossfire connection');
76 $CON->reg_cb ( 83 $CON->reg_cb (
77 #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 },
78 irc_privmsg => sub { 84 irc_privmsg => sub {
79 my ($con, $msg) = @_; 85 my ($con, $msg) = @_;
80 my $name = 'irc'; 86 my $name = 'irc';
81 my $nick = Net::IRC3::Util::prefix_nick ($msg); 87 my $nick = AnyEvent::IRC::Util::prefix_nick ($msg);
82 my $NOW = Time::HiRes::time; 88 my $NOW = Time::HiRes::time;
83 my $tmsg = $msg->{trailing}; 89
90 my $tmsg = filter_colors ($msg->{params}->[-1]);
84 $tmsg =~ s/\x01[^\x01]*\x01//g; 91 $tmsg =~ s/\x01[^\x01]*\x01//g;
85 $tmsg =~ s/\015?\012/ /g; 92 $tmsg =~ s/\015?\012/ /g;
86 utf8::encode $tmsg; # ->message not yet utf8-ified 93
94 utf8::decode $tmsg;
95
87 if ($tmsg =~ /^\!/) { 96 if ($tmsg =~ /^\!/) {
88 handle_fcmd ($name, $nick, $tmsg); 97 handle_fcmd ($name, $nick, $tmsg);
89 } elsif ($tmsg =~ m/\S/) { 98 } elsif ($tmsg =~ m/\S/) {
90 $_->ob->message ( 99 $_->ns->send_msg ($cf::CHAT_CHANNEL,
91 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE 100 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF
92 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; 101 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list;
102 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg;
93 } 103 }
94 1;
95 }, 104 },
105 connect => sub {
106 my ($con, $error) = @_;
107
108 if ($error) {
109 warn "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n";
110 undef $CON;
111
112 } else {
113 warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n";
114 }
115 },
96# registered => sub { 116 registered => sub {
97# 1; 117 warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n";
98# }, 118 },
119 error => sub {
120 my ($con, $code, $message) = @_;
121 warn "IRC: IRC ERROR ($code) $message\n";
122 },
99 disconnect => sub { 123 disconnect => sub {
100 my ($con, $reason) = @_; 124 my ($con, $reason) = @_;
101 warn "CFBOT: disconnect: $reason\n"; 125 warn "IRC: disconnect: $reason\n";
102 undef $CON; 126 undef $CON;
103 0;
104 } 127 }
105 ); 128 );
106} 129}
107 130
108Event->timer ( 131our $RECONNECT = cf::periodic 30, Coro::unblock_sub {
109 reentrant => 0, 132 check_connection;
110 after => 1, 133};
111 interval => 30,
112 data => cf::WF_AUTOCANCEL,
113 cb => Coro::unblock_sub { check_connection },
114);
115 134

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines