Component/IRC/Plugin/CycleEmpty.pm

filter: Statementstmt 24.6% 18 / 73 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 35.7% 5 / 14 Podpod 25.0% 2 / 8 totaltotal 21.7% 25 / 115 CC 12 SCAR 23.2
CC
12
Cov
19%
_cycle
30.0
S_quit
12.0
new
6.0
CRAP 10.2
« Component/IRC/Plugin/ISupport.pm ↑ Summary Component/IRC/Plugin/FollowTail.pm »
1 package POE::Component::IRC::Plugin::CycleEmpty;
2 1 our $AUTHORITY = 'cpan:HINRIK';
3 1 $POE::Component::IRC::Plugin::CycleEmpty::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 uc_irc );
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=2 SCAR=17.9
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 %self = @_;
14 0 return bless \%self, $package;
15 }
16  
17 sub PCI_register {
18 0 ▶ my ($self, $irc) = @_;
Subroutine PCI_register: not called CC=2 SCAR=17.9
Pod undocumented
19  
20 0 ▶ if (!$irc->isa('POE::Component::IRC::State')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irc->isa("POE::Component::IRC::State")) 0 0 0
21 0 die __PACKAGE__ . " requires PoCo::IRC::State or a subclass thereof";
22 }
23  
24 0 $self->{cycling} = { };
25 0 $self->{irc} = $irc;
26 0 $irc->plugin_register($self, 'SERVER', qw(join kick part quit));
27 0 return 1;
28 }
29  
30 sub PCI_unregister {
31 0 ▶ return 1;
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
32 }
33  
34 sub S_join {
35 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_join: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my $chan = ${ $_[1] };
37 0 delete $self->{cycling}->{$chan};
38 0 return PCI_EAT_NONE;
39 }
40  
41 sub S_kick {
42 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_kick: not called CC=2 SCAR=17.9
Pod undocumented
43 0 my $chan = ${ $_[1] };
44 0 my $victim = ${ $_[2] };
45 0 ▶ $self->_cycle($chan) if $victim ne $irc->nick_name();
Branch: 1 branch
BranchTrueFalseTotal
if $victim ne $irc->nick_name 0 0 0
46 0 return PCI_EAT_NONE;
47 }
48  
49 sub S_part {
50 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_part: not called CC=2 SCAR=17.9
Pod undocumented
51 0 my $parter = parse_user(${ $_[0] });
52 0 my $chan = ${ $_[1] };
53 0 ▶ $self->_cycle($chan) if $parter ne $irc->nick_name();
Branch: 1 branch
BranchTrueFalseTotal
if $parter ne $irc->nick_name 0 0 0
54 0 return PCI_EAT_NONE;
55 }
56  
57 sub S_quit {
58 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_quit: not called CC=3 SCAR=24.8
Pod undocumented
59 0 my $quitter = parse_user(${ $_[0] });
60 0 my $channels = @{ $_[2] }[0];
61 0 ▶ if ($quitter ne $irc->nick_name()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quitter ne $irc->nick_name) 0 0 0
62 0 for my $chan (@{ $channels }) {
63 0 $self->_cycle($chan);
64 }
65 }
66 0 return PCI_EAT_NONE;
67 }
68  
69 sub _cycle {
70 0 ▶ my ($self, $chan) = @_;
Subroutine _cycle: not called CC=5 SCAR=34.0
71 0 my $irc = $self->{irc};
72 0 ▶ if ($irc->channel_list($chan) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irc->channel_list($chan) == 1) 0 0 0
73 0 ▶ if (!$irc->is_channel_operator($chan, $irc->nick_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irc->is_channel_operator($chan, $irc->nick_name)) 0 0 0
74 0 $self->{cycling}->{ uc_irc($chan) } = 1;
75 0 my $topic = $irc->channel_topic($chan);
76 0 $irc->yield(part => $chan);
77 0 $irc->yield(join => $chan => $irc->channel_key($chan));
78 0 ▶ $irc->yield(topic => $chan => $topic->{Value}) if defined $topic->{Value};
Branch: 1 branch
BranchTrueFalseTotal
if defined $topic->{"Value"} 0 0 0
79 0 ▶ $irc->yield(mode => $chan => '+k ' . $irc->channel_key($chan)) if defined $irc->channel_key($chan);
Branch: 1 branch
BranchTrueFalseTotal
if defined $irc->channel_key($chan) 0 0 0
80 }
81 }
82 0 return;
83 }
84  
85 sub is_cycling {
86 0 ▶ my ($self, $value) = @_;
Subroutine is_cycling: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ return 1 if $self->{cycling}->{ uc_irc($value) };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cycling"}{uc_irc($value)} 0 0 0
88 0 return;
89 }
90  
91 1 1;
92  
93 =encoding utf8
94  
95 =head1 NAME
96  
97 POE::Component::IRC::Plugin::CycleEmpty - A PoCo-IRC plugin which cycles
98 channels if they become empty and opless.
99  
100 =head1 SYNOPSIS
101  
102 use POE::Component::IRC::Plugin::CycleEmpty;
103  
104 $irc->plugin_add('CycleEmpty', POE::Component::IRC::Plugin::CycleEmpty->new());
105  
106 =head1 DESCRIPTION
107  
108 POE::Component::IRC::Plugin::CycleEmpty is a L<POE::Component::IRC|POE::Component::IRC>
109 plugin. When a channel member quits, gets kicked, or parts, the plugin will
110 cycle the channel if the IRC component is alone on that channel and is not
111 a channel operator. If there was a topic or a key set on the channel, they
112 will be restored upon rejoining.
113  
114 This is useful for regaining ops in small channels if the IRC network does
115 not have ChanServ or IRCNet's +R channel mode.
116  
117 This plugin requires the IRC component to be
118 L<POE::Component::IRC::State|POE::Component::IRC::State> or a subclass thereof.
119  
120 =head1 METHODS
121  
122 =head2 C<new>
123  
124 Returns a plugin object suitable for feeding to
125 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
126  
127 =head2 C<is_cycling>
128  
129 One argument:
130  
131 A channel name
132  
133 Returns 1 if the plugin is currently cycling that channel, 0 otherwise.
134 Useful if need to ignore the fact that the Component just parted the channel
135 in question.
136  
137 =head1 AUTHOR
138  
139 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
140  
141 =cut
« Component/IRC/Plugin/ISupport.pm ↑ Summary Component/IRC/Plugin/FollowTail.pm »