#! perl use Time::HiRes; use Net::IRC3::Client::Connection; my $BOTSERVER = 'localhost'; my $BOTPORT = '6667'; my $BOTNAME = 'cfbot'; my $BOTCHAN = '#cf'; my $CON; # the connection sub unload { $CON->disconnect if $CON; undef $CON; } sub do_notice { my ($msg) = @_; $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) if $CON; } sub handle_fcmd { my ($name, $me, $msg) = @_; if ($msg eq "!who") { my ($numplayers, $numwiz, @plist) = (0, 0); foreach my $pl (cf::player::list) { $numplayers++; $numwiz++ if ($pl->ob->flag (cf::FLAG_WIZ)); push (@plist, $pl); } $CON->send_chan ($BOTCHAN, NOTICE => "Total Players in The World. ($numplayers) -- WIZ($numwiz)", $BOTCHAN); if ($numplayers > 0) { foreach my $pl (@plist) { $CON->send_chan ($BOTCHAN, NOTICE => "* " . $pl->ob->name . "/" . $pl->ob->level . " " . ( length $pl->own_title ? $pl->own_title : "the " . $pl->title ) . " " .($pl->ob->flag (cf::FLAG_WIZ) ? " [WIZ] " : "") ." [" . ($pl->ob->map ? $pl->ob->map->path : "NULL") . "]", $BOTCHAN); } } } elsif ($msg =~ /^\!tell/) { my (undef, $target, $tmsg) = split / /, $msg, 3; if (my $other = cf::player::find $target) { if ($tmsg) { if ($me eq $target) { $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { $CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); } else { utf8::encode $tmsg; # ->message not yet utf8-ified cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; $other->ob->message ("$name/$me tells you: $tmsg"); $other->ob->{ext_last_tell} = "$name/$me"; } } else { $CON->send_chan ($BOTCHAN, NOTICE => "$me: What do you want to tell $target?", cf::NDI_UNIQUE); } } } } sub check_connection { return if $CON; $CON = Net::IRC3::Client::Connection->new; $CON->connect ($BOTSERVER, $BOTPORT); $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 $NOW = Time::HiRes::time; my $tmsg = $msg->{trailing}; $tmsg =~ s/\x01[^\x01]*\x01//g; $tmsg =~ s/\015?\012/ /g; if ($tmsg =~ /^\!/) { handle_fcmd ($name, $nick, $tmsg); } elsif ($tmsg =~ m/\S/) { $_->ob->message ( "$name/".$nick." chats: $tmsg", cf::NDI_BLUE ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; } 1; }, # registered => sub { # 1; # }, disconnect => sub { undef $CON; 0; } ); } my $timer; sub new_timer { $timer = AnyEvent->timer (after => 10, cb => sub { check_connection (); &new_timer; # and restart the time }); } new_timer; # create first timer check_connection ();