Expand factoid variables in /call statements

This allows us to call random factoids.

E.g.:

  factadd global fact1 is /say This is fact1.
  factadd global fact2 is /say Fact2 is also a pretty neat fact.
  factadd global fact3 is /say But don't forget about fact3!
  factadd global facts is fact1 fact2 fact3
  factadd global randomfact is /call $facts

Invoking the `randomfact` factoid will now randomy pick between
fact1, fact2 and fact3.

This was added to support the `randomkr` factoid.
This commit is contained in:
Pragmatic Software 2015-07-06 19:39:33 -07:00
parent 714f79fb2e
commit a0c798dd2a
1 changed files with 31 additions and 25 deletions

View File

@ -330,6 +330,34 @@ sub find_factoid {
return @result;
}
sub expand_factoid_vars {
my ($self, $from, $action) = @_;
while ($action =~ /(?<!\\)\$([a-zA-Z0-9_\-]+)/g) {
my $v = $1;
next if $v =~ m/^[0-9]+$/;
my ($var_chan, $var) = $self->find_factoid($from, $v, undef, 0, 1);
if(defined $var && $self->{factoids}->hash->{$var_chan}->{$var}->{type} eq 'text') {
my $change = $self->{factoids}->hash->{$var_chan}->{$var}->{action};
my @list = split(/\s|(".*?")/, $change);
my @mylist;
for(my $i = 0; $i <= $#list; $i++) {
push @mylist, $list[$i] if $list[$i];
}
my $line = int(rand($#mylist + 1));
$mylist[$line] =~ s/"//g;
$action =~ s/\$$var/$mylist[$line]/;
} else {
$action =~ s/(?<!\\)\$$var/$var/;
}
}
$action =~ s/\\\$/\$/g;
return $action;
}
sub expand_action_arguments {
my ($self, $action, $input, $nick) = @_;
@ -552,11 +580,9 @@ sub interpreter {
# Check if it's an alias
if($action =~ /^\/call\s+(.*)$/) {
my $command;
my $command = $self->expand_factoid_vars($from, $1);
if(length $arguments) {
$command = "$1 $arguments";
} else {
$command = $1;
$command = "$command $arguments";
}
$pbot->{logger}->log("[" . (defined $from ? $from : "stdin") . "] ($nick!$user\@$host) [$keyword] aliased to: [$command]\n");
@ -587,27 +613,7 @@ sub interpreter {
$action =~ s/\$channel/$from/g;
$action =~ s/\$randomnick/my $random = $self->{pbot}->{nicklist}->random_nick($from); $random ? $random : $nick/ge;
while ($action =~ /(?<!\\)\$([a-zA-Z0-9_\-]+)/g) {
my $v = $1;
next if $v =~ m/^[0-9]+$/;
my ($var_chan, $var) = $self->find_factoid($from, $v, undef, 0, 1);
if(defined $var && $self->{factoids}->hash->{$var_chan}->{$var}->{type} eq 'text') {
my $change = $self->{factoids}->hash->{$var_chan}->{$var}->{action};
my @list = split(/\s|(".*?")/, $change);
my @mylist;
for(my $i = 0; $i <= $#list; $i++) {
push @mylist, $list[$i] if $list[$i];
}
my $line = int(rand($#mylist + 1));
$mylist[$line] =~ s/"//g;
$action =~ s/\$$var/$mylist[$line]/;
} else {
$action =~ s/(?<!\\)\$$var/$var/;
}
}
$action =~ s/\\\$/\$/g;
$action = $self->expand_factoid_vars($from, $action);
if($self->{factoids}->hash->{$channel}->{$keyword}->{enabled} == 0) {
$self->{pbot}->{logger}->log("$keyword disabled.\n");