Changeset 8ba9313
- Timestamp:
- Apr 3, 2011, 3:06:00 PM (14 years ago)
- Branches:
- master, release-1.10, release-1.8, release-1.9
- Children:
- 38d50c2
- Parents:
- fb96152
- git-author:
- Nelson Elhage <nelhage@nelhage.com> (02/24/11 22:14:37)
- git-committer:
- Nelson Elhage <nelhage@mit.edu> (04/03/11 15:06:00)
- Location:
- perl/modules/IRC/lib/BarnOwl/Module
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
perl/modules/IRC/lib/BarnOwl/Module/IRC.pm
r9620c8d r8ba9313 20 20 use BarnOwl::Module::IRC::Completion; 21 21 22 use Net::IRC;22 use AnyEvent::IRC; 23 23 use Getopt::Long; 24 24 use Encode; … … 70 70 71 71 register_commands(); 72 register_handlers();73 72 BarnOwl::filter(qw{irc type ^IRC$ or ( type ^admin$ and adminheader ^IRC$ )}); 74 73 } … … 76 75 sub shutdown { 77 76 for my $conn (values %ircnets) { 78 $conn->conn->disconnect( );77 $conn->conn->disconnect('Quitting'); 79 78 } 80 79 } … … 105 104 106 105 return $list; 107 }108 109 #sub mainloop_hook {110 # return unless defined $irc;111 # eval {112 # $irc->do_one_loop();113 # };114 # return;115 #}116 117 sub OwlProcess {118 return unless defined $irc;119 eval {120 $irc->do_one_loop();121 };122 return;123 }124 125 126 sub register_handlers {127 if(!$irc) {128 $irc = Net::IRC->new;129 $irc->timeout(0);130 }131 106 } 132 107 … … 406 381 } 407 382 408 my $conn = BarnOwl::Module::IRC::Connection->new($irc, $alias, 409 Nick => $nick, 410 Server => $host, 411 Port => $port, 412 Username => $username, 413 Ircname => $ircname, 414 Port => $port, 415 Password => $password, 416 SSL => $ssl 417 ); 418 419 if ($conn->conn->connected) { 420 $conn->connected("Connected to $alias as $nick"); 421 } else { 422 die("IRC::Connection->connect failed: $!"); 423 } 424 383 my $conn = BarnOwl::Module::IRC::Connection->new($alias, $host, $port, { 384 nick => $nick, 385 user => $username, 386 real => $ircname, 387 password => $password, 388 SSL => $ssl, 389 timeout => sub {0} 390 }); 425 391 return; 426 392 } … … 429 395 my $cmd = shift; 430 396 my $conn = shift; 431 if ($conn->conn-> connected) {432 $conn->conn->disconnect ;397 if ($conn->conn->is_connected) { 398 $conn->conn->disconnect("Goodbye!"); 433 399 } elsif ($reconnect{$conn->alias}) { 434 400 BarnOwl::admin_message('IRC', … … 463 429 for my $body (@msgs) { 464 430 if ($body =~ /^\/me (.*)/) { 465 $conn-> conn->me($to, Encode::encode('utf-8', $1));431 $conn->me($to, Encode::encode('utf-8', $1)); 466 432 $body = '* '.$conn->nick.' '.$1; 467 433 } else { 468 $conn->conn-> privmsg($to, Encode::encode('utf-8', $body));434 $conn->conn->send_msg('privmsg', $to, Encode::encode('utf-8', $body)); 469 435 } 470 436 my $msg = BarnOwl::Message->new( … … 491 457 my $target = shift; 492 458 $target ||= shift; 493 $conn->conn-> mode($target, @_);459 $conn->conn->send_msg(mode => $target, @_); 494 460 return; 495 461 } … … 501 467 $channels{$chan} ||= []; 502 468 push @{$channels{$chan}}, $conn; 503 $conn->conn-> join($chan, @_);469 $conn->conn->send_msg(join => $chan, @_); 504 470 return; 505 471 } … … 510 476 my $chan = shift; 511 477 $channels{$chan} = [grep {$_ ne $conn} @{$channels{$chan} || []}]; 512 $conn->conn-> part($chan);478 $conn->conn->send_msg(part => $chan); 513 479 return; 514 480 } … … 518 484 my $conn = shift; 519 485 my $nick = shift or die("Usage: $cmd <new nick>\n"); 520 $conn->conn-> nick($nick);486 $conn->conn->send_msg(nick => $nick); 521 487 return; 522 488 } … … 527 493 my $chan = shift; 528 494 $conn->names_tmp([]); 529 $conn->conn-> names($chan);495 $conn->conn->send_msg(names => $chan); 530 496 return; 531 497 } … … 535 501 my $conn = shift; 536 502 my $who = shift || die("Usage: $cmd <user>\n"); 537 $conn->conn-> whois($who);503 $conn->conn->send_msg(whois => $who); 538 504 return; 539 505 } … … 542 508 my $cmd = shift; 543 509 my $conn = shift; 544 $conn->conn-> motd;510 $conn->conn->send_msg('motd'); 545 511 return; 546 512 } … … 560 526 my $conn = shift; 561 527 my $who = shift || die("Usage: $cmd <user>\n"); 562 BarnOwl::error("WHO $cmd $conn $who"); 563 $conn->conn->who($who); 528 $conn->conn->send_msg(who => $who); 564 529 return; 565 530 } … … 569 534 my $conn = shift; 570 535 my $type = shift || die("Usage: $cmd <chiklmouy> [server] \n"); 571 $conn->conn->s tats($type, @_);536 $conn->conn->send_msg(stats => $type, @_); 572 537 return; 573 538 } … … 577 542 my $conn = shift; 578 543 my $chan = shift; 579 $conn->conn-> topic($chan, @_ ? join(" ", @_) : undef);544 $conn->conn->send_msg(topic => $chan, @_ ? join(" ", @_) : undef); 580 545 return; 581 546 } … … 584 549 my $cmd = shift; 585 550 my $conn = shift; 586 $conn->conn->s l(join(" ", @_));551 $conn->conn->send_msg(@_); 587 552 return; 588 553 } -
perl/modules/IRC/lib/BarnOwl/Module/IRC/Connection.pm
rfb6e8e3 r8ba9313 16 16 =cut 17 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); 18 use AnyEvent::IRC::Client; 19 use AnyEvent::IRC::Util qw(split_prefix prefix_nick); 20 21 use base qw(Class::Accessor); 22 use Exporter 'import'; 23 __PACKAGE__->mk_accessors(qw(conn alias motd names_tmp whois_tmp nick server autoconnect_channels)); 24 our @EXPORT_OK = qw(is_private); 23 25 24 26 use BarnOwl; 25 27 use Scalar::Util qw(weaken); 26 28 27 BEGIN {28 no strict 'refs';29 my @delegate = qw(nick server);30 for my $meth (@delegate) {31 *{"BarnOwl::Module::IRC::Connection::$meth"} = sub {32 shift->conn->$meth(@_);33 }34 }35 };36 37 29 sub new { 38 30 my $class = shift; 39 my $irc = shift;40 31 my $alias = shift; 41 my %args = (@_); 42 my $conn = Net::IRC::Connection->new($irc, %args); 32 my $host = shift; 33 my $port = shift; 34 my $args = shift; 35 my $nick = $args->{nick}; 36 my $conn = AnyEvent::IRC::Client->new(); 37 if(delete $args->{SSL}) { 38 $conn->enable_ssl; 39 } 40 $conn->connect($host, $port, $args); 43 41 my $self = bless({}, $class); 44 42 $self->conn($conn); 43 $self->autoconnect_channels([]); 45 44 $self->alias($alias); 46 $self->channels([]); 45 $self->server($host); 46 $self->nick($nick); 47 47 $self->motd(""); 48 48 $self->names_tmp(0); 49 49 $self->whois_tmp(""); 50 50 51 $self->conn->add_default_handler(sub { shift; $self->on_event(@_) }); 52 $self->conn->add_handler(['msg', 'notice', 'public', 'caction'], 53 sub { shift; $self->on_msg(@_) }); 54 $self->conn->add_handler(['welcome', 'yourhost', 'created', 55 'luserclient', 'luserop', 'luserchannels', 'luserme', 56 'error'], 57 sub { shift; $self->on_admin_msg(@_) }); 58 $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global', 59 'luserconns'], 60 sub { }); 61 $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) }); 62 $self->conn->add_handler(motd => sub { shift; $self->on_motd(@_) }); 63 $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) }); 64 $self->conn->add_handler(join => sub { shift; $self->on_join(@_) }); 65 $self->conn->add_handler(part => sub { shift; $self->on_part(@_) }); 66 $self->conn->add_handler(quit => sub { shift; $self->on_quit(@_) }); 67 $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) }); 68 $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) }); 69 $self->conn->add_handler(cping => sub { shift; $self->on_ping(@_) }); 70 $self->conn->add_handler(topic => sub { shift; $self->on_topic(@_) }); 71 $self->conn->add_handler(topicinfo => sub { shift; $self->on_topicinfo(@_) }); 72 $self->conn->add_handler(namreply => sub { shift; $self->on_namreply(@_) }); 73 $self->conn->add_handler(endofnames=> sub { shift; $self->on_endofnames(@_) }); 74 $self->conn->add_handler(endofwhois=> sub { shift; $self->on_endofwhois(@_) }); 75 $self->conn->add_handler(mode => sub { shift; $self->on_mode(@_) }); 76 $self->conn->add_handler(nosuchchannel => sub { shift; $self->on_nosuchchannel(@_) }); 51 # $self->conn->add_default_handler(sub { shift; $self->on_event(@_) }); 52 $self->conn->reg_cb(registered => sub { shift; $self->connected("Connected to $alias as $nick") }, 53 connfail => sub { shift; BarnOwl::error("Connection to $host failed!") }, 54 disconnect => sub { shift; $self->on_disconnect(@_) }, 55 publicmsg => sub { shift; $self->on_msg(@_) }, 56 privatemsg => sub { shift; $self->on_msg(@_) }); 57 for my $m (qw(welcome yourhost created 58 luserclient luserop luserchannels luserme 59 error)) { 60 $self->conn->reg_cb("irc_$m" => sub { shift; $self->on_admin_msg(@_) }); 61 } 62 $self->conn->reg_cb(irc_375 => sub { shift; $self->on_motdstart(@_) }, 63 irc_372 => sub { shift; $self->on_motd(@_) }, 64 irc_376 => sub { shift; $self->on_endofmotd(@_) }, 65 irc_join => sub { shift; $self->on_join(@_) }, 66 irc_part => sub { shift; $self->on_part(@_) }, 67 irc_quit => sub { shift; $self->on_quit(@_) }, 68 irc_433 => sub { shift; $self->on_nickinuse(@_) }, 69 channel_topic => sub { shift; $self->on_topic(@_) }, 70 irc_333 => sub { shift; $self->on_topicinfo(@_) }, 71 irc_353 => sub { shift; $self->on_namreply(@_) }, 72 irc_366 => sub { shift; $self->on_endofnames(@_) }, 73 irc_311 => sub { shift; $self->on_whois(@_) }, 74 irc_312 => sub { shift; $self->on_whois(@_) }, 75 irc_319 => sub { shift; $self->on_whois(@_) }, 76 irc_320 => sub { shift; $self->on_whois(@_) }, 77 irc_318 => sub { shift; $self->on_endofwhois(@_) }, 78 irc_mode => sub { shift; $self->on_mode(@_) }, 79 irc_401 => sub { shift; $self->on_nosuch(@_) }, 80 irc_402 => sub { shift; $self->on_nosuch(@_) }, 81 irc_403 => sub { shift; $self->on_nosuch(@_) }, 82 'irc_*' => sub { BarnOwl::debug("IRC: " . $_[1]->{command}) }); 77 83 78 84 return $self; … … 92 98 my $self = shift; 93 99 my $evt = shift; 100 my ($nick, $user, $host) = split_prefix($evt); 94 101 return BarnOwl::Message->new( 95 102 type => 'IRC', 96 103 server => $self->server, 97 104 network => $self->alias, 98 sender => $ evt->nick,99 hostname => $evt->host,100 from => $evt-> from,105 sender => $nick, 106 defined($host) ? (hostname => $host) : (), 107 from => $evt->{prefix}, 101 108 @_ 102 109 ); … … 104 111 105 112 sub on_msg { 106 my ($self, $evt) = @_; 107 my ($recipient) = $evt->to; 108 my $body = strip_irc_formatting([$evt->args]->[0]); 109 my $nick = $self->nick; 110 $body = '* '.$evt->nick.' '.$body if $evt->type eq 'caction'; 113 my ($self, $recipient, $evt) = @_; 114 my $body = strip_irc_formatting($evt->{params}->[1]); 115 111 116 my $msg = $self->new_message($evt, 112 117 direction => 'in', 113 118 recipient => $recipient, 114 body => $body,115 $evt-> typeeq 'notice' ?119 body => $body, 120 $evt->{command} eq 'notice' ? 116 121 (notice => 'true') : (), 117 122 is_private($recipient) ? 118 123 (private => 'true') : (channel => $recipient), 119 124 replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, 120 (is_private($recipient) ? $evt->nick: $recipient)),121 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),125 (is_private($recipient) ? prefix_nick($evt) : $recipient)), 126 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, prefix_nick($evt)), 122 127 ); 123 128 … … 125 130 } 126 131 127 sub on_ping {128 my ($self, $evt) = @_;129 $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args)));130 }131 132 132 sub on_admin_msg { 133 133 my ($self, $evt) = @_; 134 return if BarnOwl::Module::IRC->skip_msg($evt-> type);135 BarnOwl::admin_message("IRC", 136 BarnOwl::Style::boldify('IRC ' . $evt-> type. ' message from '134 return if BarnOwl::Module::IRC->skip_msg($evt->{command}); 135 BarnOwl::admin_message("IRC", 136 BarnOwl::Style::boldify('IRC ' . $evt->{command} . ' message from ' 137 137 . $self->alias) . "\n" 138 . strip_irc_formatting(join ' ', cdr($evt-> args)));138 . strip_irc_formatting(join ' ', cdr($evt->{params}))); 139 139 } 140 140 141 141 sub on_motdstart { 142 142 my ($self, $evt) = @_; 143 $self->motd(join "\n", cdr( $evt->args));143 $self->motd(join "\n", cdr(@{$evt->{params}})); 144 144 } 145 145 146 146 sub on_motd { 147 147 my ($self, $evt) = @_; 148 $self->motd(join "\n", $self->motd, cdr( $evt->args));148 $self->motd(join "\n", $self->motd, cdr(@{$evt->{params}})); 149 149 } 150 150 151 151 sub on_endofmotd { 152 152 my ($self, $evt) = @_; 153 $self->motd(join "\n", $self->motd, cdr( $evt->args));153 $self->motd(join "\n", $self->motd, cdr(@{$evt->{params}})); 154 154 BarnOwl::admin_message("IRC", 155 155 BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" … … 159 159 sub on_join { 160 160 my ($self, $evt) = @_; 161 my $chan = $evt->{params}[0]; 161 162 my $msg = $self->new_message($evt, 162 163 loginout => 'login', 163 164 action => 'join', 164 channel => $ evt->to,165 replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $ evt->to),166 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),165 channel => $chan, 166 replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $chan), 167 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, prefix_nick($evt)), 167 168 ); 168 169 BarnOwl::queue_message($msg); 169 push @{$self->channels}, $evt->to;170 170 } 171 171 172 172 sub on_part { 173 173 my ($self, $evt) = @_; 174 my $chan = $evt->{params}[0]; 174 175 my $msg = $self->new_message($evt, 175 176 loginout => 'logout', 176 177 action => 'part', 177 channel => $ evt->to,178 replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $ evt->to),179 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),178 channel => $chan, 179 replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $chan), 180 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, prefix_nick($evt)), 180 181 ); 181 182 BarnOwl::queue_message($msg); 182 $self->channels([ grep {$_ ne $evt->to} @{$self->channels}]);183 183 } 184 184 … … 188 188 loginout => 'logout', 189 189 action => 'quit', 190 from => $evt-> to,191 reason => [$evt->args]->[0],192 replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),193 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick),190 from => $evt->{prefix}, 191 reason => $evt->{params}->[1], 192 replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, prefix_nick($evt)), 193 replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, prefix_nick($evt)), 194 194 ); 195 195 BarnOwl::queue_message($msg); … … 207 207 } 208 208 } 209 BarnOwl::remove_io_dispatch($self->{FD});210 209 $self->motd(""); 211 210 } 212 211 213 212 sub on_disconnect { 214 my ($self, $ evt) = @_;213 my ($self, $why) = @_; 215 214 $self->disconnect; 216 215 BarnOwl::admin_message('IRC', 217 216 "[" . $self->alias . "] Disconnected from server"); 218 if ($ evt->format and $evt->format eq "error") {217 if ($why && $why =~ m{error in connection}) { 219 218 $self->schedule_reconnect; 220 } else {221 $self->channels([]);222 219 } 223 220 } … … 227 224 BarnOwl::admin_message("IRC", 228 225 "[" . $self->alias . "] " . 229 [$evt->args]->[1] . ": Nick already in use"); 230 $self->disconnect unless $self->motd; 226 $evt->{params}->[1] . ": Nick already in use"); 231 227 } 232 228 233 229 sub on_topic { 234 my ($self, $evt) = @_; 235 my @args = $evt->args; 236 if (scalar @args > 1) { 230 my ($self, $channel, $topic, $who) = @_; 231 if ($channel) { 237 232 BarnOwl::admin_message("IRC", 238 "Topic for $ args[1] on " . $self->alias . " is $args[2]");233 "Topic for $channel on " . $self->alias . " is $topic"); 239 234 } else { 240 235 BarnOwl::admin_message("IRC", 241 "Topic changed to $ args[0]");236 "Topic changed to $channel"); 242 237 } 243 238 } … … 245 240 sub on_topicinfo { 246 241 my ($self, $evt) = @_; 247 my @args = $evt->args;242 my @args = @{$evt->{params}}; 248 243 BarnOwl::admin_message("IRC", 249 244 "Topic for $args[1] set by $args[2] at " . localtime($args[3])); … … 257 252 my ($self, $evt) = @_; 258 253 return unless $self->names_tmp; 259 $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]);254 $self->names_tmp([@{$self->names_tmp}, split(' ', $evt->{params}[3])]); 260 255 } 261 256 … … 272 267 my ($self, $evt) = @_; 273 268 return unless $self->names_tmp; 274 my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n");269 my $names = BarnOwl::Style::boldify("Members of " . $evt->{params}->[1] . ":\n"); 275 270 for my $name (sort {cmp_user($a, $b)} @{$self->names_tmp}) { 276 271 $names .= " $name\n"; … … 282 277 sub on_whois { 283 278 my ($self, $evt) = @_; 279 my %names = ( 280 311 => 'user', 281 312 => 'server', 282 319 => 'channels', 283 330 => 'whowas', 284 ); 284 285 $self->whois_tmp( 285 $self->whois_tmp . "\n" . $evt->type. ":\n " .286 join("\n ", cdr(cdr($evt->args))) . "\n"287 );286 $self->whois_tmp . "\n" . $names{$evt->{command}} . ":\n " . 287 join("\n ", cdr(cdr(@{$evt->{params}}))) . "\n" 288 ); 288 289 } 289 290 … … 291 292 my ($self, $evt) = @_; 292 293 BarnOwl::popless_ztext( 293 BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") .294 BarnOwl::Style::boldify("/whois for " . $evt->{params}->[1] . ":\n") . 294 295 $self->whois_tmp 295 296 ); … … 300 301 my ($self, $evt) = @_; 301 302 BarnOwl::admin_message("IRC", 302 "[" . $self->alias . "] User " . ( $evt->nick) . + " set mode " .303 join(" ", $evt->args) . "on " . $evt->to->[0]303 "[" . $self->alias . "] User " . (prefix_nick($evt)) . + " set mode " . 304 join(" ", cdr(@{$evt->{params}})) . "on " . $evt->{params}->[0] 304 305 ); 305 306 } 306 307 307 sub on_nosuchchannel { 308 my ($self, $evt) = @_; 308 sub on_nosuch { 309 my ($self, $evt) = @_; 310 my %things = (401 => 'nick', 402 => 'server', 403 => 'channel'); 309 311 BarnOwl::admin_message("IRC", 310 312 "[" . $self->alias . "] " . 311 "No such channel: " . [$evt->args]->[1])313 "No such @{[$things{$evt->{command}}]}: @{[$evt->{params}->[1]]}") 312 314 } 313 315 … … 356 358 $self->cancel_reconnect; 357 359 $BarnOwl::Module::IRC::ircnets{$self->alias} = $self; 358 my $fd = $self->getSocket()->fileno(); 359 BarnOwl::add_io_dispatch($fd, 'r', \&BarnOwl::Module::IRC::OwlProcess); 360 $self->{FD} = $fd; 360 if ($self->autoconnect_channels) { 361 for my $c (@{$self->autoconnect_channels}) { 362 $self->conn->send_msg(join => $c); 363 } 364 $self->autoconnect_channels([]); 365 } 366 $self->conn->enable_ping(60, sub { 367 $self->disconnect("Connection timed out."); 368 $self->schedule_reconnect; 369 }); 361 370 } 362 371 … … 365 374 my $backoff = shift; 366 375 376 $self->autoconnect_channels([keys(%{$self->channel_list})]); 367 377 $self->conn->connect; 368 378 if ($self->conn->connected) { 369 379 $self->connected("Reconnected to ".$self->alias); 370 my @channels = @{$self->channels};371 $self->channels([]);372 $self->conn->join($_) for @channels;373 380 return; 374 381 }
Note: See TracChangeset
for help on using the changeset viewer.