Pocosi.pm

filter: Statementstmt 12.7% 43 / 337 Branchbran 0.0% 0 / 138 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 34.7% 16 / 46 Podpod 7.1% 1 / 14 totaltotal 10.7% 60 / 558 CC 92 SCAR 39.4
CC
92
Cov
9%
run
182.0
_status
156.0
_setup
72.0
CRAP 51.7
« Pocosi/ReadLine.pm ↑ Summary Pocosi/Status.pm »
1 package App::Pocosi;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $App::Pocosi::AUTHORITY = 'cpan:HINRIK';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $App::Pocosi::VERSION = '0.03';
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 # we want instant child process reaping
13 4 ▶sub POE::Kernel::USE_SIGCHLD () { return 1 }
Subroutine USE_SIGCHLD: called CC=1 SCAR=0.0
Pod documented
14  
15 1 ▶use App::Pocosi::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Class::Load qw(try_load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Fcntl qw(O_CREAT O_EXCL O_WRONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Glob ':glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use File::Spec::Functions 'rel2abs';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use IRC::Utils qw(decode_irc);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Net::Netmask;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my ($package, %args) = @_;
29 0 return bless \%args, $package;
30 }
31  
32 0 ▶sub run {
Subroutine run: not called CC=13 SCAR=52.0
Pod undocumented
33 0 my ($self) = @_;
34  
35 # we print IRC output, which will be UTF-8
36 0 binmode $_, ':utf8' for (*STDOUT, *STDERR);
37  
38 0 ▶ if ($self->{list_plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"list_plugins"}) 0 0 0
39 0 require Module::Pluggable;
40 0 Module::Pluggable->import(
41 sub_name => '_available_plugins',
42 search_path => 'POE::Component::Server::IRC::Plugin',
43 );
44 0 for my $plugin (sort $self->_available_plugins()) {
45 0 $plugin =~ s/^POE::Component::Server::IRC::Plugin:://;
46 0 print $plugin, "\n";
47 }
48 0 return;
49 }
50  
51 0 $self->_setup();
52  
53 0 ▶ if ($self->{check_cfg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"check_cfg"}) 0 0 0
54 0 print "The configuration is valid and all modules could be compiled.\n";
55 0 return;
56 }
57  
58 0 ▶ if ($self->{daemonize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"daemonize"}) 0 0 0
59 0 require Proc::Daemon;
60 0 eval {
61 0 Proc::Daemon::Init->();
62 0 ▶ if (defined $self->{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"log_file"}) 0 0 0
63 open STDOUT, '>>:encoding(utf8)', $self->{log_file}
64 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
65 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
66 0 STDOUT->autoflush(1);
67 }
68 0 $poe_kernel->has_forked();
69 };
70 0 chomp $@;
71 0 ▶ die "Can't daemonize: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
72 }
73  
74 0 ▶ if (defined $self->{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pid_file"}) 0 0 0
75 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. Pocosi already running?\n" 0 0 0
76 or die "Can't create pid file or it already exists. Pocosi already running?\n";
77 0 print $fh "$$\n";
78 0 close $fh;
79 }
80  
81 POE::Session->create(
82 0 object_states => [
83 $self => [qw(
84 _start
85 sig_die
86 sig_int
87 sig_term
88 ircd_plugin_add
89 ircd_plugin_del
90 ircd_plugin_error
91 ircd_plugin_status
92 ircd_shutdown
93 )],
94 ],
95 );
96  
97 0 $poe_kernel->run();
98 0 ▶ unlink $self->{pid_file} if defined $self->{pid_file};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"pid_file"} 0 0 0
99 0 return;
100 }
101  
102 0 ▶sub _setup {
Subroutine _setup: not called CC=8 SCAR=42.8
103 0 my ($self) = @_;
104  
105 0 ▶ if (defined $self->{cfg}{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cfg"}{"pid_file"}) 0 0 0
106 0 $self->{pid_file} = rel2abs(bsd_glob(delete $self->{cfg}{pid_file}));
107 }
108  
109 0 ▶ if (defined $self->{cfg}{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cfg"}{"log_file"}) 0 0 0
110 0 my $log = rel2abs(bsd_glob(delete $self->{cfg}{log_file}));
111 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
112 0 close $fh;
113 0 $self->{log_file} = $log;
114 }
115  
116 0 ▶ if (!$self->{no_color}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_color"}) 0 0 0
117 0 require Term::ANSIColor;
118 0 Term::ANSIColor->import();
119 }
120  
121 0 ▶ if (defined $self->{cfg}{lib}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cfg"}{"lib"}) 0 0 0
122 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
123 0 unshift @INC, map { rel2abs(bsd_glob($_)) } @{ delete $self->{cfg}{lib} };
124 }
125 else {
126 0 unshift @INC, rel2abs(bsd_glob(delete $self->{cfg}{lib}));
127 }
128 }
129  
130 0 $self->_load_classes();
131 0 return;
132 }
133  
134 0 ▶sub _load_classes {
Subroutine _load_classes: not called CC=7 SCAR=40.3
135 0 my ($self) = @_;
136 0 my $cfg = $self->{cfg};
137  
138 0 ▶ for my $plug_spec (@{ $cfg->{plugins} || [] }) {
Condition0%0/2
$cfg->{'plugins'} || []
l!l
00
MC/DC0%0/1
$cfg->{'plugins'} || []
$cfg->{'plugins'}
Truth table0%0/2
A
A= $cfg->{'plugins'}
Aresult
11
00
139 0 $self->_load_plugin($plug_spec);
140 }
141  
142 0 ▶ if (!defined $cfg->{config}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cfg->{"config"}) 0 0 0
143 0 die "No 'config' parameter found in config file\n";
144 }
145  
146 0 ▶ if (defined $cfg->{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cfg->{'class'}) { } 0 0 0
147 $cfg->{class} = _load_either_class(
148 "POE::Component::Server::IRC::$cfg->{class}",
149 $cfg->{class},
150 0 );
151 }
152 else {
153 0 $cfg->{class} = 'POE::Component::Server::IRC';
154 0 my ($success, $error) = try_load_class($cfg->{class});
155 0 ▶ chomp $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
156 0 ▶ die "Can't load class $cfg->{class}: $error\n" if !$success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 0 0 0
157 }
158  
159 0 return;
160 }
161  
162 # find out the canonical class name for the plugin and load it
163 0 ▶sub _load_plugin {
Subroutine _load_plugin: not called CC=3 SCAR=24.8
164 0 my ($self, $plug_spec) = @_;
165  
166 0 ▶ return if defined $plug_spec->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $plug_spec->[2] 0 0 0
167 0 my ($class, $args) = @$plug_spec;
168 0 ▶ $args = {} if !defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 0 0
169  
170 0 my $canonclass = _load_either_class(
171 "POE::Component::Server::IRC::Plugin::$class",
172 $class,
173 );
174  
175 0 $plug_spec->[1] = $args;
176 0 $plug_spec->[2] = $canonclass;
177 0 return;
178 }
179  
180 # create plugins, spawn components, and connect to IRC
181 0 ▶sub _start {
Subroutine _start: not called CC=1 SCAR=6.9
182 0 my ($kernel, $session, $self) = @_[KERNEL, SESSION, OBJECT];
183  
184 0 $kernel->sig(DIE => 'sig_die');
185 0 $kernel->sig(INT => 'sig_int');
186 0 $kernel->sig(TERM => 'sig_term');
187 0 $self->_status('normal', "Started (pid $$)");
188  
189 0 $self->_status('normal', "Constructing plugins");
190 0 my ($own_plugs, $plugins) = $self->_construct_plugins();
191  
192 0 $self->_status('normal', "Spawning IRCd component ($self->{cfg}{class})");
193 0 my $ircd = $self->_spawn_ircd();
194  
195 0 $self->_status('normal', 'Registering plugins');
196 0 $self->_register_plugins($ircd, $session->ID(), [@$own_plugs, @$plugins]);
197  
198 0 $self->{own_plugins} = $own_plugs;
199 0 $self->{ircd} = $ircd;
200  
201 0 $self->_add_auths();
202 0 $self->_add_operators();
203 0 $self->_add_denials();
204 0 $self->_add_exemptions();
205 0 $self->_add_peers();
206 0 $self->_add_listeners();
207  
208 0 return;
209 }
210  
211 0 ▶sub _construct_plugins {
Subroutine _construct_plugins: not called CC=4 SCAR=30.0
212 0 my ($self) = @_;
213  
214 0 my $plug_specs = $self->{cfg}{plugins};
215 0 my @plugins;
216 0 for my $plug_spec (@$plug_specs) {
217 0 my ($class, $args, $canonclass) = @$plug_spec;
218 0 my $obj = $canonclass->new(%$args);
219 0 my $isa = eval { $obj->isa($canonclass) };
220 0 ▶ die "isa() test failed for plugin of class $canonclass\n" if !$isa;
Branch: 1 branch
BranchTrueFalseTotal
unless $isa 0 0 0
221 0 push @plugins, [$class, $obj];
222 }
223  
224 my @own_plugs = (
225 [
226 'PocosiStatus',
227 App::Pocosi::Status->new(
228 Pocosi => $self,
229 Trace => $self->{trace},
230 Verbose => $self->{verbose},
231 0 ),
232 ],
233 );
234  
235 0 ▶ if ($self->{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"interactive"}) 0 0 0
236 0 require App::Pocosi::ReadLine;
237 0 push @own_plugs, [
238 'PocosiReadLine',
239 App::Pocosi::ReadLine->new(
240 Pocosi => $self,
241 ),
242 ];
243 }
244  
245 0 return \@own_plugs, \@plugins;
246 }
247  
248 0 ▶sub _spawn_ircd {
Subroutine _spawn_ircd: not called CC=4 SCAR=30.0
249 0 my ($self) = @_;
250  
251 0 my $class = $self->{cfg}{class};
252 my $ircd = $class->spawn(
253 plugin_debug => 1,
254 config => $self->{cfg}{config},
255 ($self->{cfg}{flood} ? (antiflood => 0) : ()),
256 0 ▶ (defined $self->{cfg}{auth} ? (auth => $self->{cfg}{auth}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$self->{'cfg'}{'flood'} ? : 0 0 0
defined $self->{'cfg'}{'auth'} ? : 0 0 0
257 );
258 0 my $isa = eval { $ircd->isa($class) };
259 0 ▶ die "isa() test failed for component of class $class\n" if !$isa;
Branch: 1 branch
BranchTrueFalseTotal
unless $isa 0 0 0
260  
261 0 return $ircd;
262 }
263  
264 0 ▶sub _load_either_class {
Subroutine _load_either_class: not called CC=4 SCAR=30.0
265 0 my ($primary, $secondary) = @_;
266  
267 0 my ($success, $error, $errors);
268 0 ($success, $error) = try_load_class($primary);
269 0 ▶ return $primary if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
270  
271 0 $errors .= $error;
272 0 ($success, $error) = try_load_class($secondary);
273 0 ▶ return $secondary if $success;
Branch: 1 branch
BranchTrueFalseTotal
if $success 0 0 0
274  
275 0 ▶ chomp $error if defined $error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $error 0 0 0
276 0 $errors .= $error;
277 0 die "Failed to load class $primary or $secondary: $errors\n";
278 }
279  
280 0 ▶sub _register_plugins {
Subroutine _register_plugins: not called CC=2 SCAR=17.9
281 0 my ($self, $ircd, $session_id, $plugins) = @_;
282  
283 0 for my $plugin (@$plugins) {
284 0 my ($name, $object) = @$plugin;
285 0 $ircd->plugin_add("${name}_$session_id", $object);
286 }
287  
288 0 return;
289 }
290  
291 0 ▶sub _add_denials {
Subroutine _add_denials: not called CC=4 SCAR=30.0
292 0 my ($self) = @_;
293 0 my $ircd = $self->{ircd};
294 0 my $denials = $self->{cfg}{denials};
295 0 ▶ return if !defined $denials;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $denials 0 0 0
296  
297 0 for my $denial (@$denials) {
298 0 my ($mask, $reason) = @$denial;
299 0 my $netmask = Net::Netmask->new2($mask);
300 0 ▶ if (!defined $netmask) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $netmask) 0 0 0
301 0 die "Invalid denial: $mask\n";
302 }
303 0 $ircd->add_denial($netmask, $reason);
304 }
305 0 return;
306 }
307  
308 0 ▶sub _add_exemptions {
Subroutine _add_exemptions: not called CC=4 SCAR=30.0
309 0 my ($self) = @_;
310 0 my $ircd = $self->{ircd};
311 0 my $exemptions = $self->{cfg}{exemptions};
312 0 ▶ return if !defined $exemptions;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exemptions 0 0 0
313  
314 0 for my $mask (@$exemptions) {
315 0 my $netmask = Net::Netmask->new2($mask);
316 0 ▶ if (!defined $netmask) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $netmask) 0 0 0
317 0 die "Invalid exemption: $mask\n";
318 }
319 0 $ircd->add_exemption($netmask);
320 }
321 0 return;
322 }
323  
324 0 ▶sub _add_operators {
Subroutine _add_operators: not called CC=7 SCAR=40.3
325 0 my ($self) = @_;
326 0 my $ircd = $self->{ircd};
327 0 my $opers = $self->{cfg}{operators};
328 0 ▶ return if !defined $opers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opers 0 0 0
329  
330 0 for my $oper (@$opers) {
331 0 ▶ die "No username supplier for operator\n" if !defined $oper->{username};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oper->{"username"} 0 0 0
332 0 ▶ if (ref $oper->{ipmask} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oper->{"ipmask"} eq "ARRAY") 0 0 0
333 0 my @netmasks;
334 0 for my $mask (@{ $oper->{ipmask} }) {
335 0 my $netmask = Net::Netmask->new2($mask);
336 0 ▶ if (!defined $netmask) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $netmask) 0 0 0
337 0 die "Invalid netmask for oper $oper->{username}: $mask\n";
338 }
339 0 push @netmasks, $netmask;
340 }
341 0 $oper->{ipmask} = \@netmasks;
342 }
343  
344 0 $ircd->add_operator(%$oper);
345 }
346 0 return;
347 }
348  
349 0 ▶sub _add_peers {
Subroutine _add_peers: not called CC=3 SCAR=24.8
350 0 my ($self) = @_;
351 0 my $ircd = $self->{ircd};
352 0 my $peers = $self->{cfg}{peers};
353 0 ▶ return if !defined $peers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peers 0 0 0
354 0 $ircd->add_peer(%$_) for @$peers;
355 0 return;
356 }
357  
358 0 ▶sub _add_auths {
Subroutine _add_auths: not called CC=3 SCAR=24.8
359 0 my ($self) = @_;
360 0 my $ircd = $self->{ircd};
361 0 my $auths = $self->{cfg}{auths};
362 0 ▶ return if !defined $auths;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auths 0 0 0
363 0 $ircd->add_auth(%$_) for @$auths;
364 0 return;
365 }
366  
367 0 ▶sub _add_listeners {
Subroutine _add_listeners: not called CC=3 SCAR=24.8
368 0 my ($self) = @_;
369 0 my $ircd = $self->{ircd};
370 0 my $listeners = $self->{cfg}{listeners};
371 0 ▶ return if !defined $listeners;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $listeners 0 0 0
372 0 $ircd->yield('add_listener', %$_) for @$listeners;
373 0 return;
374 }
375  
376 0 ▶sub _dump {
Subroutine _dump: not called CC=8 SCAR=42.8
377 0 my ($arg) = @_;
378  
379 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
380 0 my @elems;
381 0 for my $elem (@$arg) {
382 0 push @elems, _dump($elem);
383 }
384 0 return '['. join(', ', @elems) .']';
385 }
386 elsif (ref $arg eq 'HASH') {
387 0 my @pairs;
388 0 for my $key (keys %$arg) {
389 0 push @pairs, [$key, _dump($arg->{$key})];
390 }
391 0 return '{'. join(', ', map { "$_->[0] => $_->[1]" } @pairs) .'}';
392 }
393 elsif (ref $arg) {
394 0 require overload;
395 0 return overload::StrVal($arg);
396 }
397 elsif (defined $arg) {
398 0 ▶ return $arg if looks_like_number($arg);
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($arg) 0 0 0
399 0 return "'".decode_irc($arg)."'";
400 }
401 else {
402 0 return 'undef';
403 }
404 }
405  
406 0 ▶sub _event_debug {
Subroutine _event_debug: not called CC=3 SCAR=24.8
407 0 my ($self, $args, $event) = @_;
408  
409 0 ▶ if (!defined $event) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $event) 0 0 0
410 0 $event = (caller(1))[3];
411 0 $event =~ s/.*:://;
412 }
413  
414 0 my @output;
415 0 for my $i (0..$#{ $args }) {
416 0 push @output, "ARG$i: " . _dump($args->[$i]);
417 }
418 0 $self->_status('debug', "$event: ".join(', ', @output));
419 0 return;
420 }
421  
422 # we handle plugin status messages here because the status plugin won't
423 # see these for previously added plugins or plugin_del for itself, etc
424 0 ▶sub ircd_plugin_add {
Subroutine ircd_plugin_add: not called CC=2 SCAR=17.9
Pod undocumented
425 0 my ($self, $alias) = @_[OBJECT, ARG0];
426 0 ▶ $self->_event_debug([@_[ARG0..$#_]], 'IRCD_plugin_add') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
427 0 $self->_status('normal', "Added plugin $alias");
428 0 return;
429 }
430  
431 0 ▶sub ircd_plugin_del {
Subroutine ircd_plugin_del: not called CC=2 SCAR=17.9
Pod undocumented
432 0 my ($self, $alias) = @_[OBJECT, ARG0];
433 0 ▶ $self->_event_debug([@_[ARG0..$#_]], 'IRCD_plugin_del') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
434 0 $self->_status('normal', "Deleted plugin $alias");
435 0 return;
436 }
437  
438 0 ▶sub ircd_plugin_error {
Subroutine ircd_plugin_error: not called CC=2 SCAR=17.9
Pod undocumented
439 0 my ($self, $error) = @_[OBJECT, ARG0];
440 0 ▶ $self->_event_debug([@_[ARG0..$#_]], 'IRCD_plugin_error') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
441 0 $self->_status('error', $error);
442 0 return;
443 }
444  
445 0 ▶sub ircd_plugin_status {
Subroutine ircd_plugin_status: not called CC=2 SCAR=17.9
Pod undocumented
446 0 my ($self, $plugin, $type, $status) = @_[OBJECT, ARG0..ARG2];
447 0 my $ircd = $_[SENDER]->get_heap();
448 0 my $plugins = $ircd->plugin_list();
449 0 my %plug2alias = map { $plugins->{$_} => $_ } keys %$plugins;
450  
451 0 ▶ if (ref $plugin ne 'App::Pocosi::Status') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $plugin ne "App::Pocosi::Status") 0 0 0
452 0 $status = "[$plug2alias{$plugin}] $status";
453 }
454 0 $self->_status($type, $status);
455 0 return;
456 }
457  
458 0 ▶sub ircd_shutdown {
Subroutine ircd_shutdown: not called CC=2 SCAR=17.9
Pod undocumented
459 0 my ($self) = $_[OBJECT];
460 0 ▶ $self->_event_debug([@_[ARG0..$#_]], 'IRCD_shutdown') if $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trace"} 0 0 0
461 0 $self->_status('normal', 'IRCd component shut down');
462 0 return;
463 }
464  
465 0 ▶sub verbose {
Subroutine verbose: not called CC=4 SCAR=30.0
Pod undocumented
466 0 my ($self, $value) = @_;
467 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
468 0 $self->{verbose} = $value;
469 0 for my $plugin (@{ $self->{own_plugins} }) {
470 0 ▶ $plugin->[1]->verbose($value) if $plugin->[1]->can('verbose');
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->[1]->can("verbose") 0 0 0
471 }
472 }
473 0 return $self->{verbose};
474 }
475  
476 0 ▶sub trace {
Subroutine trace: not called CC=4 SCAR=30.0
Pod undocumented
477 0 my ($self, $value) = @_;
478 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
479 0 $self->{trace} = $value;
480 0 for my $plugin (@{ $self->{own_plugins} }) {
481 0 ▶ $plugin->[1]->trace($value) if $plugin->[1]->can('trace');
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->[1]->can("trace") 0 0 0
482 }
483 }
484 0 return $self->{trace};
485 }
486  
487 0 ▶sub _status {
Subroutine _status: not called CC=12 SCAR=50.5
488 0 my ($self, $type, $message) = @_;
489  
490 0 my $stamp = strftime('%Y-%m-%d %H:%M:%S', localtime);
491 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
492 0 $message = "!!! $message";
493 }
494  
495 0 my $log_line = "$stamp $message";
496 0 my $term_line = $log_line;
497  
498 0 ▶ if (!$self->{no_color}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_color"}) 0 0 0
499 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
500 0 $term_line = colored($term_line, 'red');
501 }
502 ▶ 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
503 0 $term_line = colored($term_line, 'yellow');
504 }
505 else {
506 0 $term_line = colored($term_line, 'green');
507 }
508 }
509  
510 0 ▶ print $term_line, "\n" if !$self->{daemonize};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"daemonize"} 0 0 0
511 0 ▶ if (defined $self->{log_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"log_file"}) 0 0 0
512 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
513 0 $fh->autoflush(1);
514 0 print $fh $log_line, "\n";
515 0 close $fh;
516 }
517 elsif (!$self->{daemonize}) {
518 0 warn "Can't open $self->{log_file}: $!\n";
519 }
520 }
521 0 return;
522 }
523  
524 0 ▶sub sig_die {
Subroutine sig_die: not called CC=2 SCAR=17.9
Pod undocumented
525 0 my ($kernel, $self, $ex) = @_[KERNEL, OBJECT, ARG1];
526 0 $kernel->sig_handled();
527  
528 0 chomp $ex->{error_str};
529 my $error = "Event $ex->{event} in session ".$ex->{dest_session}->ID
530 0 ." raised exception:\n $ex->{error_str}";
531  
532 0 $self->_status('error', $error);
533 0 ▶ $self->shutdown('Exiting due to an exception') if !$self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
534 0 return;
535 }
536  
537 0 ▶sub sig_int {
Subroutine sig_int: not called CC=2 SCAR=17.9
Pod undocumented
538 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
539 0 ▶ $self->shutdown('Exiting due to SIGINT') if !$self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
540 0 $kernel->sig_handled();
541 0 return;
542 }
543  
544 0 ▶sub sig_term {
Subroutine sig_term: not called CC=2 SCAR=17.9
Pod undocumented
545 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
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=3 SCAR=24.8
Pod undocumented
552 0 my ($self, $reason) = @_;
553 0 ▶ return if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 0 0
554 0 $self->_status('normal', $reason);
555 0 ▶ $self->{ircd}->shutdown() if $self->{ircd};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ircd"} 0 0 0
556 0 $self->{shutdown} = 1;
557 0 return;
558 }
559  
560 1 1;
561  
562 =encoding utf8
563  
564 =head1 NAME
565  
566 App::Pocosi - A command line tool for launching a POE::Component::Server::IRC instance
567  
568 =head1 DESCRIPTION
569  
570 This distribution provides a generic way to launch a
571 L<POE::Component::Server::IRC|POE::Component::Server::IRC> instance.
572  
573 =over 4
574  
575 =item * Prints useful status information (to your terminal and/or a log file)
576  
577 =item * Will daemonize if you so wish
578  
579 =item * Supports a configuration file
580  
581 =item * Offers a user friendly way to pass arguments to POE::Component::Server::IRC
582  
583 =item * Has an interactive mode where you can issue issue commands and
584 call methods on the IRCd component.
585  
586 =back
587  
588 =head1 CONFIGURATION
589  
590 class: POE::Component::Server::IRC
591 log_file: /my/log.file
592 pid_file: /my/pid.file
593 lib: /my/modules
594 flood: false
595 auth: true
596  
597 config:
598 servername: myserver.com
599 motd:
600 - "Welcome to this great server"
601 - ""
602 - "Enjoy your stay"
603  
604 plugins:
605 - [OperServ]
606  
607 listeners:
608 - bindaddr: "127.0.0.1"
609 port: 10023
610  
611 denials:
612 - ["12.34.56.0/24", "I don't like this IP block"]
613  
614 exemptions:
615 - "12.34.56.78"
616  
617 operators:
618 - username: jack
619 password: foo
620 ipmask: ["127.0.0.1", "1.2.3.4", "192.168.1.0/24"]
621 - username: locke
622 password: bar
623 ipmask: "10.0.0.*"
624  
625 auths:
626 - mask: "*@example.com"
627 password: hlagh
628 spoof: jacob
629 no_tilde: true
630  
631 peers:
632 - name: otherserver.com
633 rpass: hlaghpass
634 pass: hlaghpass
635 type: r
636 raddress: "127.0.0.1"
637 rport: 12345
638 auto: true
639  
640 The configuration file is in L<YAML|YAML> or L<JSON|JSON> format. It consists
641 of a hash containing the options described in the above code example. Only
642 C<config> is required.
643  
644 =head2 C<lib>
645  
646 Either the name of a directory containing Perl modules (e.g. plugins), or an
647 array of such names. Kind of like Perl's I<-I>.
648  
649 =head2 C<pid_file>
650  
651 Path to a pid file, as used by most daemons. If is specified, App::Pocosi
652 will refuse to run if the file already exists.
653  
654 =head2 C<log_file>
655  
656 Path to a log file to which status messages will be written.
657  
658 =head2 C<class>
659  
660 The IRC server component class. Defaults to
661 L<POE::Component::Server::IRC::State|POE::Component::Server::IRC::State>.
662  
663 =head2 C<config>
664  
665 This is a hash of various configuration variables for the IRCd. See
666 PoCo-Server-IRC's L<C<configure>|POE::Component::Server::IRC/configure>
667 for a list of parameters.
668  
669 =head2 C<plugins>
670  
671 An array of arrays containing a short plugin class name (e.g. 'OperServ')
672 and optionally a hash of arguments to that plugin. When figuring out the
673 correct package name, App::Pocosi will first try to load
674 POE::Component::Server::IRC::Plugin::I<YourPlugin> before trying to load
675 I<YourPlugin>.
676  
677 =head2 C<listeners>
678  
679 An array of hashes. The keys should be any of the options listed in the docs
680 for PoCo-Server-IRC-Backend's
681 L<C<add_listener>|POE::Component::Server::IRC::Backend/add_listener> method.
682  
683 =head2 C<auths>
684  
685 An array of hashes. The keys are described in the docs for PoCo-Server-IRC's
686 L<C<add_auth>|POE::Component::Server::IRC/add_auth> method.
687  
688 =head2 C<operators>
689  
690 An array of hashes. The keys are described in the docs for PoCo-Server-IRC's
691 L<C<add_operator>|POE::Component::Server::IRC/add_operator> method. You
692 you can supply an array of netmasks (the kind accepted by
693 L<Net::Netmask|Net::Netmask>'s constructor) for the B<'ipmask'> key.
694  
695 =head2 C<peers>
696  
697 An array of hashes. The keys should be any of the options listed in the docs
698 for PoCo-Server-IRC's
699 L<C<add_peer>|POE::Component::Server::IRC/add_listener> method.
700  
701 =head2 C<denials>
702  
703 An array of arrays. The first element of the inner array should be a netmask
704 accepted by L<Net::Netmask|Net::Netmask>'s constructor. The second
705 (optional) element should be a reason for the denial.
706  
707 =head2 C<exemptions>
708  
709 An array of netmasks (the kind which L<Net::Netmask|Net::Netmask>'s
710 constructor accepts).
711  
712 =head1 OUTPUT
713  
714 Here is some example output from the program:
715  
716 $ pocosi -f example/config.yml
717 2011-05-22 15:30:02 Started (pid 13191)
718 2011-05-22 15:30:02 Constructing plugins
719 2011-05-22 15:30:02 Spawning IRCd component (POE::Component::Server::IRC)
720 2011-05-22 15:30:02 Registering plugins
721 2011-05-22 15:30:02 Added plugin PocosiStatus_1
722 2011-05-22 15:30:02 Added plugin OperServ_1
723 2011-05-22 15:30:02 Started listening on 127.0.0.1:10023
724 2011-05-22 15:30:02 Connected to peer otherserver.com on 127.0.0.1:12345
725 2011-05-22 15:30:02 Server otherserver.com (hops: 1) introduced to the network by myserver.com
726 ^C2011-05-22 15:30:18 Exiting due to SIGINT
727 2011-05-22 15:30:18 Deleted plugin OperServ_1
728 2011-05-22 15:30:18 Deleted plugin PocosiStatus_1
729 2011-05-22 15:30:18 IRCd component shut down
730  
731 =head1 AUTHOR
732  
733 Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com
734  
735 =head1 LICENSE AND COPYRIGHT
736  
737 Copyright 2011 Hinrik E<Ouml>rn SigurE<eth>sson
738  
739 This program is free software, you can redistribute it and/or modify
740 it under the same terms as Perl itself.
741  
742 =cut
« Pocosi/ReadLine.pm ↑ Summary Pocosi/Status.pm »