3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 19:22:40 +01:00

Add :json modifier to escape variables for JSON

This commit is contained in:
Pragmatic Software 2017-09-23 21:04:21 -07:00
parent 346dcd3320
commit ab7aa990a0
3 changed files with 1447 additions and 946 deletions

View File

@ -62,10 +62,7 @@ sub execute_module {
$self->{pbot}->{logger}->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: Executing module [$command] $module $arguments\n"); $self->{pbot}->{logger}->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: Executing module [$command] $module $arguments\n");
$arguments =~ s/\$nick/$nick/g; $arguments = $self->{pbot}->{factoids}->expand_factoid_vars($from, $nick, $root_keyword, $arguments);
$arguments =~ s/\$channel/$from/g;
$arguments =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge;
$arguments = quotemeta($arguments); $arguments = quotemeta($arguments);
if($command eq 'code-factoid' or exists $self->{pbot}->{factoids}->{factoids}->hash->{$channel}->{$trigger}->{unquote_spaces}) { if($command eq 'code-factoid' or exists $self->{pbot}->{factoids}->{factoids}->hash->{$channel}->{$trigger}->{unquote_spaces}) {

View File

@ -372,6 +372,15 @@ sub find_factoid {
return @result; return @result;
} }
sub escape_json {
my ($self, $text) = @_;
my $thing = {thing => $text};
my $json = encode_json $thing;
$json =~ s/^{".*":"//;
$json =~ s/"}$//;
return $json;
}
sub expand_factoid_vars { sub expand_factoid_vars {
my ($self, $from, $nick, $root_keyword, $action, @exclude) = @_; my ($self, $from, $nick, $root_keyword, $action, @exclude) = @_;
@ -381,13 +390,13 @@ sub expand_factoid_vars {
while (1) { while (1) {
last if ++$depth >= 10; last if ++$depth >= 10;
my $matches = 0; my $matches = 0;
$action =~ s/\$0/$root_keyword/g; $action =~ s/\$0\b/$root_keyword/g;
$action = validate_string($action);
my $const_action = $action; my $const_action = $action;
while ($const_action =~ /(\ba\s*|\ban\s*)?(?<!\\)\$([a-zA-Z0-9_:\-#\[\]]+)/gi) { while ($const_action =~ /(\ba\s*|\ban\s*)?(?<!\\)\$([a-zA-Z0-9_:\-#\[\]]+)/gi) {
my ($a, $v) = ($1, $2); my ($a, $v) = ($1, $2);
$v =~ s/(.):$/$1/; $v =~ s/(.):$/$1/;
next if $v =~ m/^(nick|channel|randomnick|arglen|jargs|args|arg\[.+\]|[_0])$/i; # don't override special variables next if $v =~ m/^_/; # underscore-prefixed vars reserved for code-factoids
next if $v =~ m/^(nick|channel|randomnick|arglen|args|arg\[.+\]|[_0])$/i; # don't override special variables
next if @exclude && grep { $v =~ m/^\Q$_\E$/i } @exclude; next if @exclude && grep { $v =~ m/^\Q$_\E$/i } @exclude;
$matches++; $matches++;
@ -440,6 +449,9 @@ sub expand_factoid_vars {
when ('title') { when ('title') {
$mylist[$line] = ucfirst lc $mylist[$line]; $mylist[$line] = ucfirst lc $mylist[$line];
} }
when ('json') {
$mylist[$line] = $self->escape_json($mylist[$line]);
}
} }
} }
@ -457,6 +469,11 @@ sub expand_factoid_vars {
$action =~ s/\\\$/\$/g; $action =~ s/\\\$/\$/g;
unless (@exclude) { unless (@exclude) {
$action =~ s/\$nick:json/$self->escape_json($nick)/ge;
$action =~ s/\$channel:json/$self->escape_json($from)/ge;
$action =~ s/\$randomnick:json/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $self->escape_json($random) : $self->escape_json($nick)/ge;
$action =~ s/\$0:json/$self->escape_json($root_keyword)/ge;
$action =~ s/\$nick/$nick/g; $action =~ s/\$nick/$nick/g;
$action =~ s/\$channel/$from/g; $action =~ s/\$channel/$from/g;
$action =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge; $action =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge;
@ -483,11 +500,11 @@ sub expand_action_arguments {
$jsonargs =~ s/"}$//; $jsonargs =~ s/"}$//;
if (not defined $input or $input eq '') { if (not defined $input or $input eq '') {
$action =~ s/\$args:json/$jsonargs/ge;
$action =~ s/\$args(?![[\w])/$nick/g; $action =~ s/\$args(?![[\w])/$nick/g;
$action =~ s/\$jargs(?![[\w])/$jsonargs/ge;
} else { } else {
$action =~ s/\$args:json/$jsonargs/g;
$action =~ s/\$args(?![[\w])/$input/g; $action =~ s/\$args(?![[\w])/$input/g;
$action =~ s/\$jargs(?![[\w])/$jsonargs/g;
} }
my $qinput = quotemeta $input; my $qinput = quotemeta $input;

File diff suppressed because it is too large Load Diff