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 ($name, $me, $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 |
} elsif ($msg =~ /^\!tell/) { |
47 |
my (undef, $target, $tmsg) = split / /, $msg, 3; |
48 |
|
49 |
if (my $other = cf::player::find $target) { |
50 |
|
51 |
if ($tmsg) { |
52 |
if ($me eq $target) { |
53 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); |
54 |
} elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { |
55 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); |
56 |
} else { |
57 |
utf8::encode $tmsg; # ->message not yet utf8-ified |
58 |
cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; |
59 |
|
60 |
$other->ob->message ("$name/$me tells you: $tmsg"); |
61 |
$other->ob->{ext_last_tell} = "$name/$me"; |
62 |
} |
63 |
} else { |
64 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: What do you want to tell $target?", cf::NDI_UNIQUE); |
65 |
} |
66 |
|
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
sub check_connection { |
72 |
return if $CON; |
73 |
|
74 |
$CON = Net::IRC3::Client::Connection->new; |
75 |
$CON->connect ($BOTSERVER, $BOTPORT); |
76 |
$CON->send_srv (JOIN => undef, $BOTCHAN); |
77 |
$CON->register ($BOTNAME, $BOTNAME, 'crossfire connection'); |
78 |
$CON->reg_cb ( |
79 |
#d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 }, |
80 |
irc_privmsg => sub { |
81 |
my ($con, $msg) = @_; |
82 |
my $name = 'irc'; |
83 |
my $nick = Net::IRC3::Util::prefix_nick ($msg); |
84 |
my $NOW = Time::HiRes::time; |
85 |
my $tmsg = $msg->{trailing}; |
86 |
$tmsg =~ s/\x01[^\x01]*\x01//g; |
87 |
if ($tmsg =~ /^\!/) { |
88 |
handle_fcmd ($name, $nick, $tmsg); |
89 |
} elsif ($tmsg =~ m/\S/) { |
90 |
$_->ob->message ( |
91 |
"$name/".$nick." chats: $tmsg", cf::NDI_BLUE |
92 |
) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
93 |
} |
94 |
1; |
95 |
}, |
96 |
# registered => sub { |
97 |
# 1; |
98 |
# }, |
99 |
disconnect => sub { |
100 |
undef $CON; |
101 |
0; |
102 |
} |
103 |
); |
104 |
} |
105 |
|
106 |
my $timer; |
107 |
|
108 |
sub new_timer { |
109 |
$timer = AnyEvent->timer (after => 10, cb => sub { |
110 |
check_connection (); |
111 |
&new_timer; # and restart the time |
112 |
}); |
113 |
} |
114 |
|
115 |
new_timer; # create first timer |
116 |
check_connection (); |