#! perl use Time::HiRes; use AnyEvent::IRC::Client; use AnyEvent::IRC::Util qw/filter_colors/; # requires: commands.ext return unless exists $cf::CFG{irc_server}; CONF BOTSERVER : irc_server = undef; CONF BOTPORT : irc_port = undef; CONF BOTNAME : irc_nick = undef; CONF BOTCHAN : irc_chan = undef; our $CON; # the connection sub unload { $CON->disconnect if $CON; undef $CON; } sub do_notice { my ($msg) = @_; utf8::encode $msg; $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, $msg) if $CON; } sub users { $CON ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } : () } # for nicklist monitoring, #d# should be done event-based our %NICKLIST; our $NICKLIST = AE::timer 20, 59.17, sub { return unless defined &ext::nickmon::NT_OTHER; my %NEXTLIST; for (users) { &ext::nickmon::upd ("irc/$_", &ext::nickmon::NT_OTHER, "irc") unless exists $NICKLIST{$_}; delete $NICKLIST{$_}; undef $NEXTLIST{$_}; } &ext::nickmon::del ("irc/$_") for keys %NICKLIST; %NICKLIST = %NEXTLIST; }; sub handle_fcmd { my ($name, $me, $msg) = @_; if ($msg eq "!who") { # clobbers irc, http is available do_notice "see http://www.deliantra.net/userlist.crossfire.schmorp.de.html"; # do_notice $_ # for ext::commands::who_listing (0, "."); } elsif ($msg =~ /^\!tell/) { my (undef, $target, $tmsg) = split / /, $msg, 3; if (my $other = cf::player::find_active $target) { if ($tmsg) { if ($me eq $target) { $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 => $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 (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?"; } } } } sub check_connection { return if $CON; $CON = AnyEvent::IRC::Client->new; $CON->set_exception_cb (sub { my ($exp, $ev) = @_; cf::error "IRC: IRC EXCEPTION (event $ev): $exp\n"; }); $CON->connect ($BOTSERVER, $BOTPORT, { nick => $BOTNAME, user => $BOTNAME, real => 'deliantra server' }); $CON->send_srv (JOIN => undef, $BOTCHAN); $CON->reg_cb ( irc_privmsg => sub { my ($con, $msg) = @_; my $name = 'irc'; my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); my $NOW = Time::HiRes::time; my $tmsg = filter_colors ($msg->{params}->[-1]); $tmsg =~ s/\x01[^\x01]*\x01//g; $tmsg =~ s/\015?\012/ /g; utf8::decode $tmsg; if ($tmsg =~ /^\!/) { handle_fcmd ($name, $nick, $tmsg); } elsif ($tmsg =~ m/\S/) { $_->ns->send_msg ($cf::CHAT_CHANNEL, "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; } }, connect => sub { my ($con, $error) = @_; if ($error) { cf::error "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n"; undef $CON; } else { cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; } }, registered => sub { cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; }, error => sub { my ($con, $code, $message) = @_; cf::error "IRC: IRC ERROR ($code) $message\n"; }, disconnect => sub { my ($con, $reason) = @_; cf::warn "IRC: disconnect: $reason\n"; undef $CON; } ); } our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub { check_connection; };