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 |
our $BOTSERVER = $cf::CFG{irc_server}; |
12 |
our $BOTPORT = $cf::CFG{irc_port}; |
13 |
our $BOTNAME = $cf::CFG{irc_nick}; |
14 |
our $BOTCHAN = $cf::CFG{irc_chan}; |
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 |
sub handle_fcmd { |
38 |
my ($name, $me, $msg) = @_; |
39 |
|
40 |
if ($msg eq "!who") { |
41 |
# clobbers irc, http is available |
42 |
do_notice "see http://www.deliantra.net/userlist.crossfire.schmorp.de.html"; |
43 |
# do_notice $_ |
44 |
# for ext::commands::who_listing (0, "."); |
45 |
|
46 |
} elsif ($msg =~ /^\!tell/) { |
47 |
my (undef, $target, $tmsg) = split / /, $msg, 3; |
48 |
|
49 |
if (my $other = cf::player::find_active $target) { |
50 |
|
51 |
if ($tmsg) { |
52 |
if ($me eq $target) { |
53 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: You are talking to yourself, you freak!"); |
54 |
} elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { |
55 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: $target ignores what you say. Give up on it."); |
56 |
} else { |
57 |
cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; |
58 |
|
59 |
$other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF); |
60 |
} |
61 |
} else { |
62 |
do_notice "$me: What do you want to tell $target?"; |
63 |
} |
64 |
|
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
sub check_connection { |
70 |
return if $CON; |
71 |
|
72 |
$CON = AnyEvent::IRC::Client->new; |
73 |
$CON->set_exception_cb (sub { |
74 |
my ($exp, $ev) = @_; |
75 |
warn "IRC: IRC EXCEPTION (event $ev): $exp\n"; |
76 |
}); |
77 |
$CON->connect ($BOTSERVER, $BOTPORT, { |
78 |
nick => $BOTNAME, |
79 |
user => $BOTNAME, |
80 |
real => 'deliantra server' |
81 |
}); |
82 |
$CON->send_srv (JOIN => undef, $BOTCHAN); |
83 |
$CON->reg_cb ( |
84 |
irc_privmsg => sub { |
85 |
my ($con, $msg) = @_; |
86 |
my $name = 'irc'; |
87 |
my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); |
88 |
my $NOW = Time::HiRes::time; |
89 |
|
90 |
my $tmsg = filter_colors ($msg->{params}->[-1]); |
91 |
$tmsg =~ s/\x01[^\x01]*\x01//g; |
92 |
$tmsg =~ s/\015?\012/ /g; |
93 |
|
94 |
utf8::decode $tmsg; |
95 |
|
96 |
if ($tmsg =~ /^\!/) { |
97 |
handle_fcmd ($name, $nick, $tmsg); |
98 |
} elsif ($tmsg =~ m/\S/) { |
99 |
$_->ns->send_msg ($cf::CHAT_CHANNEL, |
100 |
"$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF |
101 |
) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; |
102 |
cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; |
103 |
} |
104 |
}, |
105 |
connect => sub { |
106 |
my ($con, $error) = @_; |
107 |
|
108 |
if ($error) { |
109 |
warn "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n"; |
110 |
undef $CON; |
111 |
|
112 |
} else { |
113 |
warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
114 |
} |
115 |
}, |
116 |
registered => sub { |
117 |
warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
118 |
}, |
119 |
error => sub { |
120 |
my ($con, $code, $message) = @_; |
121 |
warn "IRC: IRC ERROR ($code) $message\n"; |
122 |
}, |
123 |
disconnect => sub { |
124 |
my ($con, $reason) = @_; |
125 |
warn "IRC: disconnect: $reason\n"; |
126 |
undef $CON; |
127 |
} |
128 |
); |
129 |
} |
130 |
|
131 |
our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub { |
132 |
check_connection; |
133 |
}; |
134 |
|