From 8f1ac5b5ba0ffee241b930055cf51363cb9be3ee Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Thu, 25 Sep 2025 01:03:17 -0700 Subject: [PATCH] Factoids: variable expansions update ref-count and last-referenced --- lib/PBot/Core/Factoids/Variables.pm | 23 +++++++++++++++++++ .../Core/Storage/DualIndexSQLiteObject.pm | 4 ++-- lib/PBot/VERSION.pm | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/PBot/Core/Factoids/Variables.pm b/lib/PBot/Core/Factoids/Variables.pm index 5b4a69aa..41e9c20b 100644 --- a/lib/PBot/Core/Factoids/Variables.pm +++ b/lib/PBot/Core/Factoids/Variables.pm @@ -52,6 +52,8 @@ sub expand_factoid_vars($self, $context, $action, %opts) { my $result = ''; my $rest = $action; + my $needs_save = 0; + while (++$depth < 100) { $rest =~ s/(?[0], $factoids[0]->[1]); + my $ref_count = $self->{pbot}->{factoids}->{data}->{storage}->get_data($var_chan, $var, 'ref_count'); + + my $data = { + ref_count => $ref_count + 1, + ref_user => $context->{hostmask}, + last_referenced_in => $context->{from}, + last_referenced_on => time + }; + + $self->{pbot}->{factoids}->{data}->{storage}->add($var_chan, $var, $data, 1); + $needs_save = 1; + if ($self->{pbot}->{factoids}->{data}->{storage}->get_data($var_chan, $var, 'action') =~ m{^/call (.*)}ms) { $var = $1; @@ -206,6 +220,11 @@ sub expand_factoid_vars($self, $context, $action, %opts) { $replacement = $fixed_article . $trailing . $replacement; } + # prevent double-spaces when joining replacement to result + if ($result =~ m/ $/ && (!length($replacement) || $replacement =~ m/^ /)) { + $result =~ s/ $//; + } + $result .= $replacement; $expansions++; @@ -229,6 +248,10 @@ sub expand_factoid_vars($self, $context, $action, %opts) { # unescape certain symbols $result =~ s/(?{pbot}->{factoids}->{data}->{storage}->save; + } + return validate_string($result, $self->{pbot}->{registry}->get_value('factoids', 'max_content_length')); } diff --git a/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm b/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm index 2282aea0..5375ace1 100644 --- a/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm +++ b/lib/PBot/Core/Storage/DualIndexSQLiteObject.pm @@ -803,7 +803,7 @@ sub remove($self, $index1, $index2 = undef, $data_index = undef, $dont_save = 0) return "$name2.$data_index is not set."; } -sub set($self, $index1, $index2, $key = undef, $value = undef) { +sub set($self, $index1, $index2, $key = undef, $value = undef, $dont_save = 0) { if (not $self->exists($index1)) { my $result = "$self->{name}: $index1 not found; similiar matches: "; $result .= $self->levenshtein_matches($index1); @@ -862,7 +862,7 @@ sub set($self, $index1, $index2, $key = undef, $value = undef) { $self->{cache}->{$lc_index1}->{$lc_index2}->{$key} = $value; } - $self->save; + $self->save unless $dont_save; }; if ($@) { diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 8b493c3f..76ad96e0 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 => 4892, - BUILD_DATE => "2025-09-24", + BUILD_REVISION => 4894, + BUILD_DATE => "2025-09-25", }; sub initialize {}