--- deliantra/server/ext/irc.ext 2007/09/24 22:55:59 1.6 +++ deliantra/server/ext/irc.ext 2008/09/23 14:41:26 1.14 @@ -1,16 +1,17 @@ #! perl use Time::HiRes; -use Net::IRC3::Client::Connection; +use AnyEvent::IRC::Client; +use AnyEvent::IRC::Util; # requires: commands.ext return unless exists $cf::CFG{irc_server}; -my $BOTSERVER = $cf::CFG{irc_server}; -my $BOTPORT = $cf::CFG{irc_port}; -my $BOTNAME = $cf::CFG{irc_nick}; -my $BOTCHAN = $cf::CFG{irc_chan}; +my $BOTSERVER = $cf::CFG{irc_server} || "localhost"; +my $BOTPORT = $cf::CFG{irc_port} || 6667; +my $BOTNAME = $cf::CFG{irc_nick} || "server"; +my $BOTCHAN = $cf::CFG{irc_chan} || "cf"; my $CON; # the connection @@ -23,7 +24,7 @@ my ($msg) = @_; utf8::encode $msg; - $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) + $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, $msg) if $CON; } @@ -38,7 +39,7 @@ if ($msg eq "!who") { # clobbers irc, http is available - do_notice "see http://cf.schmorp.de/userlist.crossfire.schmorp.de.html"; + do_notice "see http://www.deliantra.net/userlist.crossfire.schmorp.de.html"; # do_notice $_ # for ext::commands::who_listing (0, "."); @@ -49,13 +50,13 @@ if ($tmsg) { if ($me eq $target) { - $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); + $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: You are talking to yourself, you freak!"); } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { - $CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); + $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: $target ignores what you say. Give up on it."); } else { cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; - $other->ns->send_msg (cf::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF); + $other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF); } } else { do_notice "$me: What do you want to tell $target?"; @@ -68,19 +69,21 @@ sub check_connection { return if $CON; - $CON = Net::IRC3::Client::Connection->new; - $CON->connect ($BOTSERVER, $BOTPORT); + $CON = AnyEvent::IRC::Client->new; + $CON->connect ($BOTSERVER, $BOTPORT, { + nick => $BOTNAME, + user => $BOTNAME, + real => 'deliantra server' + }); $CON->send_srv (JOIN => undef, $BOTCHAN); - $CON->register ($BOTNAME, $BOTNAME, 'crossfire connection'); $CON->reg_cb ( - #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 }, irc_privmsg => sub { my ($con, $msg) = @_; my $name = 'irc'; - my $nick = Net::IRC3::Util::prefix_nick ($msg); + my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); my $NOW = Time::HiRes::time; - my $tmsg = $msg->{trailing}; + my $tmsg = $msg->{params}->[-1]; $tmsg =~ s/\x01[^\x01]*\x01//g; $tmsg =~ s/\015?\012/ /g; @@ -89,29 +92,31 @@ if ($tmsg =~ /^\!/) { handle_fcmd ($name, $nick, $tmsg); } elsif ($tmsg =~ m/\S/) { - $_->ns->send_msg ($ext::chat::CHAT_CHANNEL, + $_->ns->send_msg ($cf::CHAT_CHANNEL, "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF - ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; + ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; + cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; } - 1; }, -# registered => sub { -# 1; -# }, + connect => sub { + warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; + }, + registered => sub { + warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; + }, + error => sub { + my ($con, $code, $message) = @_; + warn "IRC: IRC ERROR ($code) $message\n"; + }, disconnect => sub { my ($con, $reason) = @_; - warn "CFBOT: disconnect: $reason\n"; + warn "IRC: disconnect: $reason\n"; undef $CON; - 0; } ); } -Event->timer ( - reentrant => 0, - after => 1, - interval => 30, - data => cf::WF_AUTOCANCEL, - cb => Coro::unblock_sub { check_connection }, -); +our $RECONNECT = cf::periodic 30, Coro::unblock_sub { + check_connection; +};