2021-07-21 06:38:07 +02:00
|
|
|
# File: Registry.pm
|
2014-05-17 22:08:19 +02:00
|
|
|
#
|
2021-06-05 22:20:03 +02:00
|
|
|
# Purpose: Bot commands to manipulate Registry entries.
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
package PBot::Commands::Registry;
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2021-07-21 06:38:07 +02:00
|
|
|
sub new {
|
|
|
|
my ($class, %args) = @_;
|
|
|
|
|
|
|
|
# ensure class was passed a PBot instance
|
|
|
|
if (not exists $args{pbot}) {
|
|
|
|
Carp::croak("Missing pbot reference to $class");
|
|
|
|
}
|
|
|
|
|
|
|
|
my $self = bless { pbot => $args{pbot} }, $class;
|
|
|
|
$self->initialize(%args);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2014-05-17 22:08:19 +02:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_regset(@_) }, "regset", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_regunset(@_) }, "regunset", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_regshow(@_) }, "regshow", 0);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_regsetmeta(@_) }, "regsetmeta", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_regunsetmeta(@_) }, "regunsetmeta", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_regchange(@_) }, "regchange", 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { $self->cmd_regfind(@_) }, "regfind", 0);
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_regset {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $usage = "Usage: regset <section>.<item> [value]";
|
|
|
|
|
|
|
|
# support "<section>.<key>" syntax in addition to "<section> <key>"
|
2020-05-02 05:59:51 +02:00
|
|
|
my $section = $self->{pbot}->{interpreter}->shift_arg($context->{arglist}) // return $usage;
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($item, $value);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($section =~ m/^(.+?)\.(.+)$/) {
|
|
|
|
($section, $item) = ($1, $2);
|
2020-05-02 05:59:51 +02:00
|
|
|
($value) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 1);
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
2020-05-02 05:59:51 +02:00
|
|
|
($item, $value) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 2);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not defined $section or not defined $item) {
|
|
|
|
return $usage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined $value) {
|
|
|
|
$self->{pbot}->{registry}->add('text', $section, $item, $value);
|
|
|
|
} else {
|
|
|
|
return $self->{pbot}->{registry}->set($section, $item, 'value');
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
$self->{pbot}->{logger}->log("$context->{hostmask} set registry entry $section.$item => $value\n");
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
return "$section.$item set to $value";
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_regunset {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $usage = "Usage: regunset <section>.<item>";
|
|
|
|
|
|
|
|
# support "<section>.<key>" syntax in addition to "<section> <key>"
|
2020-05-02 05:59:51 +02:00
|
|
|
my $section = $self->{pbot}->{interpreter}->shift_arg($context->{arglist}) // return $usage;
|
2020-02-15 23:38:32 +01:00
|
|
|
my $item;
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($section =~ m/^(.+?)\.(.+)$/) {
|
|
|
|
($section, $item) = ($1, $2);
|
|
|
|
} else {
|
|
|
|
($item) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $section or not defined $item) {
|
|
|
|
return $usage;
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
if (not $self->{pbot}->{registry}->{storage}->exists($section)) {
|
2021-06-05 22:20:03 +02:00
|
|
|
return "No such registry section $section.";
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
if (not $self->{pbot}->{registry}->{storage}->exists($section, $item)) {
|
2021-06-05 22:20:03 +02:00
|
|
|
return "No such item $item in section $section.";
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{logger}->log("$context->{hostmask} removed registry entry $section.$item\n");
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{registry}->remove($section, $item);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return "$section.$item deleted from registry";
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_regsetmeta {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $usage = "Usage: regsetmeta <section>.<item> [key [value]]";
|
|
|
|
|
|
|
|
# support "<section>.<key>" syntax in addition to "<section> <key>"
|
2020-05-02 05:59:51 +02:00
|
|
|
my $section = $self->{pbot}->{interpreter}->shift_arg($context->{arglist}) // return $usage;
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($item, $key, $value);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($section =~ m/^(.+?)\.(.+)$/) {
|
|
|
|
($section, $item) = ($1, $2);
|
2021-06-05 22:20:03 +02:00
|
|
|
($key, $value) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 2);
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
2020-05-02 05:59:51 +02:00
|
|
|
($item, $key, $value) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 3);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not defined $section or not defined $item) {
|
|
|
|
return $usage;
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
$key = undef if not length $key;
|
|
|
|
$value = undef if not length $value;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self->{pbot}->{registry}->set($section, $item, $key, $value);
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_regunsetmeta {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $usage = "Usage: regunsetmeta <section>.<item> <key>";
|
|
|
|
|
|
|
|
# support "<section>.<key>" syntax in addition to "<section> <key>"
|
2020-05-02 05:59:51 +02:00
|
|
|
my $section = $self->{pbot}->{interpreter}->shift_arg($context->{arglist}) // return $usage;
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($item, $key);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($section =~ m/^(.+?)\.(.+)$/) {
|
|
|
|
($section, $item) = ($1, $2);
|
2020-05-02 05:59:51 +02:00
|
|
|
($key) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 1);
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
2020-05-02 05:59:51 +02:00
|
|
|
($item, $key) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 2);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not defined $section or not defined $item or not defined $key) {
|
|
|
|
return $usage;
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self->{pbot}->{registry}->unset($section, $item, $key);
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_regshow {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $usage = "Usage: regshow <section>.<item>";
|
2021-07-09 23:39:35 +02:00
|
|
|
my $registry = $self->{pbot}->{registry}->{storage};
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
# support "<section>.<key>" syntax in addition to "<section> <key>"
|
2020-05-02 05:59:51 +02:00
|
|
|
my $section = $self->{pbot}->{interpreter}->shift_arg($context->{arglist}) // return $usage;
|
2020-02-15 23:38:32 +01:00
|
|
|
my $item;
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($section =~ m/^(.+?)\.(.+)$/) {
|
|
|
|
($section, $item) = ($1, $2);
|
|
|
|
} else {
|
|
|
|
($item) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $section or not defined $item) {
|
|
|
|
return $usage;
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not $registry->exists($section)) {
|
|
|
|
return "No such registry section $section.";
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not $registry->exists($section, $item)) {
|
|
|
|
return "No such registry item $item in section $section.";
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($registry->get_data($section, $item, 'private')) {
|
|
|
|
return "$section.$item: <private>";
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $result = "$section.$item: " . $registry->get_data($section, $item, 'value');
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($registry->get_data($section, $item, 'type') eq 'array') {
|
|
|
|
$result .= ' [array]';
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_regfind {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
my $usage = "Usage: regfind [-showvalues] [-section section] <regex>";
|
2021-07-09 23:39:35 +02:00
|
|
|
my $registry = $self->{pbot}->{registry}->{storage};
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
my $arguments = $context->{arguments};
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
return $usage if not defined $arguments;
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
# TODO maybe use GetOptionsFromArray here
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($section, $showvalues);
|
|
|
|
$section = $1 if $arguments =~ s/-section\s+([^\b\s]+)//i;
|
|
|
|
$showvalues = 1 if $arguments =~ s/-showvalues?//i;
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
$arguments =~ s/^\s+|\s+$//g;
|
2020-02-15 23:38:32 +01:00
|
|
|
$arguments =~ s/\s+/ /g;
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
return $usage if not length $arguments;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
$section = lc $section if defined $section;
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
my ($text, $last_item, $last_section, $count);
|
|
|
|
$last_section = '';
|
|
|
|
$count = 0;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
eval {
|
2021-06-05 22:20:03 +02:00
|
|
|
use re::engine::RE2 -strict => 1; # prevent user-defined regex from exploding
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $section_key (sort $registry->get_keys) {
|
|
|
|
next if defined $section and $section_key ne $section;
|
|
|
|
foreach my $item_key (sort $registry->get_keys($section_key)) {
|
2021-06-05 22:20:03 +02:00
|
|
|
next if $item_key eq '_name'; # skip internal cached value
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($registry->get_data($section_key, $item_key, 'private')) {
|
2021-06-05 22:20:03 +02:00
|
|
|
# if private, match on key only -- do not match on value
|
2020-02-15 23:38:32 +01:00
|
|
|
next if $item_key !~ /$arguments/i;
|
|
|
|
} else {
|
2021-06-05 22:20:03 +02:00
|
|
|
# otherwise check for match on key and value
|
|
|
|
next if $item_key !~ /$arguments/i and $registry->get_data($section_key, $item_key, 'value') !~ /$arguments/i;
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
$count++;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
|
|
|
if ($section_key ne $last_section) {
|
|
|
|
$text .= "[$section_key]\n";
|
|
|
|
$last_section = $section_key;
|
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($showvalues) {
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($registry->get_data($section_key, $item_key, 'private')) {
|
|
|
|
$text .= " $item_key = <private>\n";
|
|
|
|
} else {
|
|
|
|
$text .= " $item_key = " . $registry->get_data($section_key, $item_key, 'value')
|
|
|
|
. ($registry->get_data($section_key, $item_key, 'type') eq 'array' ? " [array]\n" : "\n");
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$text .= " $item_key\n";
|
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$last_item = $item_key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
return "/msg $context->{nick} $context->{arguments}: $@" if $@;
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($count == 1) {
|
|
|
|
chomp $text;
|
|
|
|
|
|
|
|
if ($registry->get_data($last_section, $last_item, 'private')) {
|
|
|
|
return "Found one registry entry: [$last_section] $last_item: <private>";
|
|
|
|
} else {
|
2020-02-15 23:38:32 +01:00
|
|
|
return
|
|
|
|
"Found one registry entry: [$last_section] $last_item: "
|
|
|
|
. $registry->get_data($last_section, $last_item, 'value')
|
|
|
|
. ($registry->get_data($last_section, $last_item, 'type') eq 'array' ? ' [array]' : '');
|
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
} elsif ($count > 1) {
|
|
|
|
return "Found $count registry entries:\n$text";
|
2014-05-20 05:28:43 +02:00
|
|
|
} else {
|
2020-02-15 23:38:32 +01:00
|
|
|
my $sections = (defined $section ? "section $section" : 'any sections');
|
|
|
|
return "No matching registry entries found in $sections.";
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub cmd_regchange {
|
|
|
|
my ($self, $context) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($section, $item, $delim, $tochange, $changeto, $modifier);
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
my $arguments = $context->{arguments};
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
if (length $arguments) {
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($arguments =~ /^(.+?)\.([^\s]+)\s+s(.)/ or $arguments =~ /^([^\s]+) ([^\s]+)\s+s(.)/) {
|
|
|
|
$section = $1;
|
|
|
|
$item = $2;
|
|
|
|
$delim = $3;
|
|
|
|
}
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if ($arguments =~ /$delim(.*?)$delim(.*)$delim(.*)?$/) {
|
|
|
|
$tochange = $1;
|
|
|
|
$changeto = $2;
|
|
|
|
$modifier = $3;
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not defined $section or not defined $item or not defined $changeto) {
|
|
|
|
return "Usage: regchange <section>.<item> s/<pattern>/<replacement>/";
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$section = lc $section;
|
|
|
|
$item = lc $item;
|
2020-01-15 03:10:53 +01:00
|
|
|
|
2021-07-09 23:39:35 +02:00
|
|
|
my $registry = $self->{pbot}->{registry}->{storage};
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not $registry->exists($section)) {
|
|
|
|
return "No such registry section $section.";
|
|
|
|
}
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (not $registry->exists($section, $item)) {
|
|
|
|
return "No such registry item $item in section $section.";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $result = eval {
|
|
|
|
use re::engine::RE2 -strict => 1; # prevent user-defined regex from exploding
|
2014-05-17 22:08:19 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
my $value = $registry->get_data($section, $item, 'value');
|
|
|
|
|
|
|
|
if (not $value =~ s|$tochange|$changeto|) {
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{logger}->log("($context->{from}) $context->{hostmask}: failed to change $section.$item 's$delim$tochange$delim$changeto$delim$modifier\n");
|
|
|
|
return "/msg $context->{nick} Change $section.$item failed.";
|
2020-02-15 23:38:32 +01:00
|
|
|
} else {
|
2021-06-05 22:20:03 +02:00
|
|
|
$registry->set($section, $item, 'value', $value, 1);
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{logger}->log("($context->{from}) $context->{hostmask}: changed $section.$item 's/$tochange/$changeto/\n");
|
2021-06-05 22:20:03 +02:00
|
|
|
$self->{pbot}->{registry}->process_trigger($section, $item, 'value', $value);
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{registry}->save;
|
2021-06-05 22:20:03 +02:00
|
|
|
return "$section.$item set to $value";
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
|
|
|
};
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
return "/msg $context->{nick} Failed to change $section.$item: $@" if $@;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
return $result;
|
2014-05-17 22:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|