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 wrapper around Net::IRC::Connection for BarnOwl's IRC |
---|
13 | support |
---|
14 | |
---|
15 | =cut |
---|
16 | |
---|
17 | use Net::IRC::Connection; |
---|
18 | |
---|
19 | use base qw(Class::Accessor Exporter); |
---|
20 | __PACKAGE__->mk_accessors(qw(conn alias channels connected motd names_tmp whois_tmp)); |
---|
21 | our @EXPORT_OK = qw(&is_private); |
---|
22 | |
---|
23 | use BarnOwl; |
---|
24 | |
---|
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 | |
---|
35 | sub new { |
---|
36 | my $class = shift; |
---|
37 | my $irc = shift; |
---|
38 | my $alias = shift; |
---|
39 | my %args = (@_); |
---|
40 | my $conn = Net::IRC::Connection->new($irc, %args); |
---|
41 | my $self = bless({}, $class); |
---|
42 | $self->conn($conn); |
---|
43 | $self->alias($alias); |
---|
44 | $self->channels([]); |
---|
45 | $self->motd(""); |
---|
46 | $self->connected(0); |
---|
47 | $self->names_tmp([]); |
---|
48 | $self->whois_tmp(""); |
---|
49 | |
---|
50 | $self->conn->add_handler(376 => sub { shift; $self->on_connect(@_) }); |
---|
51 | $self->conn->add_default_handler(sub { shift; $self->on_event(@_) }); |
---|
52 | $self->conn->add_handler(['msg', 'notice', 'public', 'caction'], |
---|
53 | sub { shift; $self->on_msg(@_) }); |
---|
54 | $self->conn->add_handler(['welcome', 'yourhost', 'created', |
---|
55 | 'luserclient', 'luserop', 'luserchannels', 'luserme', |
---|
56 | 'error'], |
---|
57 | sub { shift; $self->on_admin_msg(@_) }); |
---|
58 | $self->conn->add_handler(['myinfo', 'map', 'n_local', 'n_global', |
---|
59 | 'luserconns'], |
---|
60 | sub { }); |
---|
61 | $self->conn->add_handler(motdstart => sub { shift; $self->on_motdstart(@_) }); |
---|
62 | $self->conn->add_handler(motd => sub { shift; $self->on_motd(@_) }); |
---|
63 | $self->conn->add_handler(endofmotd => sub { shift; $self->on_endofmotd(@_) }); |
---|
64 | $self->conn->add_handler(join => sub { shift; $self->on_join(@_) }); |
---|
65 | $self->conn->add_handler(part => sub { shift; $self->on_part(@_) }); |
---|
66 | $self->conn->add_handler(disconnect => sub { shift; $self->on_disconnect(@_) }); |
---|
67 | $self->conn->add_handler(nicknameinuse => sub { shift; $self->on_nickinuse(@_) }); |
---|
68 | $self->conn->add_handler(cping => sub { shift; $self->on_ping(@_) }); |
---|
69 | $self->conn->add_handler(topic => sub { shift; $self->on_topic(@_) }); |
---|
70 | $self->conn->add_handler(topicinfo => sub { shift; $self->on_topicinfo(@_) }); |
---|
71 | $self->conn->add_handler(namreply => sub { shift; $self->on_namreply(@_) }); |
---|
72 | $self->conn->add_handler(endofnames=> sub { shift; $self->on_endofnames(@_) }); |
---|
73 | |
---|
74 | return $self; |
---|
75 | } |
---|
76 | |
---|
77 | sub getSocket |
---|
78 | { |
---|
79 | my $self = shift; |
---|
80 | return $self->conn->socket; |
---|
81 | } |
---|
82 | |
---|
83 | ################################################################################ |
---|
84 | ############################### IRC callbacks ################################## |
---|
85 | ################################################################################ |
---|
86 | |
---|
87 | sub new_message { |
---|
88 | my $self = shift; |
---|
89 | my $evt = shift; |
---|
90 | return BarnOwl::Message->new( |
---|
91 | type => 'IRC', |
---|
92 | server => $self->server, |
---|
93 | network => $self->alias, |
---|
94 | sender => $evt->nick, |
---|
95 | hostname => $evt->host, |
---|
96 | from => $evt->from, |
---|
97 | @_ |
---|
98 | ); |
---|
99 | } |
---|
100 | |
---|
101 | sub on_msg { |
---|
102 | my ($self, $evt) = @_; |
---|
103 | my ($recipient) = $evt->to; |
---|
104 | my $body = strip_irc_formatting([$evt->args]->[0]); |
---|
105 | my $nick = $self->nick; |
---|
106 | BarnOwl::beep() if $body =~ /\b\Q$nick\E\b/; |
---|
107 | $body = BarnOwl::Style::boldify($evt->nick.' '.$body) if $evt->type eq 'caction'; |
---|
108 | my $msg = $self->new_message($evt, |
---|
109 | direction => 'in', |
---|
110 | recipient => $recipient, |
---|
111 | body => $body, |
---|
112 | $evt->type eq 'notice' ? |
---|
113 | (notice => 'true') : (), |
---|
114 | is_private($recipient) ? |
---|
115 | (isprivate => 'true') : (channel => $recipient), |
---|
116 | replycmd => 'irc-msg -a ' . $self->alias . ' ' . |
---|
117 | (is_private($recipient) ? $evt->nick : $recipient), |
---|
118 | replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
---|
119 | ); |
---|
120 | |
---|
121 | BarnOwl::queue_message($msg); |
---|
122 | } |
---|
123 | |
---|
124 | sub on_ping { |
---|
125 | my ($self, $evt) = @_; |
---|
126 | $self->conn->ctcp_reply($evt->nick, join (' ', ($evt->args))); |
---|
127 | } |
---|
128 | |
---|
129 | sub on_admin_msg { |
---|
130 | my ($self, $evt) = @_; |
---|
131 | BarnOwl::admin_message("IRC", |
---|
132 | BarnOwl::Style::boldify('IRC ' . $evt->type . ' message from ' |
---|
133 | . $self->alias) . "\n" |
---|
134 | . strip_irc_formatting(join '\n', $evt->args)); |
---|
135 | } |
---|
136 | |
---|
137 | sub on_motdstart { |
---|
138 | my ($self, $evt) = @_; |
---|
139 | $self->motd(join "\n", cdr($evt->args)); |
---|
140 | } |
---|
141 | |
---|
142 | sub on_motd { |
---|
143 | my ($self, $evt) = @_; |
---|
144 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
---|
145 | } |
---|
146 | |
---|
147 | sub on_endofmotd { |
---|
148 | my ($self, $evt) = @_; |
---|
149 | $self->motd(join "\n", $self->motd, cdr($evt->args)); |
---|
150 | if(!$self->connected) { |
---|
151 | BarnOwl::admin_message("IRC", "Connected to " . |
---|
152 | $self->server . " (" . $self->alias . ")"); |
---|
153 | $self->connected(1); |
---|
154 | |
---|
155 | } |
---|
156 | BarnOwl::admin_message("IRC", |
---|
157 | BarnOwl::Style::boldify('MOTD for ' . $self->alias) . "\n" |
---|
158 | . strip_irc_formatting($self->motd)); |
---|
159 | } |
---|
160 | |
---|
161 | sub on_join { |
---|
162 | my ($self, $evt) = @_; |
---|
163 | my $msg = $self->new_message($evt, |
---|
164 | loginout => 'login', |
---|
165 | channel => $evt->to, |
---|
166 | replycmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick, |
---|
167 | replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
---|
168 | ); |
---|
169 | BarnOwl::queue_message($msg); |
---|
170 | } |
---|
171 | |
---|
172 | sub on_part { |
---|
173 | my ($self, $evt) = @_; |
---|
174 | my $msg = $self->new_message($evt, |
---|
175 | loginout => 'logout', |
---|
176 | channel => $evt->to, |
---|
177 | replycmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick, |
---|
178 | replysendercmd => 'irc-msg -a ' . $self->alias . ' ' . $evt->nick |
---|
179 | ); |
---|
180 | BarnOwl::queue_message($msg); |
---|
181 | } |
---|
182 | |
---|
183 | sub on_disconnect { |
---|
184 | my $self = shift; |
---|
185 | delete $BarnOwl::Module::IRC::ircnets{$self->alias}; |
---|
186 | BarnOwl::remove_dispatch($self->{FD}); |
---|
187 | BarnOwl::admin_message('IRC', |
---|
188 | "[" . $self->alias . "] Disconnected from server"); |
---|
189 | } |
---|
190 | |
---|
191 | sub on_nickinuse { |
---|
192 | my ($self, $evt) = @_; |
---|
193 | BarnOwl::admin_message("IRC", |
---|
194 | "[" . $self->alias . "] " . |
---|
195 | [$evt->args]->[1] . ": Nick already in use"); |
---|
196 | unless($self->connected) { |
---|
197 | $self->conn->disconnect; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | sub on_topic { |
---|
202 | my ($self, $evt) = @_; |
---|
203 | my @args = $evt->args; |
---|
204 | if (scalar @args > 1) { |
---|
205 | BarnOwl::admin_message("IRC", |
---|
206 | "Topic for $args[1] on " . $self->alias . " is $args[2]"); |
---|
207 | } else { |
---|
208 | BarnOwl::admin_message("IRC", |
---|
209 | "Topic changed to $args[0]"); |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | sub on_topicinfo { |
---|
214 | my ($self, $evt) = @_; |
---|
215 | my @args = $evt->args; |
---|
216 | BarnOwl::admin_message("IRC", |
---|
217 | "Topic for $args[1] set by $args[2] at " . localtime($args[3])); |
---|
218 | } |
---|
219 | |
---|
220 | sub on_event { |
---|
221 | my ($self, $evt) = @_; |
---|
222 | BarnOwl::admin_message("IRC", |
---|
223 | "[" . $self->alias . "] Unhandled IRC event of type " . $evt->type . ":\n" |
---|
224 | . strip_irc_formatting(join("\n", $evt->args))) |
---|
225 | if BarnOwl::getvar('irc:spew') eq 'on'; |
---|
226 | } |
---|
227 | |
---|
228 | # IRC gives us a bunch of namreply messages, followed by an endofnames. |
---|
229 | # We need to collect them from the namreply and wait for the endofnames message. |
---|
230 | # After this happens, the names_tmp variable is cleared. |
---|
231 | |
---|
232 | sub on_namreply { |
---|
233 | my ($self, $evt) = @_; |
---|
234 | $self->names_tmp([@{$self->names_tmp}, split(' ', [$evt->args]->[3])]); |
---|
235 | } |
---|
236 | |
---|
237 | sub on_endofnames { |
---|
238 | my ($self, $evt) = @_; |
---|
239 | my $names = BarnOwl::Style::boldify("Members of " . [$evt->args]->[1] . ":\n"); |
---|
240 | for my $name (@{$self->names_tmp}) { |
---|
241 | $names .= " $name\n"; |
---|
242 | } |
---|
243 | BarnOwl::popless_ztext($names); |
---|
244 | $self->names_tmp([]); |
---|
245 | } |
---|
246 | |
---|
247 | ################################################################################ |
---|
248 | ########################### Utilities/Helpers ################################## |
---|
249 | ################################################################################ |
---|
250 | |
---|
251 | sub strip_irc_formatting { |
---|
252 | my $body = shift; |
---|
253 | # Strip mIRC colors. If someone wants to write code to convert |
---|
254 | # these to zephyr colors, be my guest. |
---|
255 | $body =~ s/\cC\d+(?:,\d+)?//g; |
---|
256 | $body =~ s/\cO//g; |
---|
257 | |
---|
258 | my @pieces = split /\cB/, $body; |
---|
259 | my $out = ''; |
---|
260 | while(@pieces) { |
---|
261 | $out .= shift @pieces; |
---|
262 | $out .= BarnOwl::Style::boldify(shift @pieces) if @pieces; |
---|
263 | } |
---|
264 | return $out; |
---|
265 | } |
---|
266 | |
---|
267 | # Determines if the given message recipient is a username, as opposed to |
---|
268 | # a channel that starts with # or &. |
---|
269 | sub is_private { |
---|
270 | return shift !~ /^[\#\&]/; |
---|
271 | } |
---|
272 | |
---|
273 | sub cdr { |
---|
274 | shift; |
---|
275 | return @_; |
---|
276 | } |
---|
277 | |
---|
278 | 1; |
---|