| 1 | package Bot::Cobalt::Core; | ||
| 2 | $Bot::Cobalt::Core::VERSION = '0.021002'; | ||
| 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=2.6
| |||
| 13 | use Bot::Cobalt::IRC; | ||
| 14 | use Bot::Cobalt::Lang; | ||
| 15 | use Bot::Cobalt::Logger; | ||
| 16 | |||
| 17 | use Bot::Cobalt::Core::ContextMeta::Auth; | ||
| 18 | use Bot::Cobalt::Core::ContextMeta::Ignore; | ||
| 19 | use Bot::Cobalt::Core::Loader; | ||
| 20 | |||
| 21 | use Scalar::Util 'blessed'; | ||
| 22 | use Try::Tiny; | ||
| 23 | |||
| 24 | use Path::Tiny; | ||
| 25 | use Types::Path::Tiny -types; | ||
| 26 | |||
| 27 | use Moo; | ||
| 28 | |||
| 29 | has cfg => ( | ||
| 30 | required => 1, | ||
| 31 | is => 'rw', | ||
| 32 | isa => InstanceOf['Bot::Cobalt::Conf'], | ||
| 33 | ); | ||
| 34 | |||
| 35 | 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 | builder => sub { shift->cfg->etc }, | ||
| 48 | ); | ||
| 49 | |||
| 50 | has log => ( | ||
| 51 | lazy => 1, | ||
| 52 | is => 'rw', | ||
| 53 | isa => HasMethods[qw/debug info warn error/], | ||
| 54 | builder => sub { | ||
| 55 | my ($self) = @_; | ||
| 56 | my %opts = ( | ||
| 57 | level => $self->loglevel, | ||
| 58 | ); | ||
| 59 | if (my $log_format = $self->cfg->core->opts->{LogFormat}) { | ||
| 60 | $opts{log_format} = $log_format | ||
| 61 | } | ||
| 62 | if (my $log_time_fmt = $self->cfg->core->opts->{LogTimeFormat}) { | ||
| 63 | $opts{time_format} = $log_time_fmt | ||
| 64 | } | ||
| 65 | Bot::Cobalt::Logger->new( %opts ) | ||
| 66 | }, | ||
| 67 | ); | ||
| 68 | |||
| 69 | has loglevel => ( | ||
| 70 | is => 'rw', | ||
| 71 | isa => Str, | ||
| 72 | builder => sub { 'info' }, | ||
| 73 | ); | ||
| 74 | |||
| 75 | has detached => ( | ||
| 76 | lazy => 1, | ||
| 77 | is => 'ro', | ||
| 78 | isa => Int, | ||
| 79 | builder => sub { 0 }, | ||
| 80 | ); | ||
| 81 | |||
| 82 | has debug => ( | ||
| 83 | lazy => 1, | ||
| 84 | isa => Int, | ||
| 85 | is => 'rw', | ||
| 86 | builder => sub { 0 }, | ||
| 87 | ); | ||
| 88 | |||
| 89 | ## version/url used for var replacement: | ||
| 90 | has version => ( | ||
| 91 | lazy => 1, | ||
| 92 | is => 'rwp', | ||
| 93 | isa => Str, | ||
| 94 | builder => sub { __PACKAGE__->VERSION // 'vcs' } | ||
| 95 | ); | ||
| 96 | |||
| 97 | has url => ( | ||
| 98 | lazy => 1, | ||
| 99 | is => 'rwp', | ||
| 100 | isa => Str, | ||
| 101 | builder => sub { "http://www.metacpan.org/release/Bot-Cobalt" }, | ||
| 102 | ); | ||
| 103 | |||
| 104 | has langset => ( | ||
| 105 | lazy => 1, | ||
| 106 | is => 'ro', | ||
| 107 | isa => InstanceOf['Bot::Cobalt::Lang'], | ||
| 108 | writer => 'set_langset', | ||
| 109 | builder => sub { | ||
| 110 | my ($self) = @_; | ||
| 111 | Bot::Cobalt::Lang->new( | ||
| 112 | use_core => 1, | ||
| 113 | lang_dir => path( $self->etc .'/langs' ), | ||
| 114 | lang => $self->cfg->core->language, | ||
| 115 | ) | ||
| 116 | }, | ||
| 117 | ); | ||
| 118 | |||
| 119 | has lang => ( | ||
| 120 | lazy => 1, | ||
| 121 | is => 'ro', | ||
| 122 | isa => HashObj, | ||
| 123 | coerce => 1, | ||
| 124 | writer => 'set_lang', | ||
| 125 | builder => sub { | ||
| 126 | my ($self) = @_; | ||
| 127 | $self->langset->rpls | ||
| 128 | }, | ||
| 129 | ); | ||
| 130 | |||
| 131 | has State => ( | ||
| 132 | lazy => 1, | ||
| 133 | ## global 'heap' of sorts | ||
| 134 | is => 'ro', | ||
| 135 | isa => HashObj, | ||
| 136 | coerce => 1, | ||
| 137 | builder => sub { | ||
| 138 | { | ||
| 139 | 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 | ); | ||
| 150 | |||
| 151 | has PluginObjects => ( | ||
| 152 | lazy => 1, | ||
| 153 | ## alias -> object mapping | ||
| 154 | is => 'rw', | ||
| 155 | isa => HashObj, | ||
| 156 | coerce => 1, | ||
| 157 | builder => sub { {} }, | ||
| 158 | ); | ||
| 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 | builder => sub { {} }, | ||
| 168 | ); | ||
| 169 | |||
| 170 | has auth => ( | ||
| 171 | lazy => 1, | ||
| 172 | is => 'rw', | ||
| 173 | isa => Object, | ||
| 174 | builder => sub { | ||
| 175 | Bot::Cobalt::Core::ContextMeta::Auth->new | ||
| 176 | }, | ||
| 177 | ); | ||
| 178 | |||
| 179 | has ignore => ( | ||
| 180 | lazy => 1, | ||
| 181 | is => 'rw', | ||
| 182 | isa => Object, | ||
| 183 | builder => sub { | ||
| 184 | Bot::Cobalt::Core::ContextMeta::Ignore->new | ||
| 185 | }, | ||
| 186 | ); | ||
| 187 | |||
| 188 | ## FIXME not documented | ||
| 189 | has resolver => ( | ||
| 190 | lazy => 1, | ||
| 191 | is => 'rwp', | ||
| 192 | isa => Object, | ||
| 193 | builder => sub { | ||
| 194 | POE::Component::Client::DNS->spawn( | ||
| 195 | Alias => 'core_resolver', | ||
| 196 | ) | ||
| 197 | }, | ||
| 198 | ); | ||
| 199 | |||
| 200 | |||
| 201 | extends 'POE::Component::Syndicator'; | ||
| 202 | with 'Bot::Cobalt::Core::Role::Singleton'; | ||
| 203 | with 'Bot::Cobalt::Core::Role::EasyAccessors'; | ||
| 204 | with 'Bot::Cobalt::Core::Role::Timers'; | ||
| 205 | with 'Bot::Cobalt::Core::Role::IRC'; | ||
| 206 | |||
| 207 | |||
| 208 | ## FIXME test needed: | ||
| 209 | sub rpl { | ||
| 210 | my ($self, $rpl) = splice @_, 0, 2; | ||
| 211 | |||
| 212 | confess "rpl() method requires a RPL tag" | ||
| 213 | unless defined $rpl; | ||
| 214 | |||
| 215 | my $string = $self->lang->{$rpl} | ||
| 216 | // return "Unknown RPL $rpl, vars: ".join(' ', @_); | ||
| 217 | |||
| 218 | rplprintf( $string, @_ ) | ||
| 219 | } | ||
| 220 | |||
| 221 | sub init { | ||
| 222 | my ($self) = @_; | ||
| 223 | |||
| 224 | my $logfile = $self->cfg->core->paths->{Logfile} | ||
| 225 | // path( $self->var .'/cobalt.log' ); | ||
| 226 | |||
| 227 | if ($self->detached) { | ||
| 228 | # Presumably our frontend closed these | ||
| 229 | open STDOUT, '>>', $logfile or die $!; | ||
| 230 | open STDERR, '>>', $logfile or die $!; | ||
| 231 | } else { | ||
| 232 | $self->log->output->add( | ||
| 233 | 'screen' => { | ||
| 234 | type => 'Term', | ||
| 235 | }, | ||
| 236 | ); | ||
| 237 | } | ||
| 238 | |||
| 239 | $self->log->output->add( | ||
| 240 | 'logfile' => { | ||
| 241 | type => 'File', | ||
| 242 | file => $logfile, | ||
| 243 | }, | ||
| 244 | ); | ||
| 245 | |||
| 246 | ## Language set check. Force attrib fill. | ||
| 247 | $self->lang; | ||
| 248 | |||
| 249 | $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 | sub syndicator_started { | ||
| 272 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 273 | |||
| 274 | $kernel->sig('INT' => 'shutdown'); | ||
| 275 | $kernel->sig('TERM' => 'shutdown'); | ||
| 276 | $kernel->sig('HUP' => 'sighup'); | ||
| 277 | |||
| 278 | $self->log->info(__PACKAGE__.' '.$self->version); | ||
| 279 | |||
| 280 | $self->log->info("--> Initializing plugins . . ."); | ||
| 281 | |||
| 282 | my $i; | ||
| 283 | my @plugins = sort { | ||
| 284 | $self->cfg->plugins->plugin($b)->priority | ||
| 285 | <=> | ||
| 286 | $self->cfg->plugins->plugin($a)->priority | ||
| 287 | } @{ $self->cfg->plugins->list_plugins }; | ||
| 288 | |||
| 289 | PLUGIN: for my $plugin (@plugins) | ||
| 290 | { | ||
| 291 | my $this_plug_cf = $self->cfg->plugins->plugin($plugin); | ||
| 292 | |||
| 293 | my $module = $this_plug_cf->module; | ||
| 294 | |||
| 295 | unless ( $this_plug_cf->autoload ) { | ||
| 296 | $self->log->debug("Skipping $plugin - NoAutoLoad is true"); | ||
| 297 | |||
| 298 | next PLUGIN | ||
| 299 | } | ||
| 300 | |||
| 301 | my $obj; | ||
| 302 | try { | ||
| 303 | $obj = Bot::Cobalt::Core::Loader->load($module); | ||
| 304 | |||
| 305 | unless ( Bot::Cobalt::Core::Loader->is_reloadable($obj) ) { | ||
| 306 | $self->State->{NonReloadable}->{$plugin} = 1; | ||
| 307 | $self->log->debug("$plugin marked non-reloadable"); | ||
| 308 | } | ||
| 309 | } catch { | ||
| 310 | $self->log->error("Load failure; $_"); | ||
| 311 | |||
| 312 | next PLUGIN | ||
| 313 | }; | ||
| 314 | |||
| 315 | ## save stringified object -> plugin mapping: | ||
| 316 | $self->PluginObjects->{$obj} = $plugin; | ||
| 317 | |||
| 318 | unless ( $self->plugin_add($plugin, $obj) ) { | ||
| 319 | $self->log->error("plugin_add failure for $plugin"); | ||
| 320 | |||
| 321 | delete $self->PluginObjects->{$obj}; | ||
| 322 | |||
| 323 | Bot::Cobalt::Core::Loader->unload($module); | ||
| 324 | |||
| 325 | next PLUGIN | ||
| 326 | } | ||
| 327 | |||
| 328 | ++$i; | ||
| 329 | } | ||
| 330 | |||
| 331 | $self->log->info("-> $i plugins loaded"); | ||
| 332 | |||
| 333 | $self->send_event('plugins_initialized', $_[ARG0]); | ||
| 334 | |||
| 335 | $self->log->info("-> started, plugins_initialized sent"); | ||
| 336 | |||
| 337 | ## kickstart timer pool | ||
| 338 | $kernel->yield('core_timer_check_pool'); | ||
| 339 | } | ||
| 340 | |||
| 341 | sub sighup { | ||
| 342 | my $self = $_[OBJECT]; | ||
| 343 | $self->log->warn("SIGHUP received"); | ||
| 344 | |||
| 345 | if ($self->detached) { | ||
| 346 | ## Caught by Plugin::Rehash if present | ||
| 347 | ## Not documented because you should be using the IRC interface | ||
| 348 | ## (...and if the bot was run with --nodetach it will die, below) | ||
| 349 | $self->log->info("sending Bot_rehash (SIGHUP)"); | ||
| 350 | $self->send_event( 'Bot_rehash' ); | ||
| 351 | } else { | ||
| 352 | ## we were (we think) attached to a terminal and it's (we think) gone | ||
| 353 | ## shut down soon as we can: | ||
| 354 | $self->log->warn("Lost terminal; shutting down"); | ||
| 355 | |||
| 356 | $_[KERNEL]->yield('shutdown'); | ||
| 357 | } | ||
| 358 | |||
| 359 | $_[KERNEL]->sig_handled(); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub shutdown { | ||
| 363 | my $self = ref $_[0] eq __PACKAGE__ ? $_[0] : $_[OBJECT]; | ||
| 364 | |||
| 365 | $self->log->warn("Shutdown called, destroying syndicator"); | ||
| 366 | |||
| 367 | $self->_syndicator_destroy(); | ||
| 368 | } | ||
| 369 | |||
| 370 | sub syndicator_stopped { | ||
| 371 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 372 | |||
| 373 | $kernel->alarm('core_timer_check_pool'); | ||
| 374 | |||
| 375 | $self->log->debug("issuing: POCOIRC_SHUTDOWN, shutdown"); | ||
| 376 | |||
| 377 | $kernel->signal( $kernel, 'POCOIRC_SHUTDOWN' ); | ||
| 378 | $kernel->post( $kernel, 'shutdown' ); | ||
| 379 | |||
| 380 | $self->log->warn("Core syndicator stopped."); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub ev_plugin_error { | ||
| 384 | my ($kernel, $self, $err) = @_[KERNEL, OBJECT, ARG0]; | ||
| 385 | |||
| 386 | ## Receives the same error as 'debug => 1' (in Syndicator init) | ||
| 387 | |||
| 388 | $self->log->error("Plugin err: $err"); | ||
| 389 | |||
| 390 | ## Bot_plugin_error | ||
| 391 | $self->send_event( 'plugin_error', $err ); | ||
| 392 | } | ||
| 393 | |||
| 394 | ### Core low-pri timer | ||
| 395 | |||
| 396 | sub core_timer_check_pool { | ||
| 397 | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||
| 398 | |||
| 399 | ## Timers are provided by Core::Role::Timers | ||
| 400 | |||
| 401 | my $timerpool = $self->TimerPool; | ||
| 402 | |||
| 403 | TIMER: for my $id (keys %$timerpool) { | ||
| 404 | my $timer = $timerpool->{$id}; | ||
| 405 | |||
| 406 | unless (blessed $timer && $timer->isa('Bot::Cobalt::Timer') ) { | ||
| 407 | ## someone's been naughty | ||
| 408 | $self->log->warn("not a Bot::Cobalt::Timer: $id"); | ||
| 409 | delete $timerpool->{$id}; | ||
| 410 | next TIMER | ||
| 411 | } | ||
| 412 | |||
| 413 | if ( $timer->execute_if_ready ) { | ||
| 414 | my $event = $timer->event; | ||
| 415 | |||
| 416 | $self->log->debug("timer execute; $id ($event)") | ||
| 417 | if $self->debug > 1; | ||
| 418 | |||
| 419 | $self->send_event( 'executed_timer', $id ); | ||
| 420 | $self->timer_del($id); | ||
| 421 | } | ||
| 422 | |||
| 423 | } ## TIMER | ||
| 424 | |||
| 425 | ## most definitely not a high-precision timer. | ||
| 426 | ## checked every second or so | ||
| 427 | $kernel->alarm('core_timer_check_pool' => time + 1); | ||
| 428 | } | ||
| 429 | |||
| 430 | 1; | ||
| 431 | __END__ | ||