Ignore:
Timestamp:
Dec 15, 2009, 12:03:05 AM (15 years ago)
Author:
Alex Vandiver <alexmv@mit.edu>
Branches:
master, release-1.10, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
cd12307
Parents:
62c91c1
git-author:
Alex Vandiver <alexmv@mit.edu> (12/12/09 00:25:49)
git-committer:
Alex Vandiver <alexmv@mit.edu> (12/15/09 00:03:05)
Message:
Reconnect and re-join channels on IRC disconnect
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm

    rf1a2736 r3b4ba7d  
    33
    44package BarnOwl::Module::IRC::Connection;
     5use BarnOwl::Timer;
    56
    67=head1 NAME
     
    1819
    1920use base qw(Class::Accessor Exporter);
    20 __PACKAGE__->mk_accessors(qw(conn alias channels connected motd names_tmp whois_tmp));
     21__PACKAGE__->mk_accessors(qw(conn alias channels motd names_tmp whois_tmp));
    2122our @EXPORT_OK = qw(&is_private);
    2223
     
    4445    $self->channels([]);
    4546    $self->motd("");
    46     $self->connected(0);
    4747    $self->names_tmp(0);
    4848    $self->whois_tmp("");
    4949
    50     $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) });
    5150    $self->conn->add_default_handler(sub { shift; $self->on_event(@_) });
    5251    $self->conn->add_handler(['msg', 'notice', 'public', 'caction'],
     
    151150    my ($self, $evt) = @_;
    152151    $self->motd(join "\n", $self->motd, cdr($evt->args));
    153     if(!$self->connected) {
    154         BarnOwl::admin_message("IRC", "Connected to " .
    155                                $self->server . " (" . $self->alias . ")");
    156         $self->connected(1);
    157        
    158     }
    159152    BarnOwl::admin_message("IRC",
    160153            BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n"
     
    172165        );
    173166    BarnOwl::queue_message($msg);
     167    push @{$self->channels}, $evt->to;
    174168}
    175169
     
    184178        );
    185179    BarnOwl::queue_message($msg);
     180    $self->channels([ grep {$_ ne $evt->to} @{$self->channels}]);
    186181}
    187182
     
    199194}
    200195
    201 sub on_disconnect {
     196sub disconnect {
    202197    my $self = shift;
    203198    delete $BarnOwl::Module::IRC::ircnets{$self->alias};
     
    211206    }
    212207    BarnOwl::remove_io_dispatch($self->{FD});
     208    $self->motd("");
     209}
     210
     211sub on_disconnect {
     212    my ($self, $evt) = @_;
     213    $self->disconnect;
    213214    BarnOwl::admin_message('IRC',
    214215                           "[" . $self->alias . "] Disconnected from server");
     216    if ($evt->format and $evt->format eq "error") {
     217        $self->schedule_reconnect;
     218    } else {
     219        $self->channels([]);
     220    }
    215221}
    216222
     
    220226                           "[" . $self->alias . "] " .
    221227                           [$evt->args]->[1] . ": Nick already in use");
    222     unless($self->connected) {
    223         $self->conn->disconnect;
    224     }
     228    $self->disconnect unless $self->motd;
    225229}
    226230
     
    304308            . strip_irc_formatting(join("\n", $evt->args)))
    305309        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 );
    306352}
    307353
Note: See TracChangeset for help on using the changeset viewer.