[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 | |
---|
| 12 | This module is a Net::IRC::Connection subclass for BarnOwl's IRC |
---|
| 13 | support |
---|
| 14 | |
---|
| 15 | =cut |
---|
| 16 | |
---|
| 17 | use base qw(Net::IRC::Connection Class::Accessor); |
---|
| 18 | __PACKAGE__->mk_accessors(qw(alias channels)); |
---|
| 19 | |
---|
| 20 | use BarnOwl; |
---|
| 21 | |
---|
| 22 | sub new { |
---|
| 23 | my $class = shift; |
---|
| 24 | my $irc = shift; |
---|
| 25 | my $alias = shift; |
---|
| 26 | my %args = (@_); |
---|
| 27 | my $self = $class->SUPER::new($irc, %args); |
---|
| 28 | $self->alias($alias); |
---|
| 29 | $self->channels([]); |
---|
| 30 | bless($self, $class); |
---|
| 31 | |
---|
| 32 | $self->add_global_handler(endofmotd => sub { goto &on_connect }); |
---|
| 33 | $self->add_global_handler(msg => sub { goto &on_msg }); |
---|
| 34 | $self->add_global_handler(notice => sub { goto &on_msg }); |
---|
| 35 | $self->add_default_handler(sub { goto &on_event; }); |
---|
| 36 | |
---|
| 37 | return $self; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | ################################################################################ |
---|
| 41 | ############################### IRC callbacks ################################## |
---|
| 42 | ################################################################################ |
---|
| 43 | |
---|
| 44 | sub on_connect { |
---|
| 45 | my ($self, $evt) = @_; |
---|
| 46 | BarnOwl::admin_message("IRC", "Connected to " . $self->server . " (" . $self->alias . ")"); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | sub on_msg { |
---|
| 50 | my ($self, $evt) = @_; |
---|
| 51 | my $replycmd = "irc-msg " . $evt->nick; |
---|
| 52 | my $msg = BarnOwl::Message->new( |
---|
| 53 | type => 'IRC', |
---|
| 54 | direction => 'in', |
---|
| 55 | server => $self->server, |
---|
| 56 | network => $self->alias, |
---|
| 57 | recipient => $self->nick, |
---|
| 58 | body => strip_irc_formatting([$evt->args]->[0]), |
---|
| 59 | sender => $evt->nick, |
---|
| 60 | hostname => $evt->host, |
---|
| 61 | from => $evt->from, |
---|
| 62 | notice => $evt->type eq 'notice' ? 'true' : '', |
---|
| 63 | isprivate => 'true', |
---|
| 64 | replycmd => $replycmd, |
---|
| 65 | replysendercmd => $replycmd |
---|
| 66 | ); |
---|
| 67 | BarnOwl::queue_message($msg); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | ################################################################################ |
---|
| 71 | ########################### Utilities/Helpers ################################## |
---|
| 72 | ################################################################################ |
---|
| 73 | |
---|
| 74 | sub strip_irc_formatting { |
---|
| 75 | my $body = shift; |
---|
| 76 | my @pieces = split /\x02/, $body; |
---|
| 77 | my $out; |
---|
| 78 | while(@pieces) { |
---|
| 79 | $out .= shift @pieces; |
---|
| 80 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
---|
| 81 | } |
---|
| 82 | return $out; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
| 86 | 1; |
---|