2010-06-18 05:19:45 +02:00
|
|
|
# File: HashObject.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-18 05:19:45 +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-18 05:19:45 +02:00
|
|
|
package PBot::HashObject;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
|
|
|
use Text::Levenshtein qw(fastdistance);
|
|
|
|
use Carp ();
|
2019-06-28 09:22:57 +02:00
|
|
|
use JSON;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
|
|
|
sub new {
|
2019-05-28 18:19:42 +02:00
|
|
|
if (ref($_[1]) eq 'HASH') {
|
2010-06-18 05:19:45 +02:00
|
|
|
Carp::croak("Options to HashObject 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-03-05 20:30:37 +01:00
|
|
|
$self->{name} = delete $conf{name} // 'hash object';
|
|
|
|
$self->{filename} = delete $conf{filename} // Carp::carp("Missing filename to HashObject, 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-18 05:19:45 +02:00
|
|
|
$self->{hash} = {};
|
|
|
|
}
|
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
sub hash_add {
|
|
|
|
my ($self, $index_key, $hash) = @_;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $hash) {
|
|
|
|
if (exists $self->hash->{$index_key}) {
|
2019-06-28 09:22:57 +02:00
|
|
|
return undef;
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $key (keys %$hash) {
|
2010-06-23 08:51:39 +02:00
|
|
|
$self->hash->{$index_key}{$key} = $hash->{$key};
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
2010-06-23 08:51:39 +02:00
|
|
|
return 1;
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2017-08-06 05:14:49 +02:00
|
|
|
sub load {
|
2010-06-18 05:19:45 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $filename = shift; } else { $filename = $self->filename; }
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $filename) {
|
2010-06-18 05:19:45 +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");
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not open(FILE, "< $filename")) {
|
2019-06-28 09:22:57 +02:00
|
|
|
Carp::carp "Skipping loading from file: Couldn't open $filename: $!\n";
|
2010-06-18 12:46:23 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
my $contents = do {
|
|
|
|
local $/;
|
|
|
|
<FILE>;
|
|
|
|
};
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{hash} = decode_json $contents;
|
|
|
|
close FILE;
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 05:14:49 +02:00
|
|
|
sub save {
|
2010-06-18 05:19:45 +02:00
|
|
|
my $self = shift;
|
|
|
|
my $filename;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $filename = shift; } else { $filename = $self->filename; }
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $filename) {
|
2010-06-18 05:19:45 +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-18 05:19:45 +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-18 05:19:45 +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-18 05:19:45 +02:00
|
|
|
close(FILE);
|
|
|
|
}
|
|
|
|
|
2017-08-06 05:14:49 +02:00
|
|
|
sub clear {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{hash} = {};
|
|
|
|
}
|
|
|
|
|
2010-06-18 05:19:45 +02:00
|
|
|
sub find_hash {
|
2018-08-06 07:43:57 +02:00
|
|
|
my ($self, $keyword) = @_;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
|
|
|
my $result = eval {
|
|
|
|
foreach my $index (keys %{ $self->hash }) {
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($keyword =~ m/^\Q$index\E$/i) {
|
2010-06-18 05:19:45 +02:00
|
|
|
return $index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return undef;
|
|
|
|
};
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($@) {
|
2014-05-18 22:09:05 +02:00
|
|
|
$self->{pbot}->{logger}->log("find_hash: bad regex: $@\n");
|
2010-06-18 05:19:45 +02:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub levenshtein_matches {
|
|
|
|
my ($self, $keyword) = @_;
|
|
|
|
my $comma = '';
|
|
|
|
my $result = "";
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2010-06-18 05:19:45 +02:00
|
|
|
foreach my $index (sort keys %{ $self->hash }) {
|
|
|
|
my $distance = fastdistance($keyword, $index);
|
|
|
|
|
|
|
|
# print "Distance $distance for $keyword (" , (length $keyword) , ") vs $index (" , length $index , ")\n";
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2010-06-18 05:19:45 +02:00
|
|
|
my $length = (length($keyword) > length($index)) ? length $keyword : length $index;
|
|
|
|
|
|
|
|
# print "Percentage: ", $distance / $length, "\n";
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if ($length != 0 && $distance / $length < 0.50) {
|
2010-06-18 05:19:45 +02:00
|
|
|
$result .= $comma . $index;
|
|
|
|
$comma = ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result =~ s/(.*), /$1 or /;
|
|
|
|
$result = "none" if $comma eq '';
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set {
|
2018-08-06 07:43:57 +02:00
|
|
|
my ($self, $index, $key, $value, $dont_save) = @_;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
|
|
|
my $hash_index = $self->find_hash($index);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not $hash_index) {
|
2010-06-18 05:19:45 +02:00
|
|
|
my $result = "No such $self->{name} object '$index'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($index);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $key) {
|
2010-06-18 06:40:12 +02:00
|
|
|
my $result = "[$self->{name}] $hash_index keys: ";
|
|
|
|
my $comma = '';
|
|
|
|
foreach my $k (sort keys %{ $self->hash->{$hash_index} }) {
|
|
|
|
$result .= $comma . "$k => " . $self->hash->{$hash_index}{$k};
|
2010-06-29 09:31:27 +02:00
|
|
|
$comma = "; ";
|
2010-06-18 06:40:12 +02:00
|
|
|
}
|
2019-05-28 18:19:42 +02:00
|
|
|
$result .= "none" if ($comma eq '');
|
2010-06-18 06:40:12 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not defined $value) {
|
2010-06-18 05:19:45 +02:00
|
|
|
$value = $self->hash->{$hash_index}{$key};
|
|
|
|
} else {
|
|
|
|
$self->hash->{$hash_index}{$key} = $value;
|
2018-08-06 07:43:57 +02:00
|
|
|
$self->save() unless $dont_save;
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return "[$self->{name}] $hash_index: '$key' " . (defined $value ? "set to '$value'" : "is not set.");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unset {
|
|
|
|
my ($self, $index, $key) = @_;
|
|
|
|
|
|
|
|
my $hash_index = $self->find_hash($index);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not $hash_index) {
|
2010-06-18 05:19:45 +02:00
|
|
|
my $result = "No such $self->{name} object '$index'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($index);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete $self->hash->{$hash_index}{$key};
|
2017-08-06 05:14:49 +02:00
|
|
|
$self->save();
|
2010-06-18 05:19:45 +02:00
|
|
|
|
|
|
|
return "[$self->{name}] $hash_index: '$key' unset.";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add {
|
2010-06-23 08:51:39 +02:00
|
|
|
my ($self, $index_key, $hash) = @_;
|
2010-06-18 05:19:45 +02:00
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
if ($self->hash_add($index_key, $hash)) {
|
2018-02-28 20:13:56 +01:00
|
|
|
$self->save();
|
2010-06-18 05:19:45 +02:00
|
|
|
} else {
|
|
|
|
return "Error occurred adding new $self->{name} object.";
|
|
|
|
}
|
|
|
|
|
2010-06-23 08:51:39 +02:00
|
|
|
return "'$index_key' added to $self->{name}.";
|
2010-06-18 05:19:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub remove {
|
|
|
|
my ($self, $index) = @_;
|
|
|
|
|
|
|
|
my $hash_index = $self->find_hash($index);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not $hash_index) {
|
2010-06-18 05:19:45 +02:00
|
|
|
my $result = "No such $self->{name} object '$index'; similiar matches: ";
|
|
|
|
$result .= $self->levenshtein_matches($index);
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete $self->hash->{$hash_index};
|
2017-08-06 05:14:49 +02:00
|
|
|
$self->save();
|
2010-06-18 05:19:45 +02:00
|
|
|
|
|
|
|
return "'$hash_index' removed from $self->{name}.";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Getters and setters
|
|
|
|
|
|
|
|
sub hash {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{hash};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub filename {
|
|
|
|
my $self = shift;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (@_) { $self->{filename} = shift; }
|
2010-06-18 05:19:45 +02:00
|
|
|
return $self->{filename};
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|