Pocoirc.pm

filter: Statementstmt 12.5% 43 / 342 Branchbran 0.0% 0 / 140 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 34.1% 14 / 41 Podpod 0.0% 0 / 15 totaltotal 9.8% 57 / 579 CC 96 SCAR 43.4
CC
96
Cov
8%
_status
210.0
run
182.0
_load_classes
182.0
CRAP 76.8
« Pocoirc/ReadLine.pm ↑ Summary Pocoirc/Status.pm »
1 package App::Pocoirc;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $App::Pocoirc::AUTHORITY = 'cpan:HINRIK';
4 }
5 {
6 1 $App::Pocoirc::VERSION = '0.47';
7 }
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use App::Pocoirc::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Class::Load qw(try_load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Fcntl qw(O_CREAT O_EXCL O_WRONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Glob ':glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec::Functions 'rel2abs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use IRC::Utils qw(decode_irc);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use POE::Component::Client::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
25 0 my ($package, %args) = @_;
26 0 return bless \%args, $package;
27 }
28  
29 0 ▶sub run {
Subroutine run: not called CC=13 SCAR=52.0
Pod undocumented
30 0 my ($self) = @_;
31  
32 # we print IRC output, which will be UTF-8
33 0 binmode $_, ':utf8' for (*STDOUT, *STDERR);
34  
35 0 ▶ if ($self->{list_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"list_plugins"}) 0 0 0
36 0 require Module::Pluggable;
37 0 Module::Pluggable->import(
38 sub_name => '_available_plugins',
39 search_path => 'POE::Component::IRC::Plugin',
40 );
41 0 for my $plugin (sort $self->_available_plugins()) {
42 0 $plugin =~ s/^POE::Component::IRC::Plugin:://;
43 0 print $plugin, "\n";
44 }
45 0 return;
46 }
47  
48 0 $self->_setup();
49  
50 0 ▶ if ($self->{check_cfg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"check_cfg"}) 0 0 0
51 0 print "The configuration is valid and all modules could be compiled.\n";
52 0 return;
53 }
54  
55 0 ▶ if ($self->{daemonize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"daemonize"}) 0 0 0
56 0 require Proc::Daemon;
57 0 eval {
58 0 Proc::Daemon::Init->();
59 0 ▶ if (defined $self->{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"log_file"}) 0 0 0
60 open STDOUT, '>>:encoding(utf8)', $self->{log_file}
61 0 ▶ or die "Can't open $self->{log_file}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">>:encoding(utf8)", $self->{"log_file"} or die qq[Can't open $self->{"log_file"}: $!\n] 0 0 0
62 0 ▶ open STDERR, '>>&STDOUT' or die "Can't redirect STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">>&STDOUT" or die "Can't redirect STDERR: $!\n" 0 0 0
63 0 STDOUT->autoflush(1);
64 }
65 0 $poe_kernel->has_forked();
66 };
67 0 chomp $@;
68 0 ▶ die "Can't daemonize: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
69 }
70  
71 0 ▶ if (defined $self->{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pid_file"}) 0 0 0
72 0 ▶ sysopen my $fh, $self->{pid_file}, O_CREAT|O_EXCL|O_WRONLY
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $self->{"pid_file"}, 193 or die "Can't create pid file or it already exists. Pocoirc already running?\n" 0 0 0
73 or die "Can't create pid file or it already exists. Pocoirc already running?\n";
74 0 print $fh "$$\n";
75 0 close $fh;
76 }
77  
78 POE::Session->create(
79 0 object_states => [
80 $self => [qw(
81 _start
82 sig_die
83 sig_int
84 sig_term
85 irc_plugin_add
86 irc_plugin_del
87 irc_plugin_error
88 irc_plugin_status
89 irc_network
90 irc_shutdown
91 )],
92 $self => {
93 irc_432 => 'irc_432_or_433',
94 irc_433 => 'irc_432_or_433',
95 },
96 ],
97 );
98  
99 0 $poe_kernel->run();
100 0 ▶ unlink $self->{pid_file} if defined $self->{pid_file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"pid_file"} 0 0 0
101 0 return;
102 }
103  
104 0 ▶sub _setup {
Subroutine _setup: not called CC=8 SCAR=42.8
105 0 my ($self) = @_;
106  
107 0 ▶ if (defined $self->{cfg}{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cfg"}{"pid_file"}) 0 0 0
108 0 $self->{pid_file} = rel2abs(bsd_glob(delete $self->{cfg}{pid_file}));
109 }
110  
111 0 ▶ if (defined $self->{cfg}{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cfg"}{"log_file"}) 0 0 0
112 0 my $log = rel2abs(bsd_glob(delete $self->{cfg}{log_file}));
113 0 ▶ open my $fh, '>>', $log or die "Can't open $log: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $log or die "Can't open ${log}: $!\n" 0 0 0
114 0 close $fh;
115 0 $self->{log_file} = $log;
116 }
117  
118 0 ▶ if (!$self->{no_color}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_color"}) 0 0 0
119 0 require Term::ANSIColor;
120 0 Term::ANSIColor->import();
121 }
122  
123 0 ▶ if (defined $self->{cfg}{lib}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cfg"}{"lib"}) 0 0 0
124 0 ▶ if (ref $self->{cfg}{lib} eq 'ARRAY' && @{ $self->{cfg}{lib} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'cfg'}{'lib'} eq 'ARRAY' and @{$self->{'cfg'}{'lib'};}) { } 0 0 0
Condition0%0/3
ref $self->{"cfg"}{"lib"} eq "ARRAY" and @{$self->{"cfg"}{"lib"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"cfg"}{"lib"} eq "ARRAY" and @{$self->{"cfg"}{"lib"};}
ref $self->{"cfg"}{"lib"} eq "ARRAY" @{$self->{"cfg"}{"lib"};}
Truth table0%0/3
A and B
A= ref $self->{"cfg"}{"lib"} eq "ARRAY"B= @{$self->{"cfg"}{"lib"};}
ABresult
0X0
100
111
125 0 unshift @INC, map { rel2abs(bsd_glob($_)) } @{ delete $self->{cfg}{lib} };
126 }
127 else {
128 0 unshift @INC, rel2abs(bsd_glob(delete $self->{cfg}{lib}));
129 }
130 }
131  
132 0 $self->_load_classes();
133 0 return;
134 }
135  
136 0 ▶sub _load_classes {
Subroutine _load_classes: not called CC=13 SCAR=52.0
137 0 my ($self) = @_;
138  
139 0 ▶ for my $plug_spec (@{ $self->{cfg}{global_plugins} || [] }) {
Condition0%0/2
$self->{'cfg'}{'global_plugins'} || []
l!l
00
MC/DC0%0/1
$self->{'cfg'}{'global_plugins'} || []
$self->{'cfg'}{'global_plugins'}
Truth table0%0/2
A
A= $self->{'cfg'}{'global_plugins'}
Aresult
11
00
140 0 $self->_load_plugin($plug_spec);
141 }
142  
143 0 ▶ while (my ($network, $opts) = each %{ $self->{cfg}{networks} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"cfg"}{"networks"};}) 0 0 0
144 0 ▶ while (my ($opt, $value) = each %{ $self->{cfg} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"cfg"};}) 0 0 0
145 0 ▶ next if $opt =~ /^(?:networks|global_plugins|local_plugins)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt =~ /^(?:networks|global_plugins|local_plugins)$/ 0 0 0
146 0 ▶ $opts->{$opt} = $value if !defined $opts->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{$opt} 0 0 0
147 }
148  
149 0 ▶ for my $plug_spec (@{ $opts->{local_plugins} || [] }) {
Condition0%0/2
$opts->{'local_plugins'} || []
l!l
00
MC/DC0%0/1
$opts->{'local_plugins'} || []
$opts->{'local_plugins'}
Truth table0%0/2
A
A= $opts->{'local_plugins'}
Aresult
11
00
150 0 $self->_load_plugin($plug_spec);
151 }
152  
153 0 ▶ if (!defined $opts->{server}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts->{"server"}) 0 0 0
154 0 die "Server for network '$network' not specified\n";
155 }
156  
157 0 ▶ if (defined $opts->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{'class'}) { } 0 0 0
158 $opts->{class} = _load_either_class(
159 "POE::Component::IRC::$opts->{class}",
160 $opts->{class},
161 0 );
162 }
163 else {
164 0 $opts->{class} = 'POE::Component::IRC::State';
165 0 my ($success, $error) = try_load_class($opts->{class});
166 0 ▶ chomp $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
167 0 ▶ die "Can't load class $opts->{class}: $error\n" if !$success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
168 }
169 }
170  
171 0 return;
172 }
173  
174 # create plugins, spawn components, and connect to IRC
175 0 ▶sub _start {
Subroutine _start: not called CC=2 SCAR=17.9
176 0 my ($kernel, $session, $self) = @_[KERNEL, SESSION, OBJECT];
177  
178 0 $kernel->sig(DIE => 'sig_die');
179 0 $kernel->sig(INT => 'sig_int');
180 0 $kernel->sig(TERM => 'sig_term');
181  
182 0 $self->_status(undef, 'normal', "Started (pid $$)");
183 0 my ($own, $global, $local) = $self->_construct_objects();
184 0 $self->_register_plugins($session->ID(), $own, $global, $local);
185 0 $self->{own_plugins} = $own;
186  
187 0 for my $entry (@{ $self->{ircs} }) {
188 0 my ($network, $irc) = @$entry;
189 0 $self->_status($network, 'normal', 'Connecting to IRC ('.$irc->server.')');
190 0 $irc->yield('connect');
191 }
192  
193 0 return;
194 }
195  
196 0 ▶sub _construct_objects {
Subroutine _construct_objects: not called CC=5 SCAR=34.0
197 0 my ($self) = @_;
198  
199 # create the shared DNS resolver
200 0 $self->{resolver} = POE::Component::Client::DNS->spawn();
201  
202 # construct global plugins
203 0 $self->_status(undef, 'normal', "Constructing global plugins");
204  
205 0 my $global_plugs = $self->_create_plugins(delete $self->{cfg}{global_plugins});
206  
207 my $own_plugs = [
208 [
209 'PocoircStatus',
210 App::Pocoirc::Status->new(
211 Pocoirc => $self,
212 Trace => $self->{trace},
213 Verbose => $self->{verbose},
214 0 ▶ Dynamic => (defined $self->{cfg_file} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'cfg_file'} ? : 0 0 0
215 ),
216 ],
217 ];
218  
219 0 ▶ if ($self->{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"interactive"}) 0 0 0
220 0 require App::Pocoirc::ReadLine;
221 0 push @$own_plugs, [
222 'PocoircReadLine',
223 App::Pocoirc::ReadLine->new(
224 Pocoirc => $self,
225 ),
226 ];
227 }
228  
229 0 my $local_plugs;
230 # construct IRC components
231 0 ▶ while (my ($network, $opts) = each %{ $self->{cfg}{networks} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"cfg"}{"networks"};}) 0 0 0
232 0 my $class = delete $opts->{class};
233  
234 # construct network-specific plugins
235 0 $self->_status($network, 'normal', 'Constructing local plugins');
236 0 $local_plugs->{$network} = $self->_create_plugins(delete $opts->{local_plugins});
237  
238 0 $self->_status($network, 'normal', "Spawning IRC component ($class)");
239 my $irc = $class->spawn(
240 %$opts,
241 Resolver => $self->{resolver},
242 0 );
243 0 my $isa = eval { $irc->isa($class) };
244 0 ▶ die "isa() test failed for component of class $class\n" if !$isa;
Branch: 1 branch
BranchTrueFalseTotal
unless $isa 0 0 0
245 0 push @{ $self->{ircs} }, [$network, $irc];
246 }
247  
248 0 return $own_plugs, $global_plugs, $local_plugs;
249 }
250  
251 0 ▶sub _load_either_class {
Subroutine _load_either_class: not called CC=9 SCAR=45.0
252 0 my ($primary, $secondary) = @_;
253  
254 0 my ($success, $error, @err);
255 0 ($success, $error) = try_load_class($primary);
256 0 ▶ return $primary if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
257  
258 0 push @err, $error;
259 0 ($success, $error) = try_load_class($secondary);
260 0 ▶ return $secondary if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
261  
262 0 ▶ chomp $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
263 0 push @err, $error;
264  
265 0 my $class = "$primary or $secondary";
266 0 ▶ if (@err == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@err == 2) 0 0 0
267 0 ▶ if ($err[0] =~ /^Can't locate / && $err[1] !~ /^Can't locate /) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err[0] =~ /^Can't locate / and not $err[1] =~ /^Can't locate /) { } 0 0 0
elsif ($err[1] =~ /^Can't locate / and not $err[0] =~ /^Can't locate /) { } 0 0 0
Condition0%0/3
$err[0] =~ /^Can't locate / and not $err[1] =~ /^Can't locate /
!ll&&!rl&&r
000
MC/DC0%0/2
$err[0] =~ /^Can't locate / and not $err[1] =~ /^Can't locate /
$err[0] =~ /^Can't locate / not $err[1] =~ /^Can't locate /
Truth table0%0/3
A and not(B)
A= $err[0] =~ /^Can't locate /B= not $err[1] =~ /^Can't locate /
ABresult
0X0
100
111
268 0 $class = $secondary;
269 0 shift @err;
270 }
271 ▶ elsif ($err[1] =~ /^Can't locate / && $err[0] !~ /^Can't locate /) {
Condition0%0/3
$err[1] =~ /^Can't locate / and not $err[0] =~ /^Can't locate /
!ll&&!rl&&r
000
MC/DC0%0/2
$err[1] =~ /^Can't locate / and not $err[0] =~ /^Can't locate /
$err[1] =~ /^Can't locate / not $err[0] =~ /^Can't locate /
Truth table0%0/3
A and not(B)
A= $err[1] =~ /^Can't locate /B= not $err[0] =~ /^Can't locate /
ABresult
0X0
100
111
272 0 $class = $primary;
273 0 pop @err;
274 }
275 }
276 0 my $reason = join "\n", map { " $_" } @err;
277 0 die "Failed to load class $class:\n$reason\n";
278 }
279  
280 0 ▶sub _register_plugins {
Subroutine _register_plugins: not called CC=3 SCAR=24.8
281 0 my ($self, $session_id, $own, $global, $local) = @_;
282  
283 0 for my $entry (@{ $self->{ircs} }) {
284 0 my ($network, $irc) = @$entry;
285 0 $self->_status($network, 'normal', 'Registering plugins');
286  
287 0 for my $plugin (@$own, @$global, @{ $local->{$network} }) {
288 0 my ($name, $object) = @$plugin;
289 0 $irc->plugin_add("${name}_$session_id", $object,
290 network => $network,
291 );
292 }
293 }
294  
295 0 return;
296 }
297  
298 0 ▶sub _dump {
Subroutine _dump: not called CC=8 SCAR=42.8
299 0 my ($arg) = @_;
300  
301 0 ▶ if (ref $arg eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 0 0 0
elsif (ref $arg eq 'HASH') { } 0 0 0
elsif (ref $arg) { } 0 0 0
elsif (defined $arg) { } 0 0 0
302 0 my @elems;
303 0 for my $elem (@$arg) {
304 0 push @elems, _dump($elem);
305 }
306 0 return '['. join(', ', @elems) .']';
307 }
308 elsif (ref $arg eq 'HASH') {
309 0 my @pairs;
310 0 for my $key (keys %$arg) {
311 0 push @pairs, [$key, _dump($arg->{$key})];
312 }
313 0 return '{'. join(', ', map { "$_->[0] => $_->[1]" } @pairs) .'}';
314 }
315 elsif (ref $arg) {
316 0 require overload;
317 0 return overload::StrVal($arg);
318 }
319 elsif (defined $arg) {
320 0 ▶ return $arg if looks_like_number($arg);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($arg) 0 0 0
321 0 return "'".decode_irc($arg)."'";
322 }
323 else {
324 0 return 'undef';
325 }
326 }
327  
328 0 ▶sub _event_debug {
Subroutine _event_debug: not called CC=3 SCAR=24.8
329 0 my ($self, $irc, $args, $event) = @_;
330  
331 0 ▶ if (!defined $event) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event) 0 0 0
332 0 $event = (caller(1))[3];
333 0 $event =~ s/.*:://;
334 }
335  
336 0 my @output;
337 0 for my $i (0..$#{ $args }) {
338 0 push @output, "ARG$i: " . _dump($args->[$i]);
339 }
340 0 $self->_status($irc, 'debug', "$event: ".join(', ', @output));
341 0 return;
342 }
343  
344 # let's log this if it's preventing us from logging in
345 0 ▶sub irc_432_or_433 {
Subroutine irc_432_or_433: not called CC=2 SCAR=17.9
Pod undocumented
346 0 my $self = $_[OBJECT];
347 0 my $irc = $_[SENDER]->get_heap();
348 0 my $reason = decode_irc($_[ARG2]->[1]);
349 0 ▶ return if $irc->logged_in();
Branch: 1 branch
BranchTrueFalseTotal
if $irc->logged_in 0 0 0
350 0 my $nick = $irc->nick_name();
351 0 $self->_status($irc, 'normal', "Login attempt failed: $reason");
352 0 return;
353 }
354  
355 # fetch the server name if we're not using a config file
356 0 ▶sub irc_network {
Subroutine irc_network: not called CC=3 SCAR=24.8
Pod undocumented
357 0 my ($self, $sender, $network) = @_[OBJECT, SENDER, ARG0];
358 0 my $irc = $sender->get_heap();
359  
360 0 for my $idx (0..$#{ $self->{ircs} }) {
361 0 ▶ if ($self->{ircs}[$idx][1] == $irc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ircs"}[$idx][1] == $irc) 0 0 0
362 0 $self->{ircs}[$idx][0] = $network;
363 0 last;
364 }
365 }
366 0 return;
367 }
368  
369 # we handle plugin status messages here because the status plugin won't
370 # see these for previously added plugins or plugin_del for itself, etc
371 0 ▶sub irc_plugin_add {
Subroutine irc_plugin_add: not called CC=2 SCAR=17.9
Pod undocumented
372 0 my ($self, $alias) = @_[OBJECT, ARG0];
373 0 my $irc = $_[SENDER]->get_heap();
374 0 ▶ $self->_event_debug($irc, [@_[ARG0..$#_]], 'S_plugin_add') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
375 0 $self->_status($irc, 'normal', "Added plugin $alias");
376 0 return;
377 }
378  
379 0 ▶sub irc_plugin_del {
Subroutine irc_plugin_del: not called CC=2 SCAR=17.9
Pod undocumented
380 0 my ($self, $alias) = @_[OBJECT, ARG0];
381 0 my $irc = $_[SENDER]->get_heap();
382 0 ▶ $self->_event_debug($irc, [@_[ARG0..$#_]], 'S_plugin_del') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
383 0 $self->_status($irc, 'normal', "Deleted plugin $alias");
384 0 return;
385 }
386  
387 0 ▶sub irc_plugin_error {
Subroutine irc_plugin_error: not called CC=2 SCAR=17.9
Pod undocumented
388 0 my ($self, $error) = @_[OBJECT, ARG0];
389 0 my $irc = $_[SENDER]->get_heap();
390 0 ▶ $self->_event_debug($irc, [@_[ARG0..$#_]], 'S_plugin_error') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
391 0 $self->_status($irc, 'error', $error);
392 0 return;
393 }
394  
395 0 ▶sub irc_plugin_status {
Subroutine irc_plugin_status: not called CC=2 SCAR=17.9
Pod undocumented
396 0 my ($self, $plugin, @args) = @_[OBJECT, ARG0..$#_];
397 0 my $irc = $_[SENDER]->get_heap();
398 0 my $plugins = $irc->plugin_list();
399 0 my %plug2alias = map { $plugins->{$_} => $_ } keys %$plugins;
400  
401 0 ▶ my $extension = ref $plugin eq 'App::Pocoirc::Status'
Branch: 1 branch
BranchTrueFalseTotal
ref $plugin eq 'App::Pocoirc::Status' ? : 0 0 0
402 ? ''
403 : "/$plug2alias{$plugin}";
404 0 $self->_status($self->_irc_to_network($irc).$extension, @args);
405 0 return;
406 }
407  
408 0 ▶sub irc_shutdown {
Subroutine irc_shutdown: not called CC=2 SCAR=17.9
Pod undocumented
409 0 my ($self) = $_[OBJECT];
410 0 my $irc = $_[SENDER]->get_heap();
411 0 ▶ $self->_event_debug($irc, [@_[ARG0..$#_]], 'S_shutdown') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
412 0 $self->_status($irc, 'normal', 'IRC component shut down');
413 0 return;
414 }
415  
416 0 ▶sub verbose {
Subroutine verbose: not called CC=4 SCAR=30.0
Pod undocumented
417 0 my ($self, $value) = @_;
418 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
419 0 $self->{verbose} = $value;
420 0 for my $plugin (@{ $self->{own_plugins} }) {
421 0 ▶ $plugin->[1]->verbose($value) if $plugin->[1]->can('verbose');
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->[1]->can("verbose") 0 0 0
422 }
423 }
424 0 return $self->{verbose};
425 }
426  
427 0 ▶sub trace {
Subroutine trace: not called CC=4 SCAR=30.0
Pod undocumented
428 0 my ($self, $value) = @_;
429 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
430 0 $self->{trace} = $value;
431 0 for my $plugin (@{ $self->{own_plugins} }) {
432 0 ▶ $plugin->[1]->trace($value) if $plugin->[1]->can('trace');
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->[1]->can("trace") 0 0 0
433 }
434 }
435 0 return $self->{trace};
436 }
437  
438 0 ▶sub _status {
Subroutine _status: not called CC=14 SCAR=53.5
439 0 my ($self, $context, $type, $message) = @_;
440  
441 0 my $stamp = strftime('%Y-%m-%d %H:%M:%S', localtime);
442 0 my $irc = eval { $context->isa('POE::Component::IRC') };
443 0 ▶ $context = $self->_irc_to_network($context) if $irc;
Branch: 1 branch
BranchTrueFalseTotal
if $irc 0 0 0
444 0 ▶ $context = defined $context ? " [$context]\t" : ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $context ? : 0 0 0
445  
446 0 ▶ if (defined $type && $type eq 'error') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq "error") 0 0 0
Condition0%0/3
defined $type and $type eq "error"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type eq "error"
defined $type $type eq "error"
Truth table0%0/3
A and B
A= defined $typeB= $type eq "error"
ABresult
0X0
100
111
447 0 $message = "!!! $message";
448 }
449  
450 0 my $log_line = "$stamp$context$message";
451 0 my $term_line = $log_line;
452  
453 0 ▶ if (!$self->{no_color}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_color"}) 0 0 0
454 0 ▶ if (defined $type && $type eq 'error') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $type and $type eq 'error') { } 0 0 0
elsif (defined $type and $type eq 'debug') { } 0 0 0
Condition0%0/3
defined $type and $type eq "error"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type eq "error"
defined $type $type eq "error"
Truth table0%0/3
A and B
A= defined $typeB= $type eq "error"
ABresult
0X0
100
111
455 0 $term_line = colored($term_line, 'red');
456 }
457 ▶ elsif (defined $type && $type eq 'debug') {
Condition0%0/3
defined $type and $type eq "debug"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type eq "debug"
defined $type $type eq "debug"
Truth table0%0/3
A and B
A= defined $typeB= $type eq "debug"
ABresult
0X0
100
111
458 0 $term_line = colored($term_line, 'yellow');
459 }
460 else {
461 0 $term_line = colored($term_line, 'green');
462 }
463 }
464  
465 0 ▶ print $term_line, "\n" if !$self->{daemonize};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"daemonize"} 0 0 0
466 0 ▶ if (defined $self->{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"log_file"}) 0 0 0
467 0 ▶ if (open my $fh, '>>:encoding(utf8)', $self->{log_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (open my $fh, '>>:encoding(utf8)', $self->{'log_file'}) { } 0 0 0
elsif (not $self->{'daemonize'}) { } 0 0 0
468 0 $fh->autoflush(1);
469 0 print $fh $log_line, "\n";
470 0 close $fh;
471 }
472 elsif (!$self->{daemonize}) {
473 0 warn "Can't open $self->{log_file}: $!\n";
474 }
475 }
476 0 return;
477 }
478  
479 0 ▶sub _irc_to_network {
Subroutine _irc_to_network: not called CC=3 SCAR=24.8
480 0 my ($self, $irc) = @_;
481  
482 0 for my $entry (@{ $self->{ircs} }) {
483 0 my ($network, $object) = @$entry;
484 0 ▶ return $network if $irc == $object;
Branch: 1 branch
BranchTrueFalseTotal
if $irc == $object 0 0 0
485 }
486  
487 0 return;
488 }
489  
490 # find out the canonical class name for the plugin and load it
491 0 ▶sub _load_plugin {
Subroutine _load_plugin: not called CC=3 SCAR=24.8
492 0 my ($self, $plug_spec) = @_;
493  
494 0 ▶ return if defined $plug_spec->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $plug_spec->[2] 0 0 0
495 0 my ($class, $args) = @$plug_spec;
496 0 ▶ $args = {} if !defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 0 0
497  
498 0 my $canonclass = _load_either_class(
499 "POE::Component::IRC::Plugin::$class",
500 $class,
501 );
502  
503 0 $plug_spec->[1] = $args;
504 0 $plug_spec->[2] = $canonclass;
505 0 return;
506 }
507  
508 0 ▶sub _create_plugins {
Subroutine _create_plugins: not called CC=3 SCAR=24.8
509 0 my ($self, $plugins) = @_;
510  
511 0 my @return;
512 0 for my $plug_spec (@$plugins) {
513 0 my ($class, $args, $canonclass) = @$plug_spec;
514 0 my $obj = $canonclass->new(%$args);
515 0 my $isa = eval { $obj->isa($canonclass) };
516 0 ▶ die "isa() test failed for plugin of class $canonclass\n" if !$isa;
Branch: 1 branch
BranchTrueFalseTotal
unless $isa 0 0 0
517 0 push @return, [$class, $obj];
518 }
519  
520 0 return \@return;
521 }
522  
523 0 ▶sub sig_die {
Subroutine sig_die: not called CC=2 SCAR=17.9
Pod undocumented
524 0 my ($kernel, $self, $ex) = @_[KERNEL, OBJECT, ARG1];
525 0 chomp $ex->{error_str};
526  
527 my $error = "Event $ex->{event} in session ".$ex->{dest_session}->ID
528 0 ." raised exception:\n $ex->{error_str}";
529  
530 0 $self->_status(undef, 'error', $error);
531 0 ▶ $self->shutdown('Exiting due to an exception') if !$self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
532 0 $kernel->sig_handled();
533 0 return;
534 }
535  
536 0 ▶sub sig_int {
Subroutine sig_int: not called CC=2 SCAR=17.9
Pod undocumented
537 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
538 0 ▶ $self->shutdown('Exiting due to SIGINT') if !$self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
539 0 $kernel->sig_handled();
540 0 return;
541 }
542  
543 0 ▶sub sig_term {
Subroutine sig_term: not called CC=2 SCAR=17.9
Pod undocumented
544 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
545  
546 0 ▶ $self->shutdown('Exiting due to SIGTERM') if !$self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
547 0 $kernel->sig_handled();
548 0 return;
549 }
550  
551 0 ▶sub shutdown {
Subroutine shutdown: not called CC=5 SCAR=34.0
Pod undocumented
552 0 my ($self, $reason) = @_;
553  
554 0 $self->_status(undef, 'normal', $reason);
555  
556 0 my $logged_in;
557 0 for my $irc (@{ $self->{ircs} }) {
558 0 my ($network, $obj) = @$irc;
559  
560 0 ▶ if (!$logged_in && $obj->logged_in()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $logged_in and $obj->logged_in) 0 0 0
Condition0%0/3
not $logged_in and $obj->logged_in
!ll&&!rl&&r
000
MC/DC0%0/2
not $logged_in and $obj->logged_in
not $logged_in $obj->logged_in
Truth table0%0/3
not(A) and B
A= not $logged_inB= $obj->logged_in
ABresult
0X0
100
111
561 0 $logged_in = 1;
562 0 $self->_status(undef, 'normal',
563 'Waiting up to 5 seconds for IRC server(s) to disconnect us');
564 }
565 0 $obj->yield('shutdown', $reason, 5);
566 }
567  
568 0 ▶ $self->{resolver}->shutdown() if $self->{resolver};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"resolver"} 0 0 0
569 0 $self->{shutdown} = 1;
570 0 return;
571 }
572  
573 1 1;
574  
575 =encoding utf8
576  
577 =head1 NAME
578  
579 App::Pocoirc - A command line tool for launching POE::Component::IRC clients
580  
581 =head1 DESCRIPTION
582  
583 This distribution provides a generic way to launch IRC clients which use
584 L<POE::Component::IRC|POE::Component::IRC>. The main features are:
585  
586 =over 4
587  
588 =item * Prints useful status information (to your terminal and/or a log file)
589  
590 =item * Will daemonize if you so wish
591  
592 =item * Supports a configuration file
593  
594 =item * Offers a user friendly way to pass arguments to POE::Component::IRC
595  
596 =item * Supports multiple IRC components and lets you specify which plugins
597 to load locally (one object per component) or globally (single object)
598  
599 =item * Has an interactive mode where you can issue issue commands and
600 call methods on the IRC component(s).
601  
602 It can be used to launch IRC bots or proxies, loaded with plugins of your
603 choice. It is very useful for testing and debugging
604 L<POE::Component::IRC|POE::Component::IRC> plugins as well as IRC servers.
605  
606 =back
607  
608 =head1 CONFIGURATION
609  
610 nick: foobar1234
611 username: foobar
612 log_file: /my/log.file
613 lib: /my/modules
614  
615 global_plugins:
616 - [CTCP]
617  
618 local_plugins:
619 - [BotTraffic]
620  
621 networks:
622 freenode:
623 server: irc.freenode.net
624 local_plugins:
625 - [AutoJoin, { Channels: ['#foodsfdsf'] } ]
626 magnet:
627 server: irc.perl.org
628 nick: hlagherf32fr
629  
630 The configuration file is in L<YAML|YAML> or L<JSON|JSON> format. It consists
631 of a hash containing C<global_plugins>, C<local_plugins>, C<networks>, C<lib>,
632 C<log_file>, C<class>, and default parameters to
633 L<POE::Component::IRC|POE::Component::IRC/spawn>. Only C<networks> is
634 required.
635  
636 C<lib> is either the name of a directory containing Perl modules (e.g.
637 plugins), or an array of such names. Kind of like Perl's I<-I>.
638  
639 C<log_file> is the path to a log file to which status messages will be written.
640  
641 C<class> is the IRC component class. Defaults to
642 L<POE::Component::IRC::State|POE::Component::IRC::State>.
643  
644 =head2 Networks
645  
646 The C<networks> option should be a hash of network hashes. The keys are the
647 names of the networks. A network hash can contain C<local_plugins> and
648 parameters to POE::Component::IRC. None are required, except C<server> if not
649 defined at the top level. The POE::Component::IRC parameters specified in this
650 hash will override the ones specified at the top level.
651  
652 =head2 Plugins
653  
654 The C<global_plugins> and C<local_plugins> options should consist of an array
655 containing the short plugin class name (e.g. 'AutoJoin') and optionally a hash
656 of arguments to that plugin. When figuring out the correct package name,
657 App::Pocoirc will first try to load POE::Component::IRC::Plugin::I<YourPlugin>
658 before trying to load I<YourPlugin>.
659  
660 The plugins in C<global_plugins> will be instantiated once and then added to
661 all IRC components. B<Note:> not all plugins are designed to be used with
662 multiple IRC components simultaneously.
663  
664 If you specify C<local_plugins> at the top level, it will serve as a default
665 list of local plugins, which can be overridden in a network hash.
666  
667 =head1 OUTPUT
668  
669 Here is some example output from the program:
670  
671 $ pocoirc -f example/config.yml
672 2011-04-18 18:10:52 Started (pid 20105)
673 2011-04-18 18:10:52 Constructing global plugins
674 2011-04-18 18:10:52 [freenode] Constructing local plugins
675 2011-04-18 18:10:52 [freenode] Spawning IRC component (POE::Component::IRC::State)
676 2011-04-18 18:10:52 [magnet] Constructing local plugins
677 2011-04-18 18:10:52 [magnet] Spawning IRC component (POE::Component::IRC::State)
678 2011-04-18 18:10:52 [freenode] Registering plugins
679 2011-04-18 18:10:52 [magnet] Registering plugins
680 2011-04-18 18:10:52 [freenode] Connecting to IRC (irc.freenode.net)
681 2011-04-18 18:10:52 [magnet] Connecting to IRC (irc.perl.org)
682 2011-04-18 18:10:52 [freenode] Added plugin Whois3
683 2011-04-18 18:10:52 [freenode] Added plugin ISupport3
684 2011-04-18 18:10:52 [freenode] Added plugin DCC3
685 2011-04-18 18:10:52 [magnet] Added plugin Whois5
686 2011-04-18 18:10:52 [magnet] Added plugin ISupport5
687 2011-04-18 18:10:52 [magnet] Added plugin DCC5
688 2011-04-18 18:10:52 [freenode] Added plugin CTCP1
689 2011-04-18 18:10:52 [freenode] Added plugin AutoJoin1
690 2011-04-18 18:10:52 [freenode] Added plugin PocoircStatus1
691 2011-04-18 18:10:52 [magnet] Added plugin CTCP1
692 2011-04-18 18:10:52 [magnet] Added plugin PocoircStatus1
693 2011-04-18 18:10:52 [magnet] Connected to server irc.perl.org
694 2011-04-18 18:10:52 [magnet] Server notice: *** Looking up your hostname...
695 2011-04-18 18:10:52 [magnet] Server notice: *** Checking Ident
696 2011-04-18 18:10:52 [freenode] Connected to server irc.freenode.net
697 2011-04-18 18:10:53 [magnet] Server notice: *** Found your hostname
698 2011-04-18 18:10:53 [freenode] Server notice: *** Looking up your hostname...
699 2011-04-18 18:10:53 [freenode] Server notice: *** Checking Ident
700 2011-04-18 18:10:53 [freenode] Server notice: *** Couldn't look up your hostname
701 2011-04-18 18:11:03 [magnet] Server notice: *** No Ident response
702 2011-04-18 18:11:03 [magnet] Logged in to server magnet.shadowcat.co.uk with nick hlagherf32fr
703 2011-04-18 18:11:07 [freenode] Server notice: *** No Ident response
704 2011-04-18 18:11:07 [freenode] Logged in to server niven.freenode.net with nick foobar1234
705 2011-04-18 18:11:11 [freenode] Joined channel #foodsfdsf
706 ^C2011-04-18 18:11:22 Exiting due to SIGINT
707 2011-04-18 18:11:22 Waiting up to 5 seconds for IRC server(s) to disconnect us
708 2011-04-18 18:11:22 [magnet] Error from IRC server: Closing Link: 212-30-192-157.static.simnet.is ()
709 2011-04-18 18:11:22 [magnet] Deleted plugin DCC5
710 2011-04-18 18:11:22 [magnet] Deleted plugin ISupport5
711 2011-04-18 18:11:22 [magnet] Deleted plugin CTCP1
712 2011-04-18 18:11:22 [magnet] Deleted plugin Whois5
713 2011-04-18 18:11:22 [magnet] Deleted plugin PocoircStatus1
714 2011-04-18 18:11:22 [magnet] IRC component shut down
715 2011-04-18 18:11:22 [freenode] Quit from IRC (Client Quit)
716 2011-04-18 18:11:22 [freenode] Error from IRC server: Closing Link: 212.30.192.157 (Client Quit)
717 2011-04-18 18:11:22 [freenode] Deleted plugin AutoJoin1
718 2011-04-18 18:11:22 [freenode] Deleted plugin CTCP1
719 2011-04-18 18:11:22 [freenode] Deleted plugin DCC3
720 2011-04-18 18:11:22 [freenode] Deleted plugin PocoircStatus1
721 2011-04-18 18:11:22 [freenode] Deleted plugin Whois3
722 2011-04-18 18:11:22 [freenode] Deleted plugin ISupport3
723 2011-04-18 18:11:22 [freenode] IRC component shut down
724  
725 =head1 AUTHOR
726  
727 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
728  
729 =head1 LICENSE AND COPYRIGHT
730  
731 Copyright 2010 Hinrik E<Ouml>rn SigurE<eth>sson
732  
733 This program is free software, you can redistribute it and/or modify
734 it under the same terms as Perl itself.
735  
736 =cut
« Pocoirc/ReadLine.pm ↑ Summary Pocoirc/Status.pm »