Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/RevBank/Accounts.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ sub create($account) {
return $account;
}

sub delete($account) {
die "No such account" if RevBank::Accounts::is_special $account;

my $user_found = 0;
rewrite $filename, sub($line) {
my @a = split " ", $line;
if (lc $a[0] ne lc $account) {
return $line;
}
if ($a[1] != 0) {
die "Account $account still has balance";
}
$user_found = 1;
return "";
};

die "No such account" if not $user_found;
}

sub update($account, $delta, $transaction_id) {
$account = assert_account($account);

Expand Down
6 changes: 6 additions & 0 deletions lib/RevBank/Accounts.pod
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ Creates an account with that name and a balance of zero. The name must not alrea

After updating the file, calls the C<account_created> hook with the account name.

=head3 delete($name)

Deletes an account.

Special accounts may not be deleted via this method. The account must have a balance of 0.00.

=head3 update($name, $delta, $transaction_id)

Given the relative change (C<$delta>), updates the user balance for an account.
Expand Down
51 changes: 51 additions & 0 deletions plugins/deluser
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!perl

use List::Util qw(any);

HELP1 "deluser <name>" => "Delete an account";

our $contra = "+donations";

sub command :Tab(deluser) ($self, $cart, $command, @) {
$command eq 'deluser' or return NEXT;

return "Name of the account to be deleted", \&confirm_delete;
}

sub confirm_delete($self, $cart, $name, @) {
my $amount = RevBank::Accounts::balance($name);

return ABORT, "No such account."
if (not defined $amount);

return ABORT, "Can not delete an account with a negative balance."
if ($amount < 0);

$self->{name} = $name;

print "This will delete account $name and donate the remaining $amount.\n";

return "Are you sure? Type DONATE_AND_DELETE to proceed", \&delete_account;
}

sub delete_account($self, $cart, $confirmation, @) {
return REJECT, "Type DONATE_AND_DELETE to proceed"
if ($confirmation ne "DONATE_AND_DELETE");

my $amount = RevBank::Accounts::balance($self->{name});

RevBank::FileIO::with_lock {
if ($amount > 0) {
my $contra_account = RevBank::Accounts::assert_account($contra);
my $transaction_id = "";
RevBank::Accounts::update($self->{name}, -$amount, $transaction_id);
RevBank::Accounts::update($contra_account, $amount, $transaction_id);
}

RevBank::Accounts::delete($self->{name});
};

print "Account $self->{name} deleted.";

return ACCEPT;
}