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.
# TODO: Add getter for factoids instead of directly accessing factoids
package PBot::FactoidCommands;
use warnings;
@ -55,12 +53,37 @@ sub initialize {
$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->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 {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my $botnick = $self->{pbot}->botnick;
my $text;
@ -108,10 +131,13 @@ sub list {
}
if($arguments =~ /^modules$/i) {
$text = "Loaded modules: ";
foreach my $command (sort keys %{ $factoids }) {
if(exists $factoids->{$command}{module}) {
$text .= "$command ";
$from = '.*' if not defined $from;
$text = "Loaded modules for channel $from: ";
foreach my $channel (sort keys %{ $self->{pbot}->factoids->factoids->hash }) {
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;
@ -156,103 +182,118 @@ sub list {
sub alias {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my ($alias, $command) = $arguments =~ /^(.*?)\s+(.*)$/ if defined $arguments;
if(not defined $command) {
$self->{pbot}->logger->log("alias: invalid usage\n");
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");
return "/msg $nick '$alias' already exists";
return "/msg $nick '$alias_trigger' already exists for channel $channel";
}
$factoids->{$alias}{text} = "/call $command";
$factoids->{$alias}{owner} = $nick;
$factoids->{$alias}{created_on} = time();
$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->add_factoid('text', $from, $nick, $alias, "/call $command");
$self->{pbot}->logger->log("$nick!$user\@$host [$from] aliased $alias => $command\n");
$self->{pbot}->factoids->save_factoids();
return "/msg $nick '$alias' aliases '$command'";
return "/msg $nick '$alias' aliases '$command' for channel $from";
}
sub add_regex {
my $self = shift;
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;
$from = '.*' if not defined $from;
if(not defined $keyword) {
$text = "";
foreach my $command (sort keys %{ $factoids }) {
if(exists $factoids->{$command}{regex}) {
$text .= $command . " ";
foreach my $trigger (sort keys %{ $factoids->{$from} }) {
if($factoids->{$from}->{$trigger}->{type} eq 'regex') {
$text .= $trigger . " ";
}
}
return "Stored regexs: $text";
return "Stored regexs for channel $from: $text";
}
if(not defined $text) {
$self->{pbot}->logger->log("add_regex: invalid usage\n");
return "/msg $nick Usage: regex <regex> <command>";
}
if(exists $factoids->{$keyword}) {
$self->{pbot}->logger->log("$nick!$user\@$host attempt to overwrite $keyword\n");
return "/msg $nick $keyword already exists.";
my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $keyword);
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;
$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}->factoids->add_factoid('regex', $from, $nick, $keyword, $text);
$self->{pbot}->logger->log("$nick!$user\@$host added [$keyword] => [$text]\n");
$self->{pbot}->factoids->save_factoids();
return "/msg $nick $keyword added.";
}
sub add_text {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my ($keyword, $text) = $arguments =~ /^(.*?)\s+is\s+(.*)$/i if defined $arguments;
if(not defined $text) {
$self->{pbot}->logger->log("add_text: invalid usage\n");
if(not defined $text or not defined $keyword) {
return "/msg $nick Usage: add <keyword> is <factoid>";
}
if(not defined $keyword) {
$self->{pbot}->logger->log("add_text: invalid usage\n");
return "/msg $nick Usage: add <keyword> is <factoid>";
}
$from = '.*' if not defined $from;
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");
return undef;
return "/msg $nick $keyword already exists.";
}
$factoids->{$keyword}{text} = $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}->factoids->add_factoid('text', $from, $nick, $keyword, $text);
$self->{pbot}->logger->log("$nick!$user\@$host added $keyword => $text\n");
$self->{pbot}->factoids->save_factoids();
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 {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
@ -283,55 +324,59 @@ sub histogram {
sub show {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my $factoids = $self->{pbot}->factoids->factoids->hash;
if(not defined $arguments) {
return "/msg $nick Usage: show <factoid>";
}
if(not exists $factoids->{$arguments}) {
return "/msg $nick $arguments not found";
$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 in channel $from";
}
if(exists $factoids->{$arguments}{command} || exists $factoids->{$arguments}{module}) {
return "/msg $nick $arguments is not a factoid";
if($factoids->{$channel}->{$trigger}->{type} eq 'module') {
return "/msg $nick $trigger is not a factoid";
}
my $type;
$type = 'text' if exists $factoids->{$arguments}{text};
$type = 'regex' if exists $factoids->{$arguments}{regex};
return "$arguments: $factoids->{$arguments}{$type}";
return "$trigger: " . $factoids->{$channel}->{$trigger}->{action};
}
sub info {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;
my $factoids = $self->{pbot}->factoids->factoids;
my $factoids = $self->{pbot}->factoids->factoids->hash;
if(not defined $arguments) {
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";
}
# factoid
if(exists $factoids->{$arguments}{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})";
if($factoids->{$channel}->{$trigger}->{type} eq 'text') {
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
if(exists $factoids->{$arguments}{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})";
if($factoids->{$channel}->{$trigger}->{type} eq 'module') {
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
if(exists $factoids->{$arguments}{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})";
if($factoids->{$channel}->{$trigger}->{type} eq 'regex') {
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 {
@ -498,9 +543,8 @@ sub find {
sub change_text {
my $self = shift;
$self->{pbot}->logger->log("Enter change_text\n");
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);
if(defined $arguments) {
@ -517,68 +561,29 @@ sub change_text {
}
if(not defined $changeto) {
$self->{pbot}->logger->log("($from) $nick!$user\@$host: improper use of change\n");
return "/msg $nick Usage: change <keyword> s/<to change>/<change to>/";
return "/msg $nick Usage: change <keyword> s/<pattern>/<replacement>/";
}
if(not exists $factoids->{$keyword}) {
$self->{pbot}->logger->log("($from) $nick!$user\@$host: attempted to change nonexistant '$keyword'\n");
return "/msg $nick $keyword not found.";
my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $keyword);
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 $regex = qr/$tochange/;
if(not $factoids->{$keyword}{$type} =~ s|$regex|$changeto|) {
$self->{pbot}->logger->log("($from) $nick!$user\@$host: failed to change '$keyword' 's$delim$tochange$delim$changeto$delim\n");
return "/msg $nick Change $keyword failed.";
if(not $factoids->{$channel}->{$trigger}->{action} =~ s|$tochange|$changeto|) {
$self->{pbot}->logger->log("($from) $nick!$user\@$host: failed to change '$trigger' 's$delim$tochange$delim$changeto$delim\n");
return "/msg $nick Change $trigger failed.";
} 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();
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;
}
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 {
my $self = shift;
my ($from, $nick, $user, $host, $arguments) = @_;

View File

@ -48,9 +48,14 @@ sub execute_module {
$arguments = "" if not defined $arguments;
my $module = $self->{pbot}->factoids->factoids->{$keyword}{module};
my $module_dir = $self->{pbot}->module_dir;
my ($channel, $trigger) = $self->{pbot}->factoids->find_factoid($from, $keyword);
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");
@ -59,21 +64,11 @@ sub execute_module {
$arguments = quotemeta($arguments);
$arguments =~ s/\\\s/ /g;
if(exists $self->{pbot}->factoids->factoids->{$keyword}{modulelauncher_subpattern}) {
if($self->{pbot}->factoids->factoids->{$keyword}{modulelauncher_subpattern} =~ m/s\/(.*?)\/(.*)\//) {
if(exists $self->{pbot}->factoids->factoids->hash->{$channel}->{$trigger}->{modulelauncher_subpattern}) {
if($self->{pbot}->factoids->factoids->hash->{$channel}->{$trigger}->{modulelauncher_subpattern} =~ m/s\/(.*?)\/(.*)\//) {
my ($p1, $p2) = ($1, $2);
$arguments =~ s/$p1/$p2/;
my $a = $1;
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 = $';
my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $before, $after) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $`, $');
$arguments =~ s/\$1/$a/g;
$arguments =~ s/\$2/$b/g;
$arguments =~ s/\$3/$c/g;
@ -86,7 +81,7 @@ sub execute_module {
$arguments =~ s/\$`/$before/g;
$arguments =~ s/\$'/$after/g;
} 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 PBot::FactoidModuleLauncher;
use PBot::DualIndexHashObject;
sub new {
if(ref($_[1]) eq 'HASH') {
@ -42,8 +43,7 @@ sub initialize {
Carp::croak("Missing pbot reference to Factoids");
}
$self->{factoids} = {};
$self->{filename} = $filename;
$self->{factoids} = PBot::DualIndexHashObject->new(name => 'Factoids', filename => $filename);
$self->{export_path} = $export_path;
$self->{export_site} = $export_site;
@ -51,117 +51,21 @@ sub initialize {
$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 {
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) {
Carp::carp "No factoids path specified -- skipping loading of factoids";
return;
}
$self->factoids->load;
$self->{pbot}->logger->log("Loading factoids from $filename ...\n");
my ($text, $regex, $modules);
open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n";
my $i = 0;
my ($text, $regex, $modules, $factoid);
foreach my $line (<FILE>) {
$i++;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
if(not $line) {
print "blank line at $i\n";
next;
foreach my $channel (keys %{ $self->factoids->hash }) {
foreach my $trigger (keys %{ $self->factoids->hash->{$channel} }) {
$text++ if $self->factoids->hash->{$channel}->{$trigger}->{type} eq 'text';
$regex++ if $self->factoids->hash->{$channel}->{$trigger}->{type} eq 'regex';
$modules++ if $self->factoids->hash->{$channel}->{$trigger}->{type} eq 'module';
}
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");
@ -170,65 +74,39 @@ sub load_factoids {
sub save_factoids {
my $self = shift;
my $filename;
if(@_) { $filename = shift; } else { $filename = $self->filename; }
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();
$self->factoids->save;
$self->export_factoids;
}
sub add_factoid {
my $self = shift;
my ($type, $channel, $owner, $command, $text) = @_;
my ($type, $channel, $owner, $trigger, $action) = @_;
$type = lc $type;
$channel = lc $channel;
$command = lc $command;
$trigger = lc $trigger;
${ $self->factoids }{$command}{enabled} = 1;
${ $self->factoids }{$command}{$type} = $text;
${ $self->factoids }{$command}{owner} = $owner;
${ $self->factoids }{$command}{channel} = $channel;
${ $self->factoids }{$command}{created_on} = gettimeofday;
${ $self->factoids }{$command}{ref_count} = 0;
${ $self->factoids }{$command}{ref_user} = "nobody";
$self->factoids->hash->{$channel}->{$trigger}->{enabled} = 1;
$self->factoids->hash->{$channel}->{$trigger}->{type} = $type;
$self->factoids->hash->{$channel}->{$trigger}->{action} = $action;
$self->factoids->hash->{$channel}->{$trigger}->{owner} = $owner;
$self->factoids->hash->{$channel}->{$trigger}->{created_on} = gettimeofday;
$self->factoids->hash->{$channel}->{$trigger}->{ref_count} = 0;
$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 {
@ -238,51 +116,61 @@ sub export_factoids {
if(@_) { $filename = shift; } else { $filename = $self->export_path; }
return if not defined $filename;
my $text;
open FILE, "> $filename" or return "Could not open export path.";
my $time = localtime;
print FILE "<html><body><i>Generated at $time</i><hr><h3>Candide's factoids:</h3><br>\n";
my $i = 0;
print FILE "<table border=\"0\">\n";
foreach my $command (sort keys %{ $self->factoids }) {
if(exists ${ $self->factoids }{$command}{text}) {
$i++;
if($i % 2) {
print FILE "<tr bgcolor=\"#dddddd\">\n";
} else {
print FILE "<tr>\n";
foreach my $channel (sort keys %{ $self->factoids->hash }) {
my $chan = $channel eq '.*' ? 'any' : $channel;
print FILE "<hr>\nChannel $chan\n<hr>\n";
print FILE "<table border=\"0\">\n";
foreach my $trigger (sort keys %{ $self->factoids->hash->{$channel} }) {
if($self->factoids->hash->{$channel}->{$trigger}->{type} eq 'text') {
$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>";
close(FILE);
#$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;
}
sub find_factoid {
my ($self, $keyword, $arguments) = @_;
my ($self, $from, $keyword, $arguments) = @_;
$from = '.*' if not defined $from;
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;
my @result = eval {
foreach my $channel (sort keys %{ $self->factoids->hash }) {
next unless $from =~ m/$channel/i;
foreach my $trigger (keys %{ $self->factoids->hash->{$channel} }) {
if($self->factoids->hash->{$channel}->{$trigger}->{type} eq 'regex') {
if($string =~ m/$trigger/i) {
return ($channel, $trigger);
}
} else {
if($keyword =~ m/^\Q$trigger\E$/i) {
return ($channel, $trigger);
}
}
}
}
@ -295,97 +183,74 @@ sub find_factoid {
return undef;
}
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;
return @result;
}
sub interpreter {
my $self = shift;
my ($from, $nick, $user, $host, $count, $keyword, $arguments, $tonick) = @_;
my $result;
my ($result, $channel);
my $pbot = $self->{pbot};
my $string = "$keyword" . (defined $arguments ? " $arguments" : "");
my $lev = lc $keyword;
$keyword = $self->find_factoid($keyword, $arguments);
return $self->levenshtein_matches($lev) if not defined $keyword;
my $original_keyword = $keyword;
($channel, $keyword) = $self->find_factoid($from, $keyword, $arguments);
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};
if(not defined $keyword) {
my $matches = $self->factoids->levenshtein_matches($from, lc $original_keyword);
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
my $command;
if($self->factoids->{$keyword}{$type} =~ /^\/call\s+(.*)$/) {
if($self->factoids->hash->{$channel}->{$keyword}->{action} =~ /^\/call\s+(.*)$/) {
my $command;
if(defined $arguments) {
$command = "$1 $arguments";
} else {
$command = $1;
}
$pbot->logger->log("[" . (defined $from ? $from : "(undef)") . "] ($nick!$user\@$host) [$keyword] aliased to: [$command]\n");
$self->factoids->{$keyword}{ref_count}++;
$self->factoids->{$keyword}{ref_user} = $nick;
$self->factoids->hash->{$channel}->{$keyword}->{ref_count}++;
$self->factoids->hash->{$channel}->{$keyword}->{ref_user} = $nick;
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");
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->factoids }{$keyword}{ref_count}++;
${ $self->factoids }{$keyword}{ref_user} = $nick;
$self->factoids->hash->{$channel}->{$keyword}->{ref_count}++;
$self->factoids->hash->{$channel}->{$keyword}->{ref_user} = $nick;
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");
# 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"))) {
$self->{pbot}->logger->log("[HACK] Bad factoid (contains /msg): ${ $self->factoids }{$keyword}{text}\n");
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->hash->{$channel}->{$keyword}->{action} . "\n");
return "You must login to use this command."
}
${ $self->factoids }{$keyword}{ref_count}++;
${ $self->factoids }{$keyword}{ref_user} = $nick;
$self->factoids->hash->{$channel}->{$keyword}->{ref_count}++;
$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
$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 }{$keyword}{text};
$result = $self->factoids->hash->{$channel}->{$keyword}->{action};
my $botnick = $self->{pbot}->botnick;
@ -398,7 +263,7 @@ sub interpreter {
$self->{pbot}->logger->log("text set to [$result]\n");
} else {
$result = ${ $self->factoids }{$keyword}{text};
$result = $self->factoids->hash->{$channel}->{$keyword}->{action};
}
if(defined $arguments) {
@ -425,12 +290,11 @@ sub interpreter {
$result =~ s/\$nick/$nick/g;
while ($result =~ /[^\\]\$([a-zA-Z0-9_\-]+)/g) {
my $var = $1;
#$self->{pbot}->logger->log("adlib: got [$var]\n");
#$self->{pbot}->logger->log("adlib: parsing variable [\$$var]\n");
if(exists ${ $self->factoids }{$var} && exists ${ $self->factoids }{$var}{text}) {
my $change = ${ $self->factoids }{$var}{text};
while ($result =~ /[^\\]\$([a-zA-Z0-9_\-\.]+)/g) {
my ($var_chan, $var) = $self->find_factoid($from, $1);
if(defined $var && $self->factoids->hash->{$var_chan}->{$var}->{type} eq 'text') {
my $change = $self->factoids->hash->{$var_chan}->{$var}->{action};
my @list = split(/\s|(".*?")/, $change);
my @mylist;
#$self->{pbot}->logger->log("adlib: list [". join(':', @mylist) ."]\n");
@ -456,22 +320,13 @@ sub interpreter {
} else {
return "$keyword is $result";
}
} elsif(exists ${ $self->factoids }{$keyword}{regex}) {
} elsif($self->factoids->hash->{$channel}->{$keyword}->{type} eq 'regex') {
$result = eval {
my $string = "$keyword" . (defined $arguments ? " $arguments" : "");
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 $a = $1;
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 = $';
my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $before, $after) = ($1, $2, $3, $4, $5, $6, $7, $8, $9, $`, $');
$cmd =~ s/\$1/$a/g;
$cmd =~ s/\$2/$b/g;
$cmd =~ s/\$3/$c/g;

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff