From a0c798dd2af714e4455064469a2d21b8bf5b589d Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 6 Jul 2015 19:39:33 -0700 Subject: [PATCH] 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. --- PBot/Factoids.pm | 56 +++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index e15603e2..017d0fc8 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -330,6 +330,34 @@ sub find_factoid { return @result; } +sub expand_factoid_vars { + my ($self, $from, $action) = @_; + + while ($action =~ /(?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/(?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 =~ /(?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/(?expand_factoid_vars($from, $action); if($self->{factoids}->hash->{$channel}->{$keyword}->{enabled} == 0) { $self->{pbot}->{logger}->log("$keyword disabled.\n");