1 |
root |
1.1 |
#! perl |
2 |
|
|
|
3 |
|
|
use Time::HiRes; |
4 |
elmex |
1.14 |
use AnyEvent::IRC::Client; |
5 |
elmex |
1.15 |
use AnyEvent::IRC::Util qw/filter_colors/; |
6 |
root |
1.1 |
|
7 |
|
|
# requires: commands.ext |
8 |
|
|
|
9 |
|
|
return unless exists $cf::CFG{irc_server}; |
10 |
|
|
|
11 |
root |
1.12 |
my $BOTSERVER = $cf::CFG{irc_server} || "localhost"; |
12 |
|
|
my $BOTPORT = $cf::CFG{irc_port} || 6667; |
13 |
|
|
my $BOTNAME = $cf::CFG{irc_nick} || "server"; |
14 |
|
|
my $BOTCHAN = $cf::CFG{irc_chan} || "cf"; |
15 |
root |
1.1 |
|
16 |
|
|
my $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 |
elmex |
1.14 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, $msg) |
28 |
root |
1.1 |
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 |
root |
1.6 |
# clobbers irc, http is available |
42 |
elmex |
1.8 |
do_notice "see http://www.deliantra.net/userlist.crossfire.schmorp.de.html"; |
43 |
root |
1.6 |
# do_notice $_ |
44 |
|
|
# for ext::commands::who_listing (0, "."); |
45 |
root |
1.1 |
|
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 |
elmex |
1.14 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: You are talking to yourself, you freak!"); |
54 |
root |
1.1 |
} elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { |
55 |
elmex |
1.14 |
$CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: $target ignores what you say. Give up on it."); |
56 |
root |
1.1 |
} else { |
57 |
|
|
cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; |
58 |
|
|
|
59 |
elmex |
1.7 |
$other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF); |
60 |
root |
1.1 |
} |
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 |
elmex |
1.14 |
$CON = AnyEvent::IRC::Client->new; |
73 |
elmex |
1.17 |
$CON->set_exception_cb (sub { |
74 |
|
|
my ($exp, $ev) = @_; |
75 |
|
|
warn "IRC: IRC EXCEPTION (event $ev): $exp\n"; |
76 |
|
|
}); |
77 |
elmex |
1.14 |
$CON->connect ($BOTSERVER, $BOTPORT, { |
78 |
|
|
nick => $BOTNAME, |
79 |
|
|
user => $BOTNAME, |
80 |
|
|
real => 'deliantra server' |
81 |
|
|
}); |
82 |
root |
1.1 |
$CON->send_srv (JOIN => undef, $BOTCHAN); |
83 |
|
|
$CON->reg_cb ( |
84 |
|
|
irc_privmsg => sub { |
85 |
|
|
my ($con, $msg) = @_; |
86 |
|
|
my $name = 'irc'; |
87 |
elmex |
1.14 |
my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); |
88 |
root |
1.1 |
my $NOW = Time::HiRes::time; |
89 |
root |
1.2 |
|
90 |
elmex |
1.15 |
my $tmsg = filter_colors ($msg->{params}->[-1]); |
91 |
root |
1.1 |
$tmsg =~ s/\x01[^\x01]*\x01//g; |
92 |
|
|
$tmsg =~ s/\015?\012/ /g; |
93 |
root |
1.2 |
|
94 |
|
|
utf8::decode $tmsg; |
95 |
|
|
|
96 |
root |
1.1 |
if ($tmsg =~ /^\!/) { |
97 |
|
|
handle_fcmd ($name, $nick, $tmsg); |
98 |
|
|
} elsif ($tmsg =~ m/\S/) { |
99 |
root |
1.11 |
$_->ns->send_msg ($cf::CHAT_CHANNEL, |
100 |
root |
1.3 |
"$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF |
101 |
root |
1.13 |
) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; |
102 |
root |
1.10 |
cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; |
103 |
root |
1.1 |
} |
104 |
|
|
}, |
105 |
elmex |
1.14 |
connect => sub { |
106 |
elmex |
1.16 |
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 |
elmex |
1.14 |
}, |
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 |
root |
1.1 |
disconnect => sub { |
124 |
|
|
my ($con, $reason) = @_; |
125 |
elmex |
1.14 |
warn "IRC: disconnect: $reason\n"; |
126 |
root |
1.1 |
undef $CON; |
127 |
|
|
} |
128 |
|
|
); |
129 |
|
|
} |
130 |
|
|
|
131 |
root |
1.9 |
our $RECONNECT = cf::periodic 30, Coro::unblock_sub { |
132 |
|
|
check_connection; |
133 |
|
|
}; |
134 |
root |
1.1 |
|