Component/IRC/Plugin/Connector.pm

filter: Statementstmt 16.2% 18 / 111 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 20.8% 5 / 24 Podpod 18.1% 2 / 11 totaltotal 14.5% 25 / 172 CC 12 SCAR 20.6
CC
12
Cov
14%
_reconnect
30.0
new
12.0
S_pong
6.0
CRAP 7.9
« Component/IRC/Plugin/NickServID.pm ↑ Summary Component/IRC.pm »
1 package POE::Component::IRC::Plugin::Connector;
2 1 our $AUTHORITY = 'cpan:HINRIK';
3 1 $POE::Component::IRC::Plugin::Connector::VERSION = '6.92';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POE::Component::IRC::Plugin qw( :ALL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 0 ▶ my ($package) = shift;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
12 0 ▶ croak "$package requires an even number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 0 0
13 0 my %args = @_;
14  
15 0 $args{ lc $_ } = delete $args{$_} for keys %args;
16 0 $args{lag} = 0;
17 0 return bless \%args, $package;
18 }
19  
20 sub PCI_register {
21 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 0 $self->{irc} = $irc;
24 0 POE::Session->create(
25 object_states => [
26 $self => [ qw(_start _auto_ping _reconnect _shutdown _start_ping _start_time_out _stop_ping _time_out) ],
27 ],
28 );
29  
30 0 $irc->raw_events(1);
31 0 $irc->plugin_register( $self, 'SERVER', qw(connected disconnected 001 error socketerr pong raw) );
32  
33 0 return 1;
34 }
35  
36 sub PCI_unregister {
37 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
38 0 delete $self->{irc};
39 0 $poe_kernel->post( $self->{SESSION_ID} => '_shutdown' );
40 0 $poe_kernel->refcount_decrement( $self->{SESSION_ID}, __PACKAGE__ );
41 0 return 1;
42 }
43  
44 sub S_connected {
45 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_connected: not called CC=1 SCAR=6.9
Pod undocumented
46 0 $poe_kernel->post( $self->{SESSION_ID}, '_start_time_out' );
47 0 return PCI_EAT_NONE;
48 }
49  
50 sub S_001 {
51 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_001: not called CC=1 SCAR=6.9
Pod undocumented
52 0 $poe_kernel->post( $self->{SESSION_ID}, '_start_ping' );
53 0 return PCI_EAT_NONE;
54 }
55  
56 sub S_disconnected {
57 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_disconnected: not called CC=1 SCAR=6.9
Pod undocumented
58 0 $poe_kernel->post( $self->{SESSION_ID}, '_stop_ping' );
59 0 $poe_kernel->post( $self->{SESSION_ID}, '_reconnect' );
60 0 return PCI_EAT_NONE;
61 }
62  
63 sub S_error {
64 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_error: not called CC=1 SCAR=6.9
Pod undocumented
65 0 $poe_kernel->post( $self->{SESSION_ID}, '_stop_ping' );
66 0 $poe_kernel->post( $self->{SESSION_ID}, '_reconnect' );
67 0 return PCI_EAT_NONE;
68 }
69  
70 sub S_socketerr {
71 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_socketerr: not called CC=1 SCAR=6.9
Pod undocumented
72 0 $poe_kernel->post( $self->{SESSION_ID}, '_stop_ping' );
73 0 $poe_kernel->post( $self->{SESSION_ID}, '_reconnect' );
74 0 return PCI_EAT_NONE;
75 }
76  
77 sub S_pong {
78 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_pong: not called CC=2 SCAR=17.9
Pod undocumented
79 0 my $ping = shift @{ $self->{pings} };
80 0 ▶ return PCI_EAT_NONE if !$ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $ping 0 0 0
81 0 $self->{lag} = time() - $ping;
82 0 $self->{seen_traffic} = 1;
83 0 return PCI_EAT_NONE;
84 }
85  
86 sub S_raw {
87 0 ▶ my ($self,$irc) = splice @_, 0, 2;
Subroutine S_raw: not called CC=1 SCAR=6.9
Pod undocumented
88 0 $self->{seen_traffic} = 1;
89 0 return PCI_EAT_NONE;
90 }
91  
92 sub lag {
93 0 ▶ return $_[0]->{lag};
Subroutine lag: not called CC=1 SCAR=6.9
Pod documented
94 }
95  
96 sub _start {
97 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _start: not called CC=2 SCAR=17.9
98  
99 0 $self->{SESSION_ID} = $_[SESSION]->ID();
100 0 $kernel->refcount_increment( $self->{SESSION_ID}, __PACKAGE__ );
101 0 ▶ $kernel->yield( '_start_ping' ) if $self->{irc}->connected();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"irc"}->connected 0 0 0
102 0 return;
103 }
104  
105 sub _start_ping {
106 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _start_ping: not called CC=2 SCAR=17.9
107 0 $self->{pings} = [ ];
108 0 $kernel->delay( '_time_out' => undef );
109 0 ▶ $kernel->delay( '_auto_ping' => $self->{delay} || 300 );
Condition0%0/2
$self->{'delay'} || 300
l!l
00
MC/DC0%0/1
$self->{'delay'} || 300
$self->{'delay'}
Truth table0%0/2
A
A= $self->{'delay'}
Aresult
11
00
110 0 return;
111 }
112  
113 sub _auto_ping {
114 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _auto_ping: not called CC=2 SCAR=17.9
115  
116 0 ▶ if (!$self->{seen_traffic}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"seen_traffic"}) 0 0 0
117 0 my $time = time();
118 0 $self->{irc}->yield( 'ping' => $time );
119 0 push @{ $self->{pings} }, $time;
120 }
121  
122 0 $self->{seen_traffic} = 0;
123 0 $kernel->yield( '_start_ping' );
124 0 return;
125 }
126  
127 sub _stop_ping {
128 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _stop_ping: not called CC=1 SCAR=6.9
129 0 delete $self->{pings};
130 0 $kernel->delay( '_auto_ping' => undef );
131 0 $kernel->delay( '_time_out' => undef );
132 0 return;
133 }
134  
135 sub _shutdown {
136 0 ▶ my ($kernel,$self) = @_[KERNEL, OBJECT];
Subroutine _shutdown: not called CC=1 SCAR=6.9
137  
138 0 $kernel->yield( '_stop_ping' );
139 0 $kernel->delay('_reconnect');
140 0 return;
141 }
142  
143 sub _reconnect {
144 0 ▶ my ($kernel, $self, $session, $sender) = @_[KERNEL, OBJECT, SESSION, SENDER];
Subroutine _reconnect: not called CC=5 SCAR=34.0
145  
146 0 my %args;
147 0 ▶ if (ref $self->{servers} eq 'ARRAY' && @{ $self->{servers} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"servers"} eq "ARRAY" and @{$self->{"servers"};}) 0 0 0
Condition0%0/3
ref $self->{"servers"} eq "ARRAY" and @{$self->{"servers"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"servers"} eq "ARRAY" and @{$self->{"servers"};}
ref $self->{"servers"} eq "ARRAY" @{$self->{"servers"};}
Truth table0%0/3
A and B
A= ref $self->{"servers"} eq "ARRAY"B= @{$self->{"servers"};}
ABresult
0X0
100
111
148 0 @args{qw(Server Port)} = @{ $self->{servers}->[0] };
149 0 push @{ $self->{servers} }, shift @{ $self->{servers} };
150 }
151  
152 0 ▶ if ($sender eq $session) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sender eq $session) { } 0 0 0
153 0 $self->{irc}->yield('connect' => %args);
154 }
155 else {
156 0 ▶ $kernel->delay( '_reconnect' => $self->{reconnect} || 60 );
Condition0%0/2
$self->{'reconnect'} || 60
l!l
00
MC/DC0%0/1
$self->{'reconnect'} || 60
$self->{'reconnect'}
Truth table0%0/2
A
A= $self->{'reconnect'}
Aresult
11
00
157 }
158  
159 0 return;
160 }
161  
162 sub _start_time_out {
163 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _start_time_out: not called CC=2 SCAR=17.9
164 0 ▶ $kernel->delay( '_time_out' => $self->{timeout} || 60 );
Condition0%0/2
$self->{'timeout'} || 60
l!l
00
MC/DC0%0/1
$self->{'timeout'} || 60
$self->{'timeout'}
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
165 0 return;
166 }
167  
168 sub _time_out {
169 0 ▶ my ($kernel, $self) = @_[KERNEL, OBJECT];
Subroutine _time_out: not called CC=1 SCAR=6.9
170 0 $self->{irc}->disconnect();
171 0 return;
172 }
173  
174 1 1;
175  
176 =encoding utf8
177  
178 =head1 NAME
179  
180 POE::Component::IRC::Plugin::Connector - A PoCo-IRC plugin that deals with the
181 messy business of staying connected to an IRC server
182  
183 =head1 SYNOPSIS
184  
185 use POE qw(Component::IRC Component::IRC::Plugin::Connector);
186  
187 my $irc = POE::Component::IRC->spawn();
188  
189 POE::Session->create(
190 package_states => [
191 main => [ qw(_start lag_o_meter) ],
192 ],
193 );
194  
195 $poe_kernel->run();
196  
197 sub _start {
198 my ($kernel, $heap) = @_[KERNEL ,HEAP];
199 $irc->yield( register => 'all' );
200  
201 $heap->{connector} = POE::Component::IRC::Plugin::Connector->new();
202  
203 $irc->plugin_add( 'Connector' => $heap->{connector} );
204  
205 $irc->yield ( connect => { Nick => 'testbot', Server => 'someserver.com' } );
206  
207 $kernel->delay( 'lag_o_meter' => 60 );
208 return;
209 }
210  
211 sub lag_o_meter {
212 my ($kernel,$heap) = @_[KERNEL,HEAP];
213 print 'Time: ' . time() . ' Lag: ' . $heap->{connector}->lag() . "\n";
214 $kernel->delay( 'lag_o_meter' => 60 );
215 return;
216 }
217  
218 =head1 DESCRIPTION
219  
220 POE::Component::IRC::Plugin::Connector is a L<POE::Component::IRC|POE::Component::IRC>
221 plugin that deals with making sure that your IRC bot stays connected to the IRC
222 network of your choice. It implements the general algorithm as demonstrated at
223 L<http://poe.perl.org/?POE_Cookbook/IRC_Bot_Reconnecting>.
224  
225 =head1 METHODS
226  
227 =head2 C<new>
228  
229 Takes two optional arguments:
230  
231 B<'delay'>, the frequency, in seconds, at which the plugin will ping the IRC
232 server. Defaults to 300.
233  
234 B<'reconnect'>, the time in seconds, to wait before trying to reconnect to
235 the server. Defaults to 60.
236  
237 B<'servers'>, an array reference of IRC servers to consider. Each element should
238 be an array reference containing a server host and (optionally) a port number.
239 The plugin will cycle through this list of servers whenever it reconnects.
240  
241 Returns a plugin object suitable for use in
242 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
243  
244 =head2 C<lag>
245  
246 Returns the current 'lag' in seconds between sending PINGs to the IRC server
247 and getting PONG responses. Probably not likely to be wholely accurate.
248  
249 =head1 AUTHOR
250  
251 Chris "BinGOs" Williams <chris@bingosnet.co.uk>
252  
253 =head1 SEE ALSO
254  
255 L<POE::Component::IRC|POE::Component::IRC>
256  
257 L<POE::Component::IRC::Plugin|POE::Component::IRC::Plugin>
258  
259 =cut
« Component/IRC/Plugin/NickServID.pm ↑ Summary Component/IRC.pm »