blib/lib/Bot/Cobalt/Core.pm

filter: Statementstmt 47.7% 84 / 176 Branchbran 7.1% 2 / 28 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 56.1% 23 / 41 Podpod 37.5% 3 / 8 totaltotal 41.6% 112 / 269 CC 21 SCAR 23.2
CC
21
Cov
40%
core_timer_check_pool
42.0
init
30.0
syndicator_started
20.0
CRAP 10.1
« blib/lib/Bot/Cobalt/Frontend/Utils.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Games/Roulette.pm »
1 package Bot::Cobalt::Core;
2 1 $Bot::Cobalt::Core::VERSION = '0.021003';
3 ## This is the core Syndicator singleton.
4  
5 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Bot::Cobalt::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Bot::Cobalt::IRC;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Bot::Cobalt::Lang;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Bot::Cobalt::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Bot::Cobalt::Core::ContextMeta::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Bot::Cobalt::Core::ContextMeta::Ignore;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Bot::Cobalt::Core::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Types::Path::Tiny -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 has cfg => (
30 required => 1,
31 is => 'rw',
32 isa => InstanceOf['Bot::Cobalt::Conf'],
33 );
34  
35 1 has var => (
36 required => 1,
37 is => 'ro',
38 isa => Path,
39 coerce => 1,
40 );
41  
42 has etc => (
43 lazy => 1,
44 is => 'ro',
45 isa => Path,
46 coerce => 1,
47 1 ▶ builder => sub { shift->cfg->etc },
Subroutine _build_etc: called CC=1 SCAR=0.0
48 1 );
49  
50 has log => (
51 lazy => 1,
52 is => 'rw',
53 isa => HasMethods[qw/debug info warn error/],
54 1 ▶ builder => sub {
Subroutine _build_log: called CC=3 SCAR=12.3
55 1 my ($self) = @_;
56 1 my %opts = (
57 level => $self->loglevel,
58 );
59 1 ▶ if (my $log_format = $self->cfg->core->opts->{LogFormat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_format = $self->cfg->core->opts->{"LogFormat"}) 0 1 1
60 0 $opts{log_format} = $log_format
61 }
62 1 ▶ if (my $log_time_fmt = $self->cfg->core->opts->{LogTimeFormat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log_time_fmt = $self->cfg->core->opts->{"LogTimeFormat"}) 0 1 1
63 0 $opts{time_format} = $log_time_fmt
64 }
65 1 Bot::Cobalt::Logger->new( %opts )
66 },
67 1 );
68  
69 has loglevel => (
70 is => 'rw',
71 isa => Str,
72 1 ▶ builder => sub { 'info' },
Subroutine _build_loglevel: called CC=1 SCAR=0.0
73 1 );
74  
75 has detached => (
76 lazy => 1,
77 is => 'ro',
78 isa => Int,
79 0 ▶ builder => sub { 0 },
Subroutine _build_detached: not called CC=1 SCAR=6.9
80 1 );
81  
82 has debug => (
83 lazy => 1,
84 isa => Int,
85 is => 'rw',
86 0 ▶ builder => sub { 0 },
Subroutine _build_debug: not called CC=1 SCAR=6.9
87 1 );
88  
89 ## version/url used for var replacement:
90 has version => (
91 lazy => 1,
92 is => 'rwp',
93 isa => Str,
94 0 ▶ builder => sub { __PACKAGE__->VERSION // 'vcs' }
Subroutine _build_version: not called CC=2 SCAR=17.9
Condition0%0/2
__PACKAGE__->VERSION // "vcs"
l!l
00
MC/DC0%0/1
__PACKAGE__->VERSION // "vcs"
__PACKAGE__->VERSION
Truth table0%0/2
A
A= __PACKAGE__->VERSION
Aresult
11
00
95 1 );
96  
97 has url => (
98 lazy => 1,
99 is => 'rwp',
100 isa => Str,
101 0 ▶ builder => sub { "http://www.metacpan.org/release/Bot-Cobalt" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 1 );
103  
104 has langset => (
105 lazy => 1,
106 is => 'ro',
107 isa => InstanceOf['Bot::Cobalt::Lang'],
108 writer => 'set_langset',
109 1 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
110 1 my ($self) = @_;
111 1 Bot::Cobalt::Lang->new(
112 use_core => 1,
113 lang_dir => path( $self->etc .'/langs' ),
114 lang => $self->cfg->core->language,
115 )
116 },
117 1 );
118  
119 has lang => (
120 lazy => 1,
121 is => 'ro',
122 isa => HashObj,
123 coerce => 1,
124 writer => 'set_lang',
125 1 ▶ builder => sub {
Subroutine _build_lang: called CC=1 SCAR=0.0
126 1 my ($self) = @_;
127 1 $self->langset->rpls
128 },
129 1 );
130  
131 has State => (
132 lazy => 1,
133 ## global 'heap' of sorts
134 is => 'ro',
135 isa => HashObj,
136 coerce => 1,
137 0 ▶ builder => sub {
Subroutine _build_State: not called CC=1 SCAR=6.9
138 {
139 0 HEAP => { },
140 StartedTS => time(),
141 Counters => {
142 Sent => 0,
143 },
144  
145 # nonreloadable plugin list keyed on alias for plugin mgrs:
146 NonReloadable => { },
147 }
148 },
149 1 );
150  
151 has PluginObjects => (
152 lazy => 1,
153 ## alias -> object mapping
154 is => 'rw',
155 isa => HashObj,
156 coerce => 1,
157 0 ▶ builder => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 1 );
159  
160 has Provided => (
161 lazy => 1,
162 ## Some plugins provide optional functionality.
163 ## This hash lets other plugins see if an event is available.
164 is => 'ro',
165 isa => HashObj,
166 coerce => 1,
167 0 ▶ builder => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 1 );
169  
170 has auth => (
171 lazy => 1,
172 is => 'rw',
173 isa => Object,
174 1 ▶ builder => sub {
Subroutine _build_auth: called CC=1 SCAR=0.0
175 1 Bot::Cobalt::Core::ContextMeta::Auth->new
176 },
177 1 );
178  
179 has ignore => (
180 lazy => 1,
181 is => 'rw',
182 isa => Object,
183 1 ▶ builder => sub {
Subroutine _build_ignore: called CC=1 SCAR=0.0
184 1 Bot::Cobalt::Core::ContextMeta::Ignore->new
185 },
186 1 );
187  
188 ## FIXME not documented
189 has resolver => (
190 lazy => 1,
191 is => 'rwp',
192 isa => Object,
193 0 ▶ builder => sub {
Subroutine _build_resolver: not called CC=1 SCAR=6.9
194 0 POE::Component::Client::DNS->spawn(
195 Alias => 'core_resolver',
196 )
197 },
198 1 );
199  
200  
201 1 extends 'POE::Component::Syndicator';
202 1 with 'Bot::Cobalt::Core::Role::Singleton';
203 1 with 'Bot::Cobalt::Core::Role::EasyAccessors';
204 1 with 'Bot::Cobalt::Core::Role::Timers';
205 1 with 'Bot::Cobalt::Core::Role::IRC';
206  
207  
208 ## FIXME test needed:
209 0 ▶sub rpl {
Subroutine rpl: not called CC=3 SCAR=24.8
Pod undocumented
210 0 my ($self, $rpl) = splice @_, 0, 2;
211  
212 0 ▶ confess "rpl() method requires a RPL tag"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rpl 0 0 0
213 unless defined $rpl;
214  
215 0 ▶ my $string = $self->lang->{$rpl}
Condition0%0/2
$self->lang->{$rpl} // (return "Unknown RPL $rpl, vars: " . join(" ", @_))
l!l
00
MC/DC0%0/1
$self->lang->{$rpl} // (return "Unknown RPL $rpl, vars: " . join(" ", @_))
$self->lang->{$rpl}
Truth table0%0/2
A
A= $self->lang->{$rpl}
Aresult
11
00
216 // return "Unknown RPL $rpl, vars: ".join(' ', @_);
217  
218 0 rplprintf( $string, @_ )
219 }
220  
221 0 ▶sub init {
Subroutine init: not called CC=5 SCAR=34.0
Pod undocumented
222 0 my ($self) = @_;
223  
224 my $logfile = $self->cfg->core->paths->{Logfile}
225 0 ▶ // path( $self->var .'/cobalt.log' );
Condition0%0/3
$self->cfg->core->paths->{"Logfile"} // path($self->var . '/cobalt.log')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->cfg->core->paths->{"Logfile"} // path($self->var . '/cobalt.log')
$self->cfg->core->paths->{"Logfile"} path($self->var . '/cobalt.log')
Truth table0%0/3
A // B
A= $self->cfg->core->paths->{"Logfile"}B= path($self->var . '/cobalt.log')
ABresult
1X1
011
000
226  
227 0 ▶ if ($self->detached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->detached) { } 0 0 0
228 # Presumably our frontend closed these
229 0 ▶ open STDOUT, '>>', $logfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">>", $logfile or die $! 0 0 0
230 0 ▶ open STDERR, '>>', $logfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">>", $logfile or die $! 0 0 0
231 } else {
232 0 $self->log->output->add(
233 'screen' => {
234 type => 'Term',
235 },
236 );
237 }
238  
239 0 $self->log->output->add(
240 'logfile' => {
241 type => 'File',
242 file => $logfile,
243 },
244 );
245  
246 ## Language set check. Force attrib fill.
247 0 $self->lang;
248  
249 0 $self->_syndicator_init(
250 prefix => 'ev_', ## event prefix for sessions
251 reg_prefix => 'Cobalt_',
252 types => [ SERVER => 'Bot', USER => 'Outgoing' ],
253 options => { },
254 object_states => [
255 $self => [
256 'syndicator_started',
257 'syndicator_stopped',
258  
259 'shutdown',
260 'sighup',
261  
262 'ev_plugin_error',
263  
264 'core_timer_check_pool',
265 ],
266 ],
267 );
268  
269 }
270  
271 0 ▶sub syndicator_started {
Subroutine syndicator_started: not called CC=4 SCAR=30.0
Pod documented
272 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
273  
274 0 $kernel->sig(INT => 'shutdown');
275 0 $kernel->sig(TERM => 'shutdown');
276 0 $kernel->sig(HUP => 'sighup');
277  
278 0 $self->log->info(__PACKAGE__.' '.$self->version);
279  
280 0 $self->log->info("--> Initializing plugins . . .");
281  
282 0 my $i;
283 my @plugins = sort {
284 0 $self->cfg->plugins->plugin($b)->priority
285 <=>
286 $self->cfg->plugins->plugin($a)->priority
287 0 } @{ $self->cfg->plugins->list_plugins };
288  
289 0 PLUGIN: for my $plugin (@plugins) {
290 0 my $this_plug_cf = $self->cfg->plugins->plugin($plugin);
291 0 my $module = $this_plug_cf->module;
292  
293 0 ▶ unless ( $this_plug_cf->autoload ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this_plug_cf->autoload) 0 0 0
294 0 $self->log->debug("Skipping $plugin - NoAutoLoad is true");
295 next PLUGIN
296 0 }
297  
298 0 my $obj;
299 0 ▶ try {
Subroutine __ANON__: not called CC=2 SCAR=17.9
300 0 $obj = Bot::Cobalt::Core::Loader->load($module);
301 0 ▶ unless ( Bot::Cobalt::Core::Loader->is_reloadable($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Bot::Cobalt::Core::Loader"->is_reloadable($obj)) 0 0 0
302 0 $self->State->{NonReloadable}->{$plugin} = 1;
303 0 $self->log->debug("$plugin marked non-reloadable");
304 }
305 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
306 0 $self->log->error("Load failure; $_");
307 next PLUGIN
308 0 };
309  
310 ## save stringified object -> plugin mapping before we plugin_add
311 0 $self->PluginObjects->{$obj} = $plugin;
312  
313 0 ▶ unless ( $self->plugin_add($plugin, $obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->plugin_add($plugin, $obj)) 0 0 0
314 0 $self->log->error("plugin_add failure for $plugin");
315 0 delete $self->PluginObjects->{$obj};
316 0 Bot::Cobalt::Core::Loader->unload($module);
317 next PLUGIN
318 0 }
319  
320 0 ++$i;
321 }
322  
323 0 $self->log->info("-> $i plugins loaded");
324  
325 0 $self->send_event('plugins_initialized', $_[ARG0]);
326  
327 0 $self->log->info("-> started, plugins_initialized sent");
328  
329 ## kickstart timer pool
330 0 $kernel->yield('core_timer_check_pool');
331 }
332  
333 0 ▶sub sighup {
Subroutine sighup: not called CC=2 SCAR=17.9
Pod undocumented
334 0 my $self = $_[OBJECT];
335 0 $self->log->warn("SIGHUP received");
336  
337 0 ▶ if ($self->detached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->detached) { } 0 0 0
338 ## Caught by Plugin::Rehash if present
339 ## Not documented because you should be using the IRC interface
340 ## (...and if the bot was run with --nodetach it will die, below)
341 0 $self->log->info("sending Bot_rehash (SIGHUP)");
342 0 $self->send_event( 'Bot_rehash' );
343 } else {
344 ## we were (we think) attached to a terminal and it's (we think) gone
345 ## shut down soon as we can:
346 0 $self->log->warn("Lost terminal; shutting down");
347  
348 0 $_[KERNEL]->yield('shutdown');
349 }
350  
351 0 $_[KERNEL]->sig_handled();
352 }
353  
354 0 ▶sub shutdown {
Subroutine shutdown: not called CC=2 SCAR=17.9
Pod documented
355 0 ▶ my $self = ref $_[0] eq __PACKAGE__ ? $_[0] : $_[OBJECT];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 0 0 0
356  
357 0 $self->log->warn("Shutdown called, destroying syndicator");
358  
359 0 $self->_syndicator_destroy();
360 }
361  
362 0 ▶sub syndicator_stopped {
Subroutine syndicator_stopped: not called CC=1 SCAR=6.9
Pod documented
363 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
364  
365 0 $kernel->alarm('core_timer_check_pool');
366  
367 0 $self->log->debug("issuing: POCOIRC_SHUTDOWN, shutdown");
368  
369 0 $kernel->signal( $kernel, 'POCOIRC_SHUTDOWN' );
370 0 $kernel->post( $kernel, 'shutdown' );
371  
372 0 $self->log->warn("Core syndicator stopped.");
373 }
374  
375 0 ▶sub ev_plugin_error {
Subroutine ev_plugin_error: not called CC=1 SCAR=6.9
Pod undocumented
376 0 my ($kernel, $self, $err) = @_[KERNEL, OBJECT, ARG0];
377  
378 ## Receives the same error as 'debug => 1' (in Syndicator init)
379  
380 0 $self->log->error("Plugin err: $err");
381  
382 ## Bot_plugin_error
383 0 $self->send_event( 'plugin_error', $err );
384 }
385  
386 ### Core low-pri timer
387  
388 0 ▶sub core_timer_check_pool {
Subroutine core_timer_check_pool: not called CC=6 SCAR=37.4
Pod undocumented
389 0 my ($kernel, $self) = @_[KERNEL, OBJECT];
390  
391 ## Timers are provided by Core::Role::Timers
392  
393 0 my $timerpool = $self->TimerPool;
394  
395 0 TIMER: for my $id (keys %$timerpool) {
396 0 my $timer = $timerpool->{$id};
397  
398 0 ▶ unless (blessed $timer && $timer->isa('Bot::Cobalt::Timer') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($timer) and $timer->isa("Bot::Cobalt::Timer")) 0 0 0
Condition0%0/3
builtin::blessed($timer) and $timer->isa("Bot::Cobalt::Timer")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($timer) and $timer->isa("Bot::Cobalt::Timer")
builtin::blessed($timer) $timer->isa("Bot::Cobalt::Timer")
Truth table0%0/3
A and B
A= builtin::blessed($timer)B= $timer->isa("Bot::Cobalt::Timer")
ABresult
0X0
100
111
399 ## someone's been naughty
400 0 $self->log->warn("not a Bot::Cobalt::Timer: $id");
401 0 delete $timerpool->{$id};
402 next TIMER
403 0 }
404  
405 0 ▶ if ( $timer->execute_if_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer->execute_if_ready) 0 0 0
406 0 my $event = $timer->event;
407  
408 0 ▶ $self->log->debug("timer execute; $id ($event)")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 0 0
409 if $self->debug > 1;
410  
411 0 $self->send_event( 'executed_timer', $id );
412 0 $self->timer_del($id);
413 }
414  
415 } ## TIMER
416  
417 ## most definitely not a high-precision timer.
418 ## checked every second or so
419 0 $kernel->alarm('core_timer_check_pool' => time + 1);
420 }
421  
422 1 1;
423 __END__
« blib/lib/Bot/Cobalt/Frontend/Utils.pm ↑ Summary blib/lib/Bot/Cobalt/Plugin/Games/Roulette.pm »