Handle module results in parent instead of child

This commit is contained in:
Pragmatic Software 2017-10-10 17:19:02 -07:00
parent fda67f3739
commit cffcd04895
1 changed files with 52 additions and 39 deletions

View File

@ -15,6 +15,7 @@ use strict;
use POSIX qw(WNOHANG); # for children process reaping
use Carp ();
use Text::Balanced qw(extract_delimited);
use JSON;
# automatically reap children processes in background
$SIG{CHLD} = sub { while(waitpid(-1, WNOHANG) > 0) {} };
@ -48,6 +49,11 @@ sub execute_module {
$arguments = "" if not defined $arguments;
my %stuff = (from => $from, tonick => $tonick, nick => $nick, user => $user, host => $host, command => $command, root_channel => $root_channel,
root_keyword => $root_keyword, keyword => $keyword, arguments => $arguments, preserve_whitespace => $preserve_whitespace, referenced => $referenced);
$self->{pbot}->{logger}->log("enter fml: [$from] [$keyword] [$command] [$arguments] [$root_channel] [$root_keyword]\n");
my @factoids = $self->{pbot}->{factoids}->find_factoid($from, $keyword, undef, 2, 2);
if(not @factoids or not $factoids[0]) {
@ -57,6 +63,9 @@ sub execute_module {
my ($channel, $trigger) = ($factoids[0]->[0], $factoids[0]->[1]);
$stuff{channel} = $channel;
$stuff{trigger} = $trigger;
my $module = $self->{pbot}->{factoids}->{factoids}->hash->{$channel}->{$trigger}->{action};
my $module_dir = $self->{pbot}->{registry}->get_value('general', 'module_dir');
@ -150,43 +159,12 @@ sub execute_module {
chdir $self->{pbot}->{factoids}->{factoids}->hash->{$channel}->{$trigger}->{workdir};
}
$text = `./$module $arguments 2>> $module-stderr`;
if ($referenced) {
exit 0 if $text =~ m/(?:no results)/i;
}
$stuff{result} = `./$module $arguments 2>> $module-stderr`;
chomp $stuff{result};
if ($command eq 'code-factoid') {
$text =~ s/\s+$//g;
$self->{pbot}->{logger}->log("No text result from code-factoid.\n") and exit 0 if not length $text;
$text = $self->{pbot}->{factoids}->handle_action($nick, $user, $host, $from, $root_channel, $root_keyword, $root_keyword, $arguments, $text, $tonick, 0, $referenced, undef, $root_keyword);
}
if(defined $tonick) {
$self->{pbot}->{logger}->log("($from): $nick!$user\@$host) sent to $tonick\n");
if(defined $text && length $text > 0) {
# get rid of original caller's nick
$text =~ s/^\/([^ ]+) \Q$nick\E:\s+/\/$1 /;
$text =~ s/^\Q$nick\E:\s+//;
print $writer "$from $tonick: $text\n";
$self->{pbot}->{interpreter}->handle_result($from, $nick, $user, $host, $command, "$keyword $arguments", "$tonick: $text", 0, $preserve_whitespace);
}
exit 0;
} else {
if($command ne 'code-factoid' and exists $self->{pbot}->{factoids}->{factoids}->hash->{$channel}->{$trigger}->{add_nick} and $self->{pbot}->{factoids}->{factoids}->hash->{$channel}->{$trigger}->{add_nick} != 0) {
print $writer "$from $nick: $text";
$self->{pbot}->{interpreter}->handle_result($from, $nick, $user, $host, $command, "$keyword $arguments", "$nick: $text", 0, $preserve_whitespace);
} else {
print $writer "$from $text";
$self->{pbot}->{interpreter}->handle_result($from, $nick, $user, $host, $command, "$keyword $arguments", $text, 0, $preserve_whitespace);
}
exit 0;
}
# er, didn't execute the module?
print $writer "$from /me moans loudly.\n";
$self->{pbot}->{interpreter}->handle_result($from, $nick, $user, $host, $command, "$keyword $arguments", "/me moans loudly.", 0, 0);
my $json = encode_json \%stuff;
print $writer "$json\n";
exit 0;
} # end child block
else {
@ -198,10 +176,45 @@ sub execute_module {
sub module_pipe_reader {
my ($self, $buf) = @_;
my ($channel, $text) = split /\s+/, $buf, 2;
return if not defined $text or not length $text;
$text = $self->{pbot}->{interpreter}->truncate_result($channel, $self->{pbot}->{registry}->get_value('irc', 'botnick'), 'undef', $text, $text, 0);
$self->{pbot}->{antiflood}->check_flood($channel, $self->{pbot}->{registry}->get_value('irc', 'botnick'), $self->{pbot}->{registry}->get_value('irc', 'username'), 'localhost', $text, 0, 0, 0);
my $stuff = decode_json $buf or return;
if (not defined $stuff->{result} or not length $stuff->{result}) {
$self->{pbot}->{logger}->log("No result from module.\n");
return;
}
if ($stuff->{referenced}) {
return if $stuff->{result} =~ m/(?:no results)/i;
}
if ($stuff->{command} 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};
$self->{pbot}->{logger}->log("fml, cf: text: [$stuff->{result}]\n");
$stuff->{result} = $self->{pbot}->{factoids}->handle_action($stuff->{nick}, $stuff->{user}, $stuff->{host},
$stuff->{from}, $stuff->{root_channel}, $stuff->{root_keyword}, $stuff->{root_keyword}, $stuff->{arguments},
$stuff->{result}, $stuff->{tonick}, 0, $stuff->{referenced}, undef, $stuff->{root_keyword});
$self->{pbot}->{logger}->log("fml, after handle action: [$stuff->{result}]\n");
}
if (defined $stuff->{tonick}) {
$self->{pbot}->{logger}->log("($stuff->{from}): $stuff->{nick}!$stuff->{user}\@$stuff->{host}) sent to $stuff->{tonick}\n");
# get rid of original caller's nick
$stuff->{result} =~ s/^\/([^ ]+) \Q$stuff->{nick}\E:\s+/\/$1 /;
$stuff->{result} =~ s/^\Q$stuff->{nick}\E:\s+//;
$self->{pbot}->{interpreter}->handle_result($stuff->{from}, $stuff->{nick}, $stuff->{user}, $stuff->{host}, $stuff->{command}, "$stuff->{keyword} $stuff->{arguments}", "$stuff->{tonick}: $stuff->{result}", 0, $stuff->{preserve_whitespace});
} else {
if ($stuff->{command} ne 'code-factoid' and exists $self->{pbot}->{factoids}->{factoids}->hash->{$stuff->{channel}}->{$stuff->{trigger}}->{add_nick} and $self->{pbot}->{factoids}->{factoids}->hash->{$stuff->{channel}}->{$stuff->{trigger}}->{add_nick} != 0) {
$self->{pbot}->{interpreter}->handle_result($stuff->{from}, $stuff->{nick}, $stuff->{user}, $stuff->{host}, $stuff->{command}, "$stuff->{keyword} $stuff->{arguments}", "$stuff->{nick}: $stuff->{result}", 0, $stuff->{preserve_whitespace});
} else {
$self->{pbot}->{interpreter}->handle_result($stuff->{from}, $stuff->{nick}, $stuff->{user}, $stuff->{host}, $stuff->{command}, "$stuff->{keyword} $stuff->{arguments}", $stuff->{result}, 0, $stuff->{preserve_whitespace});
}
}
my $text = $self->{pbot}->{interpreter}->truncate_result($stuff->{channel}, $self->{pbot}->{registry}->get_value('irc', 'botnick'), 'undef', $stuff->{result}, $stuff->{result}, 0);
$self->{pbot}->{antiflood}->check_flood($stuff->{channel}, $self->{pbot}->{registry}->get_value('irc', 'botnick'), $self->{pbot}->{registry}->get_value('irc', 'username'), 'localhost', $text, 0, 0, 0);
}
1;