| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | |
|---|
| 4 | package BarnOwl::Module::IRC::Connection; |
|---|
| 5 | |
|---|
| 6 | =head1 NAME |
|---|
| 7 | |
|---|
| 8 | BarnOwl::Module::IRC::Connection |
|---|
| 9 | |
|---|
| 10 | =head1 DESCRIPTION |
|---|
| 11 | |
|---|
| 12 | This module is a wrapper around Net::IRC::Connection for BarnOwl's IRC |
|---|
| 13 | support |
|---|
| 14 | |
|---|
| 15 | =cut |
|---|
| 16 | |
|---|
| 17 | use Net::IRC::Connection; |
|---|
| 18 | |
|---|
| 19 | use base qw(Class::Accessor Exporter); |
|---|
| 20 | __PACKAGE__->mk_accessors(qw(conn alias channels connected motd names_tmp whois_tmp)); |
|---|
| 21 | our @EXPORT_OK = qw(&is_private); |
|---|
| 22 | |
|---|
| 23 | use BarnOwl; |
|---|
| 24 | |
|---|
| 25 | BEGIN { |
|---|
| 26 | no strict 'refs'; |
|---|
| 27 | my @delegate = qw(nick server); |
|---|
| 28 | for my $meth (@delegate) { |
|---|
| 29 | *{"BarnOwl::Module::IRC::Connection::$meth"} = sub { |
|---|
| 30 | shift->conn->$meth(@_); |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | sub new { |
|---|
| 36 | my $class = shift; |
|---|
| 37 | my $irc = shift; |
|---|
| 38 | my $alias = shift; |
|---|
| 39 | my %args = (@_); |
|---|
| 40 | my $conn = Net::IRC::Connection->new($irc, %args); |
|---|
| 41 | my $self = bless({}, $class); |
|---|
| 42 | $self->conn($conn); |
|---|
| 43 | $self->alias($alias); |
|---|
| 44 | $self->channels([]); |
|---|
| 45 | $self->motd(""); |
|---|
| 46 | $self->connected(0); |
|---|
| 47 | $self->names_tmp(0); |
|---|
| 48 | $self->whois_tmp(""); |
|---|
| 49 | |
|---|
| 50 | $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) }); |
|---|
| 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(@_) }); |
|---|
| 77 | |
|---|
| 78 | return $self; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | sub getSocket |
|---|
| 82 | { |
|---|
| 83 | my $self = shift; |
|---|
| 84 | return $self->conn->socket; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | ################################################################################ |
|---|
| 88 | ############################### IRC callbacks ################################## |
|---|
| 89 | ################################################################################ |
|---|
| 90 | |
|---|
| 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 | |
|---|
| 105 | 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'; |
|---|
| 111 | my $msg = $self->new_message($evt, |
|---|
| 112 | direction => 'in', |
|---|
| 113 | recipient => $recipient, |
|---|
| 114 | body => $body, |
|---|
| 115 | $evt->type eq 'notice' ? |
|---|
| 116 | (notice => 'true') : (), |
|---|
| 117 | is_private($recipient) ? |
|---|
| 118 | (isprivate => 'true') : (channel => $recipient), |
|---|
| 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), |
|---|
| 122 | ); |
|---|
| 123 | |
|---|
| 124 | BarnOwl::queue_message($msg); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | sub on_ping { |
|---|
| 128 | my ($self, $evt) = @_; |
|---|
| 129 | $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | sub on_admin_msg { |
|---|
| 133 | my ($self, $evt) = @_; |
|---|
| 134 | BarnOwl::admin_message("IRC", |
|---|
| 135 | BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from ' |
|---|
| 136 | . $self->alias) . "\n" |
|---|
| 137 | . strip_irc_formatting(join ' ', cdr($evt->args))); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | sub on_motdstart { |
|---|
| 141 | my ($self, $evt) = @_; |
|---|
| 142 | $self->motd(join "\n", cdr($evt->args)); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | sub on_motd { |
|---|
| 146 | my ($self, $evt) = @_; |
|---|
| 147 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | sub on_endofmotd { |
|---|
| 151 | my ($self, $evt) = @_; |
|---|
| 152 | $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 | BarnOwl::admin_message("IRC", |
|---|
| 160 | BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" |
|---|
| 161 | . strip_irc_formatting($self->motd)); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | sub on_join { |
|---|
| 165 | my ($self, $evt) = @_; |
|---|
| 166 | my $msg = $self->new_message($evt, |
|---|
| 167 | loginout => 'login', |
|---|
| 168 | action => 'join', |
|---|
| 169 | channel => $evt->to, |
|---|
| 170 | replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to), |
|---|
| 171 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
|---|
| 172 | ); |
|---|
| 173 | BarnOwl::queue_message($msg); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | sub on_part { |
|---|
| 177 | my ($self, $evt) = @_; |
|---|
| 178 | my $msg = $self->new_message($evt, |
|---|
| 179 | loginout => 'logout', |
|---|
| 180 | action => 'part', |
|---|
| 181 | channel => $evt->to, |
|---|
| 182 | replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->to), |
|---|
| 183 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
|---|
| 184 | ); |
|---|
| 185 | BarnOwl::queue_message($msg); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | sub on_quit { |
|---|
| 189 | my ($self, $evt) = @_; |
|---|
| 190 | my $msg = $self->new_message($evt, |
|---|
| 191 | loginout => 'logout', |
|---|
| 192 | action => 'quit', |
|---|
| 193 | from => $evt->to, |
|---|
| 194 | reason => [$evt->args]->[0], |
|---|
| 195 | replycmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
|---|
| 196 | replysendercmd => BarnOwl::quote('irc-msg', '-a', $self->alias, $evt->nick), |
|---|
| 197 | ); |
|---|
| 198 | BarnOwl::queue_message($msg); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | sub on_disconnect { |
|---|
| 202 | my $self = shift; |
|---|
| 203 | delete $BarnOwl::Module::IRC::ircnets{$self->alias}; |
|---|
| 204 | for my $k (keys %BarnOwl::Module::IRC::channels) { |
|---|
| 205 | my @conns = grep {$_ ne $self} @{$BarnOwl::Module::IRC::channels{$k}}; |
|---|
| 206 | if(@conns) { |
|---|
| 207 | $BarnOwl::Module::IRC::channels{$k} = \@conns; |
|---|
| 208 | } else { |
|---|
| 209 | delete $BarnOwl::Module::IRC::channels{$k}; |
|---|
| 210 | } |
|---|
| 211 | } |
|---|
| 212 | BarnOwl::remove_io_dispatch($self->{FD}); |
|---|
| 213 | BarnOwl::admin_message('IRC', |
|---|
| 214 | "[" . $self->alias . "] Disconnected from server"); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | sub on_nickinuse { |
|---|
| 218 | my ($self, $evt) = @_; |
|---|
| 219 | BarnOwl::admin_message("IRC", |
|---|
| 220 | "[" . $self->alias . "] " . |
|---|
| 221 | [$evt->args]->[1] . ": Nick already in use"); |
|---|
| 222 | unless($self->connected) { |
|---|
| 223 | $self->conn->disconnect; |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | sub on_topic { |
|---|
| 228 | my ($self, $evt) = @_; |
|---|
| 229 | my @args = $evt->args; |
|---|
| 230 | if (scalar @args > 1) { |
|---|
| 231 | BarnOwl::admin_message("IRC", |
|---|
| 232 | "Topic for $args[1] on " . $self->alias . " is $args[2]"); |
|---|
| 233 | } else { |
|---|
| 234 | BarnOwl::admin_message("IRC", |
|---|
| 235 | "Topic changed to $args[0]"); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | sub on_topicinfo { |
|---|
| 240 | my ($self, $evt) = @_; |
|---|
| 241 | my @args = $evt->args; |
|---|
| 242 | BarnOwl::admin_message("IRC", |
|---|
| 243 | "Topic for $args[1] set by $args[2] at " . localtime($args[3])); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | # IRC gives us a bunch of namreply messages, followed by an endofnames. |
|---|
| 247 | # We need to collect them from the namreply and wait for the endofnames message. |
|---|
| 248 | # After this happens, the names_tmp variable is cleared. |
|---|
| 249 | |
|---|
| 250 | sub on_namreply { |
|---|
| 251 | my ($self, $evt) = @_; |
|---|
| 252 | return unless $self->names_tmp; |
|---|
| 253 | $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | sub on_endofnames { |
|---|
| 257 | my ($self, $evt) = @_; |
|---|
| 258 | return unless $self->names_tmp; |
|---|
| 259 | my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n"); |
|---|
| 260 | for my $name (@{$self->names_tmp}) { |
|---|
| 261 | $names .= " $name\n"; |
|---|
| 262 | } |
|---|
| 263 | BarnOwl::popless_ztext($names); |
|---|
| 264 | $self->names_tmp(0); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | sub on_whois { |
|---|
| 268 | my ($self, $evt) = @_; |
|---|
| 269 | $self->whois_tmp( |
|---|
| 270 | $self->whois_tmp . "\n" . $evt->type . ":\n " . |
|---|
| 271 | join("\n ", cdr(cdr($evt->args))) . "\n" |
|---|
| 272 | ); |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | sub on_endofwhois { |
|---|
| 276 | my ($self, $evt) = @_; |
|---|
| 277 | BarnOwl::popless_ztext( |
|---|
| 278 | BarnOwl::Style::boldify("/whois for " . [$evt->args]->[1] . ":\n") . |
|---|
| 279 | $self->whois_tmp |
|---|
| 280 | ); |
|---|
| 281 | $self->whois_tmp(''); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | sub on_mode { |
|---|
| 285 | my ($self, $evt) = @_; |
|---|
| 286 | BarnOwl::admin_message("IRC", |
|---|
| 287 | "[" . $self->alias . "] User " . ($evt->nick) . + " set mode " . |
|---|
| 288 | join(" ", $evt->args) . "on " . $evt->to->[0] |
|---|
| 289 | ); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | sub on_nosuchchannel { |
|---|
| 293 | my ($self, $evt) = @_; |
|---|
| 294 | BarnOwl::admin_message("IRC", |
|---|
| 295 | "[" . $self->alias . "] " . |
|---|
| 296 | "No such channel: " . [$evt->args]->[1]) |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | sub on_event { |
|---|
| 300 | my ($self, $evt) = @_; |
|---|
| 301 | return on_whois(@_) if ($evt->type =~ /^whois/); |
|---|
| 302 | BarnOwl::admin_message("IRC", |
|---|
| 303 | "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n" |
|---|
| 304 | . strip_irc_formatting(join("\n", $evt->args))) |
|---|
| 305 | if BarnOwl::getvar('irc:spew') eq 'on'; |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | ################################################################################ |
|---|
| 309 | ########################### Utilities/Helpers ################################## |
|---|
| 310 | ################################################################################ |
|---|
| 311 | |
|---|
| 312 | sub strip_irc_formatting { |
|---|
| 313 | my $body = shift; |
|---|
| 314 | # Strip mIRC colors. If someone wants to write code to convert |
|---|
| 315 | # these to zephyr colors, be my guest. |
|---|
| 316 | $body =~ s/\cC\d+(?:,\d+)?//g; |
|---|
| 317 | $body =~ s/\cO//g; |
|---|
| 318 | |
|---|
| 319 | my @pieces = split /\cB/, $body; |
|---|
| 320 | my $out = ''; |
|---|
| 321 | while(@pieces) { |
|---|
| 322 | $out .= shift @pieces; |
|---|
| 323 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
|---|
| 324 | } |
|---|
| 325 | return $out; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | # Determines if the given message recipient is a username, as opposed to |
|---|
| 329 | # a channel that starts with # or &. |
|---|
| 330 | sub is_private { |
|---|
| 331 | return shift !~ /^[\#\&]/; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | sub cdr { |
|---|
| 335 | shift; |
|---|
| 336 | return @_; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | 1; |
|---|