3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-29 23:39:24 +01:00

Add ProcessManager and Modules

Improve everything to behave nicely with ProcessManager and Modules
This commit is contained in:
Pragmatic Software 2020-02-14 18:52:41 -08:00
parent f37dcaf7f9
commit dc7cdd8a2c
8 changed files with 201 additions and 134 deletions

View File

@ -65,8 +65,6 @@ sub initialize {
$self->{pbot}->{commands}->register(sub { $self->call_factoid(@_) }, "fact", 0);
$self->{pbot}->{commands}->register(sub { $self->factfind(@_) }, "factfind", 0);
$self->{pbot}->{commands}->register(sub { $self->top20(@_) }, "top20", 0);
$self->{pbot}->{commands}->register(sub { $self->load_module(@_) }, "load", 1);
$self->{pbot}->{commands}->register(sub { $self->unload_module(@_) }, "unload", 1);
$self->{pbot}->{commands}->register(sub { $self->histogram(@_) }, "histogram", 0);
$self->{pbot}->{commands}->register(sub { $self->count(@_) }, "count", 0);
@ -1621,45 +1619,4 @@ sub factchange {
return "Changed: $trigger_name is $action";
}
# FIXME: these two functions need to use $stuff->{arglist}
sub load_module {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->{factoids}->{factoids};
my ($keyword, $module) = $arguments =~ /^(.*?)\s+(.*)$/ if defined $arguments;
if (not defined $module) {
return "Usage: load <keyword> <module>";
}
if (not $factoids->exists('.*', $keyword)) {
$self->{pbot}->{factoids}->add_factoid('module', '.*', "$nick!$user\@$host", $keyword, $module, 1);
$factoids->set('.*', $keyword, 'add_nick', 1, 1);
$factoids->set('.*', $keyword, 'nooverride', 1);
$self->{pbot}->{logger}->log("$nick!$user\@$host loaded module $keyword => $module\n");
return "Loaded module $keyword => $module";
} else {
return 'There is already a keyword named ' . $factoids->get_data('.*', $keyword, '_name') . '.';
}
}
sub unload_module {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->{factoids}->{factoids};
if (not defined $arguments) {
return "Usage: unload <keyword>";
} elsif (not $factoids->exists('.*', $arguments)) {
return "/say $arguments not found.";
} elsif ($factoids->get_data('.*', $arguments, 'type') ne 'module') {
return "/say " . $factoids->get_data('.*', $arguments, '_name') . ' is not a module.';
} else {
my $name = $factoids->get_data('.*', $arguments, '_name');
$factoids->remove('.*', $arguments);
$self->{pbot}->{logger}->log("$nick!$user\@$host unloaded module $arguments\n");
return "/say $name unloaded.";
}
}
1;

View File

@ -707,7 +707,7 @@ sub execute_code_factoid_using_vm {
$stuff->{arguments} = $json;
$stuff->{args_utf8} = 1;
$self->{pbot}->{process_manager}->execute_module($stuff);
$self->{pbot}->{modules}->execute_module($stuff);
return "";
}
@ -1033,7 +1033,7 @@ sub handle_action {
$stuff->{root_keyword} = $keyword unless defined $stuff->{root_keyword};
$stuff->{root_channel} = $channel;
my $result = $self->{pbot}->{process_manager}->execute_module($stuff);
my $result = $self->{pbot}->{modules}->execute_module($stuff);
if (length $result) {
return $ref_from . $result;
} else {

View File

@ -210,6 +210,8 @@ sub interpret {
}
my $cmdlist = $self->make_args($stuff->{command});
$stuff->{commands} = [] unless exists $stuff->{commands};
push @{$stuff->{commands}}, $stuff->{command};
if ($self->arglist_size($cmdlist) >= 4 and lc $cmdlist->[0] eq 'tell' and (lc $cmdlist->[2] eq 'about' or lc $cmdlist->[2] eq 'the')) {
# tell nick about/the cmd [args]
@ -245,6 +247,8 @@ sub interpret {
push @{$stuff->{subcmd}}, "$keyword $arguments";
$command =~ s/^\s+|\s+$//g;
$stuff->{command} = $command;
$stuff->{commands} = [];
push @{$stuff->{commands}}, $command;
$stuff->{result} = $self->interpret($stuff);
return $stuff->{result};
}

138
PBot/Modules.pm Normal file
View File

@ -0,0 +1,138 @@
# File: Modules.pm
# Author: pragma_
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
package PBot::Modules;
use parent 'PBot::Class';
use warnings; use strict;
use feature 'unicode_strings';
use IPC::Run qw/run timeout/;
use Encode;
sub initialize {
my ($self, %conf) = @_;
$self->{pbot}->{commands}->register(sub { $self->load_cmd(@_) }, "load", 1);
$self->{pbot}->{commands}->register(sub { $self->unload_cmd(@_) }, "unload", 1);
}
sub load_cmd {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my $factoids = $self->{pbot}->{factoids}->{factoids};
my ($keyword, $module) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2);
return "Usage: load <keyword> <module>" if not defined $module;
if ($factoids->exists('.*', $keyword)) {
return 'There is already a keyword named ' . $factoids->get_data('.*', $keyword, '_name') . '.';
}
$self->{pbot}->{factoids}->add_factoid('module', '.*', "$nick!$user\@$host", $keyword, $module, 1);
$factoids->set('.*', $keyword, 'add_nick', 1, 1);
$factoids->set('.*', $keyword, 'nooverride', 1);
$self->{pbot}->{logger}->log("$nick!$user\@$host loaded module $keyword => $module\n");
return "Loaded module $keyword => $module";
}
sub unload_cmd {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my $module = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist});
return "Usage: unload <keyword>" if not defined $module;
my $factoids = $self->{pbot}->{factoids}->{factoids};
return "/say $module not found." if not $factoids->exists('.*', $module);
if ($factoids->get_data('.*', $module, 'type') ne 'module') {
return "/say " . $factoids->get_data('.*', $module, '_name') . ' is not a module.';
}
my $name = $factoids->get_data('.*', $module, '_name');
$factoids->remove('.*', $module);
$self->{pbot}->{logger}->log("$nick!$user\@$host unloaded module $module\n");
return "/say $name unloaded.";
}
sub execute_module {
my ($self, $stuff) = @_;
my $text;
if ($self->{pbot}->{registry}->get_value('general', 'debugcontext')) {
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$self->{pbot}->{logger}->log("execute_module\n");
$self->{pbot}->{logger}->log(Dumper $stuff);
}
$self->{pbot}->{process_manager}->execute_process($stuff, sub { $self->launch_module(@_) });
}
sub launch_module {
my ($self, $stuff) = @_;
$stuff->{arguments} = "" if not defined $stuff->{arguments};
my @factoids = $self->{pbot}->{factoids}->find_factoid($stuff->{from}, $stuff->{keyword}, exact_channel => 2, exact_trigger => 2);
if (not @factoids or not $factoids[0]) {
$stuff->{checkflood} = 1;
$self->{pbot}->{interpreter}->handle_result($stuff, "/msg $stuff->{nick} Failed to find module for '$stuff->{keyword}' in channel $stuff->{from}\n");
return;
}
my ($channel, $trigger) = ($factoids[0]->[0], $factoids[0]->[1]);
$stuff->{channel} = $channel;
$stuff->{keyword} = $trigger;
$stuff->{trigger} = $trigger;
my $module = $self->{pbot}->{factoids}->{factoids}->get_data($channel, $trigger, 'action');
$self->{pbot}->{logger}->log("(" . (defined $stuff->{from} ? $stuff->{from} : "(undef)") . "): $stuff->{nick}!$stuff->{user}\@$stuff->{host}: Executing module [$stuff->{command}] $module $stuff->{arguments}\n");
$stuff->{arguments} = $self->{pbot}->{factoids}->expand_special_vars($stuff->{from}, $stuff->{nick}, $stuff->{root_keyword}, $stuff->{arguments});
my $module_dir = $self->{pbot}->{registry}->get_value('general', 'module_dir');
if (not chdir $module_dir) {
$self->{pbot}->{logger}->log("Could not chdir to '$module_dir': $!\n");
Carp::croak("Could not chdir to '$module_dir': $!");
}
if ($self->{pbot}->{factoids}->{factoids}->exists($channel, $trigger, 'workdir')) {
chdir $self->{pbot}->{factoids}->{factoids}->get_data($channel, $trigger, 'workdir');
}
# FIXME -- add check to ensure $module exists
my ($exitval, $stdout, $stderr) = eval {
my $args = $stuff->{arguments};
if (not $stuff->{args_utf8}) {
$args = encode('UTF-8', $args);
}
my @cmdline = ("./$module", $self->{pbot}->{interpreter}->split_line($args));
my $timeout = $self->{pbot}->{registry}->get_value('general', 'module_timeout') // 30;
my ($stdin, $stdout, $stderr);
run \@cmdline, \$stdin, \$stdout, \$stderr, timeout($timeout);
my $exitval = $? >> 8;
utf8::decode($stdout);
utf8::decode($stderr);
return ($exitval, $stdout, $stderr);
};
if ($@) {
my $error = $@;
if ($error =~ m/timeout on timer/) {
($exitval, $stdout, $stderr) = (-1, "$stuff->{trigger}: timed-out", '');
} else {
($exitval, $stdout, $stderr) = (-1, '', $error);
}
}
if (length $stderr) {
if (open(my $fh, '>>', "$module-stderr")) {
print $fh $stderr;
close $fh;
} else {
$self->{pbot}->{logger}->log("Failed to open $module-stderr: $!\n");
}
}
$stuff->{result} = $stdout;
chomp $stuff->{result};
}
1;

View File

@ -43,10 +43,11 @@ use PBot::IgnoreList;
use PBot::BlackList;
use PBot::Timer;
use PBot::Refresher;
use PBot::Plugins;
use PBot::WebPaste;
use PBot::Utils::ParseDate;
use PBot::Plugins;
use PBot::Functions;
use PBot::Modules;
use PBot::ProcessManager;
sub new {
@ -147,6 +148,7 @@ sub initialize {
$self->{logger}->log("plugin_dir: $plugin_dir\n");
$self->{timer} = PBot::Timer->new(pbot => $self, timeout => 10, %conf);
$self->{modules} = PBot::Modules->new(pbot => $self, %conf);
$self->{functions} = PBot::Functions->new(pbot => $self, %conf);
$self->{refresher} = PBot::Refresher->new(pbot => $self);

View File

@ -1,20 +1,19 @@
# File: FactoidModuleLauncher.pm
# File: ProcessManager.pm
# Author: pragma_
#
# Purpose: Handles forking and execution of module processes
# Purpose: Handles forking and execution of module/subroutine processes
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
package PBot::FactoidModuleLauncher;
package PBot::ProcessManager;
use parent 'PBot::Class';
use warnings; use strict;
use feature 'unicode_strings';
use POSIX qw(WNOHANG);
use Text::Balanced qw(extract_delimited);
use JSON;
use IPC::Run qw/run timeout/;
use Encode;
@ -24,47 +23,50 @@ $SIG{CHLD} = sub { while (waitpid(-1, WNOHANG) > 0) {} };
sub initialize {
my ($self, %conf) = @_;
$self->{pbot}->{commands}->register(sub { $self->ps_cmd(@_) }, 'ps', 0);
$self->{pbot}->{commands}->register(sub { $self->kill_cmd(@_) }, 'kill', 1);
$self->{processes} = {};
}
sub execute_module {
my ($self, $stuff) = @_;
my $text;
if ($self->{pbot}->{registry}->get_value('general', 'debugcontext')) {
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$self->{pbot}->{logger}->log("FML::execute_module\n");
$self->{pbot}->{logger}->log(Dumper $stuff);
sub ps_cmd {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my @processes;
foreach my $pid (sort keys %{$self->{processes}}) {
push @processes, "$pid: $self->{processes}->{$pid}->{commands}->[0]";
}
if (@processes) {
return "Running processes: ", join '; ', @processes;
} else {
return "No running processes.";
}
}
$stuff->{arguments} = "" if not defined $stuff->{arguments};
my @factoids = $self->{pbot}->{factoids}->find_factoid($stuff->{from}, $stuff->{keyword}, exact_channel => 2, exact_trigger => 2);
if (not @factoids or not $factoids[0]) {
$stuff->{checkflood} = 1;
$self->{pbot}->{interpreter}->handle_result($stuff, "/msg $stuff->{nick} Failed to find module for '$stuff->{keyword}' in channel $stuff->{from}\n");
return;
sub kill_cmd {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
return "Coming soon.";
}
my ($channel, $trigger) = ($factoids[0]->[0], $factoids[0]->[1]);
sub add_process {
my ($self, $pid, $stuff) = @_;
$self->{processes}->{$pid} = $stuff;
}
$stuff->{channel} = $channel;
$stuff->{keyword} = $trigger;
$stuff->{trigger} = $trigger;
sub remove_process {
my ($self, $pid) = @_;
delete $self->{processes}->{$pid};
}
my $module = $self->{pbot}->{factoids}->{factoids}->get_data($channel, $trigger, 'action');
my $module_dir = $self->{pbot}->{registry}->get_value('general', 'module_dir');
sub execute_subroutine {
}
$self->{pbot}->{logger}->log("(" . (defined $stuff->{from} ? $stuff->{from} : "(undef)") . "): $stuff->{nick}!$stuff->{user}\@$stuff->{host}: Executing module [$stuff->{command}] $module $stuff->{arguments}\n");
$stuff->{arguments} = $self->{pbot}->{factoids}->expand_special_vars($stuff->{from}, $stuff->{nick}, $stuff->{root_keyword}, $stuff->{arguments});
sub execute_process {
my ($self, $stuff, $subref) = @_;
pipe(my $reader, my $writer);
my $pid = fork;
if (not defined $pid) {
$self->{pbot}->{logger}->log("Could not fork module: $!\n");
$self->{pbot}->{logger}->log("Could not fork process: $!\n");
close $reader;
close $writer;
$stuff->{checkflood} = 1;
@ -72,8 +74,6 @@ sub execute_module {
return;
}
# FIXME -- add check to ensure $module exists
if ($pid == 0) { # start child block
close $reader;
@ -82,72 +82,40 @@ sub execute_module {
*PBot::IRC::Connection::DESTROY = sub { return; };
use warnings;
if (not chdir $module_dir) {
$self->{pbot}->{logger}->log("Could not chdir to '$module_dir': $!\n");
Carp::croak("Could not chdir to '$module_dir': $!");
}
if ($self->{pbot}->{factoids}->{factoids}->exists($channel, $trigger, 'workdir')) {
chdir $self->{pbot}->{factoids}->{factoids}->get_data($channel, $trigger, 'workdir');
}
my ($exitval, $stdout, $stderr) = eval {
my $args = $stuff->{arguments};
if (not $stuff->{args_utf8}) {
$args = encode('UTF-8', $args);
}
my @cmdline = ("./$module", $self->{pbot}->{interpreter}->split_line($args));
my $timeout = $self->{pbot}->{registry}->get_value('general', 'module_timeout') // 30;
my ($stdin, $stdout, $stderr);
run \@cmdline, \$stdin, \$stdout, \$stderr, timeout($timeout);
my $exitval = $? >> 8;
utf8::decode($stdout);
utf8::decode($stderr);
return ($exitval, $stdout, $stderr);
};
# execute the provided subroutine, results are stored in $stuff
eval { $subref->($stuff) };
# check for errors
if ($@) {
my $error = $@;
if ($error =~ m/timeout on timer/) {
($exitval, $stdout, $stderr) = (-1, "$stuff->{trigger}: timed-out", '');
} else {
($exitval, $stdout, $stderr) = (-1, '', $error);
}
$self->{pbot}->{logger}->log("Error executing process: $@\n");
}
if (length $stderr) {
if (open(my $fh, '>>', "$module-stderr")) {
print $fh $stderr;
close $fh;
} else {
$self->{pbot}->{logger}->log("Failed to open $module-stderr: $!\n");
}
}
$stuff->{result} = $stdout;
chomp $stuff->{result};
# print $stuff to pipe
my $json = encode_json $stuff;
print $writer "$json\n";
# end child
exit 0;
} # end child block
else {
} else {
# parent
close $writer;
$self->{pbot}->{select_handler}->add_reader($reader, sub { $self->module_pipe_reader(@_) });
$self->add_process($pid, $stuff);
$self->{pbot}->{select_handler}->add_reader($reader, sub { $self->process_pipe_reader($pid, @_) });
# return empty string since reader will handle the output when child is finished
return "";
}
}
sub module_pipe_reader {
my ($self, $buf) = @_;
sub process_pipe_reader {
my ($self, $pid, $buf) = @_;
$self->remove_process($pid);
my $stuff = decode_json $buf or do {
$self->{pbot}->{logger}->log("Failed to decode bad json: [$buf]\n");
return;
};
if (not defined $stuff->{result} or not length $stuff->{result}) {
$self->{pbot}->{logger}->log("No result from module.\n");
$self->{pbot}->{logger}->log("No result from process.\n");
return;
}
@ -158,9 +126,7 @@ sub module_pipe_reader {
if (exists $stuff->{special} and $stuff->{special} eq 'code-factoid') {
$stuff->{result} =~ s/\s+$//g;
$self->{pbot}->{logger}->log("No text result from code-factoid.\n") and return if not length $stuff->{result};
$stuff->{original_keyword} = $stuff->{root_keyword};
$stuff->{result} = $self->{pbot}->{factoids}->handle_action($stuff, $stuff->{result});
}

View File

@ -64,7 +64,7 @@ sub datecmd {
keyword => "date_module", arguments => "$timezone"
};
$self->{pbot}->{factoids}->{factoidmodulelauncher}->execute_module($newstuff);
$self->{pbot}->{modules}->execute_module($newstuff);
}
1;

View File

@ -74,7 +74,7 @@ sub show_url_titles {
keyword => "title", arguments => "$nick $url"
};
$self->{pbot}->{factoids}->{factoidmodulelauncher}->execute_module($stuff);
$self->{pbot}->{modules}->execute_module($stuff);
}
}
return 0;