2021-07-27 06:46:20 +02:00
# File: Interpreter.pm
#
# Purpose: Provides functionality for factoids.
2023-02-21 06:31:52 +01:00
# SPDX-FileCopyrightText: 2005-2023 Pragmatic Software <pragma78@gmail.com>
2021-07-27 06:46:20 +02:00
# SPDX-License-Identifier: MIT
package PBot::Core::Factoids::Interpreter ;
use parent 'PBot::Core::Class' ;
use PBot::Imports ;
use Time::HiRes qw( gettimeofday ) ;
use Time::Duration qw( duration ) ;
sub initialize { }
# main entry point for PBot::Core::Interpreter to interpret a factoid command
2023-04-14 06:04:12 +02:00
sub interpreter ($self, $context) {
2021-07-27 06:46:20 +02:00
# trace context and context's contents
if ( $ self - > { pbot } - > { registry } - > get_value ( 'general' , 'debugcontext' ) ) {
use Data::Dumper ;
2024-11-22 05:21:14 +01:00
$ Data:: Dumper:: Sortkeys = 1 ;
2024-10-22 18:50:10 +02:00
$ Data:: Dumper:: Indent = 2 ;
2021-07-27 06:46:20 +02:00
$ self - > { pbot } - > { logger } - > log ( "Factoids::interpreter\n" ) ;
$ self - > { pbot } - > { logger } - > log ( Dumper $ context ) ;
}
if ( not length $ context - > { keyword } ) {
$ self - > { pbot } - > { logger } - > log ( "Factoids: interpreter: no keyword.\n" ) ;
return ;
}
if ( $ context - > { interpret_depth } > $ self - > { pbot } - > { registry } - > get_value ( 'interpreter' , 'max_recursion' ) ) {
$ self - > { pbot } - > { logger } - > log ( "Factoids: interpreter: max-recursion.\n" ) ;
return ;
}
my $ strictnamespace = $ self - > { pbot } - > { registry } - > get_value ( $ context - > { from } , 'strictnamespace' ) ;
$ strictnamespace // = $ self - > { pbot } - > { registry } - > get_value ( 'general' , 'strictnamespace' ) ;
2022-07-06 08:12:12 +02:00
# factoid > nick redirection
my $ nick_regex = $ self - > { pbot } - > { registry } - > get_value ( 'regex' , 'nickname' ) ;
if ( $ context - > { arguments } =~ s/> ($nick_regex)$// ) {
my $ rcpt = $ 1 ;
if ( $ self - > { pbot } - > { nicklist } - > is_present ( $ context - > { from } , $ rcpt ) ) {
$ context - > { nickprefix } = $ rcpt ;
$ context - > { nickprefix_forced } = 1 ;
} else {
$ context - > { arguments } . = "> $rcpt" ;
}
}
2021-07-27 06:46:20 +02:00
my $ original_keyword = $ context - > { keyword } ;
2022-07-06 08:12:12 +02:00
# search for exact match of factoid
2021-07-27 06:46:20 +02:00
my ( $ channel , $ keyword ) =
$ self - > { pbot } - > { factoids } - > { data } - > find (
$ context - > { ref_from } ? $ context - > { ref_from } : $ context - > { from } ,
$ context - > { keyword } ,
arguments = > $ context - > { arguments } ,
exact_channel = > 1 ,
2022-07-06 08:12:12 +02:00
exact_trigger = > 2 ,
2021-07-27 06:46:20 +02:00
) ;
# determine if we prepend [channel] to factoid output
2022-07-06 08:12:12 +02:00
if ( defined $ channel and $ channel ne '.*'
and $ channel ne lc $ context - > { from } )
{
$ context - > { ref_from } = $ channel ;
}
elsif ( not defined $ context - > { ref_from }
2021-07-27 06:46:20 +02:00
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}" ;
my @ chanlist = ( ) ;
my ( $ fwd_chan , $ fwd_trig ) ;
unless ( $ strictnamespace ) {
# build list of which channels contain the keyword, keeping track of the last one and count
foreach my $ factoid ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_all ( "index2 = $original_keyword" , 'index1' , 'type' ) ) {
2021-11-20 03:05:50 +01:00
next if $ factoid - > { type } ne 'text' and $ factoid - > { type } ne 'applet' ;
2021-07-27 06:46:20 +02:00
push @ chanlist , $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ factoid - > { index1 } , '_name' ) ;
$ fwd_chan = $ factoid - > { index1 } ;
$ fwd_trig = $ original_keyword ;
}
}
# if multiple channels have this keyword, then ask user to disambiguate
if ( @ chanlist > 1 ) {
2023-09-23 22:52:10 +02:00
# but only if the bot is explicitly invoked
if ( not $ context - > { addressed } ) {
$ self - > { pbot } - > { logger } - > log ( "Factoid found in multiple channels; disregarding disambiguation because bot not explicitly invoked\n" ) ;
return '' ;
}
2022-07-06 08:12:12 +02:00
return "Factoid `$original_keyword` exists in " . join ( ', ' , @ chanlist ) . "; use `fact <channel> $original_keyword` to choose one." ;
2021-07-27 06:46:20 +02:00
}
# if there's just one other channel that has this keyword, trigger that instance
elsif ( @ chanlist == 1 ) {
$ self - > { pbot } - > { logger } - > log ( "Found '$original_keyword' as '$fwd_trig' in [$fwd_chan]\n" ) ;
$ context - > { keyword } = $ fwd_trig ;
$ context - > { interpret_depth } + + ;
$ context - > { ref_from } = $ fwd_chan ;
return $ self - > interpreter ( $ context ) ;
}
2023-09-24 20:24:20 +02:00
# keyword still not found, try regex factoids
2022-07-06 08:12:12 +02:00
( $ 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 ) {
2023-09-24 20:24:20 +02:00
# but only if the bot is explicitly invoked
if ( not $ context - > { addressed } ) {
$ self - > { pbot } - > { logger } - > log ( "No factoid found; disregarding error message because bot not explicitly invoked\n" ) ;
return '' ;
}
2021-08-28 04:41:51 +02:00
my $ namespace = $ context - > { from } ;
2021-07-27 06:46:20 +02:00
$ namespace = '.*' if $ namespace !~ /^#/ ;
my $ namespace_regex = $ namespace ;
if ( $ strictnamespace ) { $ namespace_regex = "(?:" . ( quotemeta $ namespace ) . '|\\.\\*)' ; }
2022-06-26 02:34:06 +02:00
$ context - > { arguments } = "$original_keyword -channel $namespace_regex" ;
2021-07-27 06:46:20 +02:00
my $ matches = $ self - > { pbot } - > { commands } - > { modules } - > { Factoids } - > cmd_factfind ( $ context ) ;
# found factfind matches
if ( $ matches !~ m/^No factoids/ ) {
return "No such factoid '$original_keyword'; $matches" ;
}
# otherwise find levenshtein closest matches
$ matches = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > levenshtein_matches ( $ namespace , lc $ original_keyword , 0.50 , $ strictnamespace ) ;
# if a non-nick argument was supplied, e.g., a sentence using the bot's nick, /msg the error to the caller
if ( length $ context - > { arguments } and not $ self - > { pbot } - > { nicklist } - > is_present ( $ context - > { from } , $ context - > { arguments } ) ) {
$ context - > { send_msg_to_caller } = 1 ;
}
# /msg the caller if nothing similiar was found
$ context - > { send_msg_to_caller } = 1 if $ matches eq 'none' ;
$ context - > { send_msg_to_caller } = 1 if $ context - > { embedded } ;
my $ msg_caller = '' ;
$ msg_caller = "/msg $context->{nick} " if $ context - > { send_msg_to_caller } ;
my $ ref_from = $ context - > { ref_from } ? "[$context->{ref_from}] " : '' ;
if ( $ matches eq 'none' ) {
return $ msg_caller . $ ref_from . "No such factoid '$original_keyword'; no similar matches." ;
} else {
return $ msg_caller . $ ref_from . "No such factoid '$original_keyword'; did you mean $matches?" ;
}
}
}
my $ channel_name = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , '_name' ) ;
my $ trigger_name = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , '_name' ) ;
$ channel_name = 'global' if $ channel_name eq '.*' ;
$ trigger_name = "\"$trigger_name\"" if $ trigger_name =~ / / ;
$ context - > { keyword } = $ keyword ;
$ context - > { trigger } = $ keyword ;
$ context - > { channel } = $ channel ;
$ context - > { original_keyword } = $ original_keyword ;
$ context - > { channel_name } = $ channel_name ;
$ context - > { trigger_name } = $ trigger_name ;
2024-11-06 01:48:59 +01:00
$ context - > { factoid } = 1 ;
2021-07-27 06:46:20 +02:00
if ( $ context - > { embedded } and $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'noembed' ) ) {
$ self - > { pbot } - > { logger } - > log ( "Factoids: interpreter: ignoring $channel.$keyword due to noembed.\n" ) ;
return ;
}
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'locked_to_channel' ) ) {
if ( $ context - > { ref_from } ne '' ) { # called from another channel
return "$trigger_name may be invoked only in $context->{ref_from}." ;
}
}
2024-11-07 10:45:15 +01:00
$ context - > { locked } = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'locked' ) // 0 ;
2024-11-06 01:48:59 +01:00
2021-07-27 06:46:20 +02:00
# rate-limiting
if ( $ context - > { interpret_depth } <= 1
and $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'last_referenced_in' ) eq $ context - > { from } )
{
my $ ratelimit = $ self - > { pbot } - > { registry } - > get_value ( $ context - > { from } , 'ratelimit_override' ) ;
$ ratelimit // = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'rate_limit' ) ;
if ( gettimeofday - $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'last_referenced_on' ) < $ ratelimit ) {
my $ ref_from = $ context - > { ref_from } ? "[$context->{ref_from}] " : '' ;
unless ( $ self - > { pbot } - > { users } - > loggedin_admin ( $ channel , "$context->{nick}!$context->{user}\@$context->{host}" ) ) {
return "/msg $context->{nick} $ref_from'$trigger_name' is rate-limited; try again in "
. duration ( $ ratelimit - int ( gettimeofday - $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'last_referenced_on' ) ) ) . "."
}
}
}
# update factoid reference-related metadata
my $ ref_count = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'ref_count' ) ;
my $ update_data = {
ref_count = > + + $ ref_count ,
ref_user = > "$context->{nick}!$context->{user}\@$context->{host}" ,
last_referenced_on = > scalar gettimeofday ,
last_referenced_in = > $ context - > { from } || 'stdin' ,
} ;
$ self - > { pbot } - > { factoids } - > { data } - > { storage } - > add ( $ channel , $ keyword , $ update_data , 1 ) ;
# show usage if usage metadata exists and context has no arguments
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > exists ( $ channel , $ keyword , 'usage' )
and not length $ context - > { arguments } )
{
my $ usage = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'usage' ) ;
$ usage =~ s/(?<!\\)\$0|(?<!\\)\$\{0\}/$trigger_name/g ;
$ context - > { alldone } = 1 ;
return $ usage ;
}
2021-09-10 20:03:20 +02:00
# turn on context->{use_output_queue}?
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'use_output_queue' ) ) {
$ context - > { use_output_queue } = 1 ;
}
2024-11-04 09:25:36 +01:00
# turn on context->{condense-whitespace}?
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'condense-whitespace' ) ) {
$ context - > { 'condense-whitespace' } = 1 ;
2021-09-10 20:03:20 +02:00
}
2021-07-28 06:27:03 +02:00
# tell PBot::Core::Interpreter to prepend caller's nick to output
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'add_nick' ) ) {
$ context - > { add_nick } = 1 ;
}
2021-07-27 06:46:20 +02:00
# factoid action
my $ action ;
# action_with_args or regular action?
if ( length $ context - > { arguments } and $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > exists ( $ channel , $ keyword , 'action_with_args' ) ) {
$ action = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'action_with_args' ) ;
} else {
$ action = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'action' ) ;
}
# fork factoid if background-process is enabled
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'background-process' ) ) {
my $ timeout = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'process-timeout' ) ;
$ timeout // = $ self - > { pbot } - > { registry } - > get_value ( 'processmanager' , 'default_timeout' ) ;
$ self - > { pbot } - > { process_manager } - > execute_process (
$ context ,
sub { $ context - > { result } = $ self - > handle_action ( $ context , $ action ) ; } ,
$ timeout ,
) ;
return '' ;
} else {
return $ self - > handle_action ( $ context , $ action ) ;
}
}
2023-04-14 06:04:12 +02:00
sub handle_action ($self, $context, $action) {
2021-07-27 06:46:20 +02:00
# trace context and context's contents
if ( $ self - > { pbot } - > { registry } - > get_value ( 'general' , 'debugcontext' ) ) {
use Data::Dumper ;
2024-11-27 21:17:45 +01:00
$ Data:: Dumper:: Sortkeys = 1 ;
2024-10-22 18:50:10 +02:00
$ Data:: Dumper:: Indent = 2 ;
2021-07-27 06:46:20 +02:00
$ self - > { pbot } - > { logger } - > log ( "Factoids::handle_action [$action]\n" ) ;
$ self - > { pbot } - > { logger } - > log ( Dumper $ context ) ;
}
if ( not length $ action ) {
$ self - > { pbot } - > { logger } - > log ( "Factoids: handle_action: no action.\n" ) ;
return '' ;
}
my ( $ channel , $ keyword ) = ( $ context - > { channel } , $ context - > { trigger } ) ;
my ( $ channel_name , $ trigger_name ) = ( $ context - > { channel_name } , $ context - > { trigger_name } ) ;
my $ ref_from = '' ;
2024-11-22 05:21:14 +01:00
unless ( $ context - > { no_ref_from } or $ context - > { pipe } or $ context - > { subcmd } ) {
2021-07-27 06:46:20 +02:00
$ ref_from = $ context - > { ref_from } ? "[$context->{ref_from}] " : '' ;
}
my $ interpolate = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'interpolate' ) ;
if ( defined $ interpolate and not $ interpolate ) {
$ context - > { interpolate } = 0 ;
} else {
$ context - > { interpolate } = 1 ;
}
if ( $ context - > { interpolate } ) {
my ( $ root_channel , $ root_keyword ) = $ self - > { pbot } - > { factoids } - > { data } - > find (
$ context - > { ref_from } ? $ context - > { ref_from } : $ context - > { from } ,
$ context - > { root_keyword } ,
arguments = > $ context - > { arguments } ,
exact_channel = > 1 ,
) ;
if ( not defined $ root_channel or not defined $ root_keyword ) {
$ root_channel = $ channel ;
$ root_keyword = $ keyword ;
}
if ( not length $ context - > { keyword_override }
and length $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ root_channel , $ root_keyword , 'keyword_override' ) )
{
$ context - > { keyword_override } = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ root_channel , $ root_keyword , 'keyword_override' ) ;
}
$ action = $ self - > { pbot } - > { factoids } - > { variables } - > expand_factoid_vars ( $ context , $ action ) ;
}
# handle arguments
if ( length $ context - > { arguments } ) {
# arguments supplied
if ( $ action =~ m/\$\{?args/ or $ action =~ m/\$\{?arg\[/ ) {
# factoid has $args, replace them
if ( $ context - > { interpolate } ) {
2024-11-27 21:17:45 +01:00
$ action = $ self - > { pbot } - > { factoids } - > { variables } - > expand_action_arguments ( $ context , $ action , $ context - > { arguments } , $ context - > { nick } ) ;
2021-07-27 06:46:20 +02:00
}
$ context - > { arguments } = '' ;
$ context - > { original_arguments } = '' ;
2022-07-10 04:33:43 +02:00
$ context - > { nickprefix_disabled } = 1 ;
2021-07-27 06:46:20 +02:00
} else {
2022-07-08 18:11:16 +02:00
# set nickprefix if args is a present nick and factoid action doesn't have $nick or $args
if ( ! $ context - > { nickprefix_disabled } && $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'type' ) eq 'text' ) {
2021-07-27 06:46:20 +02:00
my $ target = $ self - > { pbot } - > { nicklist } - > is_present_similar ( $ context - > { from } , $ context - > { arguments } ) ;
2022-07-08 18:11:16 +02:00
if ( $ target and $ action !~ /\$\{?(?:nick|args)\b/ ) {
2024-11-07 07:51:29 +01:00
my $ add_nick = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'add_nick' ) ;
unless ( defined $ add_nick && $ add_nick == 0 ) {
$ context - > { nickprefix } = $ target unless $ context - > { nickprefix_forced } ;
}
2021-07-27 06:46:20 +02:00
}
}
}
} else {
# no arguments supplied
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > exists ( $ channel , $ keyword , 'usage' ) ) {
# factoid has a usage message, show it
$ action = "/say " . $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'usage' ) ;
$ action =~ s/(?<!\\)\$0|(?<!\\)\$\{0\}/$trigger_name/g ;
$ context - > { alldone } = 1 ;
} else {
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'allow_empty_args' ) ) {
2024-11-27 21:17:45 +01:00
$ action = $ self - > { pbot } - > { factoids } - > { variables } - > expand_action_arguments ( $ context , $ action , undef , '' ) ;
2021-07-27 06:46:20 +02:00
} else {
2024-11-27 21:17:45 +01:00
$ action = $ self - > { pbot } - > { factoids } - > { variables } - > expand_action_arguments ( $ context , $ action , undef , $ context - > { nick } ) ;
2021-07-27 06:46:20 +02:00
}
}
$ context - > { nickprefix_disabled } = 0 ;
}
2024-11-15 10:15:37 +01:00
$ self - > { pbot } - > { logger } - > log ( "$context->{from}: $context->{nick}!$context->{user}\@$context->{host}: $trigger_name: action: \"$action\"\n" ) ;
my $ enabled = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'enabled' ) ;
if ( defined $ enabled and $ enabled == 0 ) {
$ self - > { pbot } - > { logger } - > log ( "$trigger_name disabled.\n" ) ;
return "${ref_from}$trigger_name is disabled." ;
}
2021-07-27 06:46:20 +02:00
# Check if it's an alias
if ( $ action =~ /^\/call\s+(.*)$/msi ) {
my $ command = $ 1 ;
$ command =~ s/\n$// ;
unless ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'require_explicit_args' ) ) {
my $ args = $ context - > { arguments } ;
$ command . = " $args" if length $ args and not $ context - > { special } eq 'code-factoid' ;
$ context - > { arguments } = '' ;
}
unless ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'no_keyword_override' ) ) {
if ( $ command =~ s/\s*--keyword-override=([^ ]+)\s*// ) { $ context - > { keyword_override } = $ 1 ; }
}
$ context - > { command } = $ command ;
$ context - > { aliased } = 1 ;
$ self - > { pbot } - > { logger } - > log ( "$context->{from}: $context->{nick}!$context->{user}\@$context->{host}: $trigger_name aliased to: $command\n" ) ;
if ( defined $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'cap-override' ) ) {
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'locked' ) ) {
$ self - > { pbot } - > { logger } - > log ( "Capability override set to " . $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'cap-override' ) . "\n" ) ;
$ context - > { 'cap-override' } = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'cap-override' ) ;
} else {
$ self - > { pbot } - > { logger } - > log ( "Ignoring cap-override of " . $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'cap-override' ) . " on unlocked factoid\n" ) ;
}
}
return $ self - > { pbot } - > { interpreter } - > interpret ( $ context ) ;
}
if ( $ context - > { interpolate } ) {
my ( $ root_channel , $ root_keyword ) = $ self - > { pbot } - > { factoids } - > { data } - > find (
$ context - > { ref_from } ? $ context - > { ref_from } : $ context - > { from } ,
$ context - > { root_keyword } ,
arguments = > $ context - > { arguments } ,
exact_channel = > 1 ,
) ;
if ( not defined $ root_channel or not defined $ root_keyword ) {
$ root_channel = $ channel ;
$ root_keyword = $ keyword ;
}
if ( not length $ context - > { keyword_override }
and length $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ root_channel , $ root_keyword , 'keyword_override' ) )
{
$ context - > { keyword_override } = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ root_channel , $ root_keyword , 'keyword_override' ) ;
}
$ action = $ self - > { pbot } - > { factoids } - > { variables } - > expand_factoid_vars ( $ context , $ action ) ;
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'allow_empty_args' ) ) {
2024-11-27 21:17:45 +01:00
$ action = $ self - > { pbot } - > { factoids } - > { variables } - > expand_action_arguments ( $ context , $ action , $ context - > { arguments } , '' ) ;
2021-07-27 06:46:20 +02:00
} else {
2024-11-27 21:17:45 +01:00
$ action = $ self - > { pbot } - > { factoids } - > { variables } - > expand_action_arguments ( $ context , $ action , $ context - > { arguments } , $ context - > { nick } ) ;
2021-07-27 06:46:20 +02:00
}
}
2024-11-04 09:25:36 +01:00
my $ condense_whitespace = $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'condense-whitespace' ) ;
2021-08-24 04:27:34 +02:00
2024-11-04 09:25:36 +01:00
if ( defined $ condense_whitespace ) {
$ context - > { 'condense-whitespace' } = $ condense_whitespace ;
2021-08-24 04:27:34 +02:00
}
2024-10-05 07:34:02 +02:00
# action is a code factoid
if ( $ action =~ m {^/code\s+([^\s]+)\s+(.+)$}msi ) {
my ( $ lang , $ code ) = ( $ 1 , $ 2 ) ;
$ context - > { lang } = $ lang ;
$ context - > { code } = $ code ;
2024-11-03 01:53:37 +01:00
return $ self - > { pbot } - > { factoids } - > { code } - > execute ( $ context ) ;
2024-10-05 07:34:02 +02:00
}
2021-07-27 06:46:20 +02:00
return $ action if $ context - > { special } eq 'code-factoid' ;
2021-11-20 03:05:50 +01:00
if ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'type' ) eq 'applet' ) {
2021-08-24 04:27:34 +02:00
$ context - > { root_keyword } = $ keyword unless defined $ context - > { root_keyword } ;
$ context - > { root_channel } = $ channel ;
2021-07-27 06:46:20 +02:00
2021-11-20 03:05:50 +01:00
my $ result = $ self - > { pbot } - > { applets } - > execute_applet ( $ context ) ;
2021-07-27 06:46:20 +02:00
2021-09-05 05:53:02 +02:00
if ( defined $ result && length $ result ) {
2021-07-27 06:46:20 +02:00
return $ ref_from . $ result ;
} else {
2021-09-05 05:53:02 +02:00
return $ result ;
2021-07-27 06:46:20 +02:00
}
}
elsif ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'type' ) eq 'text' ) {
# Don't allow user-custom /msg factoids, unless invoked by admin
if ( $ action =~ m/^\/msg/i ) {
if ( not $ self - > { pbot } - > { users } - > loggedin_admin ( $ context - > { from } , $ context - > { hostmask } ) ) {
$ self - > { pbot } - > { logger } - > log ( "[ABUSE] Bad factoid (starts with /msg): $action\n" ) ;
return "You must be an admin to use /msg." ;
}
}
if ( $ ref_from ) {
if ( $ action =~ s/^\/say\s+/$ref_from/i
|| $ action =~ s/^\/me\s+(.*)/\/me $1 $ref_from/i
|| $ action =~ s/^\/msg\s+([^ ]+)/\/msg $1 $ref_from/i
) {
return $ action ;
} else {
return $ ref_from . "$trigger_name is $action" ;
}
} else {
if ( $ action =~ m/^\/(?:say|me|msg)/i ) {
return $ action ;
} else {
return "/say $trigger_name is $action" ;
}
}
}
elsif ( $ self - > { pbot } - > { factoids } - > { data } - > { storage } - > get_data ( $ channel , $ keyword , 'type' ) eq 'regex' ) {
my $ result = eval {
my $ string = "$context->{original_keyword}" . ( length $ context - > { arguments } ? " $context->{arguments}" : '' ) ;
my $ cmd ;
if ( $ string =~ m/$keyword/i ) {
$ self - > { pbot } - > { logger } - > log ( "[$string] matches [$keyword] - calling [" . $ action . "$']\n" ) ;
$ cmd = $ action . $' ;
my ( $ a , $ b , $ c , $ d , $ e , $ f , $ g , $ h , $ i , $ before , $ after ) = ( $ 1 , $ 2 , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 , $` , $' ) ;
$ cmd =~ s/\$1/$a/g ;
$ cmd =~ s/\$2/$b/g ;
$ cmd =~ s/\$3/$c/g ;
$ cmd =~ s/\$4/$d/g ;
$ cmd =~ s/\$5/$e/g ;
$ cmd =~ s/\$6/$f/g ;
$ cmd =~ s/\$7/$g/g ;
$ cmd =~ s/\$8/$h/g ;
$ cmd =~ s/\$9/$i/g ;
$ cmd =~ s/\$`/$before/g ;
$ cmd =~ s/\$'/$after/g ;
$ cmd =~ s/^\s+// ;
$ cmd =~ s/\s+$// ;
} else {
$ cmd = $ action ;
}
$ context - > { command } = $ cmd ;
return $ self - > { pbot } - > { interpreter } - > interpret ( $ context ) ;
} ;
if ( $@ ) {
$ self - > { pbot } - > { logger } - > log ( "Factoids: bad regex: $@\n" ) ;
return '' ;
}
if ( length $ result ) {
return $ ref_from . $ result ;
} else {
return '' ;
}
} else {
$ self - > { pbot } - > { logger } - > log ( "$context->{from}: $context->{nick}!$context->{user}\@$context->{host}): bad type for $channel.$keyword\n" ) ;
return "/me blinks. $ref_from" ;
}
}
1 ;