From a170b28a8b3b62473686df087f622ca18870e6ec Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 5 Jul 2022 23:12:12 -0700 Subject: [PATCH] Factoids: fix behavior of factoid look-up --- lib/PBot/Core/Commands.pm | 1 + lib/PBot/Core/Factoids/Interpreter.pm | 70 ++++++++++++++------------- lib/PBot/VERSION.pm | 4 +- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/lib/PBot/Core/Commands.pm b/lib/PBot/Core/Commands.pm index 67aed6d2..b7f7f7d4 100644 --- a/lib/PBot/Core/Commands.pm +++ b/lib/PBot/Core/Commands.pm @@ -149,6 +149,7 @@ sub interpreter { if ($self->{pbot}->{registry}->get_value('general', 'debugcontext')) { use Data::Dumper; $Data::Dumper::Sortkeys = 1; + $Data::Dumper::Indent = 2; $self->{pbot}->{logger}->log("Commands::interpreter\n"); $self->{pbot}->{logger}->log(Dumper $context); } diff --git a/lib/PBot/Core/Factoids/Interpreter.pm b/lib/PBot/Core/Factoids/Interpreter.pm index 0cef24db..2342baf4 100644 --- a/lib/PBot/Core/Factoids/Interpreter.pm +++ b/lib/PBot/Core/Factoids/Interpreter.pm @@ -38,34 +38,8 @@ sub interpreter { } my $strictnamespace = $self->{pbot}->{registry}->get_value($context->{from}, 'strictnamespace'); - $strictnamespace //= $self->{pbot}->{registry}->get_value('general', 'strictnamespace'); - # search for factoid against global channel and current channel (from unless ref_from is defined) - my $original_keyword = $context->{keyword}; - - my ($channel, $keyword) = - $self->{pbot}->{factoids}->{data}->find( - $context->{ref_from} ? $context->{ref_from} : $context->{from}, - $context->{keyword}, - arguments => $context->{arguments}, - exact_channel => 1, - ); - - # determine if we prepend [channel] to factoid output - if (not defined $context->{ref_from} - or $context->{ref_from} eq '.*' - or $context->{ref_from} eq $context->{from}) - { - $context->{ref_from} = ''; - } - - if (defined $channel and not $channel eq '.*' - and not $channel eq lc $context->{from}) - { - $context->{ref_from} = $channel; - } - # factoid > nick redirection my $nick_regex = $self->{pbot}->{registry}->get_value('regex', 'nickname'); @@ -79,6 +53,31 @@ sub interpreter { } } + my $original_keyword = $context->{keyword}; + + # search for exact match of factoid + my ($channel, $keyword) = + $self->{pbot}->{factoids}->{data}->find( + $context->{ref_from} ? $context->{ref_from} : $context->{from}, + $context->{keyword}, + arguments => $context->{arguments}, + exact_channel => 1, + exact_trigger => 2, + ); + + # determine if we prepend [channel] to factoid output + if (defined $channel and $channel ne '.*' + and $channel ne lc $context->{from}) + { + $context->{ref_from} = $channel; + } + elsif (not defined $context->{ref_from} + or $context->{ref_from} eq '.*' + or $context->{ref_from} eq $context->{from}) + { + $context->{ref_from} = ''; + } + # if no match found, attempt to call factoid from another channel if it exists there if (not defined $keyword) { my $string = "$original_keyword $context->{arguments}"; @@ -96,12 +95,9 @@ sub interpreter { } } - my $ref_from = $context->{ref_from} ? "[$context->{ref_from}] " : ''; - # if multiple channels have this keyword, then ask user to disambiguate if (@chanlist> 1) { - return if $context->{embedded}; - return $ref_from . "Factoid `$original_keyword` exists in " . join(', ', @chanlist) . "; use `fact $original_keyword` to choose one."; + return "Factoid `$original_keyword` exists in " . join(', ', @chanlist) . "; use `fact $original_keyword` to choose one."; } # if there's just one other channel that has this keyword, trigger that instance @@ -113,8 +109,17 @@ sub interpreter { return $self->interpreter($context); } - # otherwise keyword hasn't been found, display similiar matches for all channels - else { + # keyword still not found, try regex factoids + ($channel, $keyword) = + $self->{pbot}->{factoids}->{data}->find( + $context->{ref_from} ? $context->{ref_from} : $context->{from}, + $context->{keyword}, + arguments => $context->{arguments}, + exact_channel => 1, + ); + + # no such keyword, display similiar matches for all channels + if (not defined $keyword) { my $namespace = $context->{from}; $namespace = '.*' if $namespace !~ /^#/; @@ -126,7 +131,6 @@ sub interpreter { # found factfind matches if ($matches !~ m/^No factoids/) { - return if $context->{embedded}; return "No such factoid '$original_keyword'; $matches"; } diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 10dbdef5..886b4e98 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4550, - BUILD_DATE => "2022-07-04", + BUILD_REVISION => 4551, + BUILD_DATE => "2022-07-05", }; sub initialize {}