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 motd)); |
---|
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 | $self->motd(""); |
---|
32 | bless($self, $class); |
---|
33 | |
---|
34 | $self->add_global_handler(376 => sub { goto &on_connect }); |
---|
35 | $self->add_global_handler(['msg', 'notice', 'public', 'caction'], |
---|
36 | sub { goto &on_msg }); |
---|
37 | $self->add_global_handler(['welcome', 'yourhost', 'created', |
---|
38 | 'luserclient', 'luserop', 'luserchannels', 'luserme'], |
---|
39 | sub { goto &on_admin_msg }); |
---|
40 | $self->add_global_handler(['myinfo', 'map', 'n_local', 'n_global', |
---|
41 | 'luserconns'], |
---|
42 | sub { }); |
---|
43 | $self->add_handler(375 => sub { goto &on_motdstart }); |
---|
44 | $self->add_handler(372 => sub { goto &on_motd }); |
---|
45 | $self->add_handler(376 => sub { goto &on_endofmotd }); |
---|
46 | $self->add_global_handler(cping => sub { goto &on_ping }); |
---|
47 | $self->add_global_handler(join => sub { goto &on_join }); |
---|
48 | $self->add_global_handler(part => sub { goto &on_part }); |
---|
49 | $self->add_default_handler(sub { goto &on_event; }); |
---|
50 | |
---|
51 | return $self; |
---|
52 | } |
---|
53 | |
---|
54 | ################################################################################ |
---|
55 | ############################### IRC callbacks ################################## |
---|
56 | ################################################################################ |
---|
57 | |
---|
58 | sub on_connect { |
---|
59 | my ($self, $evt) = @_; |
---|
60 | BarnOwl::admin_message("IRC", "Connected to " . $self->server . " (" . $self->alias . ")"); |
---|
61 | } |
---|
62 | |
---|
63 | sub new_message { |
---|
64 | my $self = shift; |
---|
65 | my $evt = shift; |
---|
66 | return BarnOwl::Message->new( |
---|
67 | type => 'IRC', |
---|
68 | server => $self->server, |
---|
69 | network => $self->alias, |
---|
70 | sender => $evt->nick, |
---|
71 | hostname => $evt->host, |
---|
72 | from => $evt->from, |
---|
73 | @_ |
---|
74 | ); |
---|
75 | } |
---|
76 | |
---|
77 | sub on_msg { |
---|
78 | my ($self, $evt) = @_; |
---|
79 | my ($recipient) = $evt->to; |
---|
80 | my $body = strip_irc_formatting([$evt->args]->[0]); |
---|
81 | my $nick = $self->nick; |
---|
82 | BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/; |
---|
83 | $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction'; |
---|
84 | my $msg = $self->new_message($evt, |
---|
85 | direction => 'in', |
---|
86 | recipient => $recipient, |
---|
87 | body => $body, |
---|
88 | $evt->type eq 'notice' ? |
---|
89 | (notice => 'true') : (), |
---|
90 | is_private($recipient) ? |
---|
91 | (isprivate => 'true') : (channel => $recipient), |
---|
92 | replycmd => 'irc-msg ' . |
---|
93 | (is_private($recipient) ? $evt->nick : $recipient), |
---|
94 | replysendercmd => 'irc-msg ' . $evt->nick |
---|
95 | ); |
---|
96 | |
---|
97 | BarnOwl::queue_message($msg); |
---|
98 | } |
---|
99 | |
---|
100 | sub on_ping { |
---|
101 | my ($self, $evt) = @_; |
---|
102 | $self->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
---|
103 | } |
---|
104 | |
---|
105 | sub on_admin_msg { |
---|
106 | my ($self, $evt) = @_; |
---|
107 | BarnOwl::admin_message("IRC", |
---|
108 | BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from ' |
---|
109 | . $evt->alias) . "\n" |
---|
110 | . strip_irc_formatting(join '\n', cdr $evt->args)); |
---|
111 | } |
---|
112 | |
---|
113 | sub on_motdstart { |
---|
114 | my ($self, $evt) = @_; |
---|
115 | $self->motd(join "\n", cdr $evt->args); |
---|
116 | } |
---|
117 | |
---|
118 | sub on_motd { |
---|
119 | my ($self, $evt) = @_; |
---|
120 | $self->motd(join "\n", $self->motd, cdr $evt->args); |
---|
121 | } |
---|
122 | |
---|
123 | sub on_endofmotd { |
---|
124 | my ($self, $evt) = @_; |
---|
125 | $self->motd(join "\n", $self->motd, cdr $evt->args); |
---|
126 | BarnOwl::admin_message("IRC", |
---|
127 | BarnOwl::Style::boldify('MOTD for ' . $evt->alias) . "\n" |
---|
128 | . strip_irc_formatting($self->motd)); |
---|
129 | } |
---|
130 | |
---|
131 | sub on_join { |
---|
132 | my ($self, $evt) = @_; |
---|
133 | my $msg = $self->new_message($evt, |
---|
134 | loginout => 'login', |
---|
135 | channel => $evt->to, |
---|
136 | ); |
---|
137 | BarnOwl::queue_message($msg); |
---|
138 | } |
---|
139 | |
---|
140 | sub on_part { |
---|
141 | my ($self, $evt) = @_; |
---|
142 | my $msg = $self->new_message($evt, |
---|
143 | loginout => 'logout', |
---|
144 | channel => $evt->to, |
---|
145 | ); |
---|
146 | BarnOwl::queue_message($msg); |
---|
147 | } |
---|
148 | |
---|
149 | sub on_event { |
---|
150 | my ($self, $evt) = @_; |
---|
151 | BarnOwl::admin_message("IRC", |
---|
152 | "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n" |
---|
153 | . strip_irc_formatting(join("\n", $evt->args))) |
---|
154 | if BarnOwl::getvar('irc:spew') eq 'on'; |
---|
155 | } |
---|
156 | |
---|
157 | ################################################################################ |
---|
158 | ########################### Utilities/Helpers ################################## |
---|
159 | ################################################################################ |
---|
160 | |
---|
161 | sub strip_irc_formatting { |
---|
162 | my $body = shift; |
---|
163 | # Strip mIRC colors. If someone wants to write code to convert |
---|
164 | # these to zephyr colors, be my guest. |
---|
165 | $body =~ s/\cC\d+(?:,\d+)?//g; |
---|
166 | $body =~ s/\cO//g; |
---|
167 | |
---|
168 | my @pieces = split /\cB/, $body; |
---|
169 | my $out; |
---|
170 | while(@pieces) { |
---|
171 | $out .= shift @pieces; |
---|
172 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
---|
173 | } |
---|
174 | return $out; |
---|
175 | } |
---|
176 | |
---|
177 | # Determines if the given message recipient is a username, as opposed to |
---|
178 | # a channel that starts with # or &. |
---|
179 | sub is_private { |
---|
180 | return shift !~ /^[\#\&]/; |
---|
181 | } |
---|
182 | |
---|
183 | sub cdr { |
---|
184 | shift; |
---|
185 | return @_; |
---|
186 | } |
---|
187 | |
---|
188 | 1; |
---|