3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-09-26 22:27:23 +02:00

Factoids: variable expansions update ref-count and last-referenced

This commit is contained in:
Pragmatic Software 2025-09-25 01:03:17 -07:00
parent bda93505f3
commit 8f1ac5b5ba
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
3 changed files with 27 additions and 4 deletions

View File

@ -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/$root_keyword/g;
@ -106,6 +108,18 @@ sub expand_factoid_vars($self, $context, $action, %opts) {
if (@factoids && $factoids[0]) {
($var_chan, $var) = ($factoids[0]->[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/(?<!\\)\\([\$\:\|])/$1/g;
if ($needs_save) {
$self->{pbot}->{factoids}->{data}->{storage}->save;
}
return validate_string($result, $self->{pbot}->{registry}->get_value('factoids', 'max_content_length'));
}

View File

@ -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 ($@) {

View File

@ -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 {}