Component/IRC/Plugin/AutoJoin.pm

filter: Statementstmt 14.4% 18 / 125 Branchbran 0.0% 0 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 29.4% 5 / 17 Podpod 8.3% 1 / 12 totaltotal 11.4% 24 / 210 CC 32 SCAR 34.6
CC
32
Cov
10%
PCI_register
90.0
S_isupport
42.0
S_identified
30.0
CRAP 31.9
« Component/IRC/Plugin/Console.pm ↑ Summary Component/IRC/Plugin/PlugMan.pm »
1 package POE::Component::IRC::Plugin::AutoJoin;
2 1 our $AUTHORITY = 'cpan:HINRIK';
3 1 $POE::Component::IRC::Plugin::AutoJoin::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 lc_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=9 SCAR=45.0
Pod undocumented
19  
20 0 ▶ if (!$self->{Channels}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->{'Channels'}) { } 0 0 0
elsif (ref $self->{'Channels'} eq 'ARRAY') { } 0 0 0
21 0 ▶ if ($irc->isa('POE::Component::IRC::State')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irc->isa('POE::Component::IRC::State')) { } 0 0 0
22 0 for my $chan (keys %{ $irc->channels() }) {
23 0 my $lchan = lc_irc($chan, $irc->isupport('MAPPING'));
24 # note that this will not get the real key on ircu servers
25 # in channels where we don't have ops
26 0 ▶ my $key = $irc->is_channel_mode_set($chan, 'k')
Branch: 1 branch
BranchTrueFalseTotal
$irc->is_channel_mode_set($chan, 'k') ? : 0 0 0
27 ? $irc->channel_key($chan)
28 : ''
29 ;
30  
31 0 $self->{Channels}->{$lchan} = $key;
32 }
33 }
34 else {
35 0 $self->{Channels} = {};
36 }
37 }
38 elsif (ref $self->{Channels} eq 'ARRAY') {
39 0 my %channels;
40 0 $channels{lc_irc($_, $irc->isupport('MAPPING'))} = undef for @{ $self->{Channels} };
41 0 $self->{Channels} = \%channels;
42 }
43  
44 0 $self->{tried_keys} = { };
45 0 ▶ $self->{Rejoin_delay} = 5 if !defined $self->{Rejoin_delay};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Rejoin_delay"} 0 0 0
46 0 ▶ $self->{NickServ_delay} = 5 if !defined $self->{NickServ_delay};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"NickServ_delay"} 0 0 0
47 0 $irc->plugin_register($self, 'SERVER', qw(001 474 isupport chan_mode join kick part identified));
48 0 $irc->plugin_register($self, 'USER', qw(join));
49 0 return 1;
50 }
51  
52 sub PCI_unregister {
53 0 ▶ return 1;
Subroutine PCI_unregister: not called CC=1 SCAR=6.9
Pod undocumented
54 }
55  
56 sub S_001 {
57 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_001: not called CC=1 SCAR=6.9
Pod undocumented
58 0 delete $self->{alarm_ids};
59 0 return PCI_EAT_NONE;
60 }
61  
62 # we join channels after S_isupport in case the server supports
63 # CAPAB IDENTIFY-MSG, so pocoirc can turn it on before we join channels
64 sub S_isupport {
65 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_isupport: not called CC=6 SCAR=37.4
Pod undocumented
66  
67 0 ▶ if (!grep { $_->isa('POE::Component::IRC::Plugin::NickServID') } values %{ $irc->plugin_list() }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not grep({$_->isa('POE::Component::IRC::Plugin::NickServID');} values %{$irc->plugin_list;})) { } 0 0 0
68 # we don't have to wait for NickServ, so let's join
69 0 ▶ while (my ($chan, $key) = each %{ $self->{Channels} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"Channels"};}) 0 0 0
70 0 ▶ $irc->yield(join => $chan => (defined $key ? $key : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
71 }
72 }
73 else {
74 0 ▶ while (my ($chan, $key) = each %{ $self->{Channels} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"Channels"};}) 0 0 0
75 0 ▶ push @{ $self->{alarm_ids} }, $irc->delay(
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
76 [join => $chan => (defined $key ? $key : ())],
77 $self->{NickServ_delay},
78 0 );
79 }
80 }
81 0 return PCI_EAT_NONE;
82 }
83  
84 sub S_identified {
85 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_identified: not called CC=5 SCAR=34.0
Pod undocumented
86  
87 0 ▶ if ($self->{alarm_ids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"alarm_ids"}) 0 0 0
88 0 $irc->delay_remove($_) for @{ $self->{alarm_ids} };
89 0 delete $self->{alarm_ids};
90  
91 0 ▶ while (my ($chan, $key) = each %{ $self->{Channels} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"Channels"};}) 0 0 0
92 0 ▶ $irc->yield(join => $chan => (defined $key ? $key : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
93 }
94 }
95 0 return PCI_EAT_NONE;
96 }
97  
98 # ERR_BANNEDFROMCHAN
99 sub S_474 {
100 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_474: not called CC=4 SCAR=30.0
Pod undocumented
101 0 my $chan = ${ $_[2] }->[0];
102 0 my $lchan = lc_irc($chan, $irc->isupport('MAPPING'));
103 0 ▶ return PCI_EAT_NONE if !$self->{Retry_when_banned};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Retry_when_banned"} 0 0 0
104  
105 0 my $key = $self->{Channels}{$lchan};
106 0 ▶ $key = $self->{tried_keys}{$lchan} if defined $self->{tried_keys}{$lchan};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tried_keys"}{$lchan} 0 0 0
107 0 ▶ $irc->delay([join => $chan => (defined $key ? $key : ())], $self->{Retry_when_banned});
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
108 0 return PCI_EAT_NONE;
109 }
110  
111 sub S_chan_mode {
112 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_chan_mode: not called CC=4 SCAR=30.0
Pod undocumented
113 0 pop @_;
114 0 my $chan = ${ $_[1] };
115 0 my $mode = ${ $_[2] };
116 0 ▶ my $arg = defined $_[3] ? ${ $_[3] } : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 0 0
117 0 my $lchan = lc_irc($chan, $irc->isupport('MAPPING'));
118  
119 0 ▶ $self->{Channels}->{$lchan} = $arg if $mode eq '+k';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "+k" 0 0 0
120 0 ▶ $self->{Channels}->{$lchan} = '' if $mode eq '-k';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "-k" 0 0 0
121 0 return PCI_EAT_NONE;
122 }
123  
124 sub S_join {
125 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_join: not called CC=3 SCAR=24.8
Pod undocumented
126 0 my $joiner = parse_user(${ $_[0] });
127 0 my $chan = ${ $_[1] };
128 0 my $lchan = lc_irc($chan, $irc->isupport('MAPPING'));
129  
130 0 ▶ return PCI_EAT_NONE if $joiner ne $irc->nick_name();
Branch: 1 branch
BranchTrueFalseTotal
if $joiner ne $irc->nick_name 0 0 0
131 0 delete $self->{alarm_ids};
132  
133 0 ▶ if (defined $self->{tried_keys}{$lchan}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'tried_keys'}{$lchan}) { } 0 0 0
134 0 $self->{Channels}->{$lchan} = $self->{tried_keys}{$lchan};
135 0 delete $self->{tried_keys}{$lchan};
136 }
137 else {
138 0 $self->{Channels}->{$lchan} = '';
139 }
140  
141 0 return PCI_EAT_NONE;
142 }
143  
144 sub S_kick {
145 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_kick: not called CC=4 SCAR=30.0
Pod undocumented
146 0 my $chan = ${ $_[1] };
147 0 my $victim = ${ $_[2] };
148 0 my $lchan = lc_irc($chan, $irc->isupport('MAPPING'));
149  
150 0 ▶ if ($victim eq $irc->nick_name()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($victim eq $irc->nick_name) 0 0 0
151 0 ▶ if ($self->{RejoinOnKick}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"RejoinOnKick"}) 0 0 0
152 $irc->delay([
153 'join',
154 $chan,
155 (defined $self->{Channels}->{$lchan} ? $self->{Channels}->{$lchan} : ())
156 0 ▶ ], $self->{Rejoin_delay});
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'Channels'}{$lchan} ? : 0 0 0
157 }
158 0 delete $self->{Channels}->{$lchan};
159 }
160 0 return PCI_EAT_NONE;
161 }
162  
163 sub S_part {
164 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine S_part: not called CC=2 SCAR=17.9
Pod undocumented
165 0 my $parter = parse_user(${ $_[0] });
166 0 my $chan = ${ $_[1] };
167 0 my $lchan = lc_irc($chan, $irc->isupport('MAPPING'));
168  
169 0 ▶ delete $self->{Channels}->{$lchan} if $parter eq $irc->nick_name();
Branch: 1 branch
BranchTrueFalseTotal
if $parter eq $irc->nick_name 0 0 0
170 0 return PCI_EAT_NONE;
171 }
172  
173 sub U_join {
174 0 ▶ my ($self, $irc) = splice @_, 0, 2;
Subroutine U_join: not called CC=2 SCAR=17.9
Pod undocumented
175 0 my (undef, $chan, $key) = split /\s/, ${ $_[0] }, 3;
176 0 my $lchan = lc_irc($chan, $irc->isupport('MAPPING'));
177  
178 0 ▶ $self->{tried_keys}->{$lchan} = $key if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
179 0 return PCI_EAT_NONE;
180 }
181  
182 1 1;
183  
184 =encoding utf8
185  
186 =head1 NAME
187  
188 POE::Component::IRC::Plugin::AutoJoin - A PoCo-IRC plugin which
189 keeps you on your favorite channels
190  
191 =head1 SYNOPSIS
192  
193 use POE qw(Component::IRC::State Component::IRC::Plugin::AutoJoin);
194  
195 my $nickname = 'Chatter';
196 my $server = 'irc.blahblahblah.irc';
197  
198 my %channels = (
199 '#Blah' => '',
200 '#Secret' => 'secret_password',
201 '#Foo' => '',
202 );
203  
204 POE::Session->create(
205 package_states => [
206 main => [ qw(_start irc_join) ],
207 ],
208 );
209  
210 $poe_kernel->run();
211  
212 sub _start {
213 my $irc = POE::Component::IRC::State->spawn(
214 Nick => $nickname,
215 Server => $server,
216 ) or die "Oh noooo! $!";
217  
218 $irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new( Channels => \%channels ));
219 $irc->yield(register => qw(join);
220 $irc->yield(connect => { } );
221 }
222  
223 sub irc_join {
224 my $chan = @_[ARG1];
225 $irc->yield(privmsg => $chan => "hi $channel!");
226 }
227  
228  
229 =head1 DESCRIPTION
230  
231 POE::Component::IRC::Plugin::AutoJoin is a L<POE::Component::IRC|POE::Component::IRC>
232 plugin. If you get disconnected, the plugin will join all the channels you were
233 on the next time it gets connected to the IRC server. It can also rejoin a
234 channel if the IRC component gets kicked from it. It keeps track of channel
235 keys so it will be able to rejoin keyed channels in case of reconnects/kicks.
236  
237 If a L<POE::Component::IRC::Plugin::NickServID|POE::Component::IRC::Plugin::NickServID>
238 plugin has been added to the IRC component, then AutoJoin will wait for a
239 reply from NickServ before joining channels on connect.
240  
241 This plugin requires the IRC component to be
242 L<POE::Component::IRC::State|POE::Component::IRC::State> or a subclass thereof.
243  
244 =head1 METHODS
245  
246 =head2 C<new>
247  
248 Takes the following optional arguments:
249  
250 B<'Channels'>, either an array reference of channel names, or a hash reference
251 keyed on channel name, containing the password for each channel. By default it
252 uses the channels the component is already on if you are using
253 L<POE::Component::IRC::State|POE::Component::IRC::State>.
254  
255 B<'RejoinOnKick'>, set this to 1 if you want the plugin to try to rejoin a
256 channel (once) if you get kicked from it. Default is 0.
257  
258 B<'Rejoin_delay'>, the time, in seconds, to wait before rejoining a channel
259 after being kicked (if B<'RejoinOnKick'> is on). Default is 5.
260  
261 B<'Retry_when_banned'>, if you can't join a channel due to a ban, set this
262 to the number of seconds to wait between retries. Default is 0 (disabled).
263  
264 B<'NickServ_delay'>, how long (in seconds) to wait for a reply from NickServ
265 before joining channels. Default is 5.
266  
267 Returns a plugin object suitable for feeding to
268 L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method.
269  
270 =head1 AUTHOR
271  
272 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
273  
274 =cut
« Component/IRC/Plugin/Console.pm ↑ Summary Component/IRC/Plugin/PlugMan.pm »