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