2010-06-20 08:15:10 +02:00
|
|
|
# File: DualIndexHashObject.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
2019-06-26 18:34:19 +02:00
|
|
|
# Purpose: Provides a hash-table object with an abstracted API that includes
|
2010-06-20 08:15:10 +02:00
|
|
|
# setting and deleting values, saving to and loading from files, etc.
|
|
|
|
|
2017-03-05 22:33:31 +01:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2010-06-20 08:15:10 +02:00
|
|
|
package PBot::DualIndexHashObject;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use Text::Levenshtein qw(fastdistance);
|
2019-06-28 09:22:57 +02:00
|
|
|
use JSON;
|
2010-06-20 08:15:10 +02:00
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
sub new {
|
2018-08-09 19:55:53 +02:00
|
|
|
if (ref($_[1]) eq 'HASH') {
|
2010-06-20 08:15:10 +02:00
|
|
|
Carp::croak("Options to DualIndexHashObject should be key/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{name} = delete $conf{name} // 'Dual Index hash object';
|
|
|
|
$self->{filename} = delete $conf{filename} // Carp::carp("Missing filename to DualIndexHashObject, will not be able to save to or load from file.");
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
2010-06-20 08:15:10 +02:00
|
|
|
$self->{hash} = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
sub hash_add {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $hash) = @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (defined $hash) {
|
2019-06-28 09:22:57 +02:00
|
|
|
if (exists $self->hash->{$primary_index_key}->{$secondary_index_key}) {
|
|
|
|
return undef;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $key (keys %$hash) {
|
|
|
|
$self->hash->{$primary_index_key}->{$secondary_index_key}->{$key} = $hash->{$key};
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub load {
|
2014-05-17 22:08:19 +02:00
|
|
|
my ($self, $filename) = @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$filename = $self->filename if not defined $filename;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not defined $filename) {
|
2010-06-20 08:15:10 +02:00
|
|
|
Carp::carp "No $self->{name} filename specified -- skipping loading from file";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{pbot}->{logger}->log("Loading $self->{name} from $filename ...\n");
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not open(FILE, "< $filename")) {
|
2010-06-20 08:15:10 +02:00
|
|
|
Carp::carp "Skipping loading from file: Couldn't open $filename: $!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
my $contents = do {
|
|
|
|
local $/;
|
|
|
|
<FILE>;
|
|
|
|
};
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{hash} = decode_json $contents;
|
|
|
|
close FILE;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub save {
|
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (@_) { $filename = shift; } else { $filename = $self->filename; }
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not defined $filename) {
|
2010-06-20 08:15:10 +02:00
|
|
|
Carp::carp "No $self->{name} filename specified -- skipping saving to file.\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{pbot}->{logger}->log("Saving $self->{name} to $filename\n");
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
my $json = JSON->new;
|
2019-07-01 00:08:18 +02:00
|
|
|
my $json_text = $json->pretty->utf8->encode($self->{hash});
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
|
|
|
|
print FILE "$json_text\n";
|
2010-06-20 08:15:10 +02:00
|
|
|
close FILE;
|
|
|
|
}
|
|
|
|
|
2017-08-06 05:15:15 +02:00
|
|
|
sub clear {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{hash} = {};
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:15:10 +02:00
|
|
|
sub find_index {
|
2011-01-29 02:21:17 +01:00
|
|
|
my $self = shift;
|
|
|
|
my ($primary_index_key, $secondary_index_key) = map {lc} @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
return undef if not defined $primary_index_key;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
return undef if not exists $self->hash->{$primary_index_key};
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2015-12-14 07:08:17 +01:00
|
|
|
return $primary_index_key if not defined $secondary_index_key;
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
foreach my $index (keys %{ $self->hash->{$primary_index_key} }) {
|
|
|
|
return $index if $secondary_index_key eq lc $index;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
return undef;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub levenshtein_matches {
|
2018-05-12 11:52:52 +02:00
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $distance, $strictnamespace) = @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
my $comma = '';
|
|
|
|
my $result = "";
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
$distance = 0.60 if not defined $distance;
|
|
|
|
|
2010-06-20 08:15:10 +02:00
|
|
|
$primary_index_key = '.*' if not defined $primary_index_key;
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $secondary_index_key) {
|
2010-06-20 08:15:10 +02:00
|
|
|
foreach my $index (sort keys %{ $self->hash }) {
|
2011-01-29 02:21:17 +01:00
|
|
|
my $distance_result = fastdistance($primary_index_key, $index);
|
2010-06-20 08:15:10 +02:00
|
|
|
my $length = (length($primary_index_key) > length($index)) ? length $primary_index_key : length $index;
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if ($distance_result / $length < $distance) {
|
2018-08-09 02:38:57 +02:00
|
|
|
if ($index =~ / /) {
|
|
|
|
$result .= $comma . "\"$index\"";
|
|
|
|
} else {
|
|
|
|
$result .= $comma . $index;
|
|
|
|
}
|
2010-06-20 08:15:10 +02:00
|
|
|
$comma = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $primary) {
|
2010-06-20 08:15:10 +02:00
|
|
|
return 'none';
|
|
|
|
}
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
my $last_header = "";
|
|
|
|
my $header = "";
|
2010-06-20 08:15:10 +02:00
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
foreach my $index1 (sort keys %{ $self->hash }) {
|
|
|
|
$header = "[$index1] ";
|
|
|
|
$header = "[global channel] " if $header eq "[.*] ";
|
|
|
|
|
2018-05-12 11:52:52 +02:00
|
|
|
if ($strictnamespace) {
|
|
|
|
next unless $index1 eq '.*' or $index1 eq $primary;
|
2018-05-15 04:21:58 +02:00
|
|
|
$header = "" unless $header eq '[global channel] ';
|
2018-05-12 11:52:52 +02:00
|
|
|
}
|
|
|
|
|
2011-01-29 02:21:17 +01:00
|
|
|
foreach my $index2 (sort keys %{ $self->hash->{$index1} }) {
|
|
|
|
my $distance_result = fastdistance($secondary_index_key, $index2);
|
|
|
|
my $length = (length($secondary_index_key) > length($index2)) ? length $secondary_index_key : length $index2;
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if ($distance_result / $length < $distance) {
|
2011-01-29 02:21:17 +01:00
|
|
|
$header = "" if $last_header eq $header;
|
|
|
|
$last_header = $header;
|
2018-05-15 04:21:58 +02:00
|
|
|
$comma = '; ' if $comma ne '' and $header ne '';
|
2018-08-09 02:38:57 +02:00
|
|
|
if ($index2 =~ / /) {
|
|
|
|
$result .= $comma . $header . "\"$index2\"";
|
|
|
|
} else {
|
|
|
|
$result .= $comma . $header . $index2;
|
|
|
|
}
|
2011-01-29 02:21:17 +01:00
|
|
|
$comma = ", ";
|
|
|
|
}
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result =~ s/(.*), /$1 or /;
|
|
|
|
$result = 'none' if $comma eq '';
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set {
|
2014-05-17 22:08:19 +02:00
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $key, $value, $dont_save) = @_;
|
2010-06-20 08:15:10 +02:00
|
|
|
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $primary) {
|
2014-05-17 22:08:19 +02:00
|
|
|
my $result = "No such $self->{name} object [$primary_index_key]; similiar matches: ";
|
2010-06-20 08:15:10 +02:00
|
|
|
$result .= $self->levenshtein_matches($primary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $secondary = $self->find_index($primary, $secondary_index_key);
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $secondary) {
|
|
|
|
my $secondary_text = $secondary_index_key =~ / / ? "\"$secondary_index_key\"" : $secondary_index_key;
|
|
|
|
my $result = "No such $self->{name} object [$primary_index_key] $secondary_text; similiar matches: ";
|
2010-06-20 08:15:10 +02:00
|
|
|
$result .= $self->levenshtein_matches($primary, $secondary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not defined $key) {
|
|
|
|
my $secondary_text = $secondary =~ / / ? "\"$secondary\"" : $secondary;
|
|
|
|
my $result = "[" . ($primary eq '.*' ? 'global' : $primary) . "] $secondary_text keys:\n";
|
2010-06-20 08:15:10 +02:00
|
|
|
my $comma = '';
|
|
|
|
foreach my $key (sort keys %{ $self->hash->{$primary}->{$secondary} }) {
|
|
|
|
$result .= $comma . "$key => " . $self->hash->{$primary}->{$secondary}->{$key};
|
2014-05-24 14:01:59 +02:00
|
|
|
$comma = ";\n";
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
2018-08-09 19:55:53 +02:00
|
|
|
$result .= "none" if ($comma eq '');
|
2010-06-20 08:15:10 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not defined $value) {
|
2010-06-20 08:15:10 +02:00
|
|
|
$value = $self->hash->{$primary}->{$secondary}->{$key};
|
|
|
|
} else {
|
|
|
|
$self->hash->{$primary}->{$secondary}->{$key} = $value;
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->save unless $dont_save;
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
2010-06-29 09:31:27 +02:00
|
|
|
$primary = 'global' if $primary eq '.*';
|
2018-08-09 19:55:53 +02:00
|
|
|
$secondary = "\"$secondary\"" if $secondary =~ / /;
|
2014-05-17 22:08:19 +02:00
|
|
|
return "[$primary] $secondary: '$key' " . (defined $value ? "set to '$value'" : "is not set.");
|
2010-06-20 08:15:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub unset {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $key) = @_;
|
|
|
|
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $primary) {
|
2010-06-20 08:15:10 +02:00
|
|
|
my $result = "No such $self->{name} object group '$primary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $secondary = $self->find_index($primary, $secondary_index_key);
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $secondary) {
|
2010-06-20 08:15:10 +02:00
|
|
|
my $result = "No such $self->{name} object '$secondary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary, $secondary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete $self->hash->{$primary}->{$secondary}->{$key};
|
|
|
|
$self->save();
|
|
|
|
|
2010-06-29 09:31:27 +02:00
|
|
|
$primary = 'global' if $primary eq '.*';
|
2018-08-09 19:55:53 +02:00
|
|
|
$secondary = "\"$secondary\"" if $secondary =~ / /;
|
2010-06-20 08:15:10 +02:00
|
|
|
return "[$self->{name}] ($primary) $secondary: '$key' unset.";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key, $hash) = @_;
|
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
if ($self->hash_add($primary_index_key, $secondary_index_key, $hash)) {
|
2010-06-20 08:15:10 +02:00
|
|
|
$self->save();
|
|
|
|
} else {
|
|
|
|
return "Error occurred adding new $self->{name} object.";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "'$secondary_index_key' added to $primary_index_key [$self->{name}].";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub remove {
|
|
|
|
my ($self, $primary_index_key, $secondary_index_key) = @_;
|
|
|
|
|
|
|
|
my $primary = $self->find_index($primary_index_key);
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $primary) {
|
2010-06-20 08:15:10 +02:00
|
|
|
my $result = "No such $self->{name} object group '$primary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $secondary_index_key) {
|
2010-06-20 08:15:10 +02:00
|
|
|
delete $self->hash->{$primary};
|
|
|
|
$self->save;
|
|
|
|
return "'$primary' group removed from $self->{name}.";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $secondary = $self->find_index($primary, $secondary_index_key);
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (not $secondary) {
|
2010-06-20 08:15:10 +02:00
|
|
|
my $result = "No such $self->{name} object '$secondary_index_key'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($primary, $secondary_index_key);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete $self->hash->{$primary}->{$secondary};
|
2013-07-24 14:35:40 +02:00
|
|
|
|
|
|
|
# remove primary group if no more secondaries
|
2018-08-09 19:55:53 +02:00
|
|
|
if (scalar keys %{ $self->hash->{$primary} } == 0) {
|
2013-07-24 14:35:40 +02:00
|
|
|
delete $self->hash->{$primary};
|
|
|
|
}
|
|
|
|
|
2010-06-20 08:15:10 +02:00
|
|
|
$self->save();
|
|
|
|
return "'$secondary' removed from $primary group [$self->{name}].";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Getters and setters
|
|
|
|
|
|
|
|
sub hash {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{hash};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub filename {
|
|
|
|
my $self = shift;
|
|
|
|
|
2018-08-09 19:55:53 +02:00
|
|
|
if (@_) { $self->{filename} = shift; }
|
2010-06-20 08:15:10 +02:00
|
|
|
return $self->{filename};
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|