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

FactoidCommands: reinstate regex command

This commit is contained in:
Pragmatic Software 2020-03-03 07:41:07 -08:00
parent 3ff232decd
commit 0a034814ad

View File

@ -68,9 +68,6 @@ sub initialize {
$self->{pbot}->{commands}->register(sub { $self->top20(@_) }, "top20", 0); $self->{pbot}->{commands}->register(sub { $self->top20(@_) }, "top20", 0);
$self->{pbot}->{commands}->register(sub { $self->histogram(@_) }, "histogram", 0); $self->{pbot}->{commands}->register(sub { $self->histogram(@_) }, "histogram", 0);
$self->{pbot}->{commands}->register(sub { $self->count(@_) }, "count", 0); $self->{pbot}->{commands}->register(sub { $self->count(@_) }, "count", 0);
# the following commands have not yet been updated to use the new factoid structure
# DO NOT USE!! Factoid corruption may occur.
$self->{pbot}->{commands}->register(sub { $self->add_regex(@_) }, "regex", 1); $self->{pbot}->{commands}->register(sub { $self->add_regex(@_) }, "regex", 1);
} }
@ -736,32 +733,32 @@ sub factalias {
} }
sub add_regex { sub add_regex {
my $self = shift; my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
my ($from, $nick, $user, $host, $arguments) = @_; my ($keyword, $text) = $self->{pbot}->{interpreter}->split_args($stuff->{arglist}, 2);
my $factoids = $self->{pbot}->{factoids}->{factoids};
my ($keyword, $text) = $arguments =~ /^(.*?)\s+(.*)$/ if defined $arguments;
$from = '.*' if not defined $from or $from !~ /^#/; $from = '.*' if not defined $from or $from !~ /^#/;
if (not defined $keyword) { if (not defined $keyword) { return "Usage: regex <regex> <command> | regex <channel>"; }
$text = "";
foreach my $trigger (sort $factoids->get_keys($from)) {
if ($factoids->get_data($from, $trigger, 'type') eq 'regex') { $text .= $trigger . " "; }
}
return "Stored regexs for channel $from: $text";
}
if (not defined $text) { return "Usage: regex <regex> <command>"; } if (not defined $text) {
my @regexes;
my $iter = $self->{pbot}->{factoids}->{factoids}->get_each('type = regex', "index1 = $keyword", 'index2', '_sort = index2');
while (defined (my $factoid = $self->{pbot}->{factoids}->{factoids}->get_next($iter))) {
push @regexes, $factoid->{index2};
}
$text = join '; ', @regexes;
$text = 'none' if not length $text;
return "Regex factoids for channel $keyword: $text";
}
my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from, $keyword, exact_channel => 1, exact_trigger => 1); my ($channel, $trigger) = $self->{pbot}->{factoids}->find_factoid($from, $keyword, exact_channel => 1, exact_trigger => 1);
if (defined $trigger) { if (defined $trigger) {
$self->{pbot}->{logger}->log("$nick!$user\@$host attempt to overwrite $trigger\n");
return "/say $trigger already exists for channel $channel."; return "/say $trigger already exists for channel $channel.";
} }
$self->{pbot}->{factoids}->add_factoid('regex', $from, "$nick!$user\@$host", $keyword, $text); $self->{pbot}->{factoids}->add_factoid('regex', $from, "$nick!$user\@$host", $keyword, $text);
$self->{pbot}->{logger}->log("$nick!$user\@$host added [$keyword] => [$text]\n"); $self->{pbot}->{logger}->log("$nick!$user\@$host added regex [$keyword] => [$text]\n");
return "/say $keyword added."; return "/say $keyword added.";
} }