Component/IRC/Plugin/Connector.pm

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