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