IRC3/Connection.pm

filter: Statementstmt 16.0% 22 / 137 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 28.0% 7 / 25 Podpod 100% 10 / 10 totaltotal 17.4% 39 / 224 CC 28 SCAR 28.5
CC
28
Cov
12%
_feed_irc_data
56.0
_start_writer
30.0
__ANON__
20.0
CRAP 17.4
« IRC3/Client/Connection.pm ↑ Summary IRC3.pm »
1 package Net::IRC3::Connection;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::IRC3::Util qw/mk_msg parse_irc_msg/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Net::IRC3::Connection - An IRC connection abstraction
13  
14 =head1 SYNOPSIS
15  
16 #...
17 $con->send_msg (undef, "PRIVMSG", "Hello there!", "yournick");
18 #...
19  
20 =head1 DESCRIPTION
21  
22 B<NOTE:> This module is B<DEPRECATED>, please use L<AnyEvent::IRC> for new programs,
23 and possibly port existing L<Net::IRC3> applications to L<AnyEvent::IRC>. Though the
24 API of L<AnyEvent::IRC> has incompatible changes, it's still fairly similar.
25  
26  
27 The connection class. Here the actual interesting stuff can be done,
28 such as sending and receiving IRC messages.
29  
30 Please note that CTCP support is available through the functions
31 C<encode_ctcp> and C<decode_ctcp> provided by L<Net::IRC3::Util>.
32  
33 =head2 METHODS
34  
35 =over 4
36  
37 =item B<new>
38  
39 This constructor does take no arguments.
40  
41 =cut
42  
43 sub new
44 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
45 0 my $this = shift;
46 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
47  
48 0 my $self = {
49 cbs => {},
50 heap => {},
51 outbuf => ''
52 };
53  
54 0 bless $self, $class;
55  
56 0 return $self;
57 }
58  
59 =item B<connect ($host, $port)>
60  
61 Tries to open a socket to the host C<$host> and the port C<$port>.
62 If an error occured it will die (use eval to catch the exception).
63  
64 =cut
65  
66 0 ▶sub connect {
Subroutine connect: not called CC=3 SCAR=24.8
Pod documented
67 0 my ($self, $host, $port) = @_;
68  
69 $self->{socket}
70 0 ▶ and return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"socket"} and return 0 0 0
71  
72 0 ▶ my $sock = IO::Socket::INET->new (
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port, "Proto", "tcp", "Blocking", 0) or die "couldn't connect to irc server '${host}:${port}': $!\n" 0 0 0
73 PeerAddr => $host,
74 PeerPort => $port,
75 Proto => 'tcp',
76 Blocking => 0
77 ) or die "couldn't connect to irc server '$host:$port': $!\n";;
78  
79 0 $self->{socket} = $sock;
80 0 $self->{host} = $host;
81 0 $self->{port} = $port;
82  
83 $self->{cw} =
84 0 ▶ AnyEvent->io (poll => 'w', fh => $self->{socket}, cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
85 0 my ($w) = @_;
86 # FIXME: handle EAGAIN ?
87 0 delete $self->{cw};
88  
89 0 ▶ if ($! = $sock->sockopt (SO_ERROR)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! = $sock->sockopt(4)) { } 0 0 0
90 0 $self->event ('connect_error' => $!);
91 0 $self->_clear_me;
92 } else {
93 0 $self->use_socket ($host, $port, $self->{socket});
94 }
95 0 0
96 0 });
97 0 1
98 }
99  
100 =item B<use_socket ($host, $port, $socket)>
101  
102 This method can be used instead of C<connect> to handle IRC messages
103 that are received and sent over the C<$socket>.
104  
105 In this case C<$host> and C<$port> are just documentation for the error messages.
106  
107 =cut
108  
109 0 ▶sub use_socket {
Subroutine use_socket: not called CC=1 SCAR=6.9
Pod documented
110 0 my ($self, $host, $port, $socket) = @_;
111  
112 0 $self->{host} = $host;
113 0 $self->{port} = $port;
114 0 $self->{socket} = $socket;
115 0 $socket->blocking (0);
116  
117 0 $self->{connected} = 1;
118 0 $self->event ('connect');
119 0 $self->_start_reader;
120 0 $self->_start_writer;
121 }
122  
123 0 ▶sub _start_reader {
Subroutine _start_reader: not called CC=3 SCAR=24.8
124 0 my ($self) = @_;
125 0 my ($host, $port) = ($self->{host}, $self->{port});
126  
127 0 ▶ return if $self->{rw};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rw"} 0 0 0
128 0 ▶ return unless $self->{socket};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} 0 0 0
129  
130 $self->{rw} =
131 0 ▶ AnyEvent->io (poll => 'r', fh => $self->{socket}, cb => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
132 0 my $data;
133 0 my $l = $self->{socket}->sysread ($data, 1024);
134  
135 # FIXME: handle EAGAIN
136 0 ▶ if (defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l) { } 0 0 0
137 0 ▶ if ($l == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) { } 0 0 0
138 0 $self->disconnect ("EOF from IRC server '$host:$port'");
139 return
140 0 } else {
141 0 $self->_feed_irc_data ($data);
142 }
143  
144 } else {
145 0 ▶ if ($! == EAGAIN()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 11) { } 0 0 0
146 0 return;
147  
148 } else {
149 0 $self->disconnect ("Error while reading from IRC server '$host:$port': $!");
150 0 return;
151 }
152 }
153 0 });
154 }
155  
156  
157 0 ▶sub _start_writer {
Subroutine _start_writer: not called CC=5 SCAR=34.0
158 0 my ($self) = @_;
159  
160 0 ▶ return unless $self->{socket} && $self->{connected} && length ($self->{outbuf}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"socket"} and $self->{"connected"} and length $self->{"outbuf"} > 0 0 0 0
Condition0%0/6
$self->{"socket"} and $self->{"connected"} and length $self->{"outbuf"} > 0
!ll&&!rl&&r
000
$self->{"socket"} and $self->{"connected"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"socket"} and $self->{"connected"} and length $self->{"outbuf"} > 0
$self->{"socket"} $self->{"connected"} length $self->{"outbuf"} > 0
Truth table0%0/4
A and B and C
A= $self->{"socket"}B= $self->{"connected"}C= length $self->{"outbuf"} > 0
ABCresult
0XX0
10X0
1100
1111
161  
162 0 my ($host, $port) = ($self->{host}, $self->{port});
163  
164 0 ▶ unless (defined $self->{ww}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ww"}) 0 0 0
165 $self->{ww} =
166 0 ▶ AnyEvent->io (poll => 'w', fh => $self->{socket}, cb => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
167 0 my $l = syswrite $self->{socket}, $self->{outbuf};
168  
169 0 ▶ if (defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l) { } 0 0 0
170 0 substr $self->{outbuf}, 0, $l, "";
171 0 ▶ if (length ($self->{outbuf}) == 0) { delete $self->{ww} }
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"outbuf"} == 0) 0 0 0
172  
173 } else {
174 0 ▶ if ($! == EAGAIN()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 11) { } 0 0 0
175  
176 0 return;
177 } else {
178 0 $self->disconnect ("Error while writing to IRC server '$self->{host}:$self->{port}': $!");
179 0 return;
180 }
181 }
182 0 });
183 }
184 }
185  
186 =item B<disconnect ($reason)>
187  
188 Unregisters the connection in the main Net::IRC3 object, closes
189 the sockets and send a 'disconnect' event with C<$reason> as argument.
190  
191 =cut
192  
193 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
194 0 my ($self, $reason) = @_;
195  
196 0 $self->event (disconnect => $reason);
197 0 $self->_clear_me;
198  
199 }
200  
201 =item B<is_connected>
202  
203 Returns true when this connection is connected.
204 Otherwise false.
205  
206 =cut
207  
208 0 ▶sub is_connected {
Subroutine is_connected: not called CC=2 SCAR=17.9
Pod documented
209 0 my ($self) = @_;
210 $self->{socket} && $self->{connected}
211 0 ▶}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"socket"} and $self->{"connected"} 0 0 0
212  
213 0 ▶sub _clear_me {
Subroutine _clear_me: not called CC=1 SCAR=6.9
214 0 my ($self) = @_;
215  
216 0 delete $self->{connected};
217  
218 0 delete $self->{rw};
219 0 delete $self->{ww};
220 0 delete $self->{cw};
221  
222 0 delete $self->{socket};
223  
224 0 delete $self->{cbs};
225 0 delete $self->{events};
226 }
227  
228 =item B<heap ()>
229  
230 Returns a hash reference that is local to this connection object
231 that lets you store any information you want.
232  
233 =cut
234  
235 0 ▶sub heap {
Subroutine heap: not called CC=1 SCAR=6.9
Pod documented
236 0 my ($self) = @_;
237 0 return $self->{heap};
238 }
239  
240 =item B<send_raw ($ircline)>
241  
242 This method sends C<$ircline> straight to the server without any
243 further processing done.
244  
245 =cut
246  
247 0 ▶sub send_raw {
Subroutine send_raw: not called CC=1 SCAR=6.9
Pod documented
248 0 my ($self, $ircline) = @_;
249 0 $self->_send_raw ("$ircline\015\012");
250 }
251  
252 0 ▶sub _send_raw {
Subroutine _send_raw: not called CC=1 SCAR=6.9
253 0 my ($self, $data) = @_;
254  
255 0 $self->{outbuf} .= $data;
256 0 $self->_start_writer;
257 }
258  
259 =item B<send_msg (@ircmsg)>
260  
261 This function sends a message to the server. C<@ircmsg> is the argumentlist
262 for C<Net::IRC3::Util::mk_msg>.
263  
264 =cut
265  
266 0 ▶sub send_msg {
Subroutine send_msg: not called CC=1 SCAR=6.9
Pod documented
267 0 my ($self, @msg) = @_;
268  
269 0 $self->event (sent => @msg);
270 0 $self->_send_raw (mk_msg (@msg));
271 }
272  
273 =item B<reg_cb ($cmd, $cb)> or B<reg_cb ($cmd1, $cb1, $cmd2, $cb2, ..., $cmdN, $cbN)>
274  
275 This registers a callback in the connection class.
276 These callbacks will be called by internal events and
277 by IRC protocol commands. You can also specify multiple callback registrations.
278  
279 The first argument to the callbacks is always the connection object
280 itself.
281  
282 If a callback returns a false value, it will be unregistered.
283  
284 NOTE: I<A callback has to return true to stay alive>
285  
286 If C<$cmd> starts with 'irc_' the callback C<$cb> will be registered
287 for a IRC protocol command. The command is the suffix of C<$cmd> then.
288 The second argument to the callback is the message hash reference
289 that has the layout that is returned by C<Net::IRC3::Util::parse_irc_msg>.
290  
291 With the special C<$cmd> 'irc_*' the callback will be called on I<any>
292 IRC command that is received.
293  
294 EXAMPLE:
295  
296 $con->reg_cb (irc_privmsg => \&privmsg_handler);
297 # privmsg_handler will be called if an IRC message
298 # with the command 'PRIVMSG' arrives.
299  
300 If C<$cmd> is not prefixed with a 'irc_' it will be called when an event
301 with the name C<$cmd> is emitted. The arguments to the callback depend
302 on the event that is emitted (but remember: the first argument will always be the
303 connection object)
304  
305 Following events are emitted by this module and shouldn't be emitted
306 from a module user call to C<event>.
307  
308 =over 4
309  
310 =item B<connect>
311  
312 This event is generated when the socket was successfully connected.
313  
314 =item B<connect_error $error>
315  
316 This event is generated when the socket couldn't be connected successfully.
317  
318 =item B<disconnect $reason>
319  
320 This event will be generated if the connection is somehow terminated.
321 It will also be emitted when C<disconnect> is called.
322 The second argument to the callback is C<$reason>, a string that contains
323 a clue about why the connection terminated.
324  
325 If you want to reestablish a connection, call C<connect> again.
326  
327 =item B<sent @ircmsg>
328  
329 Emitted when a message (C<@ircmsg>) was sent to the server.
330 C<@ircmsg> are the arguments to C<Net::IRC3::Util::mk_msg>.
331  
332 =item B<'*' $msg>
333  
334 =item B<read $msg>
335  
336 Emitted when a message (C<$msg>) was read from the server.
337 C<$msg> is the hash reference returned by C<Net::IRC3::Util::parse_irc_msg>;
338  
339 =back
340  
341 =cut
342  
343 0 ▶sub reg_cb {
Subroutine reg_cb: not called CC=3 SCAR=24.8
Pod documented
344 0 my ($self, %regs) = @_;
345  
346 0 for my $cmd (keys %regs) {
347 0 my $cb = $regs{$cmd};
348  
349 0 ▶ if ($cmd =~ m/^irc_(\S+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^irc_(\S+)/i) { } 0 0 0
350 0 push @{$self->{cbs}->{lc $1}}, $cb;
351  
352 } else {
353 0 push @{$self->{events}->{$cmd}}, $cb;
354 }
355 }
356  
357 0 1;
358 }
359  
360 =item B<event ($event, @args)>
361  
362 This function emits an event with the name C<$event> and the arguments C<@args>.
363 The registerd callback that has been registered with C<reg_cb> will be called
364 with the first argument being the connection object and the rest of the arguments
365 being C<@args>.
366  
367 EXAMPLE
368  
369 $con->reg_cb (test_event => sub { print "Yay, i love $_[1]!!\n");
370 $con->event (test_event => "IRC");
371  
372 # will print "Yay, i love IRC!!\n"
373  
374 =cut
375  
376 0 ▶sub event {
Subroutine event: not called CC=3 SCAR=24.8
Pod documented
377 0 my ($self, $ev, @arg) = @_;
378  
379 0 my $nxt = [];
380  
381 0 for (@{$self->{events}->{$ev}}) {
382 0 ▶ $_->($self, @arg) and push @$nxt, $_;
Branch: 1 branch
BranchTrueFalseTotal
&$_($self, @arg) and push @$nxt, $_ 0 0 0
383 }
384  
385 0 $self->{events}->{$ev} = $nxt;
386 }
387  
388 # internal function, called by the read callbacks above.
389 0 ▶sub _feed_irc_data {
Subroutine _feed_irc_data: not called CC=7 SCAR=40.3
390 0 my ($self, $data) = @_;
391  
392 0 $self->{buffer} .= $data;
393  
394 0 my @msg;
395 0 ▶ while ($self->{buffer} =~ s/^([^\015\012]*)\015?\012//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buffer"} =~ s/^([^\015\012]*)\015?\012//) 0 0 0
396 0 push @msg, $1;
397 }
398  
399 0 for (@msg) {
400 0 my $m = parse_irc_msg ($_);
401  
402 0 $self->event (read => $m);
403  
404 0 my $nxt = [];
405  
406 0 for (@{$self->{cbs}->{lc $m->{command}}}) {
407 0 ▶ $_->($self, $m) and push @$nxt, $_;
Branch: 1 branch
BranchTrueFalseTotal
&$_($self, $m) and push @$nxt, $_ 0 0 0
408 }
409  
410 0 $self->{cbs}->{lc $m->{command}} = $nxt;
411  
412 0 $nxt = [];
413  
414 0 for (@{$self->{cbs}->{'*'}}) {
415 0 ▶ $_->($self, $m) and push @$nxt, $_;
Branch: 1 branch
BranchTrueFalseTotal
&$_($self, $m) and push @$nxt, $_ 0 0 0
416 }
417  
418 0 $self->{cbs}->{'*'} = $nxt;
419 }
420 }
421  
422  
423 =back
424  
425 =head1 AUTHOR
426  
427 Robin Redeker, C<< <elmex@ta-sa.org> >>
428  
429 =head1 SEE ALSO
430  
431 L<Net::IRC3>
432  
433 L<Net::IRC3::Client::Connection>
434  
435 =head1 COPYRIGHT & LICENSE
436  
437 Copyright 2006 Robin Redeker, all rights reserved.
438  
439 This program is free software; you can redistribute it and/or modify it
440 under the same terms as Perl itself.
441  
442 =cut
443  
444 1 1;
« IRC3/Client/Connection.pm ↑ Summary IRC3.pm »