3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-14 16:09:34 +01:00
pbot/lib/PBot/Core/Commands/Applets.pm
Pragmatic Software 3d97dc2c33 Rename "modules" to "applets"
"Applet" is a much better name for the external command-line
scripts and programs that can be loaded as PBot commands. They
will no longer be confused with Perl modules.

https://en.wikipedia.org/wiki/Applet
2021-11-19 18:05:50 -08:00

74 lines
2.1 KiB
Perl

# File: Applets.pm
#
# Purpose: Registers commands to load and unload PBot applets.
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Core::Commands::Applets;
use parent 'PBot::Core::Class';
use PBot::Imports;
use IPC::Run qw/run timeout/;
use Encode;
sub initialize {
my ($self, %conf) = @_;
# bot commands to load and unload applets
$self->{pbot}->{commands}->register(sub { $self->cmd_load(@_) }, "load", 1);
$self->{pbot}->{commands}->register(sub { $self->cmd_unload(@_) }, "unload", 1);
}
sub cmd_load {
my ($self, $context) = @_;
my ($keyword, $applet) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 2);
return "Usage: load <keyword> <applet>" if not defined $applet;
my $factoids = $self->{pbot}->{factoids}->{data}->{storage};
if ($factoids->exists('.*', $keyword)) {
return 'There is already a keyword named ' . $factoids->get_data('.*', $keyword, '_name') . '.';
}
$self->{pbot}->{factoids}->{data}->add('applet', '.*', $context->{hostmask}, $keyword, $applet, 1);
$factoids->set('.*', $keyword, 'add_nick', 1, 1);
$factoids->set('.*', $keyword, 'nooverride', 1);
$self->{pbot}->{logger}->log("$context->{hostmask} loaded applet $keyword => $applet\n");
return "Loaded applet $keyword => $applet";
}
sub cmd_unload {
my ($self, $context) = @_;
my $applet = $self->{pbot}->{interpreter}->shift_arg($context->{arglist});
return "Usage: unload <keyword>" if not defined $applet;
my $factoids = $self->{pbot}->{factoids}->{data}->{storage};
if (not $factoids->exists('.*', $applet)) {
return "/say $applet not found.";
}
if ($factoids->get_data('.*', $applet, 'type') ne 'applet') {
return "/say " . $factoids->get_data('.*', $applet, '_name') . ' is not an applet.';
}
my $name = $factoids->get_data('.*', $applet, '_name');
$factoids->remove('.*', $applet);
$self->{pbot}->{logger}->log("$context->{hostmask} unloaded applet $applet\n");
return "/say $name unloaded.";
}
1;