[b38b0b2] | 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 | |
---|
[ba2ca66] | 12 | This module is a wrapper around Net::IRC::Connection for BarnOwl's IRC |
---|
[b38b0b2] | 13 | support |
---|
| 14 | |
---|
| 15 | =cut |
---|
| 16 | |
---|
[ba2ca66] | 17 | use Net::IRC::Connection; |
---|
| 18 | |
---|
| 19 | use base qw(Class::Accessor Exporter); |
---|
| 20 | __PACKAGE__->mk_accessors(qw(conn alias channels connected motd)); |
---|
[2c40dc0] | 21 | our @EXPORT_OK = qw(&is_private); |
---|
[b38b0b2] | 22 | |
---|
| 23 | use BarnOwl; |
---|
| 24 | |
---|
[ba2ca66] | 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 | |
---|
[b38b0b2] | 35 | sub new { |
---|
| 36 | my $class = shift; |
---|
| 37 | my $irc = shift; |
---|
| 38 | my $alias = shift; |
---|
| 39 | my %args = (@_); |
---|
[ba2ca66] | 40 | my $conn = Net::IRC::Connection->new($irc, %args); |
---|
| 41 | my $self = bless({}, $class); |
---|
| 42 | $self->conn($conn); |
---|
[b38b0b2] | 43 | $self->alias($alias); |
---|
| 44 | $self->channels([]); |
---|
[ba2ca66] | 45 | $self->motd(""); |
---|
| 46 | $self->connected(0); |
---|
| 47 | |
---|
[5ff830a] | 48 | $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) }); |
---|
[ba2ca66] | 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', |
---|
[5ff830a] | 53 | 'luserclient', 'luserop', 'luserchannels', 'luserme', |
---|
| 54 | 'notice', 'error'], |
---|
[ba2ca66] | 55 | sub { shift; $self->on_admin_msg(@_) }); |
---|
| 56 | $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global', |
---|
[bc0d7bc] | 57 | 'luserconns'], |
---|
| 58 | sub { }); |
---|
[ba2ca66] | 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(@_) }); |
---|
[b38b0b2] | 67 | |
---|
| 68 | return $self; |
---|
| 69 | } |
---|
| 70 | |
---|
[9c7a701] | 71 | sub getSocket |
---|
| 72 | { |
---|
| 73 | my $self = shift; |
---|
| 74 | return $self->conn->socket; |
---|
| 75 | } |
---|
| 76 | |
---|
[b38b0b2] | 77 | ################################################################################ |
---|
| 78 | ############################### IRC callbacks ################################## |
---|
| 79 | ################################################################################ |
---|
| 80 | |
---|
[47b6a5f] | 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 | |
---|
[b38b0b2] | 95 | sub on_msg { |
---|
| 96 | my ($self, $evt) = @_; |
---|
[2c40dc0] | 97 | my ($recipient) = $evt->to; |
---|
| 98 | my $body = strip_irc_formatting([$evt->args]->[0]); |
---|
[bc0d7bc] | 99 | my $nick = $self->nick; |
---|
| 100 | BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/; |
---|
[2c40dc0] | 101 | $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction'; |
---|
[47b6a5f] | 102 | my $msg = $self->new_message($evt, |
---|
[b38b0b2] | 103 | direction => 'in', |
---|
[2c40dc0] | 104 | recipient => $recipient, |
---|
[47b6a5f] | 105 | body => $body, |
---|
[2c40dc0] | 106 | $evt->type eq 'notice' ? |
---|
| 107 | (notice => 'true') : (), |
---|
| 108 | is_private($recipient) ? |
---|
[0e52069] | 109 | (isprivate => 'true') : (channel => $recipient), |
---|
[2c40dc0] | 110 | replycmd => 'irc-msg ' . |
---|
| 111 | (is_private($recipient) ? $evt->nick : $recipient), |
---|
[47b6a5f] | 112 | replysendercmd => 'irc-msg ' . $evt->nick |
---|
[b38b0b2] | 113 | ); |
---|
[47b6a5f] | 114 | |
---|
[b38b0b2] | 115 | BarnOwl::queue_message($msg); |
---|
| 116 | } |
---|
| 117 | |
---|
[2c40dc0] | 118 | sub on_ping { |
---|
| 119 | my ($self, $evt) = @_; |
---|
[ba2ca66] | 120 | $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
---|
[2c40dc0] | 121 | } |
---|
| 122 | |
---|
[bc0d7bc] | 123 | sub on_admin_msg { |
---|
| 124 | my ($self, $evt) = @_; |
---|
| 125 | BarnOwl::admin_message("IRC", |
---|
| 126 | BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from ' |
---|
[1951db8] | 127 | . $self->alias) . "\n" |
---|
[1af21e8] | 128 | . strip_irc_formatting(join '\n', cdr($evt->args))); |
---|
[bc0d7bc] | 129 | } |
---|
| 130 | |
---|
| 131 | sub on_motdstart { |
---|
| 132 | my ($self, $evt) = @_; |
---|
[1af21e8] | 133 | $self->motd(join "\n", cdr($evt->args)); |
---|
[bc0d7bc] | 134 | } |
---|
| 135 | |
---|
| 136 | sub on_motd { |
---|
| 137 | my ($self, $evt) = @_; |
---|
[1af21e8] | 138 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
---|
[bc0d7bc] | 139 | } |
---|
| 140 | |
---|
| 141 | sub on_endofmotd { |
---|
| 142 | my ($self, $evt) = @_; |
---|
[1af21e8] | 143 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
---|
[ba2ca66] | 144 | if(!$self->connected) { |
---|
[3baf77f] | 145 | BarnOwl::admin_message("IRC", "Connected to " . |
---|
| 146 | $self->server . " (" . $self->alias . ")"); |
---|
[ba2ca66] | 147 | $self->connected(1); |
---|
[3baf77f] | 148 | |
---|
| 149 | } |
---|
[bc0d7bc] | 150 | BarnOwl::admin_message("IRC", |
---|
[3baf77f] | 151 | BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" |
---|
[ba2ca66] | 152 | . strip_irc_formatting($self->motd)); |
---|
[bc0d7bc] | 153 | } |
---|
| 154 | |
---|
[47b6a5f] | 155 | sub on_join { |
---|
| 156 | my ($self, $evt) = @_; |
---|
| 157 | my $msg = $self->new_message($evt, |
---|
| 158 | loginout => 'login', |
---|
| 159 | channel => $evt->to, |
---|
| 160 | ); |
---|
| 161 | BarnOwl::queue_message($msg); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | sub on_part { |
---|
| 165 | my ($self, $evt) = @_; |
---|
| 166 | my $msg = $self->new_message($evt, |
---|
| 167 | loginout => 'logout', |
---|
| 168 | channel => $evt->to, |
---|
| 169 | ); |
---|
| 170 | BarnOwl::queue_message($msg); |
---|
| 171 | } |
---|
| 172 | |
---|
[9e02bb7] | 173 | sub on_disconnect { |
---|
| 174 | my $self = shift; |
---|
| 175 | delete $BarnOwl::Module::IRC::ircnets{$self->alias}; |
---|
[9c7a701] | 176 | BarnOwl::remove_dispatch($self->{FD}); |
---|
[9e02bb7] | 177 | BarnOwl::admin_message('IRC', |
---|
| 178 | "[" . $self->alias . "] Disconnected from server"); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | sub on_nickinuse { |
---|
| 182 | my ($self, $evt) = @_; |
---|
| 183 | BarnOwl::admin_message("IRC", |
---|
| 184 | "[" . $self->alias . "] " . |
---|
| 185 | [$evt->args]->[1] . ": Nick already in use"); |
---|
[ba2ca66] | 186 | unless($self->connected) { |
---|
| 187 | $self->conn->disconnect; |
---|
[eab7a4c] | 188 | } |
---|
[9e02bb7] | 189 | } |
---|
| 190 | |
---|
[2c40dc0] | 191 | sub on_event { |
---|
| 192 | my ($self, $evt) = @_; |
---|
| 193 | BarnOwl::admin_message("IRC", |
---|
[47b6a5f] | 194 | "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n" |
---|
[2c40dc0] | 195 | . strip_irc_formatting(join("\n", $evt->args))) |
---|
| 196 | if BarnOwl::getvar('irc:spew') eq 'on'; |
---|
| 197 | } |
---|
| 198 | |
---|
[9e02bb7] | 199 | |
---|
[b38b0b2] | 200 | ################################################################################ |
---|
| 201 | ########################### Utilities/Helpers ################################## |
---|
| 202 | ################################################################################ |
---|
| 203 | |
---|
| 204 | sub strip_irc_formatting { |
---|
| 205 | my $body = shift; |
---|
[214b790] | 206 | # Strip mIRC colors. If someone wants to write code to convert |
---|
| 207 | # these to zephyr colors, be my guest. |
---|
| 208 | $body =~ s/\cC\d+(?:,\d+)?//g; |
---|
| 209 | $body =~ s/\cO//g; |
---|
| 210 | |
---|
| 211 | my @pieces = split /\cB/, $body; |
---|
[b38b0b2] | 212 | my $out; |
---|
| 213 | while(@pieces) { |
---|
| 214 | $out .= shift @pieces; |
---|
| 215 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
---|
| 216 | } |
---|
| 217 | return $out; |
---|
| 218 | } |
---|
| 219 | |
---|
[2c40dc0] | 220 | # Determines if the given message recipient is a username, as opposed to |
---|
| 221 | # a channel that starts with # or &. |
---|
| 222 | sub is_private { |
---|
| 223 | return shift !~ /^[\#\&]/; |
---|
| 224 | } |
---|
[b38b0b2] | 225 | |
---|
[bc0d7bc] | 226 | sub cdr { |
---|
| 227 | shift; |
---|
| 228 | return @_; |
---|
| 229 | } |
---|
| 230 | |
---|
[b38b0b2] | 231 | 1; |
---|