3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 19:22:40 +01:00

MessageHistory: Add akadelete command

This commit is contained in:
Pragmatic Software 2021-09-06 12:43:18 -07:00
parent 169a4c9fc7
commit e60c4dc1f2
3 changed files with 142 additions and 7 deletions

View File

@ -24,10 +24,12 @@ sub initialize {
$self->{pbot}->{commands}->register(sub { $self->cmd_rebuild_aliases(@_) }, "rebuildaliases", 1); $self->{pbot}->{commands}->register(sub { $self->cmd_rebuild_aliases(@_) }, "rebuildaliases", 1);
$self->{pbot}->{commands}->register(sub { $self->cmd_aka_link(@_) }, "akalink", 1); $self->{pbot}->{commands}->register(sub { $self->cmd_aka_link(@_) }, "akalink", 1);
$self->{pbot}->{commands}->register(sub { $self->cmd_aka_unlink(@_) }, "akaunlink", 1); $self->{pbot}->{commands}->register(sub { $self->cmd_aka_unlink(@_) }, "akaunlink", 1);
$self->{pbot}->{commands}->register(sub { $self->cmd_aka_delete(@_) }, "akadelete", 1);
# add capabilities to admin group # add capabilities to admin group
$self->{pbot}->{capabilities}->add('admin', 'can-akalink', 1); $self->{pbot}->{capabilities}->add('admin', 'can-akalink', 1);
$self->{pbot}->{capabilities}->add('admin', 'can-akaunlink', 1); $self->{pbot}->{capabilities}->add('admin', 'can-akaunlink', 1);
$self->{pbot}->{capabilities}->add('admin', 'can-akadelete', 1);
} }
sub cmd_list_also_known_as { sub cmd_list_also_known_as {
@ -471,7 +473,7 @@ sub cmd_aka_link {
$type = $self->{pbot}->{messagehistory}->{database}->{alias_type}->{STRONG} if not defined $type; $type = $self->{pbot}->{messagehistory}->{database}->{alias_type}->{STRONG} if not defined $type;
if (not $id or not $alias) { if (not $id or not $alias) {
return "Usage: link <target id> <alias id> [type]"; return "Usage: akalink <target id> <alias id> [type]";
} }
my $source = $self->{pbot}->{messagehistory}->{database}->find_most_recent_hostmask($id); my $source = $self->{pbot}->{messagehistory}->{database}->find_most_recent_hostmask($id);
@ -498,7 +500,7 @@ sub cmd_aka_unlink {
my ($id, $alias) = split /\s+/, $context->{arguments}; my ($id, $alias) = split /\s+/, $context->{arguments};
if (not $id or not $alias) { if (not $id or not $alias) {
return "Usage: unlink <target id> <alias id>"; return "Usage: akaunlink <target id> <alias id>";
} }
my $source = $self->{pbot}->{messagehistory}->{database}->find_most_recent_hostmask($id); my $source = $self->{pbot}->{messagehistory}->{database}->find_most_recent_hostmask($id);
@ -519,4 +521,72 @@ sub cmd_aka_unlink {
} }
} }
sub cmd_aka_delete {
my ($self, $context) = @_;
my $usage = "Usage: akadelete [-hn] <account id or hostmask>; -h delete only hostmask; -n delete only nickserv";
if (not length $context->{arguments}) {
return $usage;
}
my ($delete_hostmask, $delete_nickserv);
my %opts = (
h => \$delete_hostmask,
n => \$delete_nickserv,
);
my ($opt_args, $opt_error) = $self->{pbot}->{interpreter}->getopt(
$context->{arguments},
\%opts,
['bundling_override'],
qw(h n),
);
return "/say $opt_error -- $usage" if defined $opt_error;
return "Too many arguments -- $usage" if @$opt_args > 1;
return "Missing argument -- $usage" if @$opt_args != 1;
my $id = $opt_args->[0];
my $hostmask;
if ($id !~ /^\d+$/) {
$hostmask = $id;
$id = $self->{pbot}->{messagehistory}->{database}->get_message_account_id($hostmask);
if (not defined $id) {
return "No such hostmask $hostmask found.";
}
} else {
$hostmask = $self->{pbot}->{messagehistory}->{database}->find_most_recent_hostmask($id);
if (not defined $hostmask) {
return "No such id $id found.";
}
}
my @deletions;
if ($delete_hostmask) {
$self->{pbot}->{messagehistory}->{database}->delete_hostmask($id, $hostmask);
push @deletions, 'hostmask';
}
if ($delete_nickserv) {
$self->{pbot}->{messagehistory}->{database}->delete_nickserv_accounts($id);
$self->{pbot}->{messagehistory}->{database}->set_current_nickserv_account($id, undef);
push @deletions, 'NickServ accounts';
}
if ($delete_hostmask || $delete_nickserv) {
return 'Deleted ' . (join ' and ', @deletions) . " from $hostmask ($id)";
}
$self->{pbot}->{messagehistory}->database->delete_account($id);
return "/say Deleted $hostmask ($id).";
}
1; 1;

View File

@ -190,7 +190,7 @@ sub get_gecos {
my ($self, $id) = @_; my ($self, $id) = @_;
my $gecos = eval { my $gecos = eval {
my $sth = $self->{dbh}->prepare('SELECT gecos FROM Gecos WHERE ID = ?'); my $sth = $self->{dbh}->prepare('SELECT gecos FROM Gecos WHERE id = ?');
$sth->execute($id); $sth->execute($id);
return $sth->fetchall_arrayref(); return $sth->fetchall_arrayref();
}; };
@ -202,7 +202,7 @@ sub get_nickserv_accounts {
my ($self, $id) = @_; my ($self, $id) = @_;
my $nickserv_accounts = eval { my $nickserv_accounts = eval {
my $sth = $self->{dbh}->prepare('SELECT nickserv FROM Nickserv WHERE ID = ?'); my $sth = $self->{dbh}->prepare('SELECT nickserv FROM Nickserv WHERE id = ?');
$sth->execute($id); $sth->execute($id);
return $sth->fetchall_arrayref(); return $sth->fetchall_arrayref();
}; };
@ -210,6 +210,23 @@ sub get_nickserv_accounts {
return map { $_->[0] } @$nickserv_accounts; return map { $_->[0] } @$nickserv_accounts;
} }
sub delete_nickserv_accounts {
my ($self, $id) = @_;
eval {
$self->{dbh}->do('DELETE FROM Nickserv WHERE id = ?', undef, $id);
$self->{dbh}->commit;
$self->{dbh}->begin_work;
};
if ($@) {
$self->{pbot}->{logger}->log($@);
return 0;
}
return 1;
}
sub set_current_nickserv_account { sub set_current_nickserv_account {
my ($self, $id, $nickserv) = @_; my ($self, $id, $nickserv) = @_;
@ -785,7 +802,8 @@ sub find_most_recent_hostmask {
my $hostmask = eval { my $hostmask = eval {
my $sth = $self->{dbh}->prepare('SELECT hostmask FROM Hostmasks WHERE ID = ? ORDER BY last_seen DESC LIMIT 1'); my $sth = $self->{dbh}->prepare('SELECT hostmask FROM Hostmasks WHERE ID = ? ORDER BY last_seen DESC LIMIT 1');
$sth->execute($id); $sth->execute($id);
return $sth->fetchrow_hashref()->{'hostmask'}; my $row = $sth->fetchrow_hashref();
return defined $row ? $row->{hostmask} : undef;
}; };
$self->{pbot}->{logger}->log($@) if $@; $self->{pbot}->{logger}->log($@) if $@;
return $hostmask; return $hostmask;
@ -1626,6 +1644,53 @@ sub unlink_alias {
return $ret; return $ret;
} }
sub delete_hostmask {
my ($self, $id, $hostmask) = @_;
eval {
$self->{dbh}->do('DELETE FROM Hostmasks WHERE id = ? AND hostmask = ?', undef, $id, $hostmask);
$self->{dbh}->commit;
$self->{dbh}->begin_work;
};
if ($@) {
$self->{pbot}->{logger}->log($@);
return 0;
}
return 1;
}
sub delete_account {
my ($self, $id) = @_;
$self->{dbh}->commit;
$self->{dbh}->begin_work;
my $ret = eval {
$self->{dbh}->do('DELETE FROM Hostmasks WHERE id = ?', undef, $id);
$self->{dbh}->do('DELETE FROM Accounts WHERE id = ?', undef, $id);
$self->{dbh}->do('DELETE FROM NickServ WHERE id = ?', undef, $id);
$self->{dbh}->do('DELETE FROM Gecos WHERE id = ?', undef, $id);
$self->{dbh}->do('DELETE FROM Channels WHERE id = ?', undef, $id);
$self->{dbh}->do('DELETE FROM Messages WHERE id = ?', undef, $id);
$self->{dbh}->do('DELETE FROM Aliases WHERE id = ?', undef, $id);
$self->{dbh}->do('DELETE FROM Aliases WHERE alias = ?', undef, $id);
$self->{dbh}->commit;
$self->{dbh}->begin_work;
return 1;
};
if ($@) {
$self->{pbot}->{logger}->log($@);
$self->{dbh}->rollback;
$self->{dbh}->begin_work;
return 0;
}
return $ret;
}
sub vacuum { sub vacuum {
my $self = shift; my $self = shift;

View File

@ -25,8 +25,8 @@ use PBot::Imports;
# These are set by the /misc/update_version script # These are set by the /misc/update_version script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 4376, BUILD_REVISION => 4377,
BUILD_DATE => "2021-09-04", BUILD_DATE => "2021-09-06",
}; };
sub initialize {} sub initialize {}