1 |
#! perl |
2 |
|
3 |
use Time::HiRes; |
4 |
use Net::IRC3::Client::Connection; |
5 |
|
6 |
# requires: commands.ext |
7 |
|
8 |
my $BOTSERVER = 'localhost'; |
9 |
my $BOTPORT = '6667'; |
10 |
my $BOTNAME = 'cfbot'; |
11 |
my $BOTCHAN = '#cf'; |
12 |
|
13 |
my $CON; # the connection |
14 |
|
15 |
sub unload { |
16 |
$CON->disconnect if $CON; |
17 |
undef $CON; |
18 |
} |
19 |
|
20 |
sub do_notice { |
21 |
my ($msg) = @_; |
22 |
$CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) |
23 |
if $CON; |
24 |
} |
25 |
|
26 |
sub handle_fcmd { |
27 |
my ($name, $me, $msg) = @_; |
28 |
|
29 |
if ($msg eq "!who") { |
30 |
$CON->send_chan ($BOTCHAN, NOTICE => $_, $BOTCHAN) |
31 |
for ext::commands::who_listing (); |
32 |
|
33 |
} elsif ($msg =~ /^\!tell/) { |
34 |
my (undef, $target, $tmsg) = split / /, $msg, 3; |
35 |
|
36 |
if (my $other = cf::player::find $target) { |
37 |
|
38 |
if ($tmsg) { |
39 |
if ($me eq $target) { |
40 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); |
41 |
} elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { |
42 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); |
43 |
} else { |
44 |
utf8::encode $tmsg; # ->message not yet utf8-ified |
45 |
cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; |
46 |
|
47 |
$other->ob->message ("$name/$me tells you: $tmsg"); |
48 |
$other->ob->{ext_last_tell} = "$name/$me"; |
49 |
} |
50 |
} else { |
51 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: What do you want to tell $target?", cf::NDI_UNIQUE); |
52 |
} |
53 |
|
54 |
} |
55 |
} |
56 |
} |
57 |
|
58 |
sub check_connection { |
59 |
return if $CON; |
60 |
|
61 |
$CON = Net::IRC3::Client::Connection->new; |
62 |
$CON->connect ($BOTSERVER, $BOTPORT); |
63 |
$CON->send_srv (JOIN => undef, $BOTCHAN); |
64 |
$CON->register ($BOTNAME, $BOTNAME, 'crossfire connection'); |
65 |
$CON->reg_cb ( |
66 |
#d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 }, |
67 |
irc_privmsg => sub { |
68 |
my ($con, $msg) = @_; |
69 |
my $name = 'irc'; |
70 |
my $nick = Net::IRC3::Util::prefix_nick ($msg); |
71 |
my $NOW = Time::HiRes::time; |
72 |
my $tmsg = $msg->{trailing}; |
73 |
$tmsg =~ s/\x01[^\x01]*\x01//g; |
74 |
$tmsg =~ s/\015?\012/ /g; |
75 |
if ($tmsg =~ /^\!/) { |
76 |
handle_fcmd ($name, $nick, $tmsg); |
77 |
} elsif ($tmsg =~ m/\S/) { |
78 |
$_->ob->message ( |
79 |
"$name/".$nick." chats: $tmsg", cf::NDI_BLUE |
80 |
) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
81 |
} |
82 |
1; |
83 |
}, |
84 |
# registered => sub { |
85 |
# 1; |
86 |
# }, |
87 |
disconnect => sub { |
88 |
undef $CON; |
89 |
0; |
90 |
} |
91 |
); |
92 |
} |
93 |
|
94 |
my $timer; |
95 |
|
96 |
sub new_timer { |
97 |
$timer = AnyEvent->timer (after => 10, cb => sub { |
98 |
check_connection (); |
99 |
&new_timer; # and restart the time |
100 |
}); |
101 |
} |
102 |
|
103 |
new_timer; # create first timer |
104 |
check_connection (); |