From bd2e225f7602b6c21a52c1b58faa5e64a7ddb85c Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sat, 4 Jan 2020 21:26:18 -0800 Subject: [PATCH] RegistryCommands: all commands now understand section.item syntax (e.g. irc.server) --- PBot/RegistryCommands.pm | 156 ++++++++++++++++++++++++++------------- 1 file changed, 104 insertions(+), 52 deletions(-) diff --git a/PBot/RegistryCommands.pm b/PBot/RegistryCommands.pm index 608b31a0..773e2ffa 100644 --- a/PBot/RegistryCommands.pm +++ b/PBot/RegistryCommands.pm @@ -42,13 +42,82 @@ sub initialize { $pbot->{commands}->register(sub { return $self->regfind(@_) }, "regfind", 0); } +sub regset { + my $self = shift; + my ($from, $nick, $user, $host, $arguments, $stuff) = @_; + + my $usage = "Usage: regset
. "; + + # support "
." syntax in addition to "
" + my $section = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist}) // return $usage; + my ($item, $value); + if ($section =~ m/^(.+?)\.(.+)$/) { + ($section, $item) = ($1, $2); + ($value) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 1); + } else { + ($item, $value) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2); + } + + if (not defined $section or not defined $item or not defined $value) { + return $usage; + } + + $self->{pbot}->{registry}->add('text', $section, $item, $value); + + $self->{pbot}->{logger}->log("$nick!$user\@$host set registry entry [$section] $item => $value\n"); + return "$section.$item set to $value"; +} + +sub regunset { + my $self = shift; + my ($from, $nick, $user, $host, $arguments, $stuff) = @_; + + my $usage = "Usage: regunset
."; + + # support "
." syntax in addition to "
" + my $section = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist}) // return $usage; + my $item; + if ($section =~ m/^(.+?)\.(.+)$/) { + ($section, $item) = ($1, $2); + } else { + ($item) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 1); + } + + if (not defined $section or not defined $item) { + return $usage; + } + + if (not exists $self->{pbot}->{registry}->{registry}->hash->{$section}) { + return "No such registry section $section."; + } + + if (not exists $self->{pbot}->{registry}->{registry}->hash->{$section}->{$item}) { + return "No such item $item in section $section."; + } + + $self->{pbot}->{logger}->log("$nick!$user\@$host removed registry entry $section.$item\n"); + $self->{pbot}->{registry}->remove($section, $item); + return "$section.$item deleted from registry"; +} + sub regsetmeta { my $self = shift; my ($from, $nick, $user, $host, $arguments, $stuff) = @_; - my ($section, $item, $key, $value) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 4); + + my $usage = "Usage: regsetmeta
. [key [value]]"; + + # support "
." syntax in addition to "
" + my $section = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist}) // return $usage; + my ($item, $key, $value); + if ($section =~ m/^(.+?)\.(.+)$/) { + ($section, $item) = ($1, $2); + ($key, $value) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2); + } else { + ($item, $key, $value) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 3); + } if (not defined $section or not defined $item) { - return "Usage: regsetmeta
[key [value]]"; + return $usage; } $key = undef if not length $key; @@ -60,61 +129,44 @@ sub regsetmeta { sub regunsetmeta { my $self = shift; my ($from, $nick, $user, $host, $arguments, $stuff) = @_; - my ($section, $item, $key) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 3); + + my $usage = "Usage: regunsetmeta
. "; + + # support "
." syntax in addition to "
" + my $section = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist}) // return $usage; + my ($item, $key); + if ($section =~ m/^(.+?)\.(.+)$/) { + ($section, $item) = ($1, $2); + ($key) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 1); + } else { + ($item, $key) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2); + } if (not defined $section or not defined $item or not defined $key) { - return "Usage: regunsetmeta
" + return $usage; } return $self->{pbot}->{registry}->unset($section, $item, $key); } -sub regset { - my $self = shift; - my ($from, $nick, $user, $host, $arguments, $stuff) = @_; - my ($section, $item, $value) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 3); - - if (not defined $section or not defined $item or not defined $value) { - return "Usage: regset
"; - } - - $self->{pbot}->{registry}->add('text', $section, $item, $value); - - $self->{pbot}->{logger}->log("$nick!$user\@$host set registry entry [$section] $item => $value\n"); - return "[$section] $item set to $value"; -} - -sub regunset { - my $self = shift; - my ($from, $nick, $user, $host, $arguments, $stuff) = @_; - my ($section, $item) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2); - - if (not defined $section or not defined $item) { - return "Usage: regunset
"; - } - - if (not exists $self->{pbot}->{registry}->{registry}->hash->{$section}) { - return "No such registry section $section."; - } - - if (not exists $self->{pbot}->{registry}->{registry}->hash->{$section}->{$item}) { - return "No such item $item in section $section."; - } - - $self->{pbot}->{logger}->log("$nick!$user\@$host removed registry item [$section][$item]\n"); - $self->{pbot}->{registry}->remove($section, $item); - return "Registry item $item unset from section $section."; -} - sub regshow { my $self = shift; my ($from, $nick, $user, $host, $arguments, $stuff) = @_; my $registry = $self->{pbot}->{registry}->{registry}->hash; - my ($section, $item) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2); + my $usage = "Usage: regshow
."; + + # support "
." syntax in addition to "
" + my $section = $self->{pbot}->{interpreter}->shift_arg($stuff->{arglist}) // return $usage; + my $item; + if ($section =~ m/^(.+?)\.(.+)$/) { + ($section, $item) = ($1, $2); + } else { + ($item) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 1); + } if (not defined $section or not defined $item) { - return "Usage: regshow
"; + return $usage; } if (not exists $registry->{$section}) { @@ -126,10 +178,10 @@ sub regshow { } if ($registry->{$section}->{$item}->{private}) { - return "[$section] $item: "; + return "$section.$item: "; } - my $result = "[$section] $item: $registry->{$section}->{$item}->{value}"; + my $result = "$section.$item: $registry->{$section}->{$item}->{value}"; if ($registry->{$section}->{$item}->{type} eq 'array') { $result .= ' [array]'; @@ -220,7 +272,7 @@ sub regchange { my ($section, $item, $delim, $tochange, $changeto, $modifier); if (defined $arguments) { - if ($arguments =~ /^([^\s]+) ([^\s]+)\s+s(.)/) { + if ($arguments =~ /^(.+?)\.([^\s]+)\s+s(.)/ or $arguments =~ /^([^\s]+) ([^\s]+)\s+s(.)/) { $section = $1; $item = $2; $delim = $3; @@ -234,7 +286,7 @@ sub regchange { } if (not defined $section or not defined $item or not defined $changeto) { - return "Usage: regchange
s///"; + return "Usage: regchange
. s///"; } my $registry = $self->{pbot}->{registry}->{registry}->hash; @@ -250,16 +302,16 @@ sub regchange { my $ret = eval { use re::engine::RE2 -strict => 1; if (not $registry->{$section}->{$item}->{value} =~ s|$tochange|$changeto|) { - $self->{pbot}->{logger}->log("($from) $nick!$user\@$host: failed to change [$section] $item 's$delim$tochange$delim$changeto$delim$modifier\n"); - return "/msg $nick Change [$section] $item failed."; + $self->{pbot}->{logger}->log("($from) $nick!$user\@$host: failed to change $section.$item 's$delim$tochange$delim$changeto$delim$modifier\n"); + return "/msg $nick Change $section.$item failed."; } else { - $self->{pbot}->{logger}->log("($from) $nick!$user\@$host: changed [$section] $item 's/$tochange/$changeto/\n"); + $self->{pbot}->{logger}->log("($from) $nick!$user\@$host: changed $section.$item 's/$tochange/$changeto/\n"); $self->{pbot}->{registry}->process_trigger($section, $item, 'value', $registry->{$section}->{$item}->{value}); $self->{pbot}->{registry}->save; - return "Changed: [$section] $item set to $registry->{$section}->{$item}->{value}"; + return "$section.$item set to $registry->{$section}->{$item}->{value}"; } }; - return "/msg $nick Change [$section] $item: $@" if $@; + return "/msg $nick Failed to change $section.$item: $@" if $@; return $ret; }