source: perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm @ cd12307

release-1.10release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since cd12307 was 3b4ba7d, checked in by Alex Vandiver <alexmv@mit.edu>, 14 years ago
Reconnect and re-join channels on IRC disconnect
  • Property mode set to 100644
File size: 11.6 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Module::IRC::Connection;
5use BarnOwl::Timer;
6
7=head1 NAME
8
9BarnOwl::Module::IRC::Connection
10
11=head1 DESCRIPTION
12
13This module is a wrapper around Net::IRC::Connection for BarnOwl's IRC
14support
15
16=cut
17
18use Net::IRC::Connection;
19
20use base qw(Class::Accessor Exporter);
21__PACKAGE__->mk_accessors(qw(conn alias channels motd names_tmp whois_tmp));
22our @EXPORT_OK = qw(&is_private);
23
24use BarnOwl;
25
26BEGIN {
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
36sub 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
80sub getSocket
81{
82    my $self = shift;
83    return $self->conn->socket;
84}
85
86################################################################################
87############################### IRC callbacks ##################################
88################################################################################
89
90sub 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
104sub 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          (isprivate  => '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
126sub on_ping {
127    my ($self, $evt) = @_;
128    $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));
129}
130
131sub on_admin_msg {
132    my ($self, $evt) = @_;
133    BarnOwl::admin_message("IRC",
134            BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from '
135                . $self->alias) . "\n"
136            . strip_irc_formatting(join ' ', cdr($evt->args)));
137}
138
139sub on_motdstart {
140    my ($self, $evt) = @_;
141    $self->motd(join "\n", cdr($evt->args));
142}
143
144sub on_motd {
145    my ($self, $evt) = @_;
146    $self->motd(join "\n", $self->motd, cdr($evt->args));
147}
148
149sub on_endofmotd {
150    my ($self, $evt) = @_;
151    $self->motd(join "\n", $self->motd, cdr($evt->args));
152    BarnOwl::admin_message("IRC",
153            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
154            . strip_irc_formatting($self->motd));
155}
156
157sub on_join {
158    my ($self, $evt) = @_;
159    my $msg = $self->new_message($evt,
160        loginout   => 'login',
161        action     => 'join',
162        channel    => $evt->to,
163        replycmd   => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to),
164        replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
165        );
166    BarnOwl::queue_message($msg);
167    push @{$self->channels}, $evt->to;
168}
169
170sub on_part {
171    my ($self, $evt) = @_;
172    my $msg = $self->new_message($evt,
173        loginout   => 'logout',
174        action     => 'part',
175        channel    => $evt->to,
176        replycmd   => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to),
177        replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
178        );
179    BarnOwl::queue_message($msg);
180    $self->channels([ grep {$_ ne $evt->to} @{$self->channels}]);
181}
182
183sub on_quit {
184    my ($self, $evt) = @_;
185    my $msg = $self->new_message($evt,
186        loginout   => 'logout',
187        action     => 'quit',
188        from       => $evt->to,
189        reason     => [$evt->args]->[0],
190        replycmd   => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
191        replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),
192        );
193    BarnOwl::queue_message($msg);
194}
195
196sub disconnect {
197    my $self = shift;
198    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
199    for my $k (keys %BarnOwl::Module::IRC::channels) {
200        my @conns = grep {$_ ne $self} @{$BarnOwl::Module::IRC::channels{$k}};
201        if(@conns) {
202            $BarnOwl::Module::IRC::channels{$k} = \@conns;
203        } else {
204            delete $BarnOwl::Module::IRC::channels{$k};
205        }
206    }
207    BarnOwl::remove_io_dispatch($self->{FD});
208    $self->motd("");
209}
210
211sub on_disconnect {
212    my ($self, $evt) = @_;
213    $self->disconnect;
214    BarnOwl::admin_message('IRC',
215                           "[" . $self->alias . "] Disconnected from server");
216    if ($evt->format and $evt->format eq "error") {
217        $self->schedule_reconnect;
218    } else {
219        $self->channels([]);
220    }
221}
222
223sub on_nickinuse {
224    my ($self, $evt) = @_;
225    BarnOwl::admin_message("IRC",
226                           "[" . $self->alias . "] " .
227                           [$evt->args]->[1] . ": Nick already in use");
228    $self->disconnect unless $self->motd;
229}
230
231sub on_topic {
232    my ($self, $evt) = @_;
233    my @args = $evt->args;
234    if (scalar @args > 1) {
235        BarnOwl::admin_message("IRC",
236                "Topic for $args[1] on " . $self->alias . " is $args[2]");
237    } else {
238        BarnOwl::admin_message("IRC",
239                "Topic changed to $args[0]");
240    }
241}
242
243sub on_topicinfo {
244    my ($self, $evt) = @_;
245    my @args = $evt->args;
246    BarnOwl::admin_message("IRC",
247        "Topic for $args[1] set by $args[2] at " . localtime($args[3]));
248}
249
250# IRC gives us a bunch of namreply messages, followed by an endofnames.
251# We need to collect them from the namreply and wait for the endofnames message.
252# After this happens, the names_tmp variable is cleared.
253
254sub on_namreply {
255    my ($self, $evt) = @_;
256    return unless $self->names_tmp;
257    $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]);
258}
259
260sub on_endofnames {
261    my ($self, $evt) = @_;
262    return unless $self->names_tmp;
263    my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n");
264    for my $name (@{$self->names_tmp}) {
265        $names .= "  $name\n";
266    }
267    BarnOwl::popless_ztext($names);
268    $self->names_tmp(0);
269}
270
271sub on_whois {
272    my ($self, $evt) = @_;
273    $self->whois_tmp(
274      $self->whois_tmp . "\n" . $evt->type . ":\n  " .
275      join("\n  ", cdr(cdr($evt->args))) . "\n"
276    );
277}
278
279sub on_endofwhois {
280    my ($self, $evt) = @_;
281    BarnOwl::popless_ztext(
282        BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") .
283        $self->whois_tmp
284    );
285    $self->whois_tmp('');
286}
287
288sub on_mode {
289    my ($self, $evt) = @_;
290    BarnOwl::admin_message("IRC",
291                           "[" . $self->alias . "] User " . ($evt->nick) . + " set mode " .
292                           join(" ", $evt->args) . "on " . $evt->to->[0]
293                          );
294}
295
296sub on_nosuchchannel {
297    my ($self, $evt) = @_;
298    BarnOwl::admin_message("IRC",
299                           "[" . $self->alias . "] " .
300                           "No such channel: " . [$evt->args]->[1])
301}
302
303sub on_event {
304    my ($self, $evt) = @_;
305    return on_whois(@_) if ($evt->type =~ /^whois/);
306    BarnOwl::admin_message("IRC",
307            "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n"
308            . strip_irc_formatting(join("\n", $evt->args)))
309        if BarnOwl::getvar('irc:spew') eq 'on';
310}
311
312sub schedule_reconnect {
313    my $self = shift;
314    my $interval = shift || 5;
315    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
316    $BarnOwl::Module::IRC::reconnect{$self->alias} =
317        BarnOwl::Timer->new( {
318            after => $interval,
319            cb    => sub {
320                $self->reconnect( $interval );
321            },
322        } );
323}
324
325sub connected {
326    my $self = shift;
327    my $msg = shift;
328    BarnOwl::admin_message("IRC", $msg);
329    delete $BarnOwl::Module::IRC::reconnect{$self->alias};
330    $BarnOwl::Module::IRC::ircnets{$self->alias} = $self;
331    my $fd = $self->getSocket()->fileno();
332    BarnOwl::add_io_dispatch($fd, 'r', \&BarnOwl::Module::IRC::OwlProcess);
333    $self->{FD} = $fd;
334}
335
336sub reconnect {
337    my $self = shift;
338    my $backoff = shift;
339
340    $self->conn->connect;
341    if ($self->conn->connected) {
342        $self->connected("Reconnected to ".$self->alias);
343        my @channels = @{$self->channels};
344        $self->channels([]);
345        $self->conn->join($_) for @channels;
346        return;
347    }
348
349    $backoff *= 2;
350    $backoff = 60*5 if $backoff > 60*5;
351    $self->schedule_reconnect( $backoff );
352}
353
354################################################################################
355########################### Utilities/Helpers ##################################
356################################################################################
357
358sub strip_irc_formatting {
359    my $body = shift;
360    # Strip mIRC colors. If someone wants to write code to convert
361    # these to zephyr colors, be my guest.
362    $body =~ s/\cC\d+(?:,\d+)?//g;
363    $body =~ s/\cO//g;
364   
365    my @pieces = split /\cB/, $body;
366    my $out = '';
367    while(@pieces) {
368        $out .= shift @pieces;
369        $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces;
370    }
371    return $out;
372}
373
374# Determines if the given message recipient is a username, as opposed to
375# a channel that starts with # or &.
376sub is_private {
377    return shift !~ /^[\#\&]/;
378}
379
380sub cdr {
381    shift;
382    return @_;
383}
384
3851;
Note: See TracBrowser for help on using the repository browser.