3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-29 15:29:32 +01:00

Refactored Factoids to use DualIndexHashObject; added factset/factunset to set factoid meta-data; factoids now per-channel; slightly unfinished

This commit is contained in:
Pragmatic Software 2010-06-20 06:16:48 +00:00
parent 0bbd3a469c
commit 4be7fe3b34
7 changed files with 2776 additions and 2797 deletions

View File

@ -3,8 +3,6 @@
# #
# Purpose: Administrative command subroutines. # Purpose: Administrative command subroutines.
# TODO: Add getter for factoids instead of directly accessing factoids
package PBot::FactoidCommands; package PBot::FactoidCommands;
use warnings; use warnings;
@ -55,12 +53,37 @@ sub initialize {
$pbot->commands->register(sub { return $self->unload_module(@_) }, "unload", 50); $pbot->commands->register(sub { return $self->unload_module(@_) }, "unload", 50);
$pbot->commands->register(sub { return $self->enable_command(@_) }, "enable", 10); $pbot->commands->register(sub { return $self->enable_command(@_) }, "enable", 10);
$pbot->commands->register(sub { return $self->disable_command(@_) }, "disable", 10); $pbot->commands->register(sub { return $self->disable_command(@_) }, "disable", 10);
$pbot->commands->register(sub { return $self->factset(@_) }, "factset", 10);
$pbot->commands->register(sub { return $self->factunset(@_) }, "factunset", 10);
}
sub factset {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my ($channel, $trigger, $key, $value) = split / /, $arguments, 4 if defined $arguments;
if(not defined $channel or not defined $trigger) {
return "/msg $nick Usage: factset <channel> <factoid> [key <value>]"
}
return "/msg $nick " . $self->{pbot}->factoids->factoids->set($channel, $trigger, $key, $value);
}
sub factunset {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my ($channel, $trigger, $key) = split / /, $arguments, 3 if defined $arguments;
if(not defined $channel or not defined $trigger) {
return "/msg $nick Usage: factunset <channel> <factoid> <key>"
}
return "/msg $nick " . $self->{pbot}->factoids->factoids->unset($channel, $trigger, $key);
} }
sub list { sub list {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my $botnick = $self->{pbot}->botnick; my $botnick = $self->{pbot}->botnick;
my $text; my $text;
@ -108,10 +131,13 @@ sub list {
} }
if($arguments =~ /^modules$/i) { if($arguments =~ /^modules$/i) {
$text = "Loaded modules: "; $from = '.*' if not defined $from;
foreach my $command (sort keys %{ $factoids }) { $text = "Loaded modules for channel $from: ";
if(exists $factoids->{$command}{module}) { foreach my $channel (sort keys %{ $self->{pbot}->factoids->factoids->hash }) {
$text .= "$command "; foreach my $command (sort keys %{ $self->{pbot}->factoids->factoids->hash->{$channel} }) {
if($self->{pbot}->factoids->factoids->hash->{$channel}->{$command}->{type} eq 'module') {
$text .= "$command ";
}
} }
} }
return $text; return $text;
@ -156,103 +182,118 @@ sub list {
sub alias { sub alias {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my ($alias, $command) = $arguments =~ /^(.*?)\s+(.*)$/ if defined $arguments; my ($alias, $command) = $arguments =~ /^(.*?)\s+(.*)$/ if defined $arguments;
if(not defined $command) { if(not defined $command) {
$self->{pbot}->logger->log("alias: invalid usage\n");
return "/msg $nick Usage: alias <keyword> <command>"; return "/msg $nick Usage: alias <keyword> <command>";
} }
$from = '.*' if not defined $from;
my ($channel, $alias_trigger) = $self->{pbot}->factoids->find_factoid($from, $alias);
if(exists $factoids->{$alias}) { if(defined $alias_trigger) {
$self->{pbot}->logger->log("attempt to overwrite existing command\n"); $self->{pbot}->logger->log("attempt to overwrite existing command\n");
return "/msg $nick '$alias' already exists"; return "/msg $nick '$alias_trigger' already exists for channel $channel";
} }
$factoids->{$alias}{text} = "/call $command"; $self->{pbot}->factoids->add_factoid('text', $from, $nick, $alias, "/call $command");
$factoids->{$alias}{owner} = $nick;
$factoids->{$alias}{created_on} = time(); $self->{pbot}->logger->log("$nick!$user\@$host [$from] aliased $alias => $command\n");
$factoids->{$alias}{enabled} = 1;
$factoids->{$alias}{ref_count} = 0;
$factoids->{$alias}{ref_user} = "nobody";
$self->{pbot}->logger->log("$nick!$user\@$host aliased $alias => $command\n");
$self->{pbot}->factoids->save_factoids(); $self->{pbot}->factoids->save_factoids();
return "/msg $nick '$alias' aliases '$command'"; return "/msg $nick '$alias' aliases '$command' for channel $from";
} }
sub add_regex { sub add_regex {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids; my $factoids = $self->{pbot}->factoids->factoids->hash;
my ($keyword, $text) = $arguments =~ /^(.*?)\s+(.*)$/ if defined $arguments; my ($keyword, $text) = $arguments =~ /^(.*?)\s+(.*)$/ if defined $arguments;
$from = '.*' if not defined $from;
if(not defined $keyword) { if(not defined $keyword) {
$text = ""; $text = "";
foreach my $command (sort keys %{ $factoids }) { foreach my $trigger (sort keys %{ $factoids->{$from} }) {
if(exists $factoids->{$command}{regex}) { if($factoids->{$from}->{$trigger}->{type} eq 'regex') {
$text .= $command . " "; $text .= $trigger . " ";
} }
} }
return "Stored regexs: $text"; return "Stored regexs for channel $from: $text";
} }
if(not defined $text) { if(not defined $text) {
$self->{pbot}->logger->log("add_regex: invalid usage\n");
return "/msg $nick Usage: regex <regex> <command>"; return "/msg $nick Usage: regex <regex> <command>";
} }
if(exists $factoids->{$keyword}) { my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $keyword);
$self->{pbot}->logger->log("$nick!$user\@$host attempt to overwrite $keyword\n");
return "/msg $nick $keyword already exists."; if(defined $trigger) {
$self->{pbot}->logger->log("$nick!$user\@$host attempt to overwrite $trigger\n");
return "/msg $nick $trigger already exists for channel $channel.";
} }
$factoids->{$keyword}{regex} = $text; $self->{pbot}->factoids->add_factoid('regex', $from, $nick, $keyword, $text);
$factoids->{$keyword}{owner} = $nick;
$factoids->{$keyword}{created_on} = time();
$factoids->{$keyword}{enabled} = 1;
$factoids->{$keyword}{ref_count} = 0;
$factoids->{$keyword}{ref_user} = "nobody";
$self->{pbot}->logger->log("$nick!$user\@$host added [$keyword] => [$text]\n"); $self->{pbot}->logger->log("$nick!$user\@$host added [$keyword] => [$text]\n");
$self->{pbot}->factoids->save_factoids();
return "/msg $nick $keyword added."; return "/msg $nick $keyword added.";
} }
sub add_text { sub add_text {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my ($keyword, $text) = $arguments =~ /^(.*?)\s+is\s+(.*)$/i if defined $arguments; my ($keyword, $text) = $arguments =~ /^(.*?)\s+is\s+(.*)$/i if defined $arguments;
if(not defined $text) { if(not defined $text or not defined $keyword) {
$self->{pbot}->logger->log("add_text: invalid usage\n");
return "/msg $nick Usage: add <keyword> is <factoid>"; return "/msg $nick Usage: add <keyword> is <factoid>";
} }
if(not defined $keyword) { $from = '.*' if not defined $from;
$self->{pbot}->logger->log("add_text: invalid usage\n");
return "/msg $nick Usage: add <keyword> is <factoid>";
}
if(exists $factoids->{$keyword}) { my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $keyword);
if(defined $trigger) {
$self->{pbot}->logger->log("$nick!$user\@$host attempt to overwrite $keyword\n"); $self->{pbot}->logger->log("$nick!$user\@$host attempt to overwrite $keyword\n");
return undef; return undef;
return "/msg $nick $keyword already exists."; return "/msg $nick $keyword already exists.";
} }
$factoids->{$keyword}{text} = $text; $self->{pbot}->factoids->add_factoid('text', $from, $nick, $keyword, $text);
$factoids->{$keyword}{owner} = $nick;
$factoids->{$keyword}{created_on} = time();
$factoids->{$keyword}{enabled} = 1;
$factoids->{$keyword}{ref_count} = 0;
$factoids->{$keyword}{ref_user} = "nobody";
$self->{pbot}->logger->log("$nick!$user\@$host added $keyword => $text\n"); $self->{pbot}->logger->log("$nick!$user\@$host added $keyword => $text\n");
$self->{pbot}->factoids->save_factoids();
return "/msg $nick '$keyword' added."; return "/msg $nick '$keyword' added.";
} }
sub remove_text {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids->hash;
$from = '.*' if not defined $from;
if(not defined $arguments) {
return "/msg $nick Usage: remove <keyword>";
}
my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $arguments);
if(not defined $trigger) {
return "/msg $nick $arguments not found in channel $from.";
}
if($factoids->{$channel}->{$trigger}->{type} eq 'module') {
$self->{pbot}->logger->log("$nick!$user\@$host attempted to remove $trigger [not factoid]\n");
return "/msg $nick $trigger is not a factoid.";
}
if(($nick ne $factoids->{$channel}->{$trigger}->{owner}) and (not $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host"))) {
$self->{pbot}->logger->log("$nick!$user\@$host attempted to remove $trigger [not owner]\n");
return "/msg $nick You are not the owner of '$trigger'";
}
$self->{pbot}->logger->log("$nick!$user\@$host removed [$channel][$trigger][" . $factoids->{$channel}->{$trigger}->{action} . "]\n");
$self->{pbot}->factoids->remove_factoid($channel, $trigger);
return "/msg $nick $trigger removed from channel $channel.";
}
sub histogram { sub histogram {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
@ -283,55 +324,59 @@ sub histogram {
sub show { sub show {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids; my $factoids = $self->{pbot}->factoids->factoids->hash;
if(not defined $arguments) { if(not defined $arguments) {
return "/msg $nick Usage: show <factoid>"; return "/msg $nick Usage: show <factoid>";
} }
if(not exists $factoids->{$arguments}) { $from = '.*' if not defined $from;
return "/msg $nick $arguments not found"; my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $arguments);
if(not defined $trigger) {
return "/msg $nick $arguments not found in channel $from";
} }
if(exists $factoids->{$arguments}{command} || exists $factoids->{$arguments}{module}) { if($factoids->{$channel}->{$trigger}->{type} eq 'module') {
return "/msg $nick $arguments is not a factoid"; return "/msg $nick $trigger is not a factoid";
} }
my $type; return "$trigger: " . $factoids->{$channel}->{$trigger}->{action};
$type = 'text' if exists $factoids->{$arguments}{text};
$type = 'regex' if exists $factoids->{$arguments}{regex};
return "$arguments: $factoids->{$arguments}{$type}";
} }
sub info { sub info {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids; my $factoids = $self->{pbot}->factoids->factoids->hash;
if(not defined $arguments) { if(not defined $arguments) {
return "/msg $nick Usage: info <factoid|module>"; return "/msg $nick Usage: info <factoid|module>";
} }
if(not exists $factoids->{$arguments}) { $from = '.*' if not defined $from;
my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $arguments);
if(not defined $trigger) {
return "/msg $nick $arguments not found"; return "/msg $nick $arguments not found";
} }
# factoid # factoid
if(exists $factoids->{$arguments}{text}) { if($factoids->{$channel}->{$trigger}->{type} eq 'text') {
return "$arguments: Factoid submitted by $factoids->{$arguments}{owner} on " . localtime($factoids->{$arguments}{created_on}) . ", referenced $factoids->{$arguments}{ref_count} times (last by $factoids->{$arguments}{ref_user})"; return "$trigger: Factoid submitted by " . $factoids->{$channel}->{$trigger}->{owner} . " on " . localtime($factoids->{$channel}->{$trigger}->{created_on}) . ", referenced " . $factoids->{$channel}->{$trigger}->{ref_count} . " times (last by " . $factoids->{$channel}->{$trigger}->{ref_user} . ")";
} }
# module # module
if(exists $factoids->{$arguments}{module}) { if($factoids->{$channel}->{$trigger}->{type} eq 'module') {
return "$arguments: Module loaded by $factoids->{$arguments}{owner} on " . localtime($factoids->{$arguments}{created_on}) . " -> http://code.google.com/p/pbot2-pl/source/browse/trunk/modules/$factoids->{$arguments}{module}, used $factoids->{$arguments}{ref_count} times (last by $factoids->{$arguments}{ref_user})"; return "$trigger: Module loaded by " . $factoids->{$channel}->{$trigger}->{owner} . " on " . localtime($factoids->{$channel}->{$trigger}->{created_on}) . " -> http://code.google.com/p/pbot2-pl/source/browse/trunk/modules/" . $factoids->{$channel}->{$trigger}->{action} . ", used " . $factoids->{$channel}->{$trigger}->{ref_count} . " times (last by " . $factoids->{$channel}->{$trigger}->{ref_user} . ")";
} }
# regex # regex
if(exists $factoids->{$arguments}{regex}) { if($factoids->{$channel}->{$trigger}->{type} eq 'regex') {
return "$arguments: Regex created by $factoids->{$arguments}{owner} on " . localtime($factoids->{$arguments}{created_on}) . ", used $factoids->{$arguments}{ref_count} times (last by $factoids->{$arguments}{ref_user})"; return "$trigger: Regex created by " . $factoids->{$channel}->{$trigger}->{owner} . " on " . localtime($factoids->{$channel}->{$trigger}->{created_on}) . ", used " . $factoids->{$channel}->{$trigger}->{ref_count} . " times (last by " . $factoids->{$channel}->{$trigger}->{ref_user} . ")";
} }
return "/msg $nick $arguments is not a factoid or a module"; return "/msg $nick $trigger is not a factoid or a module";
} }
sub top20 { sub top20 {
@ -498,9 +543,8 @@ sub find {
sub change_text { sub change_text {
my $self = shift; my $self = shift;
$self->{pbot}->logger->log("Enter change_text\n");
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids; my $factoids = $self->{pbot}->factoids->factoids->hash;
my ($keyword, $delim, $tochange, $changeto, $modifier); my ($keyword, $delim, $tochange, $changeto, $modifier);
if(defined $arguments) { if(defined $arguments) {
@ -517,68 +561,29 @@ sub change_text {
} }
if(not defined $changeto) { if(not defined $changeto) {
$self->{pbot}->logger->log("($from) $nick!$user\@$host: improper use of change\n"); return "/msg $nick Usage: change <keyword> s/<pattern>/<replacement>/";
return "/msg $nick Usage: change <keyword> s/<to change>/<change to>/";
} }
if(not exists $factoids->{$keyword}) { my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $keyword);
$self->{pbot}->logger->log("($from) $nick!$user\@$host: attempted to change nonexistant '$keyword'\n");
return "/msg $nick $keyword not found."; if(not defined $trigger) {
return "/msg $nick $keyword not found in channel $from.";
} }
my $type;
$type = 'text' if exists $factoids->{$keyword}{text};
$type = 'regex' if exists $factoids->{$keyword}{regex};
$self->{pbot}->logger->log("keyword: $keyword, type: $type, tochange: $tochange, changeto: $changeto\n");
my $ret = eval { my $ret = eval {
my $regex = qr/$tochange/; if(not $factoids->{$channel}->{$trigger}->{action} =~ s|$tochange|$changeto|) {
if(not $factoids->{$keyword}{$type} =~ s|$regex|$changeto|) { $self->{pbot}->logger->log("($from) $nick!$user\@$host: failed to change '$trigger' 's$delim$tochange$delim$changeto$delim\n");
$self->{pbot}->logger->log("($from) $nick!$user\@$host: failed to change '$keyword' 's$delim$tochange$delim$changeto$delim\n"); return "/msg $nick Change $trigger failed.";
return "/msg $nick Change $keyword failed.";
} else { } else {
$self->{pbot}->logger->log("($from) $nick!$user\@$host: changed '$keyword' 's/$tochange/$changeto/\n"); $self->{pbot}->logger->log("($from) $nick!$user\@$host: changed '$trigger' 's/$tochange/$changeto/\n");
$self->{pbot}->factoids->save_factoids(); $self->{pbot}->factoids->save_factoids();
return "Changed: $keyword is $factoids->{$keyword}{$type}"; return "Changed: $trigger is " . $factoids->{$channel}->{$trigger}->{action};
} }
}; };
return "/msg $nick Change $keyword: $@" if $@; return "/msg $nick Change $trigger: $@" if $@;
return $ret; return $ret;
} }
sub remove_text {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
if(not defined $arguments) {
$self->{pbot}->logger->log("remove_text: invalid usage\n");
return "/msg $nick Usage: remove <keyword>";
}
$self->{pbot}->logger->log("Attempting to remove [$arguments]\n");
if(not exists $factoids->{$arguments}) {
return "/msg $nick $arguments not found.";
}
if(exists $factoids->{$arguments}{command} || exists $factoids->{$arguments}{module}) {
$self->{pbot}->logger->log("$nick!$user\@$host attempted to remove $arguments [not factoid]\n");
return "/msg $nick $arguments is not a factoid.";
}
if(($nick ne $factoids->{$arguments}{owner}) and (not $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host"))) {
$self->{pbot}->logger->log("$nick!$user\@$host attempted to remove $arguments [not owner]\n");
return "/msg $nick You are not the owner of '$arguments'";
}
$self->{pbot}->logger->log("$nick!$user\@$host removed [$arguments][$factoids->{$arguments}{text}]\n") if(exists $factoids->{$arguments}{text});
$self->{pbot}->logger->log("$nick!$user\@$host removed [$arguments][$factoids->{$arguments}{regex}]\n") if(exists $factoids->{$arguments}{regex});
delete $factoids->{$arguments};
$self->{pbot}->factoids->save_factoids();
return "/msg $nick $arguments removed.";
}
sub load_module { sub load_module {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_; my ($from, $nick, $user, $host, $arguments) = @_;

View File

@ -48,9 +48,14 @@ sub execute_module {
$arguments = "" if not defined $arguments; $arguments = "" if not defined $arguments;
my $module = $self->{pbot}->factoids->factoids->{$keyword}{module}; my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $keyword);
my $module_dir = $self->{pbot}->module_dir;
if(not defined $trigger) {
return "/msg $nick Failed to find module for '$keyword' in channel $from\n";
}
my $module = $self->{pbot}->factoids->factoids->hash->{$channel}->{$trigger}->{action};
my $module_dir = $self->{pbot}->module_dir;
$self->{pbot}->logger->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: Executing module $module $arguments\n"); $self->{pbot}->logger->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host: Executing module $module $arguments\n");
@ -59,21 +64,11 @@ sub execute_module {
$arguments = quotemeta($arguments); $arguments = quotemeta($arguments);
$arguments =~ s/\\\s/ /g; $arguments =~ s/\\\s/ /g;
if(exists $self->{pbot}->factoids->factoids->{$keyword}{modulelauncher_subpattern}) { if(exists $self->{pbot}->factoids->factoids->hash->{$channel}->{$trigger}->{modulelauncher_subpattern}) {
if($self->{pbot}->factoids->factoids->{$keyword}{modulelauncher_subpattern} =~ m/s\/(.*?)\/(.*)\//) { if($self->{pbot}->factoids->factoids->hash->{$channel}->{$trigger}->{modulelauncher_subpattern} =~ m/s\/(.*?)\/(.*)\//) {
my ($p1, $p2) = ($1, $2); my ($p1, $p2) = ($1, $2);
$arguments =~ s/$p1/$p2/; $arguments =~ s/$p1/$p2/;
my $a = $1; my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $before, $after) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $`, $');
my $b = $2;
my $c = $3;
my $d = $4;
my $e = $5;
my $f = $6;
my $g = $7;
my $h = $8;
my $i = $9;
my $before = $`;
my $after = $';
$arguments =~ s/\$1/$a/g; $arguments =~ s/\$1/$a/g;
$arguments =~ s/\$2/$b/g; $arguments =~ s/\$2/$b/g;
$arguments =~ s/\$3/$c/g; $arguments =~ s/\$3/$c/g;
@ -86,7 +81,7 @@ sub execute_module {
$arguments =~ s/\$`/$before/g; $arguments =~ s/\$`/$before/g;
$arguments =~ s/\$'/$after/g; $arguments =~ s/\$'/$after/g;
} else { } else {
$self->{pbot}->logger->log("Invalid module substitution pattern [$self->{pbot}->factoids->factoids->{$keyword}{modulelauncher_subpattern}], ignoring.\n"); $self->{pbot}->logger->log("Invalid module substitution pattern [" . $self->{pbot}->factoids->factoids->hash->{$channel}->{$trigger}->{modulelauncher_subpattern}. "], ignoring.\n");
} }
} }

View File

@ -17,6 +17,7 @@ use Text::Levenshtein qw(fastdistance);
use Carp (); use Carp ();
use PBot::FactoidModuleLauncher; use PBot::FactoidModuleLauncher;
use PBot::DualIndexHashObject;
sub new { sub new {
if(ref($_[1]) eq 'HASH') { if(ref($_[1]) eq 'HASH') {
@ -42,8 +43,7 @@ sub initialize {
Carp::croak("Missing pbot reference to Factoids"); Carp::croak("Missing pbot reference to Factoids");
} }
$self->{factoids} = {}; $self->{factoids} = PBot::DualIndexHashObject->new(name => 'Factoids', filename => $filename);
$self->{filename} = $filename;
$self->{export_path} = $export_path; $self->{export_path} = $export_path;
$self->{export_site} = $export_site; $self->{export_site} = $export_site;
@ -51,117 +51,21 @@ sub initialize {
$self->{factoidmodulelauncher} = PBot::FactoidModuleLauncher->new(pbot => $pbot); $self->{factoidmodulelauncher} = PBot::FactoidModuleLauncher->new(pbot => $pbot);
} }
sub load_factoids_add {
my ($self, $factoid, $i, $filename) = @_;
if(defined $factoid) {
my $trigger = delete $factoid->{trigger};
if(not defined $trigger) {
Carp::croak "Missing trigger around line $i of $filename\n";
}
if(exists $self->factoids->{$trigger}) {
Carp::croak "Duplicate factoid '$trigger' found in $filename around line $i\n";
}
my $type = delete $factoid->{type};
if(not defined $type) {
Carp::croak "Missing type for factoid '$trigger' around line $i of $filename\n";
}
$type = lc $type;
my $action = delete $factoid->{action};
if(not defined $action) {
Carp::croak "Missing action for factoid '$trigger' around line $i of $filename\n";
}
$self->factoids->{$trigger}{$type} = $action;
foreach my $key (keys %$factoid) {
$self->factoids->{$trigger}{$key} = $factoid->{$key};
}
}
}
sub load_factoids { sub load_factoids {
my $self = shift; my $self = shift;
my $filename;
if(@_) { $filename = shift; } else { $filename = $self->filename; } $self->{pbot}->logger->log("Loading factoids from " . $self->factoids->filename . " ...\n");
if(not defined $filename) { $self->factoids->load;
Carp::carp "No factoids path specified -- skipping loading of factoids";
return;
}
$self->{pbot}->logger->log("Loading factoids from $filename ...\n"); my ($text, $regex, $modules);
open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n"; foreach my $channel (keys %{ $self->factoids->hash }) {
foreach my $trigger (keys %{ $self->factoids->hash->{$channel} }) {
my $i = 0; $text++ if $self->factoids->hash->{$channel}->{$trigger}->{type} eq 'text';
my ($text, $regex, $modules, $factoid); $regex++ if $self->factoids->hash->{$channel}->{$trigger}->{type} eq 'regex';
$modules++ if $self->factoids->hash->{$channel}->{$trigger}->{type} eq 'module';
foreach my $line (<FILE>) {
$i++;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
if(not $line) {
print "blank line at $i\n";
next;
} }
if($line eq '-') {
# first store the old factoid, if there is one
if($factoid) {
my $type = lc $factoid->{type};
if($type eq "text") {
$text++;
} elsif($type eq "regex") {
$regex++;
} elsif($type eq "module") {
$modules++;
} else {
Carp::croak "Unknown type '$type' in $filename around line $i\n";
}
$self->load_factoids_add($factoid, $i, $filename);
}
# start a new factoid
$factoid = {};
next;
}
my ($key, $value) = split /\:/, $line, 2;
$key =~ s/^\s+//;
$key =~ s/\s+$//;
$value =~ s/^\s+//;
$value =~ s/\s+$//;
$factoid->{$key} = $value;
}
close(FILE);
if($factoid) {
my $type = lc $factoid->{type};
if($type eq "text") {
$text++;
} elsif($type eq "regex") {
$regex++;
} elsif($type eq "module") {
$modules++;
} else {
Carp::croak "Unknown type '$type' in $filename around line $i\n";
}
$self->load_factoids_add($factoid, $i, $filename);
} }
$self->{pbot}->logger->log(" " . ($text + $regex + $modules) . " factoids loaded ($text text, $regex regexs, $modules modules).\n"); $self->{pbot}->logger->log(" " . ($text + $regex + $modules) . " factoids loaded ($text text, $regex regexs, $modules modules).\n");
@ -170,65 +74,39 @@ sub load_factoids {
sub save_factoids { sub save_factoids {
my $self = shift; my $self = shift;
my $filename;
if(@_) { $filename = shift; } else { $filename = $self->filename; } $self->factoids->save;
$self->export_factoids;
if(not defined $filename) {
Carp::carp "No factoids path specified -- skipping saving of factoids\n";
return;
}
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
foreach my $trigger (sort keys %{ $self->factoids }) {
next if $trigger eq "version";
print FILE "-\n";
print FILE "trigger: $trigger\n";
my $type;
if(defined ${ $self->factoids }{$trigger}{module}) {
$type = 'module';
} elsif(defined ${ $self->factoids }{$trigger}{text}) {
$type = 'text';
} elsif(defined ${ $self->factoids }{$trigger}{regex}) {
$type = 'regex';
} else {
$self->{pbot}->logger->log("WARNING: save_factoids: skipping unknown trigger type for $trigger\n");
#todo -- /msg logged in admins greater than level X
next;
}
print FILE "type: $type\n";
print FILE "action: ${ $self->factoids }{$trigger}{$type}\n";
foreach my $key (sort keys %{ ${ $self->factoids }{$trigger} }) {
next if $key eq 'text' or $key eq 'module' or $key eq 'regex';
print FILE "$key: ${ $self->factoids }{$trigger}{$key}\n";
}
}
close(FILE);
$self->export_factoids();
} }
sub add_factoid { sub add_factoid {
my $self = shift; my $self = shift;
my ($type, $channel, $owner, $command, $text) = @_; my ($type, $channel, $owner, $trigger, $action) = @_;
$type = lc $type; $type = lc $type;
$channel = lc $channel; $channel = lc $channel;
$command = lc $command; $trigger = lc $trigger;
${ $self->factoids }{$command}{enabled} = 1; $self->factoids->hash->{$channel}->{$trigger}->{enabled} = 1;
${ $self->factoids }{$command}{$type} = $text; $self->factoids->hash->{$channel}->{$trigger}->{type} = $type;
${ $self->factoids }{$command}{owner} = $owner; $self->factoids->hash->{$channel}->{$trigger}->{action} = $action;
${ $self->factoids }{$command}{channel} = $channel; $self->factoids->hash->{$channel}->{$trigger}->{owner} = $owner;
${ $self->factoids }{$command}{created_on} = gettimeofday; $self->factoids->hash->{$channel}->{$trigger}->{created_on} = gettimeofday;
${ $self->factoids }{$command}{ref_count} = 0; $self->factoids->hash->{$channel}->{$trigger}->{ref_count} = 0;
${ $self->factoids }{$command}{ref_user} = "nobody"; $self->factoids->hash->{$channel}->{$trigger}->{ref_user} = "nobody";
$self->save_factoids;
}
sub remove_factoid {
my $self = shift;
my ($channel, $trigger) = @_;
$channel = lc $channel;
$trigger = lc $trigger;
delete $self->factoids->hash->{$channel}->{$trigger};
$self->save_factoids;
} }
sub export_factoids { sub export_factoids {
@ -238,51 +116,61 @@ sub export_factoids {
if(@_) { $filename = shift; } else { $filename = $self->export_path; } if(@_) { $filename = shift; } else { $filename = $self->export_path; }
return if not defined $filename; return if not defined $filename;
my $text;
open FILE, "> $filename" or return "Could not open export path."; open FILE, "> $filename" or return "Could not open export path.";
my $time = localtime; my $time = localtime;
print FILE "<html><body><i>Generated at $time</i><hr><h3>Candide's factoids:</h3><br>\n"; print FILE "<html><body><i>Generated at $time</i><hr><h3>Candide's factoids:</h3><br>\n";
my $i = 0; my $i = 0;
print FILE "<table border=\"0\">\n";
foreach my $command (sort keys %{ $self->factoids }) { foreach my $channel (sort keys %{ $self->factoids->hash }) {
if(exists ${ $self->factoids }{$command}{text}) { my $chan = $channel eq '.*' ? 'any' : $channel;
$i++; print FILE "<hr>\nChannel $chan\n<hr>\n";
if($i % 2) { print FILE "<table border=\"0\">\n";
print FILE "<tr bgcolor=\"#dddddd\">\n"; foreach my $trigger (sort keys %{ $self->factoids->hash->{$channel} }) {
} else { if($self->factoids->hash->{$channel}->{$trigger}->{type} eq 'text') {
print FILE "<tr>\n"; $i++;
if($i % 2) {
print FILE "<tr bgcolor=\"#dddddd\">\n";
} else {
print FILE "<tr>\n";
}
print FILE "<td><b>$trigger</b> is " . encode_entities($self->factoids->hash->{$channel}->{$trigger}->{action}) . "</td>\n";
print FILE "<td align=\"right\">- submitted by<br> " . $self->factoids->hash->{$channel}->{$trigger}->{owner} . "<br><i>" . localtime($self->factoids->hash->{$channel}->{$trigger}->{created_on}) . "</i>\n</td>\n</tr>\n";
} }
$text = "<td><b>$command</b> is " . encode_entities(${ $self->factoids }{$command}{text}) . "</td>\n";
print FILE $text;
my ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime(${ $self->factoids }{$command}{created_on});
my $t = sprintf("%02d:%02d:%02d-%04d/%02d/%02d\n",
$hours, $minutes, $seconds, $year+1900, $month+1, $day_of_month);
print FILE "<td align=\"right\">- submitted by<br> ${ $self->factoids }{$command}{owner}<br><i>$t</i>\n";
print FILE "</td></tr>\n";
} }
print FILE "</table>\n";
} }
print FILE "</table>\n";
print FILE "<hr>$i factoids memorized.<br>"; print FILE "<hr>$i factoids memorized.<br>";
close(FILE); close(FILE);
#$self->{pbot}->logger->log("$i factoids exported to path: " . $self->export_path . ", site: " . $self->export_site . "\n"); #$self->{pbot}->logger->log("$i factoids exported to path: " . $self->export_path . ", site: " . $self->export_site . "\n");
return "$i factoids exported to " . $self->export_site; return "$i factoids exported to " . $self->export_site;
} }
sub find_factoid { sub find_factoid {
my ($self, $keyword, $arguments) = @_; my ($self, $from, $keyword, $arguments) = @_;
$from = '.*' if not defined $from;
my $string = "$keyword" . (defined $arguments ? " $arguments" : ""); my $string = "$keyword" . (defined $arguments ? " $arguments" : "");
my $result = eval { my @result = eval {
foreach my $command (keys %{ $self->factoids }) { foreach my $channel (sort keys %{ $self->factoids->hash }) {
if(exists $self->factoids->{$command}{regex}) { next unless $from =~ m/$channel/i;
if($string =~ m/$command/i) { foreach my $trigger (keys %{ $self->factoids->hash->{$channel} }) {
return $command; if($self->factoids->hash->{$channel}->{$trigger}->{type} eq 'regex') {
} if($string =~ m/$trigger/i) {
} else { return ($channel, $trigger);
my $command_quoted = quotemeta($command); }
if($keyword =~ m/^$command_quoted$/i) { } else {
return $command; if($keyword =~ m/^\Q$trigger\E$/i) {
return ($channel, $trigger);
}
} }
} }
} }
@ -295,97 +183,74 @@ sub find_factoid {
return undef; return undef;
} }
return $result; return @result;
}
sub levenshtein_matches {
my ($self, $keyword) = @_;
my $comma = '';
my $result = "I don't know about '$keyword'; did you mean ";
foreach my $command (sort keys %{ $self->factoids }) {
next if exists $self->factoids->{$command}{regex};
my $distance = fastdistance($keyword, $command);
# print "Distance $distance for $keyword (" , (length $keyword) , ") vs $command (" , length $command , ")\n";
my $length = (length($keyword) > length($command)) ? length $keyword : length $command;
# print "Percentage: ", $distance / $length, "\n";
if($distance / $length < 0.50) {
$result .= $comma . $command;
$comma = ", ";
}
}
$result =~ s/(.*), /$1 or /;
$result =~ s/$/?/;
$result = undef if $comma eq '';
return $result;
} }
sub interpreter { sub interpreter {
my $self = shift; my $self = shift;
my ($from, $nick, $user, $host, $count, $keyword, $arguments, $tonick) = @_; my ($from, $nick, $user, $host, $count, $keyword, $arguments, $tonick) = @_;
my $result; my ($result, $channel);
my $pbot = $self->{pbot}; my $pbot = $self->{pbot};
my $string = "$keyword" . (defined $arguments ? " $arguments" : ""); my $original_keyword = $keyword;
my $lev = lc $keyword; ($channel, $keyword) = $self->find_factoid($from, $keyword, $arguments);
$keyword = $self->find_factoid($keyword, $arguments);
return $self->levenshtein_matches($lev) if not defined $keyword;
my $type; if(not defined $keyword) {
$type = 'text' if exists $self->factoids->{$keyword}{text}; my $matches = $self->factoids->levenshtein_matches($from, lc $original_keyword);
$type = 'regex' if exists $self->factoids->{$keyword}{regex};
$type = 'module' if exists $self->factoids->{$keyword}{module}; return undef if $matches eq 'none';
return "No such factoid '$original_keyword'; did you mean $matches?";
}
my $type = $self->factoids->hash->{$channel}->{$keyword}->{type};
# Check if it's an alias # Check if it's an alias
my $command; if($self->factoids->hash->{$channel}->{$keyword}->{action} =~ /^\/call\s+(.*)$/) {
if($self->factoids->{$keyword}{$type} =~ /^\/call\s+(.*)$/) { my $command;
if(defined $arguments) { if(defined $arguments) {
$command = "$1 $arguments"; $command = "$1 $arguments";
} else { } else {
$command = $1; $command = $1;
} }
$pbot->logger->log("[" . (defined $from ? $from : "(undef)") . "] ($nick!$user\@$host) [$keyword] aliased to: [$command]\n"); $pbot->logger->log("[" . (defined $from ? $from : "(undef)") . "] ($nick!$user\@$host) [$keyword] aliased to: [$command]\n");
$self->factoids->{$keyword}{ref_count}++; $self->factoids->hash->{$channel}->{$keyword}->{ref_count}++;
$self->factoids->{$keyword}{ref_user} = $nick; $self->factoids->hash->{$channel}->{$keyword}->{ref_user} = $nick;
return $pbot->interpreter->interpret($from, $nick, $user, $host, $count, $command); return $pbot->interpreter->interpret($from, $nick, $user, $host, $count, $command);
} }
if(${ $self->factoids }{$keyword}{enabled} == 0) { if($self->factoids->hash->{$channel}->{$keyword}->{enabled} == 0) {
$self->{pbot}->logger->log("$keyword disabled.\n"); $self->{pbot}->logger->log("$keyword disabled.\n");
return "/msg $nick $keyword is currently disabled."; return "/msg $nick $keyword is currently disabled.";
} elsif(exists ${ $self->factoids }{$keyword}{module}) { } elsif($self->factoids->hash->{$channel}->{$keyword}->{type} eq 'module') {
$self->{pbot}->logger->log("Found module\n"); $self->{pbot}->logger->log("Found module\n");
${ $self->factoids }{$keyword}{ref_count}++; $self->factoids->hash->{$channel}->{$keyword}->{ref_count}++;
${ $self->factoids }{$keyword}{ref_user} = $nick; $self->factoids->hash->{$channel}->{$keyword}->{ref_user} = $nick;
return $self->{factoidmodulelauncher}->execute_module($from, $tonick, $nick, $user, $host, $keyword, $arguments); return $self->{factoidmodulelauncher}->execute_module($from, $tonick, $nick, $user, $host, $keyword, $arguments);
} }
elsif(exists ${ $self->factoids }{$keyword}{text}) { elsif($self->factoids->hash->{$channel}->{$keyword}->{type} eq 'text') {
$self->{pbot}->logger->log("Found factoid\n"); $self->{pbot}->logger->log("Found factoid\n");
# Don't allow user-custom /msg factoids, unless factoid triggered by admin # Don't allow user-custom /msg factoids, unless factoid triggered by admin
if((${ $self->factoids }{$keyword}{text} =~ m/^\/msg/i) and (not $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host"))) { if(($self->factoids->hash->{$channel}->{$keyword}->{action} =~ 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"); $self->{pbot}->logger->log("[HACK] Bad factoid (contains /msg): " . $self->factoids->hash->{$channel}->{$keyword}->{action} . "\n");
return "You must login to use this command." return "You must login to use this command."
} }
${ $self->factoids }{$keyword}{ref_count}++; $self->factoids->hash->{$channel}->{$keyword}->{ref_count}++;
${ $self->factoids }{$keyword}{ref_user} = $nick; $self->factoids->hash->{$channel}->{$keyword}->{ref_user} = $nick;
$self->{pbot}->logger->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host): $keyword: Displaying text \"${ $self->factoids }{$keyword}{text}\"\n"); $self->{pbot}->logger->log("(" . (defined $from ? $from : "(undef)") . "): $nick!$user\@$host): $keyword: Displaying text \"" . $self->factoids->hash->{$channel}->{$keyword}->{action} . "\"\n");
if(defined $tonick) { # !tell foo about bar if(defined $tonick) { # !tell foo about bar
$self->{pbot}->logger->log("($from): $nick!$user\@$host) sent to $tonick\n"); $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: "; my $fromnick = $self->{pbot}->admins->loggedin($from, "$nick!$user\@$host") ? "" : "$nick wants you to know: ";
$result = ${ $self->factoids }{$keyword}{text}; $result = $self->factoids->hash->{$channel}->{$keyword}->{action};
my $botnick = $self->{pbot}->botnick; my $botnick = $self->{pbot}->botnick;
@ -398,7 +263,7 @@ sub interpreter {
$self->{pbot}->logger->log("text set to [$result]\n"); $self->{pbot}->logger->log("text set to [$result]\n");
} else { } else {
$result = ${ $self->factoids }{$keyword}{text}; $result = $self->factoids->hash->{$channel}->{$keyword}->{action};
} }
if(defined $arguments) { if(defined $arguments) {
@ -425,12 +290,11 @@ sub interpreter {
$result =~ s/\$nick/$nick/g; $result =~ s/\$nick/$nick/g;
while ($result =~ /[^\\]\$([a-zA-Z0-9_\-]+)/g) { while ($result =~ /[^\\]\$([a-zA-Z0-9_\-\.]+)/g) {
my $var = $1; my ($var_chan, $var) = $self->find_factoid($from, $1);
#$self->{pbot}->logger->log("adlib: got [$var]\n");
#$self->{pbot}->logger->log("adlib: parsing variable [\$$var]\n"); if(defined $var && $self->factoids->hash->{$var_chan}->{$var}->{type} eq 'text') {
if(exists ${ $self->factoids }{$var} && exists ${ $self->factoids }{$var}{text}) { my $change = $self->factoids->hash->{$var_chan}->{$var}->{action};
my $change = ${ $self->factoids }{$var}{text};
my @list = split(/\s|(".*?")/, $change); my @list = split(/\s|(".*?")/, $change);
my @mylist; my @mylist;
#$self->{pbot}->logger->log("adlib: list [". join(':', @mylist) ."]\n"); #$self->{pbot}->logger->log("adlib: list [". join(':', @mylist) ."]\n");
@ -456,22 +320,13 @@ sub interpreter {
} else { } else {
return "$keyword is $result"; return "$keyword is $result";
} }
} elsif(exists ${ $self->factoids }{$keyword}{regex}) { } elsif($self->factoids->hash->{$channel}->{$keyword}->{type} eq 'regex') {
$result = eval { $result = eval {
my $string = "$keyword" . (defined $arguments ? " $arguments" : "");
if($string =~ m/$keyword/i) { if($string =~ m/$keyword/i) {
$self->{pbot}->logger->log("[$string] matches [$keyword] - calling [" . ${ $self->factoids }{$keyword}{regex}. "$']\n"); $self->{pbot}->logger->log("[$string] matches [$keyword] - calling [" . $self->factoids->hash->{$channel}->{$keyword}->{action} . "$']\n");
my $cmd = "${ $self->factoids }{$keyword}{regex}$'"; my $cmd = "${ $self->factoids }{$keyword}{regex}$'";
my $a = $1; my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $before, $after) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $`, $');
my $b = $2;
my $c = $3;
my $d = $4;
my $e = $5;
my $f = $6;
my $g = $7;
my $h = $8;
my $i = $9;
my $before = $`;
my $after = $';
$cmd =~ s/\$1/$a/g; $cmd =~ s/\$1/$a/g;
$cmd =~ s/\$2/$b/g; $cmd =~ s/\$2/$b/g;
$cmd =~ s/\$3/$c/g; $cmd =~ s/\$3/$c/g;

View File

@ -178,8 +178,12 @@ sub interpret {
} elsif($command =~ /^([^ ]+)\s+is\s+also\s+(.*)$/) { } elsif($command =~ /^([^ ]+)\s+is\s+also\s+(.*)$/) {
($keyword, $arguments) = ("change", "$1 s|\$| - $2|"); ($keyword, $arguments) = ("change", "$1 s|\$| - $2|");
} elsif($command =~ /^([^ ]+)\s+is\s+(.*)$/) { } elsif($command =~ /^([^ ]+)\s+is\s+(.*)$/) {
($keyword, $arguments) = ("add", join(' is ', $1, $2)) unless exists ${ $pbot->factoids }{factoids}{$1}; my ($k, $a) = ($1, $2);
($keyword, $arguments) = ($1, "is $2") if exists ${ $pbot->factoids }{factoids}{$1}; if(defined $pbot->factoids->find_factoid($from, $k)) {
($keyword, $arguments) = ($k, "is $a");
} else {
($keyword, $arguments) = ("add", join(' is ', $k, $a));
}
} elsif($command =~ /^(.*?)\s+(.*)$/) { } elsif($command =~ /^(.*?)\s+(.*)$/) {
($keyword, $arguments) = ($1, $2); ($keyword, $arguments) = ($1, $2);
} else { } else {

View File

@ -136,8 +136,8 @@ sub initialize {
export_site => $export_factoids_site, export_site => $export_factoids_site,
); );
$self->factoids->add_factoid('text', '.*', $botnick, 'version', "/say $VERSION");
$self->factoids->load_factoids() if defined $factoids_file; $self->factoids->load_factoids() if defined $factoids_file;
$self->factoids->add_factoid('text', '.*', $botnick, 'version', "/say $VERSION");
$self->module_dir($module_dir); $self->module_dir($module_dir);

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 18343, BUILD_REVISION => 184,
BUILD_DATE => "2010-06-19", BUILD_DATE => "2010-06-19",
}; };

File diff suppressed because it is too large Load Diff