3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-27 06:19:25 +01:00

Refactor Factoids.pm to allow expansion of variables in all factoid types

Variables like $args and adlib variables can now be expanded in modules
and other factoid types.
This commit is contained in:
Pragmatic Software 2015-04-03 10:11:21 -07:00
parent 060d3aee6a
commit 528f66bfed

View File

@ -405,98 +405,53 @@ sub interpreter {
} }
} }
my $type = $self->{factoids}->hash->{$channel}->{$keyword}->{type};
# Check if it's an alias
if($self->{factoids}->hash->{$channel}->{$keyword}->{action} =~ /^\/call\s+(.*)$/) {
my $command;
if(length $arguments) {
$command = "$1 $arguments";
} else {
$command = $1;
}
$pbot->{logger}->log("[" . (defined $from ? $from : "stdin") . "] ($nick!$user\@$host) [$keyword] aliased to: [$command]\n");
$self->{factoids}->hash->{$channel}->{$keyword}->{ref_count}++;
$self->{factoids}->hash->{$channel}->{$keyword}->{ref_user} = "$nick!$user\@$host";
$self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on} = gettimeofday;
return $pbot->{interpreter}->interpret($from, $nick, $user, $host, $count, $command, $tonick);
}
my $last_ref_in = 0;
if(exists $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on}) { if(exists $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on}) {
if(exists $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_in}) { if(exists $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_in}) {
if($self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_in} eq $from) { if($self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_in} eq $from) {
$last_ref_in = 1; if(gettimeofday - $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on} < $self->{factoids}->hash->{$channel}->{$keyword}->{rate_limit}) {
}
}
if(($last_ref_in == 1) and (gettimeofday - $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on} < $self->{factoids}->hash->{$channel}->{$keyword}->{rate_limit})) {
return "/msg $nick $ref_from'$keyword' is rate-limited; try again in " . ($self->{factoids}->hash->{$channel}->{$keyword}->{rate_limit} - int(gettimeofday - $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on})) . " seconds."; return "/msg $nick $ref_from'$keyword' is rate-limited; try again in " . ($self->{factoids}->hash->{$channel}->{$keyword}->{rate_limit} - int(gettimeofday - $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on})) . " seconds.";
} }
} }
if($self->{factoids}->hash->{$channel}->{$keyword}->{enabled} == 0) {
$self->{pbot}->{logger}->log("$keyword disabled.\n");
return "/msg $nick $ref_from$keyword is currently disabled.";
} }
elsif($self->{factoids}->hash->{$channel}->{$keyword}->{type} eq 'module') { }
$self->{pbot}->{logger}->log("Found module\n");
my $type = $self->{factoids}->hash->{$channel}->{$keyword}->{type};
$self->{factoids}->hash->{$channel}->{$keyword}->{ref_count}++; $self->{factoids}->hash->{$channel}->{$keyword}->{ref_count}++;
$self->{factoids}->hash->{$channel}->{$keyword}->{ref_user} = "$nick!$user\@$host"; $self->{factoids}->hash->{$channel}->{$keyword}->{ref_user} = "$nick!$user\@$host";
$self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on} = gettimeofday; $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on} = gettimeofday;
$self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_in} = $from || "stdin"; $self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_in} = $from || "stdin";
my $preserve_whitespace = $self->{factoids}->hash->{$channel}->{$keyword}->{preserve_whitespace}; my $action = $self->{factoids}->hash->{$channel}->{$keyword}->{action};
$preserve_whitespace = 0 if not defined $preserve_whitespace;
return $ref_from . $self->{factoidmodulelauncher}->execute_module($from, $tonick, $nick, $user, $host, "$keyword $arguments", $keyword, $arguments, $preserve_whitespace);
}
elsif($self->{factoids}->hash->{$channel}->{$keyword}->{type} eq 'text') {
$self->{pbot}->{logger}->log("Found factoid\n");
# Don't allow user-custom /msg factoids, unless factoid triggered by admin
if(($self->{factoids}->hash->{$channel}->{$keyword}->{action} =~ m/^\/msg/i) and (not $self->{pbot}->{admins}->loggedin($from, "$nick!$user\@$host"))) {
$self->{pbot}->{logger}->log("[ABUSE] Bad factoid (contains /msg): " . $self->{factoids}->hash->{$channel}->{$keyword}->{action} . "\n");
return "You must login to use this command."
}
$self->{factoids}->hash->{$channel}->{$keyword}->{ref_count}++;
$self->{factoids}->hash->{$channel}->{$keyword}->{ref_user} = "$nick!$user\@$host";
$self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_on} = gettimeofday;
$self->{factoids}->hash->{$channel}->{$keyword}->{last_referenced_in} = $from || "stdin";
$result = $self->{factoids}->hash->{$channel}->{$keyword}->{action};
if(length $arguments) { if(length $arguments) {
if(exists $self->{factoids}->hash->{$channel}->{$keyword}->{action_with_args}) { if(exists $self->{factoids}->hash->{$channel}->{$keyword}->{action_with_args}) {
$result = $self->{factoids}->hash->{$channel}->{$keyword}->{action_with_args}; $action = $self->{factoids}->hash->{$channel}->{$keyword}->{action_with_args};
} }
if(not $result =~ s/\$args/$arguments/gi and not exists $self->{factoids}->hash->{$channel}->{$keyword}->{action_with_args}) { my $newargs = $arguments;
# factoid doesn't take an argument, so assume argument is a nick if it is a single-word 20 characters or less
# TODO - maintain list of channel nicks and compare against this list to ensure nick exists if($action =~ m/\$args/i) {
if($arguments =~ /^[^.+-, ]{1,20}$/) { $newargs = "";
# might be a nick }
if($result =~ /^\/.+? /) {
$result =~ s/^(\/.+?) /$1 $arguments: /; if(not $action =~ s/\$args/$arguments/gi and not exists $self->{factoids}->hash->{$channel}->{$keyword}->{action_with_args} and $type eq 'text') {
if($self->{pbot}->{nicklist}->is_present($from, $arguments)) {
if($action =~ /^\/.+? /) {
$action =~ s/^(\/.+?) /$1 $arguments: /;
} else { } else {
$result =~ s/^/\/say $arguments: $keyword is / unless defined $tonick; $action =~ s/^/\/say $arguments: $keyword is / unless defined $tonick;
}
} else {
# return "";
} }
} }
}
$arguments = $newargs;
} else { } else {
# no arguments supplied # no arguments supplied
if(defined $tonick) { if(defined $tonick) {
$result =~ s/\$args/$tonick/gi; $action =~ s/\$args/$tonick/gi;
} else { } else {
$result =~ s/\$args/$nick/gi; $action =~ s/\$args/$nick/gi;
} }
} }
@ -505,25 +460,25 @@ sub interpreter {
my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick'); my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick');
# get rid of original caller's nick # get rid of original caller's nick
$result =~ s/^\/([^ ]+) \Q$nick\E:\s+/\/$1 /; $action =~ s/^\/([^ ]+) \Q$nick\E:\s+/\/$1 /;
$result =~ s/^\Q$nick\E:\s+//; $action =~ s/^\Q$nick\E:\s+//;
if($result =~ s/^\/say\s+//i || $result =~ s/^\/me\s+/* $botnick /i if($action =~ s/^\/say\s+//i || $action =~ s/^\/me\s+/* $botnick /i
|| $result =~ /^\/msg\s+/i) { || $action =~ /^\/msg\s+/i) {
$result = "/say $tonick: $result"; $action = "/say $tonick: $action";
} else { } else {
$result = "/say $tonick: $keyword is $result"; $action = "/say $tonick: $keyword is $action";
} }
$self->{pbot}->{logger}->log("result set to [$result]\n"); $self->{pbot}->{logger}->log("result set to [$action]\n");
} }
$self->{pbot}->{logger}->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: $keyword: Displaying text \"" . $result . "\"\n"); $self->{pbot}->{logger}->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: $keyword: Displaying text \"$action\"\n");
$result =~ s/\$nick/$nick/g; $action =~ s/\$nick/$nick/g;
$result =~ s/\$channel/$from/g; $action =~ s/\$channel/$from/g;
while ($result =~ /[^\\]\$([a-zA-Z0-9_\-]+)/g) { while ($action =~ /[^\\]\$([a-zA-Z0-9_\-]+)/g) {
#$self->{pbot}->{logger}->log("adlib: looking for [$1]\n"); #$self->{pbot}->{logger}->log("adlib: looking for [$1]\n");
#$self->{pbot}->{logger}->log("calling find_factoid in Factoids.pm, interpreter() to look for adlib"); #$self->{pbot}->{logger}->log("calling find_factoid in Factoids.pm, interpreter() to look for adlib");
my ($var_chan, $var) = $self->find_factoid($from, $1, undef, 0, 1); my ($var_chan, $var) = $self->find_factoid($from, $1, undef, 0, 1);
@ -539,28 +494,63 @@ sub interpreter {
} }
my $line = int(rand($#mylist + 1)); my $line = int(rand($#mylist + 1));
$mylist[$line] =~ s/"//g; $mylist[$line] =~ s/"//g;
$result =~ s/\$$var/$mylist[$line]/; $action =~ s/\$$var/$mylist[$line]/;
#$self->{pbot}->{logger}->log("adlib: found: change: $result\n"); #$self->{pbot}->{logger}->log("adlib: found: change: $action\n");
} else { } else {
$result =~ s/\$$var/$var/g; $action =~ s/\$$var/$var/g;
#$self->{pbot}->{logger}->log("adlib: not found: change: $result\n"); #$self->{pbot}->{logger}->log("adlib: not found: change: $action\n");
} }
} }
$result =~ s/\\\$/\$/g; $action =~ s/\\\$/\$/g;
# Check if it's an alias
if($action =~ /^\/call\s+(.*)$/) {
my $command;
if(length $arguments) {
$command = "$1 $arguments";
} else {
$command = $1;
}
$pbot->{logger}->log("[" . (defined $from ? $from : "stdin") . "] ($nick!$user\@$host) [$keyword] aliased to: [$command]\n");
return $pbot->{interpreter}->interpret($from, $nick, $user, $host, $count, $command, $tonick);
}
if($self->{factoids}->hash->{$channel}->{$keyword}->{enabled} == 0) {
$self->{pbot}->{logger}->log("$keyword disabled.\n");
return "/msg $nick $ref_from$keyword is currently disabled.";
}
if($self->{factoids}->hash->{$channel}->{$keyword}->{type} eq 'module') {
$self->{pbot}->{logger}->log("Found module\n");
my $preserve_whitespace = $self->{factoids}->hash->{$channel}->{$keyword}->{preserve_whitespace};
$preserve_whitespace = 0 if not defined $preserve_whitespace;
return $ref_from . $self->{factoidmodulelauncher}->execute_module($from, $tonick, $nick, $user, $host, "$keyword $arguments", $keyword, $arguments, $preserve_whitespace);
}
elsif($self->{factoids}->hash->{$channel}->{$keyword}->{type} eq 'text') {
$self->{pbot}->{logger}->log("Found factoid\n");
# Don't allow user-custom /msg factoids, unless factoid triggered by admin
if(($action =~ m/^\/msg/i) and (not $self->{pbot}->{admins}->loggedin($from, "$nick!$user\@$host"))) {
$self->{pbot}->{logger}->log("[ABUSE] Bad factoid (contains /msg): $action\n");
return "You must login to use this command."
}
if($ref_from) { if($ref_from) {
if($result =~ s/^\/say\s+/$ref_from/i || $result =~ s/^\/me\s+(.*)/\/me $1 $ref_from/i if($action =~ s/^\/say\s+/$ref_from/i || $action =~ s/^\/me\s+(.*)/\/me $1 $ref_from/i
|| $result =~ s/^\/msg\s+([^ ]+)/\/msg $1 $ref_from/i) { || $action =~ s/^\/msg\s+([^ ]+)/\/msg $1 $ref_from/i) {
return $result; return $action;
} else { } else {
return $ref_from . "$keyword is $result"; return $ref_from . "$keyword is $action";
} }
} else { } else {
if($result =~ m/^\/say/i || $result =~ m/^\/me/i || $result =~ m/^\/msg/i) { if($action =~ m/^\/say/i || $action =~ m/^\/me/i || $action =~ m/^\/msg/i) {
return $result; return $action;
} else { } else {
return "$keyword is $result"; return "$keyword is $action";
} }
} }
} elsif($self->{factoids}->hash->{$channel}->{$keyword}->{type} eq 'regex') { } elsif($self->{factoids}->hash->{$channel}->{$keyword}->{type} eq 'regex') {
@ -568,8 +558,8 @@ sub interpreter {
my $string = "$original_keyword" . (defined $arguments ? " $arguments" : ""); my $string = "$original_keyword" . (defined $arguments ? " $arguments" : "");
my $cmd; my $cmd;
if($string =~ m/$keyword/i) { if($string =~ m/$keyword/i) {
$self->{pbot}->{logger}->log("[$string] matches [$keyword] - calling [" . $self->{factoids}->hash->{$channel}->{$keyword}->{action} . "$']\n"); $self->{pbot}->{logger}->log("[$string] matches [$keyword] - calling [" . $action . "$']\n");
$cmd = $self->{factoids}->hash->{$channel}->{$keyword}->{action} . $'; $cmd = $action . $';
my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $before, $after) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $`, $'); 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/\$1/$a/g;
$cmd =~ s/\$2/$b/g; $cmd =~ s/\$2/$b/g;
@ -585,7 +575,7 @@ sub interpreter {
$cmd =~ s/^\s+//; $cmd =~ s/^\s+//;
$cmd =~ s/\s+$//; $cmd =~ s/\s+$//;
} else { } else {
$cmd = $self->{factoids}->hash->{$channel}->{$keyword}->{action}; $cmd = $action;
} }
$result = $pbot->{interpreter}->interpret($from, $nick, $user, $host, $count, $cmd, $tonick); $result = $pbot->{interpreter}->interpret($from, $nick, $user, $host, $count, $cmd, $tonick);
@ -602,10 +592,6 @@ sub interpreter {
$self->{pbot}->{logger}->log("($from): $nick!$user\@$host): Unknown command type for '$keyword'\n"); $self->{pbot}->{logger}->log("($from): $nick!$user\@$host): Unknown command type for '$keyword'\n");
return "/me blinks." . " $ref_from"; return "/me blinks." . " $ref_from";
} }
# should never be reached; if it has, something has gone horribly wrong.
# (advanced notification of corruption or a waste of space?)
return "/me wrinkles her nose." . " $ref_from";
} }
sub export_path { sub export_path {