metabot/extern/cel-bot/CelBot/Control.pm
2021-05-20 17:59:36 -04:00

111 lines
1.8 KiB
Perl

package CelBot::Control;
use strict;
use POSIX qw( SIGHUP );
use CelBot::Commands;
# This needs to be a package (not lexical) variable so it keeps its value when
# the code is reloaded
our @ON_GLOBAL_RELOAD;
sub new
{
my $class = shift;
my ( $core ) = @_;
my $self = bless {
core => $core,
irc => $core->get_plugin( "irc" ),
}, $class;
return $self;
}
sub quit
{
my $self = shift;
my ( $message ) = @_;
# THIS IS A BOT-GLOBAL ACTION
# TODO: Find all the IRC objects, create a MergePoint on them,
# only exit(0) when they're all done
$self->{irc}->disconnect(
message => $message,
on_disconnected => sub { exit( 0 ); },
);
}
# THIS IS A BOT-GLOBAL ACTION
sub global_reload
{
# Take a copy of it in case it gets modified
my @reload_callbacks = @ON_GLOBAL_RELOAD;
foreach my $cb ( @reload_callbacks ) {
$cb->();
}
return 1; # TRUE
}
###
# Commands
###
sub register_commands
{
my $self = shift;
my ( $commands_plugin ) = @_;
$commands_plugin->register(
plugin => $self,
command => "quit",
perm => 'owner',
args => [
CelBot::Commands::ArgSpec->new( 'message', eatall => 1 ),
],
summary => "Disconnect all connections and quit",
);
$commands_plugin->register(
plugin => $self,
command => "reload",
perm => 'owner',
args => [],
summary => "Reload configuration and Perl code",
);
}
sub command_quit
{
my $self = shift;
my ( $context, $message ) = @_;
$context->respond( "OK" );
$self->quit( $message );
}
sub command_reload
{
my $self = shift;
my ( $context ) = @_;
if( defined eval { global_reload } ) {
return ( "OK" );
}
else {
return ( "Failed: $@" );
}
}
# Keep perl happy; keep Britain tidy
1;