From 0ef979f829af27d4ae6abce291f6baeabb260396 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 15 Mar 2026 16:08:00 -0700 Subject: [PATCH] Factoids: `factfind` can now limit search to keywords or contents --- doc/Factoids.md | 4 ++-- lib/PBot/Core/Commands/Factoids.pm | 35 +++++++++++++++++++++--------- lib/PBot/VERSION.pm | 4 ++-- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/doc/Factoids.md b/doc/Factoids.md index 2f313eef..093177f1 100644 --- a/doc/Factoids.md +++ b/doc/Factoids.md @@ -675,9 +675,9 @@ To search the database for a factoid, use the 'factfind` command. You may optio If there is only one match for the query, it will display that factoid and its text, otherwise it will list all matching keywords. -Usage: `factfind [-channel channel] [-owner nick] [-by nick] [-regex] [text]` +Usage: `factfind [-channel channel] [-owner regex] [-editby regex] [-refby regex] [-keywords] [-contents] [-regex] [text]` -If you specify the `-regex` flag, the `text` argument will be treated as a regex. +If you specify the `-regex` flag, the `text` argument will be treated as a regex. Use `-keywords` or `-contents` to limit the search respectively. !factfind cast 3 factoids match: [##c] NULL casting dontcastmalloc diff --git a/lib/PBot/Core/Commands/Factoids.pm b/lib/PBot/Core/Commands/Factoids.pm index d1bc03df..56a4413a 100644 --- a/lib/PBot/Core/Commands/Factoids.pm +++ b/lib/PBot/Core/Commands/Factoids.pm @@ -892,6 +892,12 @@ sub cmd_factadd($self, $context) { $from_chan = '.*' if $from_chan !~ /^#/; + $keyword =~ s/^\s+|\s+$//g; + + if (!length $keyword) { + return "/say $context->{nick}: Specify a factoid name."; + } + if (length $keyword > $self->{pbot}->{registry}->get_value('factoids', 'max_name_length')) { return "/say $context->{nick}: I don't think the factoid name needs to be that long."; } @@ -1239,16 +1245,23 @@ sub quotemeta2($text) { sub cmd_factfind($self, $context) { my $arguments = $context->{arguments}; - my $usage = "Usage: factfind [-channel channel] [-owner regex] [-editby regex] [-refby regex] [-regex] [text]"; + my $usage = "Usage: factfind [-channel channel] [-owner regex] [-editby regex] [-refby regex] [-keywords] [-contents] [-regex] [text]"; return $usage if not length $arguments; my $factoids = $self->{pbot}->{factoids}->{data}->{storage}; - my ($channel, $owner, $refby, $editby, $use_regex); + my ($channel, $owner, $refby, $editby, $use_regex, $keywords, $contents); $channel = $1 if $arguments =~ s/\s*-channel\s+([^\b\s]+)//i; $owner = $1 if $arguments =~ s/\s*-owner\s+([^\b\s]+)//i; $refby = $1 if $arguments =~ s/\s*-refby\s+([^\b\s]+)//i; $editby = $1 if $arguments =~ s/\s*-editby\s+([^\b\s]+)//i; $use_regex = 1 if $arguments =~ s/\s*-regex\b//i; + $keywords = 1 if $arguments =~ s/\s*-keywords\b//i; + $contents = 1 if $arguments =~ s/\s*-contents\b//i; + + if (!$keywords && !$contents) { + $keywords = 1; + $contents = 1; + } $arguments =~ s/^\s+//; $arguments =~ s/\s+$//; @@ -1271,14 +1284,10 @@ sub cmd_factfind($self, $context) { } if ($arguments ne "") { - my $unquoted_args = $arguments; - $unquoted_args =~ s/(?:\\(?!\\))//g; - $unquoted_args =~ s/(?:\\\\)/\\/g; - if (not defined $argtype) { - $argtype = "containing '$unquoted_args'"; + $argtype = "containing '$arguments'"; } else { - $argtype .= " and containing '$unquoted_args'"; + $argtype .= " and containing '$arguments'"; } } @@ -1320,8 +1329,14 @@ sub cmd_factfind($self, $context) { $match = 1 if $factoid->{edited_by} =~ /^$editby/i; } - if ($arguments ne "" && ($factoid->{action} =~ /$regex/i || $factoid->{index2} =~ /$regex/i)) { - $match = 1; + if ($arguments ne "") { + if ($keywords && $factoid->{index2} =~ /$regex/i) { + $match = 1; + } + + if ($contents && $factoid->{action} =~ /$regex/i) { + $match = 1; + } } if ($match) { diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 93db57ba..b0cadabf 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 => 4940, - BUILD_DATE => "2026-03-11", + BUILD_REVISION => 4941, + BUILD_DATE => "2026-03-15", }; sub initialize {}