Add `akalink` and `akaunlink` commands

This commit is contained in:
Pragmatic Software 2015-05-12 12:59:45 -07:00
parent 9031d97910
commit 20aeeea634
2 changed files with 118 additions and 0 deletions

View File

@ -49,6 +49,8 @@ sub initialize {
$self->{pbot}->{commands}->register(sub { $self->recall_message(@_) }, "recall", 0);
$self->{pbot}->{commands}->register(sub { $self->list_also_known_as(@_) }, "aka", 0);
$self->{pbot}->{commands}->register(sub { $self->rebuild_aliases(@_) }, "rebuildaliases", 90);
$self->{pbot}->{commands}->register(sub { $self->aka_link(@_) }, "akalink", 60);
$self->{pbot}->{commands}->register(sub { $self->aka_unlink(@_) }, "akaunlink", 60);
$self->{pbot}->{atexit}->register(sub { $self->{database}->end(); return; });
}
@ -69,6 +71,60 @@ sub rebuild_aliases {
$self->{database}->rebuild_aliases_table;
}
sub aka_link {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
my ($id, $alias) = split /\s+/, $arguments;
if (not $id or not $alias) {
return "Usage: link <target id> <alias id>";
}
my $source = $self->{database}->find_most_recent_hostmask($id);
my $target = $self->{database}->find_most_recent_hostmask($alias);
if (not $source) {
return "No such id $id found.";
}
if (not $target) {
return "No such id $alias found.";
}
if ($self->{database}->link_alias($id, $alias)) {
return "$source linked to $target.";
} else {
return "Link failed.";
}
}
sub aka_unlink {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
my ($id, $alias) = split /\s+/, $arguments;
if (not $id or not $alias) {
return "Usage: unlink <target id> <alias id>";
}
my $source = $self->{database}->find_most_recent_hostmask($id);
my $target = $self->{database}->find_most_recent_hostmask($alias);
if (not $source) {
return "No such id $id found.";
}
if (not $target) {
return "No such id $alias found.";
}
if ($self->{database}->unlink_alias($id, $alias)) {
return "$source unlinked from $target.";
} else {
return "Unlink failed.";
}
}
sub list_also_known_as {
my ($self, $from, $nick, $user, $host, $arguments) = @_;

View File

@ -840,6 +840,68 @@ sub link_aliases {
$self->{pbot}->{logger}->log($@) if $@;
}
sub link_alias {
my ($self, $id, $alias) = @_;
my $ret = eval {
my $ret = 0;
my $sth = $self->{dbh}->prepare('INSERT INTO Aliases SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM Aliases WHERE id = ? AND alias = ?)');
$sth->bind_param(1, $alias);
$sth->bind_param(2, $id);
$sth->bind_param(3, $alias);
$sth->bind_param(4, $id);
$sth->execute();
if ($sth->rows) {
$self->{new_entries}++;
$ret = 1;
}
$sth->bind_param(1, $id);
$sth->bind_param(2, $alias);
$sth->bind_param(3, $id);
$sth->bind_param(4, $alias);
$sth->execute();
if ($sth->rows) {
$self->{new_entries}++;
$ret = 1;
} else {
$ret = 0;
}
return $ret;
};
$self->{pbot}->{logger}->log($@) if $@;
return $ret;
}
sub unlink_alias {
my ($self, $id, $alias) = @_;
my $ret = eval {
my $ret = 0;
my $sth = $self->{dbh}->prepare('DELETE FROM Aliases WHERE id = ? AND alias = ?');
$sth->bind_param(1, $id);
$sth->bind_param(2, $alias);
$sth->execute();
if ($sth->rows) {
$self->{new_entries}++;
$ret = 1;
}
$sth->bind_param(1, $alias);
$sth->bind_param(2, $id);
$sth->execute();
if ($sth->rows) {
$self->{new_entries}++;
$ret = 1;
} else {
$ret = 0;
}
return $ret;
};
$self->{pbot}->{logger}->log($@) if $@;
return $ret;
}
sub vacuum {
my $self = shift;