… | |
… | |
6 | |
6 | |
7 | # requires: commands.ext |
7 | # requires: commands.ext |
8 | |
8 | |
9 | return unless exists $cf::CFG{irc_server}; |
9 | return unless exists $cf::CFG{irc_server}; |
10 | |
10 | |
11 | my $BOTSERVER = $cf::CFG{irc_server} || "localhost"; |
11 | CONF BOTSERVER : irc_server = undef; |
12 | my $BOTPORT = $cf::CFG{irc_port} || 6667; |
12 | CONF BOTPORT : irc_port = undef; |
13 | my $BOTNAME = $cf::CFG{irc_nick} || "server"; |
13 | CONF BOTNAME : irc_nick = undef; |
14 | my $BOTCHAN = $cf::CFG{irc_chan} || "cf"; |
14 | CONF BOTCHAN : irc_chan = undef; |
15 | |
15 | |
16 | my $CON; # the connection |
16 | our $CON; # the connection |
17 | |
17 | |
18 | sub unload { |
18 | sub unload { |
19 | $CON->disconnect if $CON; |
19 | $CON->disconnect if $CON; |
20 | undef $CON; |
20 | undef $CON; |
21 | } |
21 | } |
… | |
… | |
29 | } |
29 | } |
30 | |
30 | |
31 | sub users { |
31 | sub users { |
32 | $CON |
32 | $CON |
33 | ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } |
33 | ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } |
34 | : () |
34 | : () |
35 | } |
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 | }; |
36 | |
56 | |
37 | sub handle_fcmd { |
57 | sub handle_fcmd { |
38 | my ($name, $me, $msg) = @_; |
58 | my ($name, $me, $msg) = @_; |
39 | |
59 | |
40 | if ($msg eq "!who") { |
60 | if ($msg eq "!who") { |
… | |
… | |
68 | |
88 | |
69 | sub check_connection { |
89 | sub check_connection { |
70 | return if $CON; |
90 | return if $CON; |
71 | |
91 | |
72 | $CON = AnyEvent::IRC::Client->new; |
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 | }); |
73 | $CON->connect ($BOTSERVER, $BOTPORT, { |
97 | $CON->connect ($BOTSERVER, $BOTPORT, { |
74 | nick => $BOTNAME, |
98 | nick => $BOTNAME, |
75 | user => $BOTNAME, |
99 | user => $BOTNAME, |
76 | real => 'deliantra server' |
100 | real => 'deliantra server' |
77 | }); |
101 | }); |
… | |
… | |
100 | }, |
124 | }, |
101 | connect => sub { |
125 | connect => sub { |
102 | my ($con, $error) = @_; |
126 | my ($con, $error) = @_; |
103 | |
127 | |
104 | if ($error) { |
128 | if ($error) { |
105 | warn "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n"; |
129 | cf::error "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n"; |
106 | undef $CON; |
130 | undef $CON; |
107 | |
131 | |
108 | } else { |
132 | } else { |
109 | warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
133 | cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
110 | } |
134 | } |
111 | }, |
135 | }, |
112 | registered => sub { |
136 | registered => sub { |
113 | warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
137 | cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
114 | }, |
138 | }, |
115 | error => sub { |
139 | error => sub { |
116 | my ($con, $code, $message) = @_; |
140 | my ($con, $code, $message) = @_; |
117 | warn "IRC: IRC ERROR ($code) $message\n"; |
141 | cf::error "IRC: IRC ERROR ($code) $message\n"; |
118 | }, |
142 | }, |
119 | disconnect => sub { |
143 | disconnect => sub { |
120 | my ($con, $reason) = @_; |
144 | my ($con, $reason) = @_; |
121 | warn "IRC: disconnect: $reason\n"; |
145 | cf::warn "IRC: disconnect: $reason\n"; |
122 | undef $CON; |
146 | undef $CON; |
123 | } |
147 | } |
124 | ); |
148 | ); |
125 | } |
149 | } |
126 | |
150 | |
127 | our $RECONNECT = cf::periodic 30, Coro::unblock_sub { |
151 | our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub { |
128 | check_connection; |
152 | check_connection; |
129 | }; |
153 | }; |
130 | |
154 | |