From beef46f120a8a789aef2fe888e91f92e93c44f62 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Thu, 21 May 2020 19:23:30 -0700 Subject: [PATCH] Variables such as $0 can now be escaped, e.g. in `factset`, etc --- PBot/Commands.pm | 1 - PBot/Factoids.pm | 28 ++++++++++++++-------------- PBot/Modules.pm | 12 ++++++++---- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/PBot/Commands.pm b/PBot/Commands.pm index 8b31d7f7..0e6c9fd7 100644 --- a/PBot/Commands.pm +++ b/PBot/Commands.pm @@ -234,7 +234,6 @@ sub interpreter { $context->{action} = $context->{arguments}; $context->{arguments} = $self->{pbot}->{factoids}->expand_factoid_vars($context); - $context->{arguments} = $self->{pbot}->{factoids}->expand_special_vars($context->{from}, $context->{nick}, $context->{keyword}, $context->{arguments}); delete $context->{action}; $context->{no_nickoverride} = 1; diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 4ff38aae..c6957369 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -436,16 +436,16 @@ sub escape_json { sub expand_special_vars { my ($self, $from, $nick, $root_keyword, $action) = @_; - $action =~ s/\$nick:json|\$\{nick:json\}/$self->escape_json($nick)/ge; - $action =~ s/\$channel:json|\$\{channel:json\}/$self->escape_json($from)/ge; + $action =~ s/(?escape_json($nick)/ge; + $action =~ s/(?escape_json($from)/ge; $action =~ - s/\$randomnick:json|\$\{randomnick:json\}/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $self->escape_json($random) : $self->escape_json($nick)/ge; - $action =~ s/\$0:json|\$\{0:json\}/$self->escape_json($root_keyword)/ge; + s/(?{pbot}->{nicklist}->random_nick($from); $random ? $self->escape_json($random) : $self->escape_json($nick)/ge; + $action =~ s/(?escape_json($root_keyword)/ge; - $action =~ s/\$nick|\$\{nick\}/$nick/g; - $action =~ s/\$channel|\$\{channel\}/$from/g; - $action =~ s/\$randomnick|\$\{randomnick\}/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge; - $action =~ s/\$0\b|\$\{0\}\b/$root_keyword/g; + $action =~ s/(?{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge; + $action =~ s/(?{pbot}->{registry}->get_value('factoids', 'max_content_length')); } @@ -475,7 +475,7 @@ sub expand_factoid_vars { my $offset = 0; my $matches = 0; my $expansions = 0; - $action =~ s/\$0/$root_keyword/g; + $action =~ s/(?{pbot}->{logger}->log("action: $const_action\n") if $debug; @@ -609,10 +609,10 @@ sub expand_factoid_vars { last if $matches == 0 or $expansions == 0; } - $action =~ s/\\\$/\$/g; - unless (@exclude) { $action = $self->expand_special_vars($from, $nick, $root_keyword, $action); } + $action =~ s/\\\$/\$/g; + return validate_string($action, $self->{pbot}->{registry}->get_value('factoids', 'max_content_length')); } @@ -897,7 +897,7 @@ sub interpreter { if ($self->{factoids}->exists($channel, $keyword, 'usage') and not length $context->{arguments} and $self->{factoids}->get_data($channel, $keyword, 'requires_arguments')) { $context->{alldone} = 1; my $usage = $self->{factoids}->get_data($channel, $keyword, 'usage'); - $usage =~ s/\$0|\$\{0\}/$trigger_name/g; + $usage =~ s/(?{factoids}->exists($channel, $keyword, 'usage') and not length $context->{arguments}) { $context->{alldone} = 1; my $usage = $self->{factoids}->get_data($channel, $keyword, 'usage'); - $usage =~ s/\$0|\$\{0\}/$trigger_name/g; + $usage =~ s/(?{factoids}->exists($channel, $keyword, 'usage')) { $action = "/say " . $self->{factoids}->get_data($channel, $keyword, 'usage'); - $action =~ s/\$0|\$\{0\}/$trigger_name/g; + $action =~ s/(?{alldone} = 1; } else { if ($self->{factoids}->get_data($channel, $keyword, 'allow_empty_args')) { $action = $self->expand_action_arguments($action, undef, ''); } diff --git a/PBot/Modules.pm b/PBot/Modules.pm index dfdbf63a..1f4a9c6c 100644 --- a/PBot/Modules.pm +++ b/PBot/Modules.pm @@ -81,10 +81,14 @@ sub launch_module { $context->{trigger} = $trigger; my $module = $self->{pbot}->{factoids}->{factoids}->get_data($channel, $trigger, 'action'); - $self->{pbot}->{logger}->log("(" - . (defined $context->{from} ? $context->{from} : "(undef)") - . "): $context->{nick}!$context->{user}\@$context->{host}: Executing module [$context->{command}] $module $context->{arguments}\n"); - $context->{arguments} = $self->{pbot}->{factoids}->expand_special_vars($context->{from}, $context->{nick}, $context->{root_keyword}, $context->{arguments}); + $self->{pbot}->{logger}->log( + "(" . (defined $context->{from} ? $context->{from} : "(undef)") + . "): $context->{nick}!$context->{user}\@$context->{host}: Executing module [$context->{command}] $module $context->{arguments}\n" + ); + + $context->{action} = $context->{arguments}; + $context->{arguments} = $self->{pbot}->{factoids}->expand_factoid_vars($context); + delete $context->{action}; my $module_dir = $self->{pbot}->{registry}->get_value('general', 'module_dir'); if (not chdir $module_dir) {