mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-17 01:19:31 +01:00
Fix white-space handling; add suppress-no-output
This commit is contained in:
parent
1f232a9455
commit
4c866d39b6
@ -20,23 +20,30 @@ sub split_line ($line, %opts) {
|
||||
my %default_opts = (
|
||||
strip_quotes => 0,
|
||||
keep_spaces => 0,
|
||||
preserve_escapes => 1,
|
||||
preserve_escapes => 0,
|
||||
strip_commas => 0,
|
||||
);
|
||||
|
||||
print STDERR "split: [$line]\n";
|
||||
|
||||
%opts = (%default_opts, %opts);
|
||||
|
||||
return () if not length $line;
|
||||
|
||||
my @chars = split //, $line;
|
||||
|
||||
my @args;
|
||||
my $escaped = 0;
|
||||
my $ch;
|
||||
my $pos;
|
||||
my $quote;
|
||||
my $escaped = 0;
|
||||
my $token = '';
|
||||
my $last_token = '';
|
||||
my $ch = ' ';
|
||||
my $i = 0;
|
||||
my $pos = 0;
|
||||
my $ignore_quote = 0;
|
||||
my $spaces = 0;
|
||||
my $add_token = 0;
|
||||
my $got_ch = 0;
|
||||
|
||||
while (1) {
|
||||
if ($i >= @chars) {
|
||||
@ -48,28 +55,36 @@ sub split_line ($line, %opts) {
|
||||
$token = $last_token;
|
||||
} else {
|
||||
# add final token and exit
|
||||
push @args, $token if length $token;
|
||||
$token .= '\\' if $escaped;
|
||||
push @args, $token;
|
||||
last;
|
||||
}
|
||||
}
|
||||
|
||||
$ch = $chars[$i++];
|
||||
|
||||
my $dquote = $quote // 'undef';
|
||||
$spaces = 0 if $ch ne ' ';
|
||||
|
||||
if ($escaped) {
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
if ($opts{preserve_escapes}) {
|
||||
$token .= "\\$ch";
|
||||
} else {
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
$escaped = 0;
|
||||
next;
|
||||
}
|
||||
|
||||
if ($ch eq '\\') {
|
||||
$escaped = 1;
|
||||
$got_ch = 1;
|
||||
next;
|
||||
}
|
||||
|
||||
@ -86,6 +101,14 @@ sub split_line ($line, %opts) {
|
||||
}
|
||||
|
||||
if (not defined $quote and ($ch eq "'" or $ch eq '"')) {
|
||||
$got_ch = 1;
|
||||
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
if ($ignore_quote) {
|
||||
# treat unbalanced quote as part of this argument
|
||||
$token .= $ch;
|
||||
@ -100,20 +123,34 @@ sub split_line ($line, %opts) {
|
||||
next;
|
||||
}
|
||||
|
||||
if ($ch eq ' ') {
|
||||
if ($ch eq ' ' or $ch eq "\n" or $ch eq "\t" or ($opts{strip_commas} and $ch eq ',')) {
|
||||
if (++$spaces > 1 and $opts{keep_spaces}) {
|
||||
$token .= $ch;
|
||||
next;
|
||||
} else {
|
||||
push @args, $token if length $token;
|
||||
$token = '';
|
||||
if ($opts{keep_spaces} && $ch eq "\n") {
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
unless ($opts{strip_commas} and $token eq ',') {
|
||||
$add_token = 1 if $got_ch;;
|
||||
}
|
||||
next;
|
||||
}
|
||||
}
|
||||
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
$got_ch = 1;
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
use Data::Dumper;
|
||||
print STDERR "split: ", Dumper(\@args), "\n";
|
||||
return @args;
|
||||
}
|
||||
|
||||
|
@ -20,23 +20,30 @@ sub split_line ($line, %opts) {
|
||||
my %default_opts = (
|
||||
strip_quotes => 0,
|
||||
keep_spaces => 0,
|
||||
preserve_escapes => 1,
|
||||
preserve_escapes => 0,
|
||||
strip_commas => 0,
|
||||
);
|
||||
|
||||
print STDERR "split: [$line]\n";
|
||||
|
||||
%opts = (%default_opts, %opts);
|
||||
|
||||
return () if not length $line;
|
||||
|
||||
my @chars = split //, $line;
|
||||
|
||||
my @args;
|
||||
my $escaped = 0;
|
||||
my $ch;
|
||||
my $pos;
|
||||
my $quote;
|
||||
my $escaped = 0;
|
||||
my $token = '';
|
||||
my $last_token = '';
|
||||
my $ch = ' ';
|
||||
my $i = 0;
|
||||
my $pos = 0;
|
||||
my $ignore_quote = 0;
|
||||
my $spaces = 0;
|
||||
my $add_token = 0;
|
||||
my $got_ch = 0;
|
||||
|
||||
while (1) {
|
||||
if ($i >= @chars) {
|
||||
@ -48,7 +55,8 @@ sub split_line ($line, %opts) {
|
||||
$token = $last_token;
|
||||
} else {
|
||||
# add final token and exit
|
||||
push @args, $token if length $token;
|
||||
$token .= '\\' if $escaped;
|
||||
push @args, $token;
|
||||
last;
|
||||
}
|
||||
}
|
||||
@ -58,17 +66,25 @@ sub split_line ($line, %opts) {
|
||||
$spaces = 0 if $ch ne ' ';
|
||||
|
||||
if ($escaped) {
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
if ($opts{preserve_escapes}) {
|
||||
$token .= "\\$ch";
|
||||
} else {
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
$escaped = 0;
|
||||
next;
|
||||
}
|
||||
|
||||
if ($ch eq '\\') {
|
||||
$escaped = 1;
|
||||
$got_ch = 1;
|
||||
next;
|
||||
}
|
||||
|
||||
@ -85,6 +101,14 @@ sub split_line ($line, %opts) {
|
||||
}
|
||||
|
||||
if (not defined $quote and ($ch eq "'" or $ch eq '"')) {
|
||||
$got_ch = 1;
|
||||
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
if ($ignore_quote) {
|
||||
# treat unbalanced quote as part of this argument
|
||||
$token .= $ch;
|
||||
@ -99,20 +123,34 @@ sub split_line ($line, %opts) {
|
||||
next;
|
||||
}
|
||||
|
||||
if ($ch eq ' ') {
|
||||
if ($opts{keep_spaces} && (++$spaces > 1 || $ch eq "\n")) {
|
||||
if ($ch eq ' ' or $ch eq "\n" or $ch eq "\t" or ($opts{strip_commas} and $ch eq ',')) {
|
||||
if (++$spaces > 1 and $opts{keep_spaces}) {
|
||||
$token .= $ch;
|
||||
next;
|
||||
} else {
|
||||
push @args, $token if length $token;
|
||||
$token = '';
|
||||
if ($opts{keep_spaces} && $ch eq "\n") {
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
unless ($opts{strip_commas} and $token eq ',') {
|
||||
$add_token = 1 if $got_ch;;
|
||||
}
|
||||
next;
|
||||
}
|
||||
}
|
||||
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
$got_ch = 1;
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
use Data::Dumper;
|
||||
print STDERR "split: ", Dumper(\@args), "\n";
|
||||
return @args;
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,7 @@ our %factoid_metadata = (
|
||||
'ref_user' => 'TEXT',
|
||||
'require_explicit_args' => 'INTEGER',
|
||||
'requires_arguments' => 'INTEGER',
|
||||
'suppress-no-output' => 'INTEGER',
|
||||
'type' => 'TEXT',
|
||||
'unquote_spaces' => 'INTEGER',
|
||||
'usage' => 'TEXT',
|
||||
|
@ -392,6 +392,14 @@ sub interpret($self, $context) {
|
||||
}
|
||||
}
|
||||
|
||||
if ($self->{pbot}->{commands}->get_meta($keyword, 'suppress-no-output')
|
||||
or $self->{pbot}->{factoids}->{data}->get_meta($fact_channel, $fact_trigger, 'suppress-no-output'))
|
||||
{
|
||||
$context->{'suppress_no_output'} = 1;
|
||||
} else {
|
||||
delete $context->{'suppress_no_output'};
|
||||
}
|
||||
|
||||
if ($self->{pbot}->{commands}->get_meta($keyword, 'dont-replace-pronouns')
|
||||
or $self->{pbot}->{factoids}->{data}->get_meta($fact_channel, $fact_trigger, 'dont-replace-pronouns'))
|
||||
{
|
||||
@ -1203,18 +1211,19 @@ sub split_line($self, $line, %opts) {
|
||||
my @chars = split //, $line;
|
||||
|
||||
my @args;
|
||||
my $escaped = 0;
|
||||
my $ch;
|
||||
my $pos;
|
||||
my $quote;
|
||||
my $escaped = 0;
|
||||
my $token = '';
|
||||
my $last_token = '';
|
||||
my $ch = ' ';
|
||||
my $i = 0;
|
||||
my $pos;
|
||||
my $ignore_quote = 0;
|
||||
my $spaces = 0;
|
||||
my $add_token = 0;
|
||||
my $got_ch = 0;
|
||||
|
||||
while (1) {
|
||||
|
||||
if ($i >= @chars) {
|
||||
if (defined $quote) {
|
||||
# reached end, but unbalanced quote... reset to beginning of quote and ignore it
|
||||
@ -1225,7 +1234,7 @@ sub split_line($self, $line, %opts) {
|
||||
} else {
|
||||
# add final token and exit
|
||||
$token .= '\\' if $escaped;
|
||||
push @args, $token if length $token;
|
||||
push @args, $token;
|
||||
last;
|
||||
}
|
||||
}
|
||||
@ -1235,17 +1244,25 @@ sub split_line($self, $line, %opts) {
|
||||
$spaces = 0 if $ch ne ' ';
|
||||
|
||||
if ($escaped) {
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
if ($opts{preserve_escapes}) {
|
||||
$token .= "\\$ch";
|
||||
} else {
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
$escaped = 0;
|
||||
next;
|
||||
}
|
||||
|
||||
if ($ch eq '\\') {
|
||||
$escaped = 1;
|
||||
$got_ch = 1;
|
||||
next;
|
||||
}
|
||||
|
||||
@ -1262,6 +1279,14 @@ sub split_line($self, $line, %opts) {
|
||||
}
|
||||
|
||||
if (not defined $quote and ($ch eq "'" or $ch eq '"')) {
|
||||
$got_ch = 1;
|
||||
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
if ($ignore_quote) {
|
||||
# treat unbalanced quote as part of this argument
|
||||
$token .= $ch;
|
||||
@ -1286,14 +1311,19 @@ sub split_line($self, $line, %opts) {
|
||||
}
|
||||
|
||||
unless ($opts{strip_commas} and $token eq ',') {
|
||||
push @args, $token if length $token;
|
||||
$add_token = 1 if $got_ch;;
|
||||
}
|
||||
|
||||
$token = '';
|
||||
next;
|
||||
}
|
||||
}
|
||||
|
||||
if ($add_token) {
|
||||
push @args, $token;
|
||||
$token = '';
|
||||
$add_token = 0;
|
||||
}
|
||||
|
||||
$got_ch = 1;
|
||||
$token .= $ch;
|
||||
}
|
||||
|
||||
@ -1303,7 +1333,7 @@ sub split_line($self, $line, %opts) {
|
||||
# creates an array of arguments from a string
|
||||
sub make_args($self, $string, %opts) {
|
||||
my %default_opts = (
|
||||
keep_spaces => 1,
|
||||
keep_spaces => 0,
|
||||
preserve_escapes => 1,
|
||||
);
|
||||
|
||||
|
@ -153,9 +153,12 @@ sub process_pipe_reader($self, $pid, $buf) {
|
||||
# check for output
|
||||
if (not defined $context->{result} or not length $context->{result}) {
|
||||
$self->{pbot}->{logger}->log("No result from process.\n");
|
||||
return if $context->{suppress_no_output};
|
||||
if ($context->{suppress_no_output}) {
|
||||
$context->{result} = '';
|
||||
} else {
|
||||
$context->{result} = "No output.";
|
||||
}
|
||||
}
|
||||
|
||||
# don't output unnecessary result if command was embedded within a message
|
||||
if ($context->{embedded}) {
|
||||
@ -165,12 +168,6 @@ sub process_pipe_reader($self, $pid, $buf) {
|
||||
# handle code factoid result
|
||||
if (exists $context->{special} and $context->{special} eq 'code-factoid') {
|
||||
$context->{result} =~ s/\s+$//g;
|
||||
|
||||
if (not length $context->{result}) {
|
||||
$self->{pbot}->{logger}->log("No text result from code-factoid.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
$context->{original_keyword} = $context->{root_keyword};
|
||||
$context->{result} = $self->{pbot}->{factoids}->{interpreter}->handle_action($context, $context->{result});
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ use PBot::Imports;
|
||||
# These are set by the /misc/update_version script
|
||||
use constant {
|
||||
BUILD_NAME => "PBot",
|
||||
BUILD_REVISION => 4830,
|
||||
BUILD_REVISION => 4831,
|
||||
BUILD_DATE => "2024-11-03",
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user