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