… | |
… | |
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 | } |
… | |
… | |
68 | |
68 | |
69 | sub check_connection { |
69 | sub check_connection { |
70 | return if $CON; |
70 | return if $CON; |
71 | |
71 | |
72 | $CON = AnyEvent::IRC::Client->new; |
72 | $CON = AnyEvent::IRC::Client->new; |
|
|
73 | $CON->set_exception_cb (sub { |
|
|
74 | my ($exp, $ev) = @_; |
|
|
75 | cf::error "IRC: IRC EXCEPTION (event $ev): $exp\n"; |
|
|
76 | }); |
73 | $CON->connect ($BOTSERVER, $BOTPORT, { |
77 | $CON->connect ($BOTSERVER, $BOTPORT, { |
74 | nick => $BOTNAME, |
78 | nick => $BOTNAME, |
75 | user => $BOTNAME, |
79 | user => $BOTNAME, |
76 | real => 'deliantra server' |
80 | real => 'deliantra server' |
77 | }); |
81 | }); |
… | |
… | |
100 | }, |
104 | }, |
101 | connect => sub { |
105 | connect => sub { |
102 | my ($con, $error) = @_; |
106 | my ($con, $error) = @_; |
103 | |
107 | |
104 | if ($error) { |
108 | if ($error) { |
105 | warn "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n"; |
109 | cf::error "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n"; |
106 | undef $CON; |
110 | undef $CON; |
107 | |
111 | |
108 | } else { |
112 | } else { |
109 | warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
113 | cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; |
110 | } |
114 | } |
111 | }, |
115 | }, |
112 | registered => sub { |
116 | registered => sub { |
113 | warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
117 | cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; |
114 | }, |
118 | }, |
115 | error => sub { |
119 | error => sub { |
116 | my ($con, $code, $message) = @_; |
120 | my ($con, $code, $message) = @_; |
117 | warn "IRC: IRC ERROR ($code) $message\n"; |
121 | cf::error "IRC: IRC ERROR ($code) $message\n"; |
118 | }, |
122 | }, |
119 | disconnect => sub { |
123 | disconnect => sub { |
120 | my ($con, $reason) = @_; |
124 | my ($con, $reason) = @_; |
121 | warn "IRC: disconnect: $reason\n"; |
125 | cf::warn "IRC: disconnect: $reason\n"; |
122 | undef $CON; |
126 | undef $CON; |
123 | } |
127 | } |
124 | ); |
128 | ); |
125 | } |
129 | } |
126 | |
130 | |
127 | our $RECONNECT = cf::periodic 30, Coro::unblock_sub { |
131 | our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub { |
128 | check_connection; |
132 | check_connection; |
129 | }; |
133 | }; |
130 | |
134 | |