mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-28 23:09:39 +01:00
001f2024c4
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
125 lines
3.6 KiB
Perl
125 lines
3.6 KiB
Perl
# File: Modifiers.pm
|
|
#
|
|
# Purpose: Implements factoid expansion modifiers.
|
|
|
|
# SPDX-FileCopyrightText: 2010-2023 Pragmatic Software <pragma78@gmail.com>
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
package PBot::Core::Factoids::Modifiers;
|
|
use parent 'PBot::Core::Class';
|
|
|
|
use PBot::Imports;
|
|
|
|
sub initialize {
|
|
}
|
|
|
|
sub parse($self, $modifier, $bracketed = 0) {
|
|
my %modifiers;
|
|
|
|
my $interp = $self->{pbot}->{interpreter};
|
|
|
|
my $modregex;
|
|
my $defregex;
|
|
|
|
if ($bracketed) {
|
|
$modregex = qr/^:(?=.+?:?)/;
|
|
$defregex = qr/^-([^:]+)/;
|
|
} else {
|
|
$modregex = qr/^:(?=[\w+-]+)/;
|
|
$defregex = qr/^-([\w]+)/;
|
|
}
|
|
|
|
while ($$modifier =~ s/$modregex//) {
|
|
if ($$modifier =~ s/^join\s*(?=\(.*?(?=\)))//) {
|
|
my ($params, $rest) = $interp->extract_bracketed($$modifier, '(', ')', '', 1);
|
|
$$modifier = $rest;
|
|
my @args = $interp->split_line($params, strip_quotes => 1, strip_commas => 1);
|
|
$modifiers{'join'} = $args[0];
|
|
next;
|
|
}
|
|
|
|
if ($$modifier=~ s/^\+?sort//) {
|
|
$modifiers{'sort+'} = 1;
|
|
next;
|
|
}
|
|
|
|
if ($$modifier=~ s/^\-sort//) {
|
|
$modifiers{'sort-'} = 1;
|
|
next;
|
|
}
|
|
|
|
if ($$modifier =~ s/$defregex//) {
|
|
$modifiers{'default'} = $1;
|
|
next;
|
|
}
|
|
|
|
if ($$modifier=~ s/^pick_unique\s*(?=\(.*?(?=\)))//) {
|
|
my ($params, $rest) = $interp->extract_bracketed($$modifier, '(', ')', '', 1);
|
|
$$modifier = $rest;
|
|
my @args = $interp->split_line($params, strip_quotes => 1, strip_commas => 1);
|
|
|
|
$modifiers{'pick'} = 1;
|
|
$modifiers{'unique'} = 1;
|
|
|
|
if (@args == 2) {
|
|
$modifiers{'random'} = 1;
|
|
$modifiers{'pick_min'} = $args[0];
|
|
$modifiers{'pick_max'} = $args[1];
|
|
} elsif (@args == 1) {
|
|
$modifiers{'pick_min'} = 1;
|
|
$modifiers{'pick_max'} = $args[0];
|
|
} else {
|
|
push @{$modifiers{errors}}, "pick_unique(): missing argument(s)";
|
|
}
|
|
|
|
next;
|
|
}
|
|
|
|
if ($$modifier=~ s/^pick\s*(?=\(.*?(?=\)))//) {
|
|
my ($params, $rest) = $interp->extract_bracketed($$modifier, '(', ')', '', 1);
|
|
$$modifier = $rest;
|
|
my @args = $interp->split_line($params, strip_quotes => 1, strip_commas => 1);
|
|
|
|
$modifiers{'pick'} = 1;
|
|
|
|
if (@args == 2) {
|
|
$modifiers{'random'} = 1;
|
|
$modifiers{'pick_min'} = $args[0];
|
|
$modifiers{'pick_max'} = $args[1];
|
|
} elsif (@args == 1) {
|
|
$modifiers{'pick_min'} = 1;
|
|
$modifiers{'pick_max'} = $args[0];
|
|
} else {
|
|
push @{$modifiers{errors}}, "pick(): missing argument(s)";
|
|
}
|
|
|
|
next;
|
|
}
|
|
|
|
if ($$modifier=~ s/^index\s*(?=\(.*?(?=\)))//) {
|
|
my ($params, $rest) = $interp->extract_bracketed($$modifier, '(', ')', '', 1);
|
|
$$modifier = $rest;
|
|
my @args = $interp->split_line($params, strip_quotes => 1, strip_commas => 1);
|
|
if (@args == 1) {
|
|
$modifiers{'index'} = $args[0];
|
|
} else {
|
|
push @{$modifiers{errors}}, "index(): missing argument";
|
|
}
|
|
next;
|
|
}
|
|
|
|
if ($$modifier =~ s/^(enumerate|comma|ucfirst|lcfirst|title|uc|lc|json)//) {
|
|
$modifiers{$1} = 1;
|
|
next;
|
|
}
|
|
|
|
if ($$modifier =~ s/^(\w+)//) {
|
|
push @{$modifiers{errors}}, "Unknown modifier `$1`";
|
|
}
|
|
}
|
|
|
|
return %modifiers;
|
|
}
|
|
|
|
1;
|