3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-28 23:09:39 +01:00
pbot/lib/PBot/Core/Factoids/Code.pm
Pragmatic Software 001f2024c4
Factoids: add default value modifier
Uses POSIX shell parameter expansion syntax.

    <pragma-> !factadd cookie /me gives a cookie to ${args:-nobody. What a shame}!
       <PBot> cookie added to the global channel.

    <pragma-> !cookie Bob
            * PBot gives a cookie to Bob!

    <pragma-> !cookie
            * PBot gives a cookie to nobody. What a shame!

    <pragma-> !factadd sum /call calc $arg[0]:-1 + $arg[1]:-2
       <PBot> sum added to the global channel.

    <pragma-> !sum
       <PBot> 1 + 2 = 3

    <pragma-> !sum 3
       <PBot> 3 + 2 = 5

    <pragma-> !sum 4 6
       <PBot> 4 + 6 = 10
2024-11-27 12:17:45 -08:00

88 lines
3.3 KiB
Perl

# File: Code.pm
#
# Purpose: Launching pad for code factoids. Configures $context as a code
# factoid and executes the vm-client applet.
# SPDX-FileCopyrightText: 2007-2023 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Core::Factoids::Code;
use parent 'PBot::Core::Class';
use PBot::Imports;
use JSON;
sub initialize {}
sub execute($self, $context) {
my $factoids = $self->{pbot}->{factoids}->{data}->{storage};
my $interpolate = $factoids->get_data($context->{channel}, $context->{keyword}, 'interpolate');
unless (defined $interpolate and not $interpolate) {
if ($context->{code} =~ m/(?:\$\{?nick\b|\$\{?args\b|\$\{?arg\[)/ and length $context->{arguments}) {
# disable nick overriding
$context->{nickprefix_disabled} = 1;
} else {
# allow nick overriding
$context->{nickprefix_disabled} = 0;
}
my $variables = $self->{pbot}->{factoids}->{variables};
# expand factoid variables/selectors/etc in code
$context->{code} = $variables->expand_factoid_vars($context, $context->{code});
# expand factoid variables/selectors/etc in arguments
$context->{arguments} = $variables->expand_factoid_vars($context, $context->{arguments});
# expand factoid action $args
if ($factoids->get_data($context->{channel}, $context->{keyword}, 'allow_empty_args')) {
$context->{code} = $variables->expand_action_arguments($context, $context->{code}, $context->{arguments}, '');
} else {
$context->{code} = $variables->expand_action_arguments($context, $context->{code}, $context->{arguments}, $context->{nick});
}
} else {
# otherwise allow nick overriding
$context->{nickprefix_disabled} = 0;
}
# set up `vm-client` applet arguments
my %args = (
nick => $context->{nick},
channel => $context->{from},
lang => $context->{lang},
code => $context->{code},
arguments => $context->{arguments},
factoid => "$context->{channel}:$context->{keyword}",
);
# the vm can persist filesystem data to external storage identified by a key.
# if the `persist-key` factoid metadata is set, then use this key.
my $persist_key = $factoids->get_data($context->{channel}, $context->{keyword}, 'persist-key');
if (defined $persist_key) {
$args{'persist-key'} = $persist_key;
}
# encode args to utf8 json string
my $json = encode_json \%args;
# update context details
$context->{special} = 'code-factoid'; # ensure handle_result(), etc, process this as a code-factoid
$context->{root_channel} = $context->{channel}; # override root channel to current channel
$context->{keyword} = 'vm-client'; # code-factoid uses `vm-client` command to invoke vm
$context->{arguments} = $json; # set arguments to json string as `vm-client` expects
$context->{args_utf8} = 1; # arguments are utf8 encoded by encode_json
# launch the `vm-client` applet
$self->{pbot}->{applets}->execute_applet($context);
# return undef since the applet process reader will
# pass the output along to the result handler
return undef;
}
1;