mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-29 15:29:32 +01:00
Regex factoids restored
This commit is contained in:
parent
a901a8a4c8
commit
0b78e4ed79
@ -104,7 +104,7 @@ sub join_channel {
|
||||
# FIXME -- update %channels hash?
|
||||
$self->{pbot}->logger->log("$nick!$user\@$host made me join $arguments\n");
|
||||
$self->{pbot}->conn->join($arguments);
|
||||
return "/msg $nick Joined $arguments";
|
||||
return "/msg $nick Joining $arguments";
|
||||
}
|
||||
|
||||
sub part_channel {
|
||||
@ -114,7 +114,7 @@ sub part_channel {
|
||||
# FIXME -- update %channels hash?
|
||||
$self->{pbot}->logger->log("$nick!$user\@$host made me part $arguments\n");
|
||||
$self->{pbot}->conn->part($arguments);
|
||||
return "/msg $nick Parted $arguments";
|
||||
return "/msg $nick Parting $arguments";
|
||||
}
|
||||
|
||||
sub ack_die {
|
||||
|
124
PBot/Factoids.pm
124
PBot/Factoids.pm
@ -210,19 +210,54 @@ sub export_factoids {
|
||||
return "$i factoids exported to " . $self->export_site;
|
||||
}
|
||||
|
||||
sub find_factoid {
|
||||
my ($self, $keyword, $arguments) = @_;
|
||||
|
||||
my $string = "$keyword" . (defined $arguments ? " $arguments" : "");
|
||||
|
||||
my $result = eval {
|
||||
foreach my $command (keys %{ $self->factoids }) {
|
||||
if(exists $self->factoids->{$command}{regex}) {
|
||||
if($string =~ m/$command/i) {
|
||||
return $command;
|
||||
}
|
||||
} else {
|
||||
my $command_quoted = quotemeta($command);
|
||||
if($keyword =~ m/^$command_quoted$/i) {
|
||||
return $command;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return undef;
|
||||
};
|
||||
|
||||
if($@) {
|
||||
$self->{pbot}->logger->log("find_factoid: bad regex: $@\n");
|
||||
return undef;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
sub interpreter {
|
||||
my $self = shift;
|
||||
my ($from, $nick, $user, $host, $count, $keyword, $arguments, $tonick) = @_;
|
||||
my $result;
|
||||
|
||||
$keyword = lc $keyword;
|
||||
|
||||
my $pbot = $self->{pbot};
|
||||
|
||||
my $string = "$keyword" . (defined $arguments ? " $arguments" : "");
|
||||
$keyword = $self->find_factoid($keyword, $arguments);
|
||||
return undef if not defined $keyword;
|
||||
|
||||
my $type;
|
||||
$type = 'text' if exists $self->factoids->{$keyword}{text};
|
||||
$type = 'regex' if exists $self->factoids->{$keyword}{regex};
|
||||
$type = 'module' if exists $self->factoids->{$keyword}{module};
|
||||
|
||||
# Check if it's an alias
|
||||
if(exists $self->factoids->{$keyword} and exists $self->factoids->{$keyword}{text}) {
|
||||
my $command;
|
||||
if($self->factoids->{$keyword}{text} =~ /^\/call\s+(.*)$/) {
|
||||
if($self->factoids->{$keyword}{$type} =~ /^\/call\s+(.*)$/) {
|
||||
if(defined $arguments) {
|
||||
$command = "$1 $arguments";
|
||||
} else {
|
||||
@ -235,18 +270,11 @@ sub interpreter {
|
||||
|
||||
return $pbot->interpreter->interpret($from, $nick, $user, $host, $count, $command);
|
||||
}
|
||||
}
|
||||
|
||||
foreach my $command (keys %{ $self->factoids }) {
|
||||
if($keyword eq lc $command) {
|
||||
|
||||
$self->{pbot}->logger->log("=======================\n");
|
||||
$self->{pbot}->logger->log("[$keyword] == [$command]\n");
|
||||
|
||||
if(${ $self->factoids }{$command}{enabled} == 0) {
|
||||
$self->{pbot}->logger->log("$command disabled.\n");
|
||||
return "$command is currently disabled.";
|
||||
} elsif(exists ${ $self->factoids }{$command}{module}) {
|
||||
if(${ $self->factoids }{$keyword}{enabled} == 0) {
|
||||
$self->{pbot}->logger->log("$keyword disabled.\n");
|
||||
return "/msg $nick $keyword is currently disabled.";
|
||||
} elsif(exists ${ $self->factoids }{$keyword}{module}) {
|
||||
$self->{pbot}->logger->log("Found module\n");
|
||||
|
||||
${ $self->factoids }{$keyword}{ref_count}++;
|
||||
@ -254,37 +282,37 @@ sub interpreter {
|
||||
|
||||
return $self->{factoidmodulelauncher}->execute_module($from, $tonick, $nick, $user, $host, $keyword, $arguments);
|
||||
}
|
||||
elsif(exists ${ $self->factoids }{$command}{text}) {
|
||||
elsif(exists ${ $self->factoids }{$keyword}{text}) {
|
||||
$self->{pbot}->logger->log("Found factoid\n");
|
||||
|
||||
# Don't allow user-custom /msg factoids, unless factoid triggered by admin
|
||||
if((${ $self->factoids }{$command}{text} =~ m/^\/msg/i) and (not $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host"))) {
|
||||
$self->{pbot}->logger->log("[HACK] Bad factoid (contains /msg): ${ $self->factoids }{$command}{text}\n");
|
||||
if((${ $self->factoids }{$keyword}{text} =~ m/^\/msg/i) and (not $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host"))) {
|
||||
$self->{pbot}->logger->log("[HACK] Bad factoid (contains /msg): ${ $self->factoids }{$keyword}{text}\n");
|
||||
return "You must login to use this command."
|
||||
}
|
||||
|
||||
${ $self->factoids }{$command}{ref_count}++;
|
||||
${ $self->factoids }{$command}{ref_user} = $nick;
|
||||
${ $self->factoids }{$keyword}{ref_count}++;
|
||||
${ $self->factoids }{$keyword}{ref_user} = $nick;
|
||||
|
||||
$self->{pbot}->logger->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host): $command: Displaying text \"${ $self->factoids }{$command}{text}\"\n");
|
||||
$self->{pbot}->logger->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host): $keyword: Displaying text \"${ $self->factoids }{$keyword}{text}\"\n");
|
||||
|
||||
if(defined $tonick) { # !tell foo about bar
|
||||
$self->{pbot}->logger->log("($from): $nick!$user\@$host) sent to $tonick\n");
|
||||
my $fromnick = $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host") ? "" : "$nick wants you to know: ";
|
||||
$result = ${ $self->factoids }{$command}{text};
|
||||
$result = ${ $self->factoids }{$keyword}{text};
|
||||
|
||||
my $botnick = "wtf"; # FIXME: wtf
|
||||
my $botnick = $self->{pbot}->botnick;
|
||||
|
||||
if($result =~ s/^\/say\s+//i || $result =~ s/^\/me\s+/* $botnick /i
|
||||
|| $result =~ /^\/msg\s+/i) {
|
||||
$result = "/msg $tonick $fromnick$result";
|
||||
} else {
|
||||
$result = "/msg $tonick $fromnick$command is $result";
|
||||
$result = "/msg $tonick $fromnick$keyword is $result";
|
||||
}
|
||||
|
||||
$self->{pbot}->logger->log("text set to [$result]\n");
|
||||
} else {
|
||||
$result = ${ $self->factoids }{$command}{text};
|
||||
$result = ${ $self->factoids }{$keyword}{text};
|
||||
}
|
||||
|
||||
if(defined $arguments) {
|
||||
@ -300,11 +328,11 @@ sub interpreter {
|
||||
if($result =~ /^\/.+? /) {
|
||||
$result =~ s/^(\/.+?) /$1 $arguments: /;
|
||||
} else {
|
||||
$result =~ s/^/\/say $arguments: $command is / unless (defined $tonick);
|
||||
$result =~ s/^/\/say $arguments: $keyword is / unless (defined $tonick);
|
||||
}
|
||||
} else {
|
||||
if($result !~ /^\/.+? /) {
|
||||
$result =~ s/^/\/say $command is / unless (defined $tonick);
|
||||
$result =~ s/^/\/say $keyword is / unless (defined $tonick);
|
||||
}
|
||||
}
|
||||
$self->{pbot}->logger->log("updated text: [$result]\n");
|
||||
@ -342,39 +370,17 @@ sub interpreter {
|
||||
|
||||
$result =~ s/\\\$/\$/g;
|
||||
|
||||
# $self->{pbot}->logger->log("finally... [$result]\n");
|
||||
if($result =~ s/^\/say\s+//i || $result =~ /^\/me\s+/i
|
||||
|| $result =~ /^\/msg\s+/i) {
|
||||
# $self->{pbot}->logger->log("ret1\n");
|
||||
return $result;
|
||||
} else {
|
||||
# $self->{pbot}->logger->log("ret2\n");
|
||||
return "$command is $result";
|
||||
return "$keyword is $result";
|
||||
}
|
||||
|
||||
$self->{pbot}->logger->log("unknown3: [$result]\n");
|
||||
} else {
|
||||
$self->{pbot}->logger->log("($from): $nick!$user\@$host): Unknown command type for '$command'\n");
|
||||
return "/me blinks.";
|
||||
}
|
||||
$self->{pbot}->logger->log("unknown4: [$result]\n");
|
||||
} # else no match
|
||||
} # end foreach
|
||||
|
||||
#$self->{pbot}->logger->log("Checking regex factoids\n");
|
||||
|
||||
# Otherwise, the command was not found.
|
||||
# Lets try regexp factoids ...
|
||||
my $string = "$keyword" . (defined $arguments ? " $arguments" : "");
|
||||
|
||||
foreach my $command (sort keys %{ $self->factoids }) {
|
||||
if(exists ${ $self->factoids }{$command}{regex}) {
|
||||
eval {
|
||||
my $regex = qr/$command/i;
|
||||
# $self->{pbot}->logger->log("testing $string =~ $regex\n");
|
||||
if($string =~ $regex) {
|
||||
$self->{pbot}->logger->log("[$string] matches [$command][$regex] - calling [" . ${ $self->factoids }{$command}{regex}. "$']\n");
|
||||
my $cmd = "${ $self->factoids }{$command}{regex}$'";
|
||||
} elsif(exists ${ $self->factoids }{$keyword}{regex}) {
|
||||
$result = eval {
|
||||
if($string =~ m/$keyword/i) {
|
||||
$self->{pbot}->logger->log("[$string] matches [$keyword] - calling [" . ${ $self->factoids }{$keyword}{regex}. "$']\n");
|
||||
my $cmd = "${ $self->factoids }{$keyword}{regex}$'";
|
||||
my $a = $1;
|
||||
my $b = $2;
|
||||
my $c = $3;
|
||||
@ -407,7 +413,11 @@ sub interpreter {
|
||||
$self->{pbot}->logger->log("Regex fail: $@\n");
|
||||
return "/msg $nick Fail.";
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
} else {
|
||||
$self->{pbot}->logger->log("($from): $nick!$user\@$host): Unknown command type for '$keyword'\n");
|
||||
return "/me blinks.";
|
||||
}
|
||||
return undef;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user