1 |
#! perl |
2 |
|
3 |
use Time::HiRes; |
4 |
use Net::IRC3::Client::Connection; |
5 |
|
6 |
my $BOTSERVER = 'localhost'; |
7 |
my $BOTPORT = '6667'; |
8 |
my $BOTNAME = 'cfbot'; |
9 |
my $BOTCHAN = '#cf'; |
10 |
|
11 |
my $CON; # the connection |
12 |
|
13 |
sub unload { |
14 |
$CON->disconnect if $CON; |
15 |
undef $CON; |
16 |
} |
17 |
|
18 |
sub do_notice { |
19 |
my ($msg) = @_; |
20 |
$CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) |
21 |
if $CON; |
22 |
} |
23 |
|
24 |
sub handle_fcmd { |
25 |
my ($msg) = @_; |
26 |
|
27 |
if ($msg eq "!who") { |
28 |
my ($numplayers, $numwiz, @plist) = (0, 0); |
29 |
|
30 |
foreach my $pl (cf::player::list) { |
31 |
$numplayers++; |
32 |
$numwiz++ |
33 |
if ($pl->ob->flag (cf::FLAG_WIZ)); |
34 |
push (@plist, $pl); |
35 |
} |
36 |
|
37 |
$CON->send_chan ($BOTCHAN, NOTICE => "Total Players in The World. ($numplayers) -- WIZ($numwiz)", $BOTCHAN); |
38 |
|
39 |
if ($numplayers > 0) { |
40 |
foreach my $pl (@plist) { |
41 |
$CON->send_chan ($BOTCHAN, NOTICE => |
42 |
"* " . $pl->ob->name . "/" . $pl->ob->level . ($pl->ob->flag (cf::FLAG_WIZ) ? " [WIZ] " : "") |
43 |
." [" . ($pl->ob->map ? $pl->ob->map->path : "NULL") . "]", $BOTCHAN); |
44 |
} |
45 |
} |
46 |
} |
47 |
} |
48 |
|
49 |
sub check_connection { |
50 |
return if $CON; |
51 |
|
52 |
$CON = Net::IRC3::Client::Connection->new; |
53 |
$CON->connect ($BOTSERVER, $BOTPORT); |
54 |
$CON->send_srv (JOIN => undef, $BOTCHAN); |
55 |
$CON->register ($BOTNAME, $BOTNAME, 'crossfire connection'); |
56 |
$CON->reg_cb ( |
57 |
#d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 }, |
58 |
irc_privmsg => sub { |
59 |
my ($con, $msg) = @_; |
60 |
my $name = 'irc'; |
61 |
my $NOW = Time::HiRes::time; |
62 |
my $tmsg = $msg->{trailing}; |
63 |
$tmsg =~ s/\x01[^\x01]*\x01//g; |
64 |
if ($tmsg =~ /^\!/) { |
65 |
handle_fcmd ($tmsg); |
66 |
} elsif ($tmsg =~ m/\S/) { |
67 |
$_->ob->message ( |
68 |
"$name/".Net::IRC3::Util::prefix_nick ($msg)." chats: $tmsg", cf::NDI_BLUE |
69 |
) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
70 |
} |
71 |
1; |
72 |
}, |
73 |
# registered => sub { |
74 |
# 1; |
75 |
# }, |
76 |
disconnect => sub { |
77 |
undef $CON; |
78 |
0; |
79 |
} |
80 |
); |
81 |
} |
82 |
|
83 |
my $timer; |
84 |
|
85 |
sub new_timer { |
86 |
$timer = AnyEvent->timer (after => 10, cb => sub { |
87 |
check_connection (); |
88 |
&new_timer; # and restart the time |
89 |
}); |
90 |
} |
91 |
|
92 |
new_timer; # create first timer |
93 |
check_connection (); |