2010-06-18 05:19:45 +02:00
|
|
|
# File: HashObject.pm
|
|
|
|
#
|
2019-06-26 18:34:19 +02:00
|
|
|
# Purpose: Provides a hash-table object with an abstracted API that includes
|
2020-01-15 03:10:53 +01:00
|
|
|
# setting and deleting values, saving to and loading from files, etc. Provides
|
|
|
|
# case-insensitive access to the index key while preserving original case when
|
|
|
|
# displaying index key.
|
2021-07-21 06:38:07 +02:00
|
|
|
#
|
|
|
|
# Data is stored in working memory for lightning fast performance. If a filename
|
2021-07-24 04:22:25 +02:00
|
|
|
# is provided, data is written to the file after any modifications.
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2023-02-21 06:31:52 +01:00
|
|
|
# SPDX-FileCopyrightText: 2010-2023 Pragmatic Software <pragma78@gmail.com>
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-24 04:22:25 +02:00
|
|
|
package PBot::Core::Storage::HashObject;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2021-08-28 04:41:51 +02:00
|
|
|
use Text::Levenshtein::XS qw(distance);
|
2019-06-28 09:22:57 +02:00
|
|
|
use JSON;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub new($class, %args) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $self = bless {}, $class;
|
2021-06-19 06:23:34 +02:00
|
|
|
Carp::croak("Missing pbot reference to " . __FILE__) unless exists $args{pbot};
|
|
|
|
$self->{pbot} = delete $args{pbot};
|
|
|
|
$self->initialize(%args);
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self;
|
2020-02-14 22:32:12 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub initialize($self, %conf) {
|
2021-06-19 06:23:34 +02:00
|
|
|
$self->{name} = $conf{name} // 'unnammed';
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{hash} = {};
|
2021-06-19 06:23:34 +02:00
|
|
|
$self->{filename} = $conf{filename};
|
|
|
|
|
|
|
|
if (not defined $self->{filename}) {
|
|
|
|
Carp::carp("Missing filename for $self->{name} HashObject, will not be able to save to or load from file.");
|
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub load($self, $filename = undef) {
|
2021-06-19 06:23:34 +02:00
|
|
|
# allow overriding $self->{filename} with $filename parameter
|
|
|
|
$filename //= $self->{filename};
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# no filename? nothing to load
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not defined $filename) {
|
|
|
|
Carp::carp "No $self->{name} filename specified -- skipping loading from file";
|
|
|
|
return;
|
|
|
|
}
|
2020-01-15 03:10:53 +01:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
$self->{pbot}->{logger}->log("Loading $self->{name} from $filename\n");
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
if (not open(FILE, "< $filename")) {
|
|
|
|
$self->{pbot}->{logger}->log("Skipping loading from file: Couldn't open $filename: $!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# slurp file into $contents
|
2020-02-15 23:38:32 +01:00
|
|
|
my $contents = do {
|
|
|
|
local $/;
|
|
|
|
<FILE>;
|
|
|
|
};
|
|
|
|
|
|
|
|
close FILE;
|
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
eval {
|
|
|
|
# first try to deocde json, throws exception on misparse/errors
|
|
|
|
my $newhash = decode_json $contents;
|
|
|
|
|
|
|
|
# clear current hash only if decode succeeded
|
|
|
|
$self->clear;
|
|
|
|
|
|
|
|
# update internal hash
|
|
|
|
$self->{hash} = $newhash;
|
|
|
|
|
|
|
|
# update existing entries to use _name to preserve typographical casing
|
|
|
|
# e.g., when someone edits a config file by hand, they might add an
|
|
|
|
# entry with uppercase characters in its name.
|
|
|
|
foreach my $index (keys %{$self->{hash}}) {
|
|
|
|
if (not exists $self->{hash}->{$index}->{_name}) {
|
|
|
|
if ($index ne lc $index) {
|
|
|
|
if (exists $self->{hash}->{lc $index}) {
|
|
|
|
Carp::croak "Cannot update $self->{name} object $index; duplicate object found";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $data = delete $self->{hash}->{$index};
|
|
|
|
$data->{_name} = $index; # _name is original typographical case
|
|
|
|
$self->{hash}->{lc $index} = $data; # index key is lowercased
|
2020-04-23 01:46:09 +02:00
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-19 06:23:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
# json parse error or such
|
|
|
|
$self->{pbot}->{logger}->log("Warning: failed to load $filename: $@\n");
|
2020-01-15 03:10:53 +01:00
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub save($self, $filename = undef) {
|
2021-06-19 06:23:34 +02:00
|
|
|
# allow parameter overriding internal field
|
|
|
|
$filename //= $self->{filename};
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# no filename? nothing to save
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not defined $filename) {
|
|
|
|
Carp::carp "No $self->{name} filename specified -- skipping saving to file.\n";
|
|
|
|
return;
|
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{logger}->log("Saving $self->{name} to $filename\n");
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# add update_version to metadata
|
2020-04-22 04:44:14 +02:00
|
|
|
if (not $self->get_data('$metadata$', 'update_version')) {
|
|
|
|
$self->add('$metadata$', { update_version => PBot::VERSION::BUILD_REVISION });
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# ensure `name` metadata is current
|
2020-04-25 08:04:07 +02:00
|
|
|
$self->set('$metadata$', 'name', $self->{name}, 1);
|
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# encode hash as JSON
|
2020-02-15 23:38:32 +01:00
|
|
|
my $json = JSON->new;
|
|
|
|
my $json_text = $json->pretty->canonical->utf8->encode($self->{hash});
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# print JSON to file
|
2020-02-15 23:38:32 +01:00
|
|
|
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
|
|
|
|
print FILE "$json_text\n";
|
|
|
|
close(FILE);
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub clear($self) {
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{hash} = {};
|
2017-08-06 05:14:49 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub levenshtein_matches($self, $keyword) {
|
2021-06-19 06:23:34 +02:00
|
|
|
my @matches;
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $index (sort keys %{$self->{hash}}) {
|
2021-08-28 04:41:51 +02:00
|
|
|
my $distance = distance($keyword, $index, 20);
|
|
|
|
next if not defined $distance;
|
2021-06-19 06:23:34 +02:00
|
|
|
|
|
|
|
my $length_a = length $keyword;
|
|
|
|
my $length_b = length $index;
|
|
|
|
my $length = $length_a > $length_b ? $length_a : $length_b;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($length != 0 && $distance / $length < 0.50) {
|
2021-06-19 06:23:34 +02:00
|
|
|
push @matches, $index;
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
return 'none' if not @matches;
|
|
|
|
|
|
|
|
my $result = join ', ', @matches;
|
|
|
|
|
|
|
|
# "a, b, c, d" -> "a, b, c or d"
|
2020-02-15 23:38:32 +01:00
|
|
|
$result =~ s/(.*), /$1 or /;
|
2021-06-19 06:23:34 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $result;
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub set($self, $index, $key = undef, $value = undef, $dont_save = 0) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $lc_index = lc $index;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
# find similarly named keys
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not exists $self->{hash}->{$lc_index}) {
|
2021-06-19 06:23:34 +02:00
|
|
|
my $result = "$self->{name}: $index not found; similar matches: ";
|
2020-02-15 23:38:32 +01:00
|
|
|
$result .= $self->levenshtein_matches($index);
|
|
|
|
return $result;
|
2010-06-18 06:40:12 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not defined $key) {
|
2021-06-19 06:23:34 +02:00
|
|
|
# if no key provided, then list all keys and values
|
2020-04-23 01:46:09 +02:00
|
|
|
my $result = "[$self->{name}] " . $self->get_key_name($lc_index) . " keys: ";
|
2021-06-19 06:23:34 +02:00
|
|
|
|
|
|
|
my @entries;
|
|
|
|
|
|
|
|
foreach my $key (sort grep { $_ ne '_name' } keys %{$self->{hash}->{$lc_index}}) {
|
|
|
|
push @entries, "$key: $self->{hash}->{$lc_index}->{$key}";
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2021-06-19 06:23:34 +02:00
|
|
|
|
|
|
|
if (@entries) {
|
|
|
|
$result .= join ";\n", @entries;
|
|
|
|
} else {
|
|
|
|
$result .= 'none';
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-04-25 08:04:07 +02:00
|
|
|
if (not defined $value) {
|
2021-06-19 06:23:34 +02:00
|
|
|
# if no value provided, then show this key's value
|
2020-04-25 08:04:07 +02:00
|
|
|
$value = $self->{hash}->{$lc_index}->{$key};
|
|
|
|
} else {
|
2021-06-19 06:23:34 +02:00
|
|
|
# otherwise update the value belonging to key
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{hash}->{$lc_index}->{$key} = $value;
|
|
|
|
$self->save unless $dont_save;
|
|
|
|
}
|
2021-06-19 06:23:34 +02:00
|
|
|
|
2020-04-23 01:46:09 +02:00
|
|
|
return "[$self->{name}] " . $self->get_key_name($lc_index) . ": $key " . (defined $value ? "set to $value" : "is not set.");
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub unset($self, $index, $key = undef) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $lc_index = lc $index;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not exists $self->{hash}->{$lc_index}) {
|
2021-06-19 06:23:34 +02:00
|
|
|
my $result = "$self->{name}: $index not found; similar matches: ";
|
2020-02-15 23:38:32 +01:00
|
|
|
$result .= $self->levenshtein_matches($index);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined delete $self->{hash}->{$lc_index}->{$key}) {
|
|
|
|
$self->save;
|
2020-04-23 01:46:09 +02:00
|
|
|
return "[$self->{name}] " . $self->get_key_name($lc_index) . ": $key unset.";
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
2020-04-23 01:46:09 +02:00
|
|
|
return "[$self->{name}] " . $self->get_key_name($lc_index) . ": $key does not exist.";
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub exists($self, $index, $data_index = undef) {
|
2020-02-15 23:38:32 +01:00
|
|
|
return exists $self->{hash}->{lc $index} if not defined $data_index;
|
|
|
|
return exists $self->{hash}->{lc $index}->{$data_index};
|
2020-02-12 06:49:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub get_key_name($self, $index) {
|
2020-04-23 01:46:09 +02:00
|
|
|
my $lc_index = lc $index;
|
|
|
|
return $lc_index if not exists $self->{hash}->{$lc_index};
|
|
|
|
return exists $self->{hash}->{$lc_index}->{_name} ? $self->{hash}->{$lc_index}->{_name} : $lc_index;
|
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub get_keys($self, $index = undef) {
|
2020-04-22 04:44:14 +02:00
|
|
|
return grep { $_ ne '$metadata$' } keys %{$self->{hash}} if not defined $index;
|
2020-02-15 23:38:32 +01:00
|
|
|
return grep { $_ ne '_name' } keys %{$self->{hash}->{lc $index}};
|
2020-01-19 06:41:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub get_data($self, $index, $data_index = undef) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $lc_index = lc $index;
|
|
|
|
return undef if not exists $self->{hash}->{$lc_index};
|
|
|
|
return $self->{hash}->{$lc_index} if not defined $data_index;
|
|
|
|
return $self->{hash}->{$lc_index}->{$data_index};
|
2020-02-03 18:47:21 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub add($self, $index, $data, $dont_save = 0) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $lc_index = lc $index;
|
2020-04-23 01:46:09 +02:00
|
|
|
|
|
|
|
# preserve case of index
|
|
|
|
if ($index ne $lc_index) {
|
|
|
|
$data->{_name} = $index;
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{hash}->{$lc_index} = $data;
|
|
|
|
$self->save unless $dont_save;
|
|
|
|
return "$index added to $self->{name}.";
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 06:04:12 +02:00
|
|
|
sub remove($self, $index, $data_index = undef, $dont_save = 0) {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $lc_index = lc $index;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not exists $self->{hash}->{$lc_index}) {
|
2021-06-19 06:23:34 +02:00
|
|
|
my $result = "$self->{name}: $index not found; similar matches: ";
|
2020-02-15 23:38:32 +01:00
|
|
|
$result .= $self->levenshtein_matches($lc_index);
|
|
|
|
return $result;
|
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (defined $data_index) {
|
|
|
|
if (defined delete $self->{hash}->{$lc_index}->{$data_index}) {
|
|
|
|
delete $self->{hash}->{$lc_index} if keys(%{$self->{hash}->{$lc_index}}) == 1;
|
|
|
|
$self->save unless $dont_save;
|
2020-04-23 01:46:09 +02:00
|
|
|
return $self->get_key_name($lc_index) . ".$data_index removed from $self->{name}";
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
2020-04-23 01:46:09 +02:00
|
|
|
return "$self->{name}: " . $self->get_key_name($lc_index) . ".$data_index does not exist.";
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2020-02-12 07:56:42 +01:00
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $data = delete $self->{hash}->{$lc_index};
|
|
|
|
if (defined $data) {
|
|
|
|
$self->save unless $dont_save;
|
2020-04-23 01:46:09 +02:00
|
|
|
my $name = exists $data->{_name} ? $data->{_name} : $lc_index;
|
|
|
|
return "$name removed from $self->{name}.";
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
|
|
|
return "$self->{name}: $data_index does not exist.";
|
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|