1 | use strict; |
---|
2 | use warnings; |
---|
3 | |
---|
4 | package BarnOwl::Module::IRC::Connection; |
---|
5 | use BarnOwl::Timer; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | BarnOwl::Module::IRC::Connection |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | This module is a wrapper around Net::IRC::Connection for BarnOwl's IRC |
---|
14 | support |
---|
15 | |
---|
16 | =cut |
---|
17 | |
---|
18 | use Net::IRC::Connection; |
---|
19 | |
---|
20 | use base qw(Class::Accessor Exporter); |
---|
21 | __PACKAGE__->mk_accessors(qw(conn alias channels motd names_tmp whois_tmp)); |
---|
22 | our @EXPORT_OK = qw(&is_private); |
---|
23 | |
---|
24 | use BarnOwl; |
---|
25 | |
---|
26 | BEGIN { |
---|
27 | no strict 'refs'; |
---|
28 | my @delegate = qw(nick server); |
---|
29 | for my $meth (@delegate) { |
---|
30 | *{"BarnOwl::Module::IRC::Connection::$meth"} = sub { |
---|
31 | shift->conn->$meth(@_); |
---|
32 | } |
---|
33 | } |
---|
34 | }; |
---|
35 | |
---|
36 | sub new { |
---|
37 | my $class = shift; |
---|
38 | my $irc = shift; |
---|
39 | my $alias = shift; |
---|
40 | my %args = (@_); |
---|
41 | my $conn = Net::IRC::Connection->new($irc, %args); |
---|
42 | my $self = bless({}, $class); |
---|
43 | $self->conn($conn); |
---|
44 | $self->alias($alias); |
---|
45 | $self->channels([]); |
---|
46 | $self->motd(""); |
---|
47 | $self->names_tmp(0); |
---|
48 | $self->whois_tmp(""); |
---|
49 | |
---|
50 | $self->conn->add_default_handler(sub { shift; $self->on_event(@_) }); |
---|
51 | $self->conn->add_handler(['msg', 'notice', 'public', 'caction'], |
---|
52 | sub { shift; $self->on_msg(@_) }); |
---|
53 | $self->conn->add_handler(['welcome', 'yourhost', 'created', |
---|
54 | 'luserclient', 'luserop', 'luserchannels', 'luserme', |
---|
55 | 'error'], |
---|
56 | sub { shift; $self->on_admin_msg(@_) }); |
---|
57 | $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global', |
---|
58 | 'luserconns'], |
---|
59 | sub { }); |
---|
60 | $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) }); |
---|
61 | $self->conn->add_handler(motd => sub { shift; $self->on_motd(@_) }); |
---|
62 | $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) }); |
---|
63 | $self->conn->add_handler(join => sub { shift; $self->on_join(@_) }); |
---|
64 | $self->conn->add_handler(part => sub { shift; $self->on_part(@_) }); |
---|
65 | $self->conn->add_handler(quit => sub { shift; $self->on_quit(@_) }); |
---|
66 | $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) }); |
---|
67 | $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) }); |
---|
68 | $self->conn->add_handler(cping => sub { shift; $self->on_ping(@_) }); |
---|
69 | $self->conn->add_handler(topic => sub { shift; $self->on_topic(@_) }); |
---|
70 | $self->conn->add_handler(topicinfo => sub { shift; $self->on_topicinfo(@_) }); |
---|
71 | $self->conn->add_handler(namreply => sub { shift; $self->on_namreply(@_) }); |
---|
72 | $self->conn->add_handler(endofnames=> sub { shift; $self->on_endofnames(@_) }); |
---|
73 | $self->conn->add_handler(endofwhois=> sub { shift; $self->on_endofwhois(@_) }); |
---|
74 | $self->conn->add_handler(mode => sub { shift; $self->on_mode(@_) }); |
---|
75 | $self->conn->add_handler(nosuchchannel => sub { shift; $self->on_nosuchchannel(@_) }); |
---|
76 | |
---|
77 | return $self; |
---|
78 | } |
---|
79 | |
---|
80 | sub getSocket |
---|
81 | { |
---|
82 | my $self = shift; |
---|
83 | return $self->conn->socket; |
---|
84 | } |
---|
85 | |
---|
86 | ################################################################################ |
---|
87 | ############################### IRC callbacks ################################## |
---|
88 | ################################################################################ |
---|
89 | |
---|
90 | sub new_message { |
---|
91 | my $self = shift; |
---|
92 | my $evt = shift; |
---|
93 | return BarnOwl::Message->new( |
---|
94 | type => 'IRC', |
---|
95 | server => $self->server, |
---|
96 | network => $self->alias, |
---|
97 | sender => $evt->nick, |
---|
98 | hostname => $evt->host, |
---|
99 | from => $evt->from, |
---|
100 | @_ |
---|
101 | ); |
---|
102 | } |
---|
103 | |
---|
104 | sub on_msg { |
---|
105 | my ($self, $evt) = @_; |
---|
106 | my ($recipient) = $evt->to; |
---|
107 | my $body = strip_irc_formatting([$evt->args]->[0]); |
---|
108 | my $nick = $self->nick; |
---|
109 | $body = '* '.$evt->nick.' '.$body if $evt->type eq 'caction'; |
---|
110 | my $msg = $self->new_message($evt, |
---|
111 | direction => 'in', |
---|
112 | recipient => $recipient, |
---|
113 | body => $body, |
---|
114 | $evt->type eq 'notice' ? |
---|
115 | (notice => 'true') : (), |
---|
116 | is_private($recipient) ? |
---|
117 | (private => 'true') : (channel => $recipient), |
---|
118 | replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, |
---|
119 | (is_private($recipient) ? $evt->nick : $recipient)), |
---|
120 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
---|
121 | ); |
---|
122 | |
---|
123 | BarnOwl::queue_message($msg); |
---|
124 | } |
---|
125 | |
---|
126 | sub on_ping { |
---|
127 | my ($self, $evt) = @_; |
---|
128 | $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
---|
129 | } |
---|
130 | |
---|
131 | sub on_admin_msg { |
---|
132 | my ($self, $evt) = @_; |
---|
133 | return if BarnOwl::Module::IRC->skip_msg($evt->type); |
---|
134 | BarnOwl::admin_message("IRC", |
---|
135 | BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from ' |
---|
136 | . $self->alias) . "\n" |
---|
137 | . strip_irc_formatting(join ' ', cdr($evt->args))); |
---|
138 | } |
---|
139 | |
---|
140 | sub on_motdstart { |
---|
141 | my ($self, $evt) = @_; |
---|
142 | $self->motd(join "\n", cdr($evt->args)); |
---|
143 | } |
---|
144 | |
---|
145 | sub on_motd { |
---|
146 | my ($self, $evt) = @_; |
---|
147 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
---|
148 | } |
---|
149 | |
---|
150 | sub on_endofmotd { |
---|
151 | my ($self, $evt) = @_; |
---|
152 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
---|
153 | BarnOwl::admin_message("IRC", |
---|
154 | BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" |
---|
155 | . strip_irc_formatting($self->motd)); |
---|
156 | } |
---|
157 | |
---|
158 | sub on_join { |
---|
159 | my ($self, $evt) = @_; |
---|
160 | my $msg = $self->new_message($evt, |
---|
161 | loginout => 'login', |
---|
162 | action => 'join', |
---|
163 | channel => $evt->to, |
---|
164 | replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to), |
---|
165 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
---|
166 | ); |
---|
167 | BarnOwl::queue_message($msg); |
---|
168 | push @{$self->channels}, $evt->to; |
---|
169 | } |
---|
170 | |
---|
171 | sub on_part { |
---|
172 | my ($self, $evt) = @_; |
---|
173 | my $msg = $self->new_message($evt, |
---|
174 | loginout => 'logout', |
---|
175 | action => 'part', |
---|
176 | channel => $evt->to, |
---|
177 | replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to), |
---|
178 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
---|
179 | ); |
---|
180 | BarnOwl::queue_message($msg); |
---|
181 | $self->channels([ grep {$_ ne $evt->to} @{$self->channels}]); |
---|
182 | } |
---|
183 | |
---|
184 | sub on_quit { |
---|
185 | my ($self, $evt) = @_; |
---|
186 | my $msg = $self->new_message($evt, |
---|
187 | loginout => 'logout', |
---|
188 | action => 'quit', |
---|
189 | from => $evt->to, |
---|
190 | reason => [$evt->args]->[0], |
---|
191 | replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
---|
192 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
---|
193 | ); |
---|
194 | BarnOwl::queue_message($msg); |
---|
195 | } |
---|
196 | |
---|
197 | sub disconnect { |
---|
198 | my $self = shift; |
---|
199 | delete $BarnOwl::Module::IRC::ircnets{$self->alias}; |
---|
200 | for my $k (keys %BarnOwl::Module::IRC::channels) { |
---|
201 | my @conns = grep {$_ ne $self} @{$BarnOwl::Module::IRC::channels{$k}}; |
---|
202 | if(@conns) { |
---|
203 | $BarnOwl::Module::IRC::channels{$k} = \@conns; |
---|
204 | } else { |
---|
205 | delete $BarnOwl::Module::IRC::channels{$k}; |
---|
206 | } |
---|
207 | } |
---|
208 | BarnOwl::remove_io_dispatch($self->{FD}); |
---|
209 | $self->motd(""); |
---|
210 | } |
---|
211 | |
---|
212 | sub on_disconnect { |
---|
213 | my ($self, $evt) = @_; |
---|
214 | $self->disconnect; |
---|
215 | BarnOwl::admin_message('IRC', |
---|
216 | "[" . $self->alias . "] Disconnected from server"); |
---|
217 | if ($evt->format and $evt->format eq "error") { |
---|
218 | $self->schedule_reconnect; |
---|
219 | } else { |
---|
220 | $self->channels([]); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | sub on_nickinuse { |
---|
225 | my ($self, $evt) = @_; |
---|
226 | BarnOwl::admin_message("IRC", |
---|
227 | "[" . $self->alias . "] " . |
---|
228 | [$evt->args]->[1] . ": Nick already in use"); |
---|
229 | $self->disconnect unless $self->motd; |
---|
230 | } |
---|
231 | |
---|
232 | sub on_topic { |
---|
233 | my ($self, $evt) = @_; |
---|
234 | my @args = $evt->args; |
---|
235 | if (scalar @args > 1) { |
---|
236 | BarnOwl::admin_message("IRC", |
---|
237 | "Topic for $args[1] on " . $self->alias . " is $args[2]"); |
---|
238 | } else { |
---|
239 | BarnOwl::admin_message("IRC", |
---|
240 | "Topic changed to $args[0]"); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | sub on_topicinfo { |
---|
245 | my ($self, $evt) = @_; |
---|
246 | my @args = $evt->args; |
---|
247 | BarnOwl::admin_message("IRC", |
---|
248 | "Topic for $args[1] set by $args[2] at " . localtime($args[3])); |
---|
249 | } |
---|
250 | |
---|
251 | # IRC gives us a bunch of namreply messages, followed by an endofnames. |
---|
252 | # We need to collect them from the namreply and wait for the endofnames message. |
---|
253 | # After this happens, the names_tmp variable is cleared. |
---|
254 | |
---|
255 | sub on_namreply { |
---|
256 | my ($self, $evt) = @_; |
---|
257 | return unless $self->names_tmp; |
---|
258 | $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]); |
---|
259 | } |
---|
260 | |
---|
261 | sub on_endofnames { |
---|
262 | my ($self, $evt) = @_; |
---|
263 | return unless $self->names_tmp; |
---|
264 | my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n"); |
---|
265 | for my $name (@{$self->names_tmp}) { |
---|
266 | $names .= " $name\n"; |
---|
267 | } |
---|
268 | BarnOwl::popless_ztext($names); |
---|
269 | $self->names_tmp(0); |
---|
270 | } |
---|
271 | |
---|
272 | sub on_whois { |
---|
273 | my ($self, $evt) = @_; |
---|
274 | $self->whois_tmp( |
---|
275 | $self->whois_tmp . "\n" . $evt->type . ":\n " . |
---|
276 | join("\n ", cdr(cdr($evt->args))) . "\n" |
---|
277 | ); |
---|
278 | } |
---|
279 | |
---|
280 | sub on_endofwhois { |
---|
281 | my ($self, $evt) = @_; |
---|
282 | BarnOwl::popless_ztext( |
---|
283 | BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") . |
---|
284 | $self->whois_tmp |
---|
285 | ); |
---|
286 | $self->whois_tmp(''); |
---|
287 | } |
---|
288 | |
---|
289 | sub on_mode { |
---|
290 | my ($self, $evt) = @_; |
---|
291 | BarnOwl::admin_message("IRC", |
---|
292 | "[" . $self->alias . "] User " . ($evt->nick) . + " set mode " . |
---|
293 | join(" ", $evt->args) . "on " . $evt->to->[0] |
---|
294 | ); |
---|
295 | } |
---|
296 | |
---|
297 | sub on_nosuchchannel { |
---|
298 | my ($self, $evt) = @_; |
---|
299 | BarnOwl::admin_message("IRC", |
---|
300 | "[" . $self->alias . "] " . |
---|
301 | "No such channel: " . [$evt->args]->[1]) |
---|
302 | } |
---|
303 | |
---|
304 | sub on_event { |
---|
305 | my ($self, $evt) = @_; |
---|
306 | return on_whois(@_) if ($evt->type =~ /^whois/); |
---|
307 | BarnOwl::admin_message("IRC", |
---|
308 | "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n" |
---|
309 | . strip_irc_formatting(join("\n", $evt->args))) |
---|
310 | if BarnOwl::getvar('irc:spew') eq 'on'; |
---|
311 | } |
---|
312 | |
---|
313 | sub schedule_reconnect { |
---|
314 | my $self = shift; |
---|
315 | my $interval = shift || 5; |
---|
316 | delete $BarnOwl::Module::IRC::ircnets{$self->alias}; |
---|
317 | $BarnOwl::Module::IRC::reconnect{$self->alias} = |
---|
318 | BarnOwl::Timer->new( { |
---|
319 | after => $interval, |
---|
320 | cb => sub { |
---|
321 | $self->reconnect( $interval ); |
---|
322 | }, |
---|
323 | } ); |
---|
324 | } |
---|
325 | |
---|
326 | sub connected { |
---|
327 | my $self = shift; |
---|
328 | my $msg = shift; |
---|
329 | BarnOwl::admin_message("IRC", $msg); |
---|
330 | delete $BarnOwl::Module::IRC::reconnect{$self->alias}; |
---|
331 | $BarnOwl::Module::IRC::ircnets{$self->alias} = $self; |
---|
332 | my $fd = $self->getSocket()->fileno(); |
---|
333 | BarnOwl::add_io_dispatch($fd, 'r', \&BarnOwl::Module::IRC::OwlProcess); |
---|
334 | $self->{FD} = $fd; |
---|
335 | } |
---|
336 | |
---|
337 | sub reconnect { |
---|
338 | my $self = shift; |
---|
339 | my $backoff = shift; |
---|
340 | |
---|
341 | $self->conn->connect; |
---|
342 | if ($self->conn->connected) { |
---|
343 | $self->connected("Reconnected to ".$self->alias); |
---|
344 | my @channels = @{$self->channels}; |
---|
345 | $self->channels([]); |
---|
346 | $self->conn->join($_) for @channels; |
---|
347 | return; |
---|
348 | } |
---|
349 | |
---|
350 | $backoff *= 2; |
---|
351 | $backoff = 60*5 if $backoff > 60*5; |
---|
352 | $self->schedule_reconnect( $backoff ); |
---|
353 | } |
---|
354 | |
---|
355 | ################################################################################ |
---|
356 | ########################### Utilities/Helpers ################################## |
---|
357 | ################################################################################ |
---|
358 | |
---|
359 | sub strip_irc_formatting { |
---|
360 | my $body = shift; |
---|
361 | # Strip mIRC colors. If someone wants to write code to convert |
---|
362 | # these to zephyr colors, be my guest. |
---|
363 | $body =~ s/\cC\d+(?:,\d+)?//g; |
---|
364 | $body =~ s/\cO//g; |
---|
365 | |
---|
366 | my @pieces = split /\cB/, $body; |
---|
367 | my $out = ''; |
---|
368 | while(@pieces) { |
---|
369 | $out .= shift @pieces; |
---|
370 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
---|
371 | } |
---|
372 | return $out; |
---|
373 | } |
---|
374 | |
---|
375 | # Determines if the given message recipient is a username, as opposed to |
---|
376 | # a channel that starts with # or &. |
---|
377 | sub is_private { |
---|
378 | return shift !~ /^[\#\&]/; |
---|
379 | } |
---|
380 | |
---|
381 | sub cdr { |
---|
382 | shift; |
---|
383 | return @_; |
---|
384 | } |
---|
385 | |
---|
386 | 1; |
---|