| 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)); |
|---|
| 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 | |
|---|
| 48 | $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) }); |
|---|
| 49 | $self->conn->add_default_handler(sub { shift; $self->on_event(@_) }); |
|---|
| 50 | $self->conn->add_handler(['msg', 'notice', 'public', 'caction'], |
|---|
| 51 | sub { shift; $self->on_msg(@_) }); |
|---|
| 52 | $self->conn->add_handler(['welcome', 'yourhost', 'created', |
|---|
| 53 | 'luserclient', 'luserop', 'luserchannels', 'luserme', |
|---|
| 54 | 'error'], |
|---|
| 55 | sub { shift; $self->on_admin_msg(@_) }); |
|---|
| 56 | $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global', |
|---|
| 57 | 'luserconns'], |
|---|
| 58 | sub { }); |
|---|
| 59 | $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) }); |
|---|
| 60 | $self->conn->add_handler(motd => sub { shift; $self->on_motd(@_) }); |
|---|
| 61 | $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) }); |
|---|
| 62 | $self->conn->add_handler(join => sub { shift; $self->on_join(@_) }); |
|---|
| 63 | $self->conn->add_handler(part => sub { shift; $self->on_part(@_) }); |
|---|
| 64 | $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) }); |
|---|
| 65 | $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) }); |
|---|
| 66 | $self->conn->add_handler(cping => sub { shift; $self->on_ping(@_) }); |
|---|
| 67 | |
|---|
| 68 | return $self; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | sub getSocket |
|---|
| 72 | { |
|---|
| 73 | my $self = shift; |
|---|
| 74 | return $self->conn->socket; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | ################################################################################ |
|---|
| 78 | ############################### IRC callbacks ################################## |
|---|
| 79 | ################################################################################ |
|---|
| 80 | |
|---|
| 81 | sub new_message { |
|---|
| 82 | my $self = shift; |
|---|
| 83 | my $evt = shift; |
|---|
| 84 | return BarnOwl::Message->new( |
|---|
| 85 | type => 'IRC', |
|---|
| 86 | server => $self->server, |
|---|
| 87 | network => $self->alias, |
|---|
| 88 | sender => $evt->nick, |
|---|
| 89 | hostname => $evt->host, |
|---|
| 90 | from => $evt->from, |
|---|
| 91 | @_ |
|---|
| 92 | ); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | sub on_msg { |
|---|
| 96 | my ($self, $evt) = @_; |
|---|
| 97 | my ($recipient) = $evt->to; |
|---|
| 98 | my $body = strip_irc_formatting([$evt->args]->[0]); |
|---|
| 99 | my $nick = $self->nick; |
|---|
| 100 | BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/; |
|---|
| 101 | $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction'; |
|---|
| 102 | my $msg = $self->new_message($evt, |
|---|
| 103 | direction => 'in', |
|---|
| 104 | recipient => $recipient, |
|---|
| 105 | body => $body, |
|---|
| 106 | $evt->type eq 'notice' ? |
|---|
| 107 | (notice => 'true') : (), |
|---|
| 108 | is_private($recipient) ? |
|---|
| 109 | (isprivate => 'true') : (channel => $recipient), |
|---|
| 110 | replycmd => 'irc-msg -a ' . $self->alias . ' ' . |
|---|
| 111 | (is_private($recipient) ? $evt->nick : $recipient), |
|---|
| 112 | replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
|---|
| 113 | ); |
|---|
| 114 | |
|---|
| 115 | BarnOwl::queue_message($msg); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | sub on_ping { |
|---|
| 119 | my ($self, $evt) = @_; |
|---|
| 120 | $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | sub on_admin_msg { |
|---|
| 124 | my ($self, $evt) = @_; |
|---|
| 125 | BarnOwl::admin_message("IRC", |
|---|
| 126 | BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from ' |
|---|
| 127 | . $self->alias) . "\n" |
|---|
| 128 | . strip_irc_formatting(join '\n', $evt->args)); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | sub on_motdstart { |
|---|
| 132 | my ($self, $evt) = @_; |
|---|
| 133 | $self->motd(join "\n", cdr($evt->args)); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | sub on_motd { |
|---|
| 137 | my ($self, $evt) = @_; |
|---|
| 138 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | sub on_endofmotd { |
|---|
| 142 | my ($self, $evt) = @_; |
|---|
| 143 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
|---|
| 144 | if(!$self->connected) { |
|---|
| 145 | BarnOwl::admin_message("IRC", "Connected to " . |
|---|
| 146 | $self->server . " (" . $self->alias . ")"); |
|---|
| 147 | $self->connected(1); |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | BarnOwl::admin_message("IRC", |
|---|
| 151 | BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" |
|---|
| 152 | . strip_irc_formatting($self->motd)); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | sub on_join { |
|---|
| 156 | my ($self, $evt) = @_; |
|---|
| 157 | my $msg = $self->new_message($evt, |
|---|
| 158 | loginout => 'login', |
|---|
| 159 | channel => $evt->to, |
|---|
| 160 | replycmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
|---|
| 161 | replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
|---|
| 162 | ); |
|---|
| 163 | BarnOwl::queue_message($msg); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | sub on_part { |
|---|
| 167 | my ($self, $evt) = @_; |
|---|
| 168 | my $msg = $self->new_message($evt, |
|---|
| 169 | loginout => 'logout', |
|---|
| 170 | channel => $evt->to, |
|---|
| 171 | replycmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
|---|
| 172 | replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
|---|
| 173 | ); |
|---|
| 174 | BarnOwl::queue_message($msg); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | sub on_disconnect { |
|---|
| 178 | my $self = shift; |
|---|
| 179 | delete $BarnOwl::Module::IRC::ircnets{$self->alias}; |
|---|
| 180 | BarnOwl::remove_dispatch($self->{FD}); |
|---|
| 181 | BarnOwl::admin_message('IRC', |
|---|
| 182 | "[" . $self->alias . "] Disconnected from server"); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | sub on_nickinuse { |
|---|
| 186 | my ($self, $evt) = @_; |
|---|
| 187 | BarnOwl::admin_message("IRC", |
|---|
| 188 | "[" . $self->alias . "] " . |
|---|
| 189 | [$evt->args]->[1] . ": Nick already in use"); |
|---|
| 190 | unless($self->connected) { |
|---|
| 191 | $self->conn->disconnect; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | sub on_event { |
|---|
| 196 | my ($self, $evt) = @_; |
|---|
| 197 | BarnOwl::admin_message("IRC", |
|---|
| 198 | "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n" |
|---|
| 199 | . strip_irc_formatting(join("\n", $evt->args))) |
|---|
| 200 | if BarnOwl::getvar('irc:spew') eq 'on'; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | ################################################################################ |
|---|
| 205 | ########################### Utilities/Helpers ################################## |
|---|
| 206 | ################################################################################ |
|---|
| 207 | |
|---|
| 208 | sub strip_irc_formatting { |
|---|
| 209 | my $body = shift; |
|---|
| 210 | # Strip mIRC colors. If someone wants to write code to convert |
|---|
| 211 | # these to zephyr colors, be my guest. |
|---|
| 212 | $body =~ s/\cC\d+(?:,\d+)?//g; |
|---|
| 213 | $body =~ s/\cO//g; |
|---|
| 214 | |
|---|
| 215 | my @pieces = split /\cB/, $body; |
|---|
| 216 | my $out; |
|---|
| 217 | while(@pieces) { |
|---|
| 218 | $out .= shift @pieces; |
|---|
| 219 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
|---|
| 220 | } |
|---|
| 221 | return $out; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | # Determines if the given message recipient is a username, as opposed to |
|---|
| 225 | # a channel that starts with # or &. |
|---|
| 226 | sub is_private { |
|---|
| 227 | return shift !~ /^[\#\&]/; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | sub cdr { |
|---|
| 231 | shift; |
|---|
| 232 | return @_; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | 1; |
|---|