3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 22:09:26 +01:00

Make channel intelligently optional for more fact* commands

This commit is contained in:
Pragmatic Software 2017-08-27 00:56:55 -07:00
parent fb671ab4de
commit cb37eb1ce7

View File

@ -155,11 +155,12 @@ sub log_factoid {
} }
sub find_factoid_with_optional_channel { sub find_factoid_with_optional_channel {
my ($self, $from, $arguments, $command) = @_; my ($self, $from, $arguments, $command, $usage) = @_;
my ($from_chan, $from_trigger) = split / /, $arguments; my ($from_chan, $from_trigger, $remaining_args) = split / /, $arguments, 3;
if(not defined $from_chan and not defined $from_trigger) { if(not defined $from_chan and not defined $from_trigger) {
return "Usage: $command [channel] <keyword>"; return "Usage: $command [channel] <keyword>" if not $usage;
return $usage;
} }
my $needs_disambig; my $needs_disambig;
@ -167,6 +168,7 @@ sub find_factoid_with_optional_channel {
if (not defined $from_trigger) { if (not defined $from_trigger) {
$from_trigger = $from_chan; $from_trigger = $from_chan;
$from_chan = $from; $from_chan = $from;
$remaining_args = "";
#$needs_disambig = 1; #$needs_disambig = 1;
} }
@ -209,7 +211,7 @@ sub find_factoid_with_optional_channel {
return "$trigger belongs to $channel, not $from_chan. Please switch to or explicitly specify $channel."; return "$trigger belongs to $channel, not $from_chan. Please switch to or explicitly specify $channel.";
} }
return ($channel, $trigger); return ($channel, $trigger, $remaining_args);
} }
sub factundo { sub factundo {
@ -219,7 +221,6 @@ sub factundo {
my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factundo'); my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factundo');
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
my $channel_path = $channel; my $channel_path = $channel;
$channel_path = 'global' if $channel_path eq '.*'; $channel_path = 'global' if $channel_path eq '.*';
@ -264,7 +265,6 @@ sub factredo {
return "There are no more redos reamining for [$channel] $trigger."; return "There are no more redos reamining for [$channel] $trigger.";
} }
$undos->{idx}++; $undos->{idx}++;
store $undos, "$path/$trigger.$channel_path.undo"; store $undos, "$path/$trigger.$channel_path.undo";
@ -276,13 +276,31 @@ sub factredo {
sub factset { sub factset {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $args) = @_;
my ($channel, $trigger, $key, $value) = split / /, $arguments, 4 if defined $arguments;
if(not defined $channel or not defined $trigger) { my ($chan, $trig, $rest) = split / /, $args, 3;
return "Usage: factset <channel> <factoid> [key [value]]";
if (not defined $trig and defined $chan) {
$trig = $chan;
$chan = $from;
} }
if (defined $chan) {
if ($chan eq $nick) {
$args = $trig;
} elsif ($chan =~ /(?:^#|^\.\*$)/) {
$args = "$chan $trig";
} else {
$args = "$from $chan $trig";
}
$args .= " $rest" if defined $rest;
}
my ($channel, $trigger, $arguments) = $self->find_factoid_with_optional_channel($from, $args, 'factset', 'Usage: factset <channel> <factoid> [key [value]]');
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
my ($key, $value) = split / /, $arguments, 2;
$channel = '.*' if $channel !~ /^#/; $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, undef, 1, 1);
@ -742,51 +760,15 @@ sub factrem {
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->{factoids}->{factoids}->hash; my $factoids = $self->{pbot}->{factoids}->{factoids}->hash;
my ($from_chan, $from_trigger) = split / /, $arguments; my ($from_chan, $from_trig) = split / /, $arguments;
if(not defined $from_chan and not defined $from_trigger) { if (not defined $from_trig) {
return "Usage: factrem [channel] <keyword>"; $from_trig = $from_chan;
}
my $needs_disambig;
if (not defined $from_trigger) {
$from_trigger = $from_chan;
$from_chan = $from; $from_chan = $from;
#$needs_disambig = 1;
} }
$from_chan = '.*' if $from_chan !~ /^#/; my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factrem');
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
$from_chan = lc $from_chan;
my @factoids = $self->{pbot}->{factoids}->find_factoid($from_chan, $from_trigger, undef, 0, 1);
if (not @factoids or not $factoids[0]) {
if ($needs_disambig) {
return "$from_trigger not found";
} else {
$from_chan = 'global channel' if $from_chan eq '.*';
return "$from_trigger not found in $from_chan";
}
}
my ($channel, $trigger);
if (@factoids > 1) {
if ($needs_disambig or not grep { $_->[0] eq $from_chan } @factoids) {
return "$from_trigger found in multiple channels: " . (join ', ', sort map { $_->[0] eq '.*' ? 'global' : $_->[0] } @factoids) . "; use `factrem <channel> $from_trigger` to disambiguate.";
} else {
foreach my $factoid (@factoids) {
if ($factoid->[0] eq $from_chan) {
($channel, $trigger) = ($factoid->[0], $factoid->[1]);
last;
}
}
}
} else {
($channel, $trigger) = ($factoids[0]->[0], $factoids[0]->[1]);
}
$channel = '.*' if $channel eq 'global'; $channel = '.*' if $channel eq 'global';
$from_chan = '.*' if $channel eq 'global'; $from_chan = '.*' if $channel eq 'global';
@ -853,49 +835,13 @@ sub factshow {
my ($chan, $trig) = split / /, $arguments; my ($chan, $trig) = split / /, $arguments;
if(not defined $chan and not defined $trig) {
return "Usage: factshow [channel] <trigger>";
}
my $needs_disambig;
if (not defined $trig) { if (not defined $trig) {
$trig = $chan; $trig = $chan;
$chan = $from; $chan = $from;
$needs_disambig = 1;
} }
$chan = '.*' if $chan !~ /^#/; my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factshow');
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
$chan = lc $chan;
my @factoids = $self->{pbot}->{factoids}->find_factoid($chan, $trig, undef, 0, 1);
if (not @factoids or not $factoids[0]) {
if ($needs_disambig) {
return "$trig not found";
} else {
$chan = 'global channel' if $chan eq '.*';
return "$trig not found in $chan";
}
}
my ($channel, $trigger);
if (@factoids > 1) {
if ($needs_disambig or not grep { $_->[0] eq $chan } @factoids) {
return "$trig found in multiple channels: " . (join ', ', sort map { $_->[0] eq '.*' ? 'global' : $_->[0] } @factoids) . "; use `factshow <channel> $trig` to disambiguate.";
} else {
foreach my $factoid (@factoids) {
if ($factoid->[0] eq $chan) {
($channel, $trigger) = ($factoid->[0], $factoid->[1]);
last;
}
}
}
} else {
($channel, $trigger) = ($factoids[0]->[0], $factoids[0]->[1]);
}
my $result = "$trigger: " . $factoids->{$channel}->{$trigger}->{action}; my $result = "$trigger: " . $factoids->{$channel}->{$trigger}->{action};
@ -934,48 +880,8 @@ sub factlog {
return "Too many arguments -- $usage" if @$args > 2; return "Too many arguments -- $usage" if @$args > 2;
return "Missing argument -- $usage" if not @$args; return "Missing argument -- $usage" if not @$args;
my ($chan, $trig) = (@$args[0], @$args[1]); my ($channel, $trigger) = $self->find_factoid_with_optional_channel(@$args[0], @$args[1], 'factlog', $usage);
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
if(not defined $chan and not defined $trig) {
return $usage;
}
my $needs_disambig = 0;
if (not defined $trig) {
$trig = $chan;
$chan = '.*';
$needs_disambig = 1;
}
$chan = '.*' if $chan eq 'global';
$chan = lc $chan;
my @factoids = $self->{pbot}->{factoids}->find_factoid($chan, $trig, undef, 0, 1);
my ($channel, $trigger);
if (@factoids > 1) {
if ($needs_disambig or not grep { $_->[0] eq $chan } @factoids) {
return "$trig found in multiple channels: " . (join ', ', sort map { $_->[0] eq '.*' ? 'global' : $_->[0] } @factoids) . "; use `factlog <channel> $trig` to disambiguate.";
} else {
foreach my $factoid (@factoids) {
if ($factoid->[0] eq $chan) {
($channel, $trigger) = ($factoid->[0], $factoid->[1]);
last;
}
}
}
} elsif (@factoids and $factoids[0]) {
if ($chan eq '.*') {
($channel, $trigger) = ($factoids[0]->[0], $factoids[0]->[1]);
} else {
($channel, $trigger) = ($chan, $trig);
}
} else {
($channel, $trigger) = ($chan, $trig);
}
my $result; my $result;
my $path = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/factlog'; my $path = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/factlog';
@ -1014,49 +920,13 @@ sub factinfo {
my ($chan, $trig) = split / /, $arguments; my ($chan, $trig) = split / /, $arguments;
if(not defined $chan and not defined $trig) {
return "Usage: factinfo [channel] <trigger>";
}
my $needs_disambig;
if (not defined $trig) { if (not defined $trig) {
$trig = $chan; $trig = $chan;
$chan = '.*'; $chan = $from;
$needs_disambig = 1;
} }
$chan = '.*' if $chan eq 'global'; my ($channel, $trigger) = $self->find_factoid_with_optional_channel($from, $arguments, 'factinfo');
return $channel if not defined $trigger; # if $trigger is not defined, $channel is an error message
$chan = lc $chan;
my @factoids = $self->{pbot}->{factoids}->find_factoid($chan, $trig, undef, 0, 1);
if (not @factoids or not $factoids[0]) {
if ($needs_disambig) {
return "$trig not found";
} else {
$chan = 'global channel' if $chan eq '.*';
return "$trig not found in $chan";
}
}
my ($channel, $trigger);
if (@factoids > 1) {
if ($needs_disambig or not grep { $_->[0] eq $chan } @factoids) {
return "$trig found in multiple channels: " . (join ', ', sort map { $_->[0] eq '.*' ? 'global' : $_->[0] } @factoids) . "; use `factinfo <channel> $trig` to disambiguate.";
} else {
foreach my $factoid (@factoids) {
if ($factoid->[0] eq $chan) {
($channel, $trigger) = ($factoid->[0], $factoid->[1]);
last;
}
}
}
} else {
($channel, $trigger) = ($factoids[0]->[0], $factoids[0]->[1]);
}
my $created_ago = ago(gettimeofday - $factoids->{$channel}->{$trigger}->{created_on}); my $created_ago = ago(gettimeofday - $factoids->{$channel}->{$trigger}->{created_on});
my $ref_ago = ago(gettimeofday - $factoids->{$channel}->{$trigger}->{last_referenced_on}) if defined $factoids->{$channel}->{$trigger}->{last_referenced_on}; my $ref_ago = ago(gettimeofday - $factoids->{$channel}->{$trigger}->{last_referenced_on}) if defined $factoids->{$channel}->{$trigger}->{last_referenced_on};