revbank/plugins/take

69 lines
1.8 KiB
Perl

#!perl
HELP "take <account>... <amount> <reason>" => "Transfer money from them to you";
sub command :Tab(take,steal) {
my ($self, $cart, $command) = @_;
$command eq 'take' or $command eq 'steal'
or return NEXT;
$self->{users} = [];
return "User to take from", \&arg;
}
sub arg :Tab(USERS) {
my ($self, $cart, $arg) = @_;
my @users = @{ $self->{users} };
my $amount = parse_amount($arg);
if (@users and $amount) {
my $each = RevBank::Amount->new_from_float($amount->float / @users);
my $total = $each * @users;
if ($total != $amount) {
print "Adjusted total amount to $total because of rounding.\n";
}
$self->{each} = $each;
$self->{total} = $total;
my $them = @users == 1 ? $users[0] : 'each';
return "Why are you taking $each from $them", \&reason;
}
my $user = parse_user($arg);
if ($user) {
push @{ $self->{users} }, $user;
} else {
return REJECT, "$arg: No such user" .
($amount ? "." : ", and not a valid amount.");
}
return "User to take from, or total amount to finish", \&arg;
}
sub reason :Tab(bbq,NOABORT) { # finish
my ($self, $cart, $reason) = @_;
return REJECT, "'$reason' is a username, not a description :)."
if parse_user($reason);
return REJECT, "'$reason' is an amount, not a description :)."
if parse_amount($reason);
my @users = @{ $self->{users} };
my $each = $self->{each};
my $total = $self->{total};
my $users = join '/', @users;
my $entry = $cart->add($total, "Taken from $users ($reason)", { is_take => 1 });
for my $user (@users) {
$entry->add_contra( $user, -$each, "Taken by \$you ($reason)" );
}
return ACCEPT;
}