1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | use Time::HiRes; |
|
|
4 | use AnyEvent::IRC::Client; |
3 | use AnyEvent::IRC::Client; |
5 | use AnyEvent::IRC::Util qw/filter_colors/; |
4 | use AnyEvent::IRC::Util qw/filter_colors/; |
6 | |
5 | |
7 | # requires: commands.ext |
6 | # requires: commands.ext |
8 | |
7 | |
9 | return unless exists $cf::CFG{irc_server}; |
8 | return unless exists $cf::CFG{irc_server}; |
10 | |
9 | |
11 | my $BOTSERVER = $cf::CFG{irc_server} || "localhost"; |
10 | CONF BOTSERVER : irc_server = undef; |
12 | my $BOTPORT = $cf::CFG{irc_port} || 6667; |
11 | CONF BOTPORT : irc_port = undef; |
13 | my $BOTNAME = $cf::CFG{irc_nick} || "server"; |
12 | CONF BOTNAME : irc_nick = undef; |
14 | my $BOTCHAN = $cf::CFG{irc_chan} || "cf"; |
13 | CONF BOTCHAN : irc_chan = undef; |
15 | |
14 | |
16 | my $CON; # the connection |
15 | our $CON; # the connection |
17 | |
16 | |
18 | sub unload { |
17 | sub unload { |
19 | $CON->disconnect if $CON; |
18 | $CON->disconnect if $CON; |
20 | undef $CON; |
19 | undef $CON; |
21 | } |
20 | } |
… | |
… | |
29 | } |
28 | } |
30 | |
29 | |
31 | sub users { |
30 | sub users { |
32 | $CON |
31 | $CON |
33 | ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } |
32 | ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } |
34 | : () |
33 | : () |
35 | } |
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 | }; |
36 | |
55 | |
37 | sub handle_fcmd { |
56 | sub handle_fcmd { |
38 | my ($name, $me, $msg) = @_; |
57 | my ($name, $me, $msg) = @_; |
39 | |
58 | |
40 | if ($msg eq "!who") { |
59 | if ($msg eq "!who") { |
… | |
… | |
68 | |
87 | |
69 | sub check_connection { |
88 | sub check_connection { |
70 | return if $CON; |
89 | return if $CON; |
71 | |
90 | |
72 | $CON = AnyEvent::IRC::Client->new; |
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 | }); |
73 | $CON->connect ($BOTSERVER, $BOTPORT, { |
96 | $CON->connect ($BOTSERVER, $BOTPORT, { |
74 | nick => $BOTNAME, |
97 | nick => $BOTNAME, |
75 | user => $BOTNAME, |
98 | user => $BOTNAME, |
76 | real => 'deliantra server' |
99 | real => 'deliantra server' |
77 | }); |
100 | }); |
… | |
… | |
79 | $CON->reg_cb ( |
102 | $CON->reg_cb ( |
80 | irc_privmsg => sub { |
103 | irc_privmsg => sub { |
81 | my ($con, $msg) = @_; |
104 | my ($con, $msg) = @_; |
82 | my $name = 'irc'; |
105 | my $name = 'irc'; |
83 | my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); |
106 | my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); |
84 | my $NOW = Time::HiRes::time; |
107 | my $NOW = EV::time; |
85 | |
108 | |
86 | my $tmsg = filter_colors ($msg->{params}->[-1]); |
109 | my $tmsg = filter_colors ($msg->{params}->[-1]); |
87 | $tmsg =~ s/\x01[^\x01]*\x01//g; |
110 | $tmsg =~ s/\x01[^\x01]*\x01//g; |
88 | $tmsg =~ s/\015?\012/ /g; |
111 | $tmsg =~ s/\015?\012/ /g; |
89 | |
112 | |
… | |
… | |
97 | ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; |
120 | ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; |
98 | cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; |
121 | cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; |
99 | } |
122 | } |
100 | }, |
123 | }, |
101 | connect => sub { |
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 { |
102 | warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
132 | cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
|
|
133 | } |
103 | }, |
134 | }, |
104 | registered => sub { |
135 | registered => sub { |
105 | warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
136 | cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
106 | }, |
137 | }, |
107 | error => sub { |
138 | error => sub { |
108 | my ($con, $code, $message) = @_; |
139 | my ($con, $code, $message) = @_; |
109 | warn "IRC: IRC ERROR ($code) $message\n"; |
140 | cf::error "IRC: IRC ERROR ($code) $message\n"; |
110 | }, |
141 | }, |
111 | disconnect => sub { |
142 | disconnect => sub { |
112 | my ($con, $reason) = @_; |
143 | my ($con, $reason) = @_; |
113 | warn "IRC: disconnect: $reason\n"; |
144 | cf::warn "IRC: disconnect: $reason\n"; |
114 | undef $CON; |
145 | undef $CON; |
115 | } |
146 | } |
116 | ); |
147 | ); |
117 | } |
148 | } |
118 | |
149 | |
119 | our $RECONNECT = cf::periodic 30, Coro::unblock_sub { |
150 | our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub { |
120 | check_connection; |
151 | check_connection; |
121 | }; |
152 | }; |
122 | |
153 | |