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 Exporter); |
---|
18 | __PACKAGE__->mk_accessors(qw(alias channels)); |
---|
19 | our @EXPORT_OK = qw(&is_private); |
---|
20 | |
---|
21 | use BarnOwl; |
---|
22 | |
---|
23 | sub new { |
---|
24 | my $class = shift; |
---|
25 | my $irc = shift; |
---|
26 | my $alias = shift; |
---|
27 | my %args = (@_); |
---|
28 | my $self = $class->SUPER::new($irc, %args); |
---|
29 | $self->alias($alias); |
---|
30 | $self->channels([]); |
---|
31 | bless($self, $class); |
---|
32 | |
---|
33 | $self->add_global_handler(376 => sub { goto &on_connect }); |
---|
34 | $self->add_global_handler(['msg', 'notice', 'public', 'caction'], |
---|
35 | sub { goto &on_msg }); |
---|
36 | $self->add_global_handler(cping => sub { goto &on_ping }); |
---|
37 | $self->add_default_handler(sub { goto &on_event; }); |
---|
38 | |
---|
39 | return $self; |
---|
40 | } |
---|
41 | |
---|
42 | ################################################################################ |
---|
43 | ############################### IRC callbacks ################################## |
---|
44 | ################################################################################ |
---|
45 | |
---|
46 | sub on_connect { |
---|
47 | my ($self, $evt) = @_; |
---|
48 | BarnOwl::admin_message("IRC", "Connected to " . $self->server . " (" . $self->alias . ")"); |
---|
49 | } |
---|
50 | |
---|
51 | sub on_msg { |
---|
52 | my ($self, $evt) = @_; |
---|
53 | my ($recipient) = $evt->to; |
---|
54 | my $body = strip_irc_formatting([$evt->args]->[0]); |
---|
55 | $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction'; |
---|
56 | my $msg = BarnOwl::Message->new( |
---|
57 | type => 'IRC', |
---|
58 | direction => 'in', |
---|
59 | server => $self->server, |
---|
60 | network => $self->alias, |
---|
61 | recipient => $recipient, |
---|
62 | body => $body, |
---|
63 | sender => $evt->nick, |
---|
64 | hostname => $evt->host, |
---|
65 | from => $evt->from, |
---|
66 | $evt->type eq 'notice' ? |
---|
67 | (notice => 'true') : (), |
---|
68 | is_private($recipient) ? |
---|
69 | (isprivate => 'true') : (channel => $recipient), |
---|
70 | replycmd => 'irc-msg ' . |
---|
71 | (is_private($recipient) ? $evt->nick : $recipient), |
---|
72 | replysendercmd => 'irc-msg ' . $evt->nick, |
---|
73 | ); |
---|
74 | BarnOwl::queue_message($msg); |
---|
75 | } |
---|
76 | |
---|
77 | sub on_ping { |
---|
78 | my ($self, $evt) = @_; |
---|
79 | $self->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
---|
80 | } |
---|
81 | |
---|
82 | sub on_event { |
---|
83 | my ($self, $evt) = @_; |
---|
84 | BarnOwl::admin_message("IRC", |
---|
85 | "Unhandled IRC event of type " . $evt->type . ":\n" |
---|
86 | . strip_irc_formatting(join("\n", $evt->args))) |
---|
87 | if BarnOwl::getvar('irc:spew') eq 'on'; |
---|
88 | } |
---|
89 | |
---|
90 | ################################################################################ |
---|
91 | ########################### Utilities/Helpers ################################## |
---|
92 | ################################################################################ |
---|
93 | |
---|
94 | sub strip_irc_formatting { |
---|
95 | my $body = shift; |
---|
96 | my @pieces = split /\x02/, $body; |
---|
97 | my $out; |
---|
98 | while(@pieces) { |
---|
99 | $out .= shift @pieces; |
---|
100 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
---|
101 | } |
---|
102 | return $out; |
---|
103 | } |
---|
104 | |
---|
105 | # Determines if the given message recipient is a username, as opposed to |
---|
106 | # a channel that starts with # or &. |
---|
107 | sub is_private { |
---|
108 | return shift !~ /^[\#\&]/; |
---|
109 | } |
---|
110 | |
---|
111 | 1; |
---|