2014-05-17 22:08:19 +02:00
|
|
|
# File: Registry.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Provides a centralized registry of configuration settings that can
|
|
|
|
# easily be examined and updated via set/unset commands without restarting.
|
|
|
|
|
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/.
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
package PBot::Registry;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
use Time::HiRes qw(gettimeofday);
|
|
|
|
use Carp ();
|
|
|
|
|
|
|
|
use PBot::DualIndexHashObject;
|
|
|
|
use PBot::RegistryCommands;
|
|
|
|
|
|
|
|
sub new {
|
2019-05-28 18:19:42 +02:00
|
|
|
if (ref($_[1]) eq 'HASH') {
|
2014-05-17 22:08:19 +02:00
|
|
|
Carp::croak("Options to " . __FILE__ . " should be item/value pairs, not hash reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2019-12-22 04:01:32 +01:00
|
|
|
$self->{pbot} = $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
|
|
|
my $filename = $conf{filename} // Carp::croak("Missing filename reference in " . __FILE__);
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{registry} = PBot::DualIndexHashObject->new(name => 'Registry', filename => $filename, pbot => $self->{pbot});
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{triggers} = {};
|
|
|
|
|
|
|
|
$self->{pbot}->{atexit}->register(sub { $self->save; return; });
|
|
|
|
|
|
|
|
PBot::RegistryCommands->new(pbot => $self->{pbot});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub load {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{registry}->load;
|
|
|
|
foreach my $section (keys %{ $self->{registry}->hash }) {
|
|
|
|
foreach my $item (keys %{ $self->{registry}->hash->{$section} }) {
|
|
|
|
$self->process_trigger($section, $item, $self->{registry}->hash->{$section}->{$item}->{value});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub save {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{registry}->save;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add_default {
|
|
|
|
my ($self, $type, $section, $item, $value) = @_;
|
|
|
|
$self->add($type, $section, $item, $value, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub add {
|
|
|
|
my $self = shift;
|
|
|
|
my ($type, $section, $item, $value, $is_default) = @_;
|
|
|
|
|
|
|
|
$type = lc $type;
|
|
|
|
$section = lc $section;
|
|
|
|
$item = lc $item;
|
|
|
|
|
2016-01-29 22:08:40 +01:00
|
|
|
if ($is_default) {
|
|
|
|
return if exists $self->{registry}->hash->{$section} and exists $self->{registry}->hash->{$section}->{$item};
|
|
|
|
}
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{registry}->hash->{$section}->{$item}->{value} = $value;
|
2019-12-28 05:20:07 +01:00
|
|
|
$self->{registry}->hash->{$section}->{$item}->{type} = $type unless exists $self->{registry}->hash->{$section}->{$item}->{type};
|
2014-05-17 22:08:19 +02:00
|
|
|
|
|
|
|
$self->process_trigger($section, $item, $value) unless $is_default;
|
2014-08-05 00:50:22 +02:00
|
|
|
$self->save unless $is_default;
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub remove {
|
|
|
|
my $self = shift;
|
|
|
|
my ($section, $item) = @_;
|
|
|
|
|
|
|
|
$section = lc $section;
|
|
|
|
|
|
|
|
delete $self->{registry}->hash->{$section}->{$item};
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (not scalar keys %{ $self->{registry}->hash->{$section} }) {
|
2014-05-17 22:08:19 +02:00
|
|
|
delete $self->{registry}->hash->{$section};
|
|
|
|
}
|
|
|
|
|
2014-08-05 00:50:22 +02:00
|
|
|
$self->save;
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2015-07-06 02:47:12 +02:00
|
|
|
sub set_default {
|
2014-05-17 22:08:19 +02:00
|
|
|
my ($self, $section, $item, $key, $value) = @_;
|
2015-07-06 02:47:12 +02:00
|
|
|
$self->set($section, $item, $key, $value, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub set {
|
2019-12-22 04:01:32 +01:00
|
|
|
my ($self, $section, $item, $key, $value, $is_default, $dont_save) = @_;
|
2014-05-17 22:08:19 +02:00
|
|
|
|
|
|
|
$section = lc $section;
|
|
|
|
$item = lc $item;
|
|
|
|
$key = lc $key if defined $key;
|
|
|
|
|
2016-01-29 22:08:40 +01:00
|
|
|
if ($is_default) {
|
|
|
|
return if exists $self->{registry}->hash->{$section}
|
|
|
|
and exists $self->{registry}->hash->{$section}->{$item}
|
|
|
|
and exists $self->{registry}->hash->{$section}->{$item}->{$key};
|
|
|
|
}
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
my $oldvalue = $self->get_value($section, $item, 1) if defined $value;
|
|
|
|
$oldvalue = '' if not defined $oldvalue;
|
|
|
|
|
|
|
|
my $result = $self->{registry}->set($section, $item, $key, $value, 1);
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (defined $key and $key eq 'value' and defined $value and $oldvalue ne $value) {
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->process_trigger($section, $item, $value);
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:01:32 +01:00
|
|
|
$self->save if !$dont_save && $result =~ m/set to/ && not $is_default;
|
2015-06-26 05:53:59 +02:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub unset {
|
|
|
|
my ($self, $section, $item, $key) = @_;
|
|
|
|
|
|
|
|
$section = lc $section;
|
|
|
|
$item = lc $item;
|
|
|
|
$key = lc $key;
|
|
|
|
|
2015-06-26 05:53:59 +02:00
|
|
|
my $result = $self->{registry}->unset($section, $item, $key);
|
|
|
|
$self->save;
|
|
|
|
return $result;
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub get_value {
|
2019-11-25 22:56:55 +01:00
|
|
|
my ($self, $section, $item, $as_text, $stuff) = @_;
|
|
|
|
my $key = $item;
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2019-11-25 22:56:55 +01:00
|
|
|
if (defined $stuff and exists $stuff->{nick}) {
|
|
|
|
if (exists $self->{registry}->hash->{$section} and exists $self->{registry}->hash->{$section}->{"$item.nick.$stuff->{nick}"}) {
|
|
|
|
$key = "$item.nick.$stuff->{nick}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists $self->{registry}->hash->{$section} and exists $self->{registry}->hash->{$section}->{$key}) {
|
|
|
|
if (not $as_text and $self->{registry}->hash->{$section}->{$key}->{type} eq 'array') {
|
|
|
|
return split /\s*,\s*/, $self->{registry}->hash->{$section}->{$key}->{value};
|
2014-05-17 22:08:19 +02:00
|
|
|
} else {
|
2019-11-25 22:56:55 +01:00
|
|
|
return $self->{registry}->hash->{$section}->{$key}->{value};
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2014-05-19 11:55:47 +02:00
|
|
|
sub get_array_value {
|
2019-11-25 22:56:55 +01:00
|
|
|
my ($self, $section, $item, $index, $stuff) = @_;
|
|
|
|
my $key = $item;
|
|
|
|
|
|
|
|
if (defined $stuff and exists $stuff->{nick}) {
|
|
|
|
if (exists $self->{registry}->hash->{$section} and exists $self->{registry}->hash->{$section}->{"$item.nick.$stuff->{nick}"}) {
|
|
|
|
$key = "$item.nick.$stuff->{nick}";
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 11:55:47 +02:00
|
|
|
|
2019-11-25 22:56:55 +01:00
|
|
|
if (exists $self->{registry}->hash->{$section} and exists $self->{registry}->hash->{$section}->{$key}) {
|
|
|
|
if ($self->{registry}->hash->{$section}->{$key}->{type} eq 'array') {
|
|
|
|
my @array = split /\s*,\s*/, $self->{registry}->hash->{$section}->{$key}->{value};
|
2014-05-19 11:55:47 +02:00
|
|
|
return $array[$index >= $#array ? $#array : $index];
|
|
|
|
} else {
|
2019-11-25 22:56:55 +01:00
|
|
|
return $self->{registry}->hash->{$section}->{$key}->{value};
|
2014-05-19 11:55:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
sub add_trigger {
|
|
|
|
my ($self, $section, $item, $subref) = @_;
|
2015-07-06 02:47:12 +02:00
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
$self->{triggers}->{$section}->{$item} = $subref;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_trigger {
|
|
|
|
my $self = shift;
|
|
|
|
my ($section, $item) = @_;
|
|
|
|
|
2019-05-28 18:19:42 +02:00
|
|
|
if (exists $self->{triggers}->{$section} and exists $self->{triggers}->{$section}->{$item}) {
|
2014-05-17 22:08:19 +02:00
|
|
|
return &{ $self->{triggers}->{$section}->{$item} }(@_);
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|