From 6454ff82aa51493f0b8806e8c47bbd8b964502d1 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 6 Aug 2018 20:23:35 -0700 Subject: [PATCH] Interpreter: support array of positional arguments --- PBot/Interpreter.pm | 78 +++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/PBot/Interpreter.pm b/PBot/Interpreter.pm index 237ee0d3..ecbe6968 100644 --- a/PBot/Interpreter.pm +++ b/PBot/Interpreter.pm @@ -16,7 +16,7 @@ use base 'PBot::Registerable'; use Time::HiRes qw/gettimeofday/; use Time::Duration; -use Text::Balanced qw/extract_bracketed/; +use Text::Balanced qw/extract_bracketed extract_quotelike/; use Carp (); use PBot::Utils::ValidateString; @@ -106,32 +106,6 @@ sub process_line { my $cmd_text = $text; $cmd_text =~ s/^\/me\s+//; -=cut - # check for code compiler invocation - my $has_code; - if ($cmd_text =~ m/^(?:$botnick.?)?\s*{\s*(.+)\s*}\s*$/) { - $has_code = $1; - $preserve_whitespace = 1; - } elsif ($cmd_text =~ m/^\s*($nick_regex)[,:]*\s*{\s*(.+)\s*}\s*$/) { - my $possible_nick_override = $1; - $has_code = $2 if $possible_nick_override !~ /^(?:enum|struct|union)$/; - $preserve_whitespace = 1; - $nick_override = $self->{pbot}->{nicklist}->is_present($from, $possible_nick_override); - } - - if (defined $has_code) { - $processed += 1000; # hint to other plugins that this message has been handled - if($pbot->{registry}->get_value('general', 'compile_blocks') and not $pbot->{registry}->get_value($from, 'no_compile_blocks') - and not grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_ignore_channels') - and grep { $from =~ /$_/i } $pbot->{registry}->get_value('general', 'compile_blocks_channels')) { - if (not defined $nick_override or (defined $nick_override and $nick_override != 0)) { - return "Using {} to compile code is temporarily disabled. Use the `cc` command instead."; - #return $pbot->{factoids}->{factoidmodulelauncher}->execute_module($from, undef, $nick, $user, $host, $text, "compiler_block", $from, '{', (defined $nick_override ? $nick_override : $nick) . " $from $has_code }", $preserve_whitespace); - } - } - } -=cut - # check for bot command invocation my @commands; my $command; @@ -344,11 +318,61 @@ sub interpret { # unescape any escaped pipes $arguments =~ s/\\\|\s*\{/| {/g if defined $arguments; + # set arguments as a plain string $stuff->{arguments} = $arguments; + # set arguments as a positional array + my @args = split / /, $arguments; + my @pargs; + + while (@args) { + my $arg = shift @args; + + if ($arg =~ /^["']/) { + my $string = $arg; + if (@args) { + $string .= ' '; + $string .= join(' ', @args); + } + my ($extracted, $rest) = extract_quotelike $string; + if (defined $extracted) { + # strip quote characters + $extracted =~ s/^(.)//; + $extracted =~ s/$1$//; + push @pargs, $extracted; + $rest =~ s/^ //; + @args = split / /, $rest; + } else { + # mismatched quotes, shove the remainder as the last positional argument + push @pargs, $rest; + last; + } + } else { + push @pargs, $arg; + } + } + + $stuff->{argumentspos} = \@pargs; + return $self->SUPER::execute_all($stuff); } +# splits array of arguments into array with overflow arguments filling up last position +# split_args(qw/dog cat bird hamster/, 3) => ("dog", "cat", "bird hamster") +sub split_args { + my ($self, $args, $count) = @_; + my @result; + + while (--$count) { + my $arg = shift @$args; + push @result, $arg; + } + + my $rest = join ' ', @$args; + push @result, $rest; + return @result; +} + sub truncate_result { my ($self, $from, $nick, $text, $original_result, $result, $paste) = @_; my $max_msg_len = $self->{pbot}->{registry}->get_value('irc', 'max_msg_len');