diff --git a/PBot/FactoidCommands.pm b/PBot/FactoidCommands.pm index 37c0df0c..603f2438 100644 --- a/PBot/FactoidCommands.pm +++ b/PBot/FactoidCommands.pm @@ -104,7 +104,7 @@ sub call_factoid { return "Usage: fact [arguments]"; } - my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($chan, $keyword, $args, 1, 1); + my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($chan, $keyword, arguments => $args, exact_channel => 1, exact_trigger => 1); if (not defined $trigger) { return "No such factoid '$keyword' exists for channel '$chan'"; @@ -168,14 +168,14 @@ sub log_factoid { } sub find_factoid_with_optional_channel { - my ($self, $from, $arguments, $command, $usage, $explicit, $exact_channel) = @_; - + my ($self, $from, $arguments, $command, %opts) = @_; + my $arglist = $self->{pbot}->{interpreter}->make_args($arguments); my ($from_chan, $from_trigger, $remaining_args) = $self->{pbot}->{interpreter}->split_args($arglist, 3); if (not defined $from_chan or (not defined $from_chan and not defined $from_trigger)) { - return "Usage: $command [channel] " if not $usage; - return $usage; + return "Usage: $command [channel] " if not $opts{usage}; + return $opts{usage}; } my $needs_disambig; @@ -202,15 +202,15 @@ sub find_factoid_with_optional_channel { my ($channel, $trigger); - if (defined $exact_channel and $exact_channel == 1) { - ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from_chan, $from_trigger, undef, 1, 1); + if ($opts{exact_channel} == 1) { + ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from_chan, $from_trigger, exact_channel => 1, exact_trigger => 1); if (not defined $channel) { $from_chan = 'the global channel' if $from_chan eq '.*'; return "/say $from_trigger not found in $from_chan."; } } else { - my @factoids = $self->{pbot}->{factoids}->find_factoid($from_chan, $from_trigger, undef, 0, 1); + my @factoids = $self->{pbot}->{factoids}->find_factoid($from_chan, $from_trigger, exact_trigger => 1); if (not @factoids or not $factoids[0]) { if ($needs_disambig) { @@ -223,7 +223,7 @@ sub find_factoid_with_optional_channel { if (@factoids > 1) { if ($needs_disambig or not grep { $_->[0] eq $from_chan } @factoids) { - unless ($explicit) { + unless ($opts{explicit}) { foreach my $factoid (@factoids) { if ($factoid->[0] eq '.*') { ($channel, $trigger) = ($factoid->[0], $factoid->[1]); @@ -249,7 +249,7 @@ sub find_factoid_with_optional_channel { $channel = '.*' if $channel eq 'global'; $from_chan = '.*' if $channel eq 'global'; - if ($explicit and $channel =~ /^#/ and $from_chan =~ /^#/ and $channel ne $from_chan) { + if ($opts{explicit} and $channel =~ /^#/ and $from_chan =~ /^#/ and $channel ne $from_chan) { return "/say $trigger belongs to $channel, not $from_chan. Please switch to or explicitly specify $channel."; } @@ -359,7 +359,7 @@ sub factundo { $arguments = join ' ', @$args; my $arglist = $self->{pbot}->{interpreter}->make_args($arguments); - my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factundo', undef, 1, 1); + my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factundo', explicit => 1, exact_channel => 1); my $deleted; if (not defined $trigger) { @@ -459,7 +459,7 @@ sub factredo { $arguments = join ' ', @$args; - my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factredo', undef, 1, 1); + my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factredo', explicit => 1, exact_channel => 1); return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message my $channel_path = $channel; @@ -529,14 +529,14 @@ sub factset { my $self = shift; my ($from, $nick, $user, $host, $args) = @_; - my ($channel, $trigger, $arguments) = $self->find_factoid_with_optional_channel($from, $args, 'factset', 'Usage: factset [channel] [key [value]]', 1); + my ($channel, $trigger, $arguments) = $self->find_factoid_with_optional_channel($from, $args, 'factset', usage => 'Usage: factset [channel] [key [value]]', explicit => 1); return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message my $arglist = $self->{pbot}->{interpreter}->make_args($arguments); my ($key, $value) = $self->{pbot}->{interpreter}->split_args($arglist, 2); $channel = '.*' if $channel !~ /^#/; - my ($owner_channel, $owner_trigger) = $self->{pbot}->{factoids}->find_factoid($channel, $trigger, undef, 1, 1); + my ($owner_channel, $owner_trigger) = $self->{pbot}->{factoids}->find_factoid($channel, $trigger, exact_channel => 1, exact_trigger => 1); my $admininfo; if (defined $owner_channel) { @@ -611,14 +611,14 @@ sub factunset { my $usage = 'Usage: factunset [channel] '; - my ($channel, $trigger, $arguments) = $self->find_factoid_with_optional_channel($from, $args, 'factunset', $usage, 1); + my ($channel, $trigger, $arguments) = $self->find_factoid_with_optional_channel($from, $args, 'factunset', usage => $usage, explicit => 1); return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message my ($key) = $self->{pbot}->{interpreter}->split_line($arguments, strip_quotes => 1); return $usage if not length $key; - my ($owner_channel, $owner_trigger) = $self->{pbot}->{factoids}->find_factoid($channel, $trigger, undef, 1, 1); + my ($owner_channel, $owner_trigger) = $self->{pbot}->{factoids}->find_factoid($channel, $trigger, exact_channel => 1, exact_trigger => 1); my $admininfo; @@ -774,7 +774,7 @@ sub factmove { return "/say $nick: I don't think the channel name needs to be that long."; } - my ($found_src_channel, $found_source) = $self->{pbot}->{factoids}->find_factoid($src_channel, $source, undef, 1, 1); + my ($found_src_channel, $found_source) = $self->{pbot}->{factoids}->find_factoid($src_channel, $source, exact_channel => 1, exact_trigger => 1); if (not defined $found_src_channel) { return "Source factoid $source not found in channel $src_channel"; @@ -794,13 +794,13 @@ sub factmove { return "/say $found_source is locked; unlock before moving."; } - my ($found_target_channel, $found_target) = $self->{pbot}->{factoids}->find_factoid($target_channel, $target, undef, 1, 1); + my ($found_target_channel, $found_target) = $self->{pbot}->{factoids}->find_factoid($target_channel, $target, exact_channel => 1, exact_trigger => 1); if (defined $found_target_channel) { return "Target factoid $target already exists in channel $target_channel"; } - my ($overchannel, $overtrigger) = $self->{pbot}->{factoids}->find_factoid('.*', $target, undef, 1, 1); + my ($overchannel, $overtrigger) = $self->{pbot}->{factoids}->find_factoid('.*', $target, exact_channel => 1, exact_trigger => 1); if (defined $overtrigger and $self->{pbot}->{factoids}->{factoids}->hash->{'.*'}->{$overtrigger}->{'nooverride'}) { $self->{pbot}->{logger}->log("$nick!$user\@$host attempt to override $target\n"); return "/say $target already exists for the global channel and cannot be overridden for " . ($target_channel eq '.*' ? 'the global channel' : $target_channel) . "."; @@ -863,14 +863,14 @@ sub factalias { return "/say $nick: I don't think the channel name needs to be that long."; } - my ($channel, $alias_trigger) = $self->{pbot}->{factoids}->find_factoid($chan, $alias, undef, 1, 1); + my ($channel, $alias_trigger) = $self->{pbot}->{factoids}->find_factoid($chan, $alias, exact_channel => 1, exact_trigger => 1); if (defined $alias_trigger) { $self->{pbot}->{logger}->log("attempt to overwrite existing command\n"); return "'$alias_trigger' already exists for channel $channel"; } - my ($overchannel, $overtrigger) = $self->{pbot}->{factoids}->find_factoid('.*', $alias, undef, 1, 1); + my ($overchannel, $overtrigger) = $self->{pbot}->{factoids}->find_factoid('.*', $alias, exact_channel => 1, exact_trigger => 1); if (defined $overtrigger and $self->{pbot}->{factoids}->{factoids}->hash->{'.*'}->{$overtrigger}->{'nooverride'}) { $self->{pbot}->{logger}->log("$nick!$user\@$host attempt to override $alias\n"); return "/say $alias already exists for the global channel and cannot be overridden for " . ($chan eq '.*' ? 'the global channel' : $chan) . "."; @@ -909,7 +909,7 @@ sub add_regex { return "Usage: regex "; } - my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from, $keyword, undef, 1, 1); + my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from, $keyword, exact_channel => 1, exact_trigger => 1); if (defined $trigger) { $self->{pbot}->{logger}->log("$nick!$user\@$host attempt to overwrite $trigger\n"); @@ -979,7 +979,7 @@ sub factadd { my $keyword_text = $keyword =~ / / ? "\"$keyword\"" : $keyword; - my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from_chan, $keyword, undef, 1, 1); + my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from_chan, $keyword, exact_channel => 1, exact_trigger => 1); if (defined $trigger) { if (not $force) { $self->{pbot}->{logger}->log("$nick!$user\@$host attempt to overwrite $keyword\n"); @@ -1001,7 +1001,7 @@ sub factadd { } } - ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid('.*', $keyword, undef, 1, 1); + ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid('.*', $keyword, exact_channel => 1, exact_trigger => 1); if (defined $trigger and $self->{pbot}->{factoids}->{factoids}->hash->{'.*'}->{$trigger}->{'nooverride'}) { $self->{pbot}->{logger}->log("$nick!$user\@$host attempt to override $keyword_text\n"); return "/say $keyword_text already exists for the global channel and cannot be overridden for " . ($from_chan eq '.*' ? 'the global channel' : $from_chan) . "."; @@ -1029,7 +1029,7 @@ sub factrem { $from_chan = $from; } - my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factrem', undef, 1); + my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factrem', explicit => 1); return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message $channel = '.*' if $channel eq 'global'; @@ -1149,7 +1149,7 @@ sub factlog { return "Too many arguments -- $usage" if @$args > 2; return "Missing argument -- $usage" if not @$args; - my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, "@$args", 'factlog', $usage, 0, 1); + my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, "@$args", 'factlog', usage => $usage, exact_channel => 1); if (not defined $trigger) { # factoid not found or some error, try to continue and load factlog file if it exists @@ -1508,7 +1508,7 @@ sub factchange { } my ($from_trigger, $from_chan) = ($keyword, $channel); - my @factoids = $self->{pbot}->{factoids}->find_factoid($from_chan, $keyword, undef, 0, 1); + my @factoids = $self->{pbot}->{factoids}->find_factoid($from_chan, $keyword, exact_trigger => 1); if (not @factoids or not $factoids[0]) { $from_chan = 'global channel' if $from_chan eq '.*'; diff --git a/PBot/FactoidModuleLauncher.pm b/PBot/FactoidModuleLauncher.pm index a3d83cb5..c9f6c989 100644 --- a/PBot/FactoidModuleLauncher.pm +++ b/PBot/FactoidModuleLauncher.pm @@ -56,7 +56,7 @@ sub execute_module { $stuff->{arguments} = "" if not defined $stuff->{arguments}; - my @factoids = $self->{pbot}->{factoids}->find_factoid($stuff->{from}, $stuff->{keyword}, undef, 2, 2); + my @factoids = $self->{pbot}->{factoids}->find_factoid($stuff->{from}, $stuff->{keyword}, exact_channel => 2, exact_trigger => 2); if (not @factoids or not $factoids[0]) { $stuff->{checkflood} = 1; diff --git a/PBot/Factoids.pm b/PBot/Factoids.pm index 9859946a..fbf8150e 100644 --- a/PBot/Factoids.pm +++ b/PBot/Factoids.pm @@ -269,11 +269,17 @@ sub export_factoids { } sub find_factoid { - my ($self, $from, $keyword, $arguments, $exact_channel, $exact_trigger, $find_alias) = @_; + my ($self, $from, $keyword, %opts) = @_; my $debug = 0; - $self->{pbot}->{logger}->log("find_factoid: from: [$from], kw: [$keyword], args: [" . (defined $arguments ? $arguments : "undef") . "], " . (defined $exact_channel ? $exact_channel : "undef") . ", " . (defined $exact_trigger ? $exact_trigger : "undef") . "\n") if $debug; + if ($debug) { + use Data::Dumper; + my $dump = Dumper \%opts; + $self->{pbot}->{logger}->log("find_factiod: from: $from, kw: $keyword, opts: $dump\n"); + } + + my $arguments = exists $opts{arguments} ? $opts{arguments} : ""; $from = '.*' if not defined $from or $from !~ /^#/; $from = lc $from; @@ -283,13 +289,13 @@ sub find_factoid { my @result = eval { my @results; for (my $depth = 0; $depth < 5; $depth++) { - my $string = $keyword . (defined $arguments ? " $arguments" : ""); + my $string = $keyword . (length $arguments ? " $arguments" : ""); $self->{pbot}->{logger}->log("string: $string\n") if $debug; return undef if $self->{pbot}->{commands}->exists($keyword); # check factoids foreach my $channel (sort keys %{ $self->{factoids}->hash }) { - if ($exact_channel) { - if (defined $exact_trigger && $exact_trigger == 1) { + if ($opts{exact_channel}) { + if ($opts{exact_trigger} == 1) { next unless $from eq lc $channel; } else { next unless $from eq lc $channel or $channel eq '.*'; @@ -300,7 +306,7 @@ sub find_factoid { if ($keyword =~ m/^\Q$trigger\E$/i) { $self->{pbot}->{logger}->log("return $channel: $trigger\n") if $debug; - if ($find_alias && $self->{factoids}->hash->{$channel}->{$trigger}->{action} =~ /^\/call\s+(.*)$/) { + if ($opts{find_alias} && $self->{factoids}->hash->{$channel}->{$trigger}->{action} =~ /^\/call\s+(.*)$/) { my $command; if (length $arguments) { $command = "$1 $arguments"; @@ -312,7 +318,7 @@ sub find_factoid { goto NEXT_DEPTH; } - if (defined $exact_channel && $exact_channel == 1) { + if ($opts{exact_channel} == 1) { return ($channel, $trigger); } else { push @results, [$channel, $trigger]; @@ -322,9 +328,9 @@ sub find_factoid { } # then check regex factoids - if (not $exact_trigger) { + if (not $opts{exact_trigger}) { foreach my $channel (sort keys %{ $self->{factoids}->hash }) { - if ($exact_channel) { + if ($opts{exact_channel}) { next unless $from eq lc $channel or $channel eq '.*'; } @@ -334,7 +340,7 @@ sub find_factoid { if ($string =~ m/$trigger/i) { $self->{pbot}->{logger}->log("return regex $channel: $trigger\n") if $debug; - if ($find_alias) { + if ($opts{find_alias}) { my $command = $self->{factoids}->hash->{$channel}->{$trigger}->{action}; my $arglist = $self->{pbot}->{interpreter}->make_args($command); ($keyword, $arguments) = $self->{pbot}->{interpreter}->split_args($arglist, 2); @@ -342,7 +348,7 @@ sub find_factoid { goto NEXT_DEPTH; } - if ($exact_channel == 1) { + if ($opts{exact_channel} == 1) { return ($channel, $trigger); } else { push @results, [$channel, $trigger]; @@ -354,7 +360,7 @@ sub find_factoid { } NEXT_DEPTH: - last if not $find_alias; + last if not $opts{find_alias}; } if ($debug) { @@ -454,7 +460,7 @@ sub expand_factoid_vars { my $recurse = 0; ALIAS: - my @factoids = $self->find_factoid($from, $test_v, undef, 2, 2); + my @factoids = $self->find_factoid($from, $test_v, exact_channel => 2, exact_trigger => 2); next if not @factoids or not $factoids[0]; my ($var_chan, $var) = ($factoids[0]->[0], $factoids[0]->[1]); @@ -750,8 +756,7 @@ sub interpreter { # search for factoid against global channel and current channel (from unless ref_from is defined) my $original_keyword = $stuff->{keyword}; - # $self->{pbot}->{logger}->log("calling find_factoid in Factoids.pm, interpreter()\n"); - my ($channel, $keyword) = $self->find_factoid($stuff->{ref_from} ? $stuff->{ref_from} : $stuff->{from}, $stuff->{keyword}, $stuff->{arguments}, 1); + my ($channel, $keyword) = $self->find_factoid($stuff->{ref_from} ? $stuff->{ref_from} : $stuff->{from}, $stuff->{keyword}, arguments => $stuff->{arguments}, exact_channel => 1); if (not $stuff->{ref_from} or $stuff->{ref_from} eq '.*' or $stuff->{ref_from} eq $stuff->{from}) { $stuff->{ref_from} = ""; @@ -928,7 +933,7 @@ sub handle_action { my $ref_from = $stuff->{ref_from} ? "[$stuff->{ref_from}] " : ""; unless (exists $self->{factoids}->hash->{$channel}->{$keyword}->{interpolate} and $self->{factoids}->hash->{$channel}->{$keyword}->{interpolate} eq '0') { - my ($root_channel, $root_keyword) = $self->find_factoid($stuff->{ref_from} ? $stuff->{ref_from} : $stuff->{from}, $stuff->{root_keyword}, $stuff->{arguments}, 1); + my ($root_channel, $root_keyword) = $self->find_factoid($stuff->{ref_from} ? $stuff->{ref_from} : $stuff->{from}, $stuff->{root_keyword}, arguments => $stuff->{arguments}, exact_channel => 1); if (not defined $root_channel or not defined $root_keyword) { $root_channel = $channel; $root_keyword = $keyword; @@ -1017,7 +1022,7 @@ sub handle_action { } unless (exists $self->{factoids}->hash->{$channel}->{$keyword}->{interpolate} and $self->{factoids}->hash->{$channel}->{$keyword}->{interpolate} eq '0') { - my ($root_channel, $root_keyword) = $self->find_factoid($stuff->{ref_from} ? $stuff->{ref_from} : $stuff->{from}, $stuff->{root_keyword}, $stuff->{arguments}, 1); + my ($root_channel, $root_keyword) = $self->find_factoid($stuff->{ref_from} ? $stuff->{ref_from} : $stuff->{from}, $stuff->{root_keyword}, arguments => $stuff->{arguments}, exact_channel => 1); if (not defined $root_channel or not defined $root_keyword) { $root_channel = $channel; $root_keyword = $keyword; diff --git a/PBot/Interpreter.pm b/PBot/Interpreter.pm index f1cfd1dc..b434dc71 100644 --- a/PBot/Interpreter.pm +++ b/PBot/Interpreter.pm @@ -815,7 +815,7 @@ sub handle_result { my $cmdlist = $self->make_args($stuff->{command}); my ($cmd, $args) = $self->split_args($cmdlist, 2); if (not $self->{pbot}->{commands}->exists($cmd)) { - my ($chan, $trigger) = $self->{pbot}->{factoids}->find_factoid($stuff->{from}, $cmd, $args, 1, 0, 1); + my ($chan, $trigger) = $self->{pbot}->{factoids}->find_factoid($stuff->{from}, $cmd, arguments => $args, exact_channel => 1, exact_trigger => 0, find_alias => 1); if (defined $trigger) { if ($stuff->{preserve_whitespace} == 0) { $stuff->{preserve_whitespace} = $self->{pbot}->{factoids}->{factoids}->hash->{$chan}->{$trigger}->{preserve_whitespace};