Changeset 3b4ba7d for perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
- Timestamp:
- Dec 15, 2009, 12:03:05 AM (15 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
rf1a2736 r3b4ba7d 3 3 4 4 package BarnOwl::Module::IRC::Connection; 5 use BarnOwl::Timer; 5 6 6 7 =head1 NAME … … 18 19 19 20 use base qw(Class::Accessor Exporter); 20 __PACKAGE__->mk_accessors(qw(conn alias channels connectedmotd names_tmp whois_tmp));21 __PACKAGE__->mk_accessors(qw(conn alias channels motd names_tmp whois_tmp)); 21 22 our @EXPORT_OK = qw(&is_private); 22 23 … … 44 45 $self->channels([]); 45 46 $self->motd(""); 46 $self->connected(0);47 47 $self->names_tmp(0); 48 48 $self->whois_tmp(""); 49 49 50 $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) });51 50 $self->conn->add_default_handler(sub { shift; $self->on_event(@_) }); 52 51 $self->conn->add_handler(['msg', 'notice', 'public', 'caction'], … … 151 150 my ($self, $evt) = @_; 152 151 $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 }159 152 BarnOwl::admin_message("IRC", 160 153 BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" … … 172 165 ); 173 166 BarnOwl::queue_message($msg); 167 push @{$self->channels}, $evt->to; 174 168 } 175 169 … … 184 178 ); 185 179 BarnOwl::queue_message($msg); 180 $self->channels([ grep {$_ ne $evt->to} @{$self->channels}]); 186 181 } 187 182 … … 199 194 } 200 195 201 sub on_disconnect {196 sub disconnect { 202 197 my $self = shift; 203 198 delete $BarnOwl::Module::IRC::ircnets{$self->alias}; … … 211 206 } 212 207 BarnOwl::remove_io_dispatch($self->{FD}); 208 $self->motd(""); 209 } 210 211 sub on_disconnect { 212 my ($self, $evt) = @_; 213 $self->disconnect; 213 214 BarnOwl::admin_message('IRC', 214 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 } 215 221 } 216 222 … … 220 226 "[" . $self->alias . "] " . 221 227 [$evt->args]->[1] . ": Nick already in use"); 222 unless($self->connected) { 223 $self->conn->disconnect; 224 } 228 $self->disconnect unless $self->motd; 225 229 } 226 230 … … 304 308 . strip_irc_formatting(join("\n", $evt->args))) 305 309 if BarnOwl::getvar('irc:spew') eq 'on'; 310 } 311 312 sub 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 325 sub 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 336 sub 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 ); 306 352 } 307 353
Note: See TracChangeset
for help on using the changeset viewer.