1 |
#! perl |
2 |
|
3 |
use Time::HiRes; |
4 |
use AnyEvent::IRC::Client; |
5 |
use AnyEvent::IRC::Util qw/filter_colors/; |
6 |
|
7 |
# requires: commands.ext |
8 |
|
9 |
return unless exists $cf::CFG{irc_server}; |
10 |
|
11 |
CONF BOTSERVER : irc_server = undef; |
12 |
CONF BOTPORT : irc_port = undef; |
13 |
CONF BOTNAME : irc_nick = undef; |
14 |
CONF BOTCHAN : irc_chan = undef; |
15 |
|
16 |
our $CON; # the connection |
17 |
|
18 |
sub unload { |
19 |
$CON->disconnect if $CON; |
20 |
undef $CON; |
21 |
} |
22 |
|
23 |
sub do_notice { |
24 |
my ($msg) = @_; |
25 |
|
26 |
utf8::encode $msg; |
27 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, $msg) |
28 |
if $CON; |
29 |
} |
30 |
|
31 |
sub users { |
32 |
$CON |
33 |
? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } |
34 |
: () |
35 |
} |
36 |
|
37 |
# for nicklist monitoring, #d# should be done event-based |
38 |
our %NICKLIST; |
39 |
our $NICKLIST = AE::timer 20, 59.17, sub { |
40 |
return unless defined &ext::nickmon::NT_OTHER; |
41 |
|
42 |
my %NEXTLIST; |
43 |
for (users) { |
44 |
&ext::nickmon::upd ("irc/$_", &ext::nickmon::NT_OTHER, "irc") |
45 |
unless exists $NICKLIST{$_}; |
46 |
|
47 |
delete $NICKLIST{$_}; |
48 |
undef $NEXTLIST{$_}; |
49 |
} |
50 |
|
51 |
&ext::nickmon::del ("irc/$_") |
52 |
for keys %NICKLIST; |
53 |
|
54 |
%NICKLIST = %NEXTLIST; |
55 |
}; |
56 |
|
57 |
sub handle_fcmd { |
58 |
my ($name, $me, $msg) = @_; |
59 |
|
60 |
if ($msg eq "!who") { |
61 |
# clobbers irc, http is available |
62 |
do_notice "see http://www.deliantra.net/userlist.crossfire.schmorp.de.html"; |
63 |
# do_notice $_ |
64 |
# for ext::commands::who_listing (0, "."); |
65 |
|
66 |
} elsif ($msg =~ /^\!tell/) { |
67 |
my (undef, $target, $tmsg) = split / /, $msg, 3; |
68 |
|
69 |
if (my $other = cf::player::find_active $target) { |
70 |
|
71 |
if ($tmsg) { |
72 |
if ($me eq $target) { |
73 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: You are talking to yourself, you freak!"); |
74 |
} elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { |
75 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: $target ignores what you say. Give up on it."); |
76 |
} else { |
77 |
cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; |
78 |
|
79 |
$other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF); |
80 |
} |
81 |
} else { |
82 |
do_notice "$me: What do you want to tell $target?"; |
83 |
} |
84 |
|
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
sub check_connection { |
90 |
return if $CON; |
91 |
|
92 |
$CON = AnyEvent::IRC::Client->new; |
93 |
$CON->set_exception_cb (sub { |
94 |
my ($exp, $ev) = @_; |
95 |
cf::error "IRC: IRC EXCEPTION (event $ev): $exp\n"; |
96 |
}); |
97 |
$CON->connect ($BOTSERVER, $BOTPORT, { |
98 |
nick => $BOTNAME, |
99 |
user => $BOTNAME, |
100 |
real => 'deliantra server' |
101 |
}); |
102 |
$CON->send_srv (JOIN => undef, $BOTCHAN); |
103 |
$CON->reg_cb ( |
104 |
irc_privmsg => sub { |
105 |
my ($con, $msg) = @_; |
106 |
my $name = 'irc'; |
107 |
my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); |
108 |
my $NOW = Time::HiRes::time; |
109 |
|
110 |
my $tmsg = filter_colors ($msg->{params}->[-1]); |
111 |
$tmsg =~ s/\x01[^\x01]*\x01//g; |
112 |
$tmsg =~ s/\015?\012/ /g; |
113 |
|
114 |
utf8::decode $tmsg; |
115 |
|
116 |
if ($tmsg =~ /^\!/) { |
117 |
handle_fcmd ($name, $nick, $tmsg); |
118 |
} elsif ($tmsg =~ m/\S/) { |
119 |
$_->ns->send_msg ($cf::CHAT_CHANNEL, |
120 |
"$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF |
121 |
) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; |
122 |
cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; |
123 |
} |
124 |
}, |
125 |
connect => sub { |
126 |
my ($con, $error) = @_; |
127 |
|
128 |
if ($error) { |
129 |
cf::error "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n"; |
130 |
undef $CON; |
131 |
|
132 |
} else { |
133 |
cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
134 |
} |
135 |
}, |
136 |
registered => sub { |
137 |
cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
138 |
}, |
139 |
error => sub { |
140 |
my ($con, $code, $message) = @_; |
141 |
cf::error "IRC: IRC ERROR ($code) $message\n"; |
142 |
}, |
143 |
disconnect => sub { |
144 |
my ($con, $reason) = @_; |
145 |
cf::warn "IRC: disconnect: $reason\n"; |
146 |
undef $CON; |
147 |
} |
148 |
); |
149 |
} |
150 |
|
151 |
our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub { |
152 |
check_connection; |
153 |
}; |
154 |
|