revbank/plugins/give
Juerd Waalboer 09411bb6c0 give: Don't do checkout with description as username
Originally, this command didn't have a description parameter. Foo would
use `give xyzzy 10 foo`. Then, a description parameter was added. For
backwards compatibility, if you would enter a username (like `foo` in
this example) in the place of the description, it would finalize the
transaction using that.

However, as the user base grows, several reasonable descriptions exist as
user account names, and that would finalize the transaction under the
wrong user.

It's time to break backward compatibility. If you don't want to leave a
message (it's still optional), that can be done with `x` (like the
`donate` command), or in advanced mode, with `""`.

Because it's likely that people are still very much used to just leaving
the description out, if you enter something that happens to match an
existing username, the input will be rejected.

The current equivalent command line would be `give xyzzy 10 ""; foo` or
`give xyzzy 10 x; foo`. If the `;` (new since v5.0.0) is left out, the
trailing `foo` has to be confirmed with a second press of the enter key.
2023-12-26 05:47:29 +01:00

40 lines
1.2 KiB
Perl

#!perl
HELP "give <account> <amount> [<reason>]" => "Transfer money to user's account";
sub command :Tab(give) ($self, $cart, $command, @) {
return NEXT if $command ne 'give';
return "Beneficiary", \&beneficiary;
}
sub beneficiary :Tab(USERS) ($self, $cart, $input, @) {
$self->{beneficiary} = parse_user($input)
or return REJECT, "$input: No such user.";
return "Amount to give to $self->{beneficiary}", \&amount;
}
sub amount($self, $cart, $input, @) {
$self->{amount} = parse_amount($input)
or return REJECT, "$input: Invalid amount.";
return "Short description ('x' for no message)", \&reason;
}
sub reason :Tab(whatevah) ($self, $cart, $input, @) {
return REJECT, "'$input' is a username, not a description :)."
if parse_user($input);
return REJECT, "'$input' is an amount, not a description :)."
if parse_amount($input);
my $beneficiary = $self->{beneficiary};
my $amount = $self->{amount};
my $reason = $input =~ /^x?$/ ? "" : " ($input)";
$cart
->add(-$amount, "Given to $beneficiary" . $reason)
->add_contra($beneficiary, +$amount, "Received from \$you" . $reason);
return ACCEPT;
}