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