Component/IRC/Plugin/NickReclaim.pm

filter: Statementstmt 25.3% 18 / 71 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 38.4% 5 / 13 Podpod 12.5% 1 / 8 totaltotal 16.9% 24 / 142 CC 21 SCAR 31.8
CC
21
Cov
16%
S_quit
42.0
S_nick
42.0
new
30.0
CRAP 24.0
« Component/IRC/Plugin/BotAddressed.pm ↑ Summary Component/IRC/Plugin/BotTraffic.pm »
1 package POE::Component::IRC::Plugin::NickReclaim;
2 1 our $AUTHORITY = 'cpan:HINRIK';
3 1 $POE::Component::IRC::Plugin::NickReclaim::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 IRC::Utils qw(parse_user);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POE::Component::IRC::Plugin qw(PCI_EAT_NONE);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 0 ▶ my ($package) = shift;
Subroutine new: not called CC=5 SCAR=34.0
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 0 $args{ lc $_ } = delete $args{$_} for keys %args;
15  
16 0 ▶ if (!defined $args{poll} || $args{poll} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"poll"} and $args{"poll"} =~ /^\d+$/) 0 0 0
Condition0%0/3
defined $args{"poll"} and $args{"poll"} =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"poll"} and $args{"poll"} =~ /^\d+$/
defined $args{"poll"} $args{"poll"} =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $args{"poll"}B= $args{"poll"} =~ /^\d+$/
ABresult
0X0
100
111
17 0 $args{poll} = 30;
18 }
19  
20 0 return bless \%args, $package;
21 }
22  
23 sub PCI_register {
24 0 ▶ my ($self, $irc) = @_;
Subroutine PCI_register: not called CC=1 SCAR=6.9
Pod undocumented
25 0 $irc->plugin_register( $self, 'SERVER', qw(001 433 nick quit) );
26 0 $irc->plugin_register( $self, 'USER', qw(nick) );
27  
28 0 $self->{_desired_nick} = $irc->nick_name();
29 0 return 1;
30 }
31  
32 sub PCI_unregister {
33 0 ▶ return 1;
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
34 }
35  
36 sub U_nick {
37 0 ▶ my $self = shift;
Subroutine U_nick: not called CC=3 SCAR=24.8
Pod undocumented
38 0 my ($nick) = ${ $_[1] } =~ /^NICK +(.+)/i;
39  
40 0 ▶ if (!defined $self->{_temp_nick} || $self->{_temp_nick} ne $nick) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"_temp_nick"} or $self->{"_temp_nick"} ne $nick) 0 0 0
Condition0%0/3
not defined $self->{"_temp_nick"} or $self->{"_temp_nick"} ne $nick
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->{"_temp_nick"} or $self->{"_temp_nick"} ne $nick
not defined $self->{"_temp_nick"} $self->{"_temp_nick"} ne $nick
Truth table0%0/3
not(A) or B
A= not defined $self->{"_temp_nick"}B= $self->{"_temp_nick"} ne $nick
ABresult
1X1
011
000
41 0 delete $self->{_temp_nick};
42 0 $self->{_desired_nick} = $nick;
43 }
44 0 return PCI_EAT_NONE;
45 }
46  
47 sub S_001 {
48 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_001: not called CC=2 SCAR=17.9
Pod undocumented
49 0 ▶ $self->{_reclaimed} = $irc->nick_name eq $self->{_desired_nick} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$irc->nick_name eq $self->{'_desired_nick'} ? : 0 0 0
50 0 return PCI_EAT_NONE;
51 }
52  
53 # ERR_NICKNAMEINUSE
54 sub S_433 {
55 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_433: not called CC=4 SCAR=30.0
Pod undocumented
56 0 my $offending = ${ $_[2] }->[0];
57  
58 0 ▶ if (!$irc->logged_in || $irc->nick_name() eq $offending) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $irc->logged_in or $irc->nick_name eq $offending) 0 0 0
Condition0%0/3
not $irc->logged_in or $irc->nick_name eq $offending
l!l&&r!l&&!r
000
MC/DC0%0/2
not $irc->logged_in or $irc->nick_name eq $offending
not $irc->logged_in $irc->nick_name eq $offending
Truth table0%0/3
not(A) or B
A= not $irc->logged_inB= $irc->nick_name eq $offending
ABresult
1X1
011
000
59 0 my $temp_nick = "${offending}_";
60 0 $self->{_temp_nick} = $temp_nick;
61  
62 0 $irc->yield('nick', $temp_nick);
63 }
64  
65 0 ▶ $irc->delay_remove($self->{_alarm_id}) if defined $self->{_alarm_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_alarm_id"} 0 0 0
66 $self->{_alarm_id} = $irc->delay(
67 ['nick', $self->{_desired_nick} ],
68 $self->{poll}
69 0 );
70  
71 0 return PCI_EAT_NONE;
72 }
73  
74 sub S_quit {
75 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_quit: not called CC=6 SCAR=37.4
Pod undocumented
76 0 my $who = parse_user(${ $_[0] });
77  
78 0 ▶ if ($who eq $irc->nick_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($who eq $irc->nick_name) { } 0 0 0
elsif (not $self->{'_reclaimed'} and $who eq $self->{'_desired_nick'}) { } 0 0 0
79 0 ▶ $irc->delay_remove($self->{_alarm_id}) if defined $self->{_alarm_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_alarm_id"} 0 0 0
80 }
81 ▶ elsif (!$self->{_reclaimed} && $who eq $self->{_desired_nick}) {
Condition0%0/3
not $self->{"_reclaimed"} and $who eq $self->{"_desired_nick"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"_reclaimed"} and $who eq $self->{"_desired_nick"}
not $self->{"_reclaimed"} $who eq $self->{"_desired_nick"}
Truth table0%0/3
not(A) and B
A= not $self->{"_reclaimed"}B= $who eq $self->{"_desired_nick"}
ABresult
0X0
100
111
82 0 ▶ $irc->delay_remove($self->{_alarm_id}) if defined $self->{_alarm_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_alarm_id"} 0 0 0
83 0 $irc->yield('nick', $self->{_desired_nick});
84 }
85  
86 0 return PCI_EAT_NONE;
87 }
88  
89 sub S_nick {
90 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_nick: not called CC=6 SCAR=37.4
Pod undocumented
91 0 my $old_nick = parse_user(${ $_[0] });
92 0 my $new_nick = ${ $_[1] };
93  
94 0 ▶ if ($new_nick eq $irc->nick_name) {
Branch: 2 branches
BranchTrueFalseTotal
if ($new_nick eq $irc->nick_name) { } 0 0 0
elsif ($old_nick eq $self->{'_desired_nick'}) { } 0 0 0
95 0 ▶ if ($new_nick eq $self->{_desired_nick}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_nick eq $self->{"_desired_nick"}) 0 0 0
96 0 $self->{_reclaimed} = 1;
97 0 ▶ $irc->delay_remove($self->{_alarm_id}) if defined $self->{_alarm_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_alarm_id"} 0 0 0
98 }
99 }
100 elsif ($old_nick eq $self->{_desired_nick}) {
101 0 ▶ $irc->delay_remove($self->{_alarm_id}) if defined $self->{_alarm_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_alarm_id"} 0 0 0
102 0 $irc->yield('nick', $self->{_desired_nick});
103 }
104  
105 0 return PCI_EAT_NONE;
106 }
107  
108 1 1;
109  
110 =encoding utf8
111  
112 =head1 NAME
113  
114 POE::Component::IRC::Plugin::NickReclaim - A PoCo-IRC plugin for reclaiming
115 your nickname
116  
117 =head1 SYNOPSIS
118  
119 use strict;
120 use warnings;
121 use POE qw(Component::IRC Component::IRC::Plugin::NickReclaim);
122  
123 my $nickname = 'Flibble' . $$;
124 my $ircname = 'Flibble the Sailor Bot';
125 my $ircserver = 'irc.blahblahblah.irc';
126 my $port = 6667;
127  
128 my $irc = POE::Component::IRC->spawn(
129 nick => $nickname,
130 server => $ircserver,
131 port => $port,
132 ircname => $ircname,
133 ) or die "Oh noooo! $!";
134  
135 POE::Session->create(
136 package_states => [
137 main => [ qw(_start) ],
138 ],
139 );
140  
141 $poe_kernel->run();
142  
143 sub _start {
144 $irc->yield( register => 'all' );
145  
146 # Create and load our NickReclaim plugin, before we connect
147 $irc->plugin_add( 'NickReclaim' =>
148 POE::Component::IRC::Plugin::NickReclaim->new( poll => 30 ) );
149  
150 $irc->yield( connect => { } );
151 return;
152 }
153  
154 =head1 DESCRIPTION
155  
156 POE::Component::IRC::Plugin::NickReclaim - A
157 L<POE::Component::IRC|POE::Component::IRC> plugin automagically deals with
158 your bot's nickname being in use and reclaims it when it becomes available
159 again.
160  
161 It registers and handles 'irc_433' events. On receiving a 433 event it will
162 reset the nickname to the 'nick' specified with C<spawn> or C<connect>,
163 appendedwith an underscore, and then poll to try and change it to the
164 original nickname. If someone in your channel who has the nickname you're
165 after quits or changes nickname, the plugin will try to reclaim it
166 immediately.
167  
168 =head1 METHODS
169  
170 =head2 C<new>
171  
172 Takes one optional argument:
173  
174 B<'poll'>, the number of seconds between nick change attempts, default is 30;
175  
176 Returns a plugin object suitable for feeding to
177 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
178  
179 =head1 AUTHOR
180  
181 Chris 'BinGOs' Williams
182  
183 With amendments applied by Zoffix Znet
184  
185 =head1 SEE ALSO
186  
187 L<POE::Component::IRC|POE::Component::IRC>
188  
189 =cut
« Component/IRC/Plugin/BotAddressed.pm ↑ Summary Component/IRC/Plugin/BotTraffic.pm »