From 20aeeea63445b374b54df5b883366819cb810a07 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 12 May 2015 12:59:45 -0700 Subject: [PATCH] Add `akalink` and `akaunlink` commands --- PBot/MessageHistory.pm | 56 +++++++++++++++++++++++++++++++ PBot/MessageHistory_SQLite.pm | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/PBot/MessageHistory.pm b/PBot/MessageHistory.pm index c9b63d92..fb736e3f 100644 --- a/PBot/MessageHistory.pm +++ b/PBot/MessageHistory.pm @@ -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 "; + } + + 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 "; + } + + 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) = @_; diff --git a/PBot/MessageHistory_SQLite.pm b/PBot/MessageHistory_SQLite.pm index 703226a6..b418d7ca 100644 --- a/PBot/MessageHistory_SQLite.pm +++ b/PBot/MessageHistory_SQLite.pm @@ -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;