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->call_factoid(@_) }, "fact", 0);
$self->{pbot}->{commands}->register(sub { $self->factfind(@_) }, "factfind", 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->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->histogram(@_) }, "histogram", 0);
$self->{pbot}->{commands}->register(sub { $self->count(@_) }, "count", 0); $self->{pbot}->{commands}->register(sub { $self->count(@_) }, "count", 0);
@ -1621,45 +1619,4 @@ sub factchange {
return "Changed: $trigger_name is $action"; 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; 1;

View File

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

View File

@ -210,6 +210,8 @@ sub interpret {
} }
my $cmdlist = $self->make_args($stuff->{command}); 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')) { 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] # tell nick about/the cmd [args]
@ -245,6 +247,8 @@ sub interpret {
push @{$stuff->{subcmd}}, "$keyword $arguments"; push @{$stuff->{subcmd}}, "$keyword $arguments";
$command =~ s/^\s+|\s+$//g; $command =~ s/^\s+|\s+$//g;
$stuff->{command} = $command; $stuff->{command} = $command;
$stuff->{commands} = [];
push @{$stuff->{commands}}, $command;
$stuff->{result} = $self->interpret($stuff); $stuff->{result} = $self->interpret($stuff);
return $stuff->{result}; 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::BlackList;
use PBot::Timer; use PBot::Timer;
use PBot::Refresher; use PBot::Refresher;
use PBot::Plugins;
use PBot::WebPaste; use PBot::WebPaste;
use PBot::Utils::ParseDate; use PBot::Utils::ParseDate;
use PBot::Plugins;
use PBot::Functions; use PBot::Functions;
use PBot::Modules;
use PBot::ProcessManager; use PBot::ProcessManager;
sub new { sub new {
@ -147,6 +148,7 @@ sub initialize {
$self->{logger}->log("plugin_dir: $plugin_dir\n"); $self->{logger}->log("plugin_dir: $plugin_dir\n");
$self->{timer} = PBot::Timer->new(pbot => $self, timeout => 10, %conf); $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->{functions} = PBot::Functions->new(pbot => $self, %conf);
$self->{refresher} = PBot::Refresher->new(pbot => $self); $self->{refresher} = PBot::Refresher->new(pbot => $self);

View File

@ -1,20 +1,19 @@
# File: FactoidModuleLauncher.pm # File: ProcessManager.pm
# Author: pragma_ # 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 # 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 # 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/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
package PBot::FactoidModuleLauncher; package PBot::ProcessManager;
use parent 'PBot::Class'; use parent 'PBot::Class';
use warnings; use strict; use warnings; use strict;
use feature 'unicode_strings'; use feature 'unicode_strings';
use POSIX qw(WNOHANG); use POSIX qw(WNOHANG);
use Text::Balanced qw(extract_delimited);
use JSON; use JSON;
use IPC::Run qw/run timeout/; use IPC::Run qw/run timeout/;
use Encode; use Encode;
@ -24,47 +23,50 @@ $SIG{CHLD} = sub { while (waitpid(-1, WNOHANG) > 0) {} };
sub initialize { sub initialize {
my ($self, %conf) = @_; 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 { sub ps_cmd {
my ($self, $stuff) = @_; my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my $text; my @processes;
foreach my $pid (sort keys %{$self->{processes}}) {
if ($self->{pbot}->{registry}->get_value('general', 'debugcontext')) { push @processes, "$pid: $self->{processes}->{$pid}->{commands}->[0]";
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
$self->{pbot}->{logger}->log("FML::execute_module\n");
$self->{pbot}->{logger}->log(Dumper $stuff);
} }
if (@processes) {
$stuff->{arguments} = "" if not defined $stuff->{arguments}; return "Running processes: ", join '; ', @processes;
} else {
my @factoids = $self->{pbot}->{factoids}->find_factoid($stuff->{from}, $stuff->{keyword}, exact_channel => 2, exact_trigger => 2); return "No running processes.";
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]); sub kill_cmd {
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
return "Coming soon.";
}
$stuff->{channel} = $channel; sub add_process {
$stuff->{keyword} = $trigger; my ($self, $pid, $stuff) = @_;
$stuff->{trigger} = $trigger; $self->{processes}->{$pid} = $stuff;
}
my $module = $self->{pbot}->{factoids}->{factoids}->get_data($channel, $trigger, 'action'); sub remove_process {
my $module_dir = $self->{pbot}->{registry}->get_value('general', 'module_dir'); my ($self, $pid) = @_;
delete $self->{processes}->{$pid};
}
$self->{pbot}->{logger}->log("(" . (defined $stuff->{from} ? $stuff->{from} : "(undef)") . "): $stuff->{nick}!$stuff->{user}\@$stuff->{host}: Executing module [$stuff->{command}] $module $stuff->{arguments}\n"); sub execute_subroutine {
}
$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); pipe(my $reader, my $writer);
my $pid = fork; my $pid = fork;
if (not defined $pid) { 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 $reader;
close $writer; close $writer;
$stuff->{checkflood} = 1; $stuff->{checkflood} = 1;
@ -72,8 +74,6 @@ sub execute_module {
return; return;
} }
# FIXME -- add check to ensure $module exists
if ($pid == 0) { # start child block if ($pid == 0) { # start child block
close $reader; close $reader;
@ -82,72 +82,40 @@ sub execute_module {
*PBot::IRC::Connection::DESTROY = sub { return; }; *PBot::IRC::Connection::DESTROY = sub { return; };
use warnings; use warnings;
if (not chdir $module_dir) { # execute the provided subroutine, results are stored in $stuff
$self->{pbot}->{logger}->log("Could not chdir to '$module_dir': $!\n"); eval { $subref->($stuff) };
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);
};
# check for errors
if ($@) { if ($@) {
my $error = $@; $self->{pbot}->{logger}->log("Error executing process: $@\n");
if ($error =~ m/timeout on timer/) {
($exitval, $stdout, $stderr) = (-1, "$stuff->{trigger}: timed-out", '');
} else {
($exitval, $stdout, $stderr) = (-1, '', $error);
}
} }
if (length $stderr) { # print $stuff to pipe
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};
my $json = encode_json $stuff; my $json = encode_json $stuff;
print $writer "$json\n"; print $writer "$json\n";
# end child
exit 0; exit 0;
} # end child block } else {
else { # parent
close $writer; close $writer;
$self->{pbot}->{select_handler}->add_reader($reader, sub { $self->module_pipe_reader(@_) }); $self->add_process($pid, $stuff);
return ""; $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 { sub process_pipe_reader {
my ($self, $buf) = @_; my ($self, $pid, $buf) = @_;
$self->remove_process($pid);
my $stuff = decode_json $buf or do { my $stuff = decode_json $buf or do {
$self->{pbot}->{logger}->log("Failed to decode bad json: [$buf]\n"); $self->{pbot}->{logger}->log("Failed to decode bad json: [$buf]\n");
return; return;
}; };
if (not defined $stuff->{result} or not length $stuff->{result}) { 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; return;
} }
@ -158,9 +126,7 @@ sub module_pipe_reader {
if (exists $stuff->{special} and $stuff->{special} eq 'code-factoid') { if (exists $stuff->{special} and $stuff->{special} eq 'code-factoid') {
$stuff->{result} =~ s/\s+$//g; $stuff->{result} =~ s/\s+$//g;
$self->{pbot}->{logger}->log("No text result from code-factoid.\n") and return if not length $stuff->{result}; $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->{original_keyword} = $stuff->{root_keyword};
$stuff->{result} = $self->{pbot}->{factoids}->handle_action($stuff, $stuff->{result}); $stuff->{result} = $self->{pbot}->{factoids}->handle_action($stuff, $stuff->{result});
} }

View File

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

View File

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