mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-23 12:29:27 +01:00
New factoid loading/saving -- much more flexible!
This commit is contained in:
parent
dfab440437
commit
8ebe546850
@ -168,12 +168,12 @@ sub alias {
|
|||||||
return "/msg $nick '$alias' already exists";
|
return "/msg $nick '$alias' already exists";
|
||||||
}
|
}
|
||||||
|
|
||||||
$factoids->{$alias}{text} = "/call $command";
|
$factoids->{$alias}{text} = "/call $command";
|
||||||
$factoids->{$alias}{owner} = $nick;
|
$factoids->{$alias}{owner} = $nick;
|
||||||
$factoids->{$alias}{timestamp} = time();
|
$factoids->{$alias}{created_on} = time();
|
||||||
$factoids->{$alias}{enabled} = 1;
|
$factoids->{$alias}{enabled} = 1;
|
||||||
$factoids->{$alias}{ref_count} = 0;
|
$factoids->{$alias}{ref_count} = 0;
|
||||||
$factoids->{$alias}{ref_user} = "nobody";
|
$factoids->{$alias}{ref_user} = "nobody";
|
||||||
$self->{pbot}->logger->log("$nick!$user\@$host aliased $alias => $command\n");
|
$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'";
|
||||||
@ -205,12 +205,12 @@ sub add_regex {
|
|||||||
return "/msg $nick $keyword already exists.";
|
return "/msg $nick $keyword already exists.";
|
||||||
}
|
}
|
||||||
|
|
||||||
$factoids->{$keyword}{regex} = $text;
|
$factoids->{$keyword}{regex} = $text;
|
||||||
$factoids->{$keyword}{owner} = $nick;
|
$factoids->{$keyword}{owner} = $nick;
|
||||||
$factoids->{$keyword}{timestamp} = time();
|
$factoids->{$keyword}{created_on} = time();
|
||||||
$factoids->{$keyword}{enabled} = 1;
|
$factoids->{$keyword}{enabled} = 1;
|
||||||
$factoids->{$keyword}{ref_count} = 0;
|
$factoids->{$keyword}{ref_count} = 0;
|
||||||
$factoids->{$keyword}{ref_user} = "nobody";
|
$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();
|
$self->{pbot}->factoids->save_factoids();
|
||||||
return "/msg $nick $keyword added.";
|
return "/msg $nick $keyword added.";
|
||||||
@ -240,12 +240,12 @@ sub add_text {
|
|||||||
return "/msg $nick $keyword already exists.";
|
return "/msg $nick $keyword already exists.";
|
||||||
}
|
}
|
||||||
|
|
||||||
$factoids->{$keyword}{text} = $text;
|
$factoids->{$keyword}{text} = $text;
|
||||||
$factoids->{$keyword}{owner} = $nick;
|
$factoids->{$keyword}{owner} = $nick;
|
||||||
$factoids->{$keyword}{timestamp} = time();
|
$factoids->{$keyword}{created_on} = time();
|
||||||
$factoids->{$keyword}{enabled} = 1;
|
$factoids->{$keyword}{enabled} = 1;
|
||||||
$factoids->{$keyword}{ref_count} = 0;
|
$factoids->{$keyword}{ref_count} = 0;
|
||||||
$factoids->{$keyword}{ref_user} = "nobody";
|
$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");
|
||||||
|
|
||||||
@ -319,17 +319,17 @@ sub info {
|
|||||||
|
|
||||||
# factoid
|
# factoid
|
||||||
if(exists $factoids->{$arguments}{text}) {
|
if(exists $factoids->{$arguments}{text}) {
|
||||||
return "$arguments: Factoid submitted by $factoids->{$arguments}{owner} on " . localtime($factoids->{$arguments}{timestamp}) . ", referenced $factoids->{$arguments}{ref_count} times (last by $factoids->{$arguments}{ref_user})";
|
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})";
|
||||||
}
|
}
|
||||||
|
|
||||||
# module
|
# module
|
||||||
if(exists $factoids->{$arguments}{module}) {
|
if(exists $factoids->{$arguments}{module}) {
|
||||||
return "$arguments: Module loaded by $factoids->{$arguments}{owner} on " . localtime($factoids->{$arguments}{timestamp}) . " -> 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 "$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})";
|
||||||
}
|
}
|
||||||
|
|
||||||
# regex
|
# regex
|
||||||
if(exists $factoids->{$arguments}{regex}) {
|
if(exists $factoids->{$arguments}{regex}) {
|
||||||
return "$arguments: Regex created by $factoids->{$arguments}{owner} on " . localtime($factoids->{$arguments}{timestamp}) . ", used $factoids->{$arguments}{ref_count} times (last by $factoids->{$arguments}{ref_user})";
|
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 "/msg $nick $arguments is not a factoid or a module";
|
return "/msg $nick $arguments is not a factoid or a module";
|
||||||
@ -356,8 +356,8 @@ sub top20 {
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
if(lc $arguments eq "recent") {
|
if(lc $arguments eq "recent") {
|
||||||
foreach my $command (sort { $factoids->{$b}{timestamp} <=> $factoids->{$a}{timestamp} } keys %{ $factoids }) {
|
foreach my $command (sort { $factoids->{$b}{created_on} <=> $factoids->{$a}{created_on} } keys %{ $factoids }) {
|
||||||
#my ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime($factoids->{$command}{timestamp});
|
#my ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime($factoids->{$command}{created_on});
|
||||||
#my $t = sprintf("%04d/%02d/%02d", $year+1900, $month+1, $day_of_month);
|
#my $t = sprintf("%04d/%02d/%02d", $year+1900, $month+1, $day_of_month);
|
||||||
|
|
||||||
$text .= "$command ";
|
$text .= "$command ";
|
||||||
@ -594,7 +594,7 @@ sub load_module {
|
|||||||
$factoids->{$keyword}{module} = $module;
|
$factoids->{$keyword}{module} = $module;
|
||||||
$factoids->{$keyword}{enabled} = 1;
|
$factoids->{$keyword}{enabled} = 1;
|
||||||
$factoids->{$keyword}{owner} = $nick;
|
$factoids->{$keyword}{owner} = $nick;
|
||||||
$factoids->{$keyword}{timestamp} = time();
|
$factoids->{$keyword}{created_on} = time();
|
||||||
$self->{pbot}->logger->log("$nick!$user\@$host loaded $keyword => $module\n");
|
$self->{pbot}->logger->log("$nick!$user\@$host loaded $keyword => $module\n");
|
||||||
$self->{pbot}->factoids->save_factoids();
|
$self->{pbot}->factoids->save_factoids();
|
||||||
return "/msg $nick Loaded $keyword => $module";
|
return "/msg $nick Loaded $keyword => $module";
|
||||||
|
147
PBot/Factoids.pm
147
PBot/Factoids.pm
@ -51,6 +51,43 @@ 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;
|
my $filename;
|
||||||
@ -65,37 +102,56 @@ sub load_factoids {
|
|||||||
$self->{pbot}->logger->log("Loading factoids from $filename ...\n");
|
$self->{pbot}->logger->log("Loading factoids from $filename ...\n");
|
||||||
|
|
||||||
open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n";
|
open(FILE, "< $filename") or Carp::croak "Couldn't open $filename: $!\n";
|
||||||
my @contents = <FILE>;
|
|
||||||
close(FILE);
|
|
||||||
|
|
||||||
my $i = 0;
|
my $i = 0;
|
||||||
my ($text, $regex, $modules);
|
my ($text, $regex, $modules, $factoid);
|
||||||
|
|
||||||
foreach my $line (@contents) {
|
foreach my $line (<FILE>) {
|
||||||
chomp $line;
|
|
||||||
$i++;
|
$i++;
|
||||||
|
|
||||||
my ($command, $type, $enabled, $owner, $timestamp, $ref_count, $ref_user, $value) = split(/\s+/, $line, 8);
|
$line =~ s/^\s+//;
|
||||||
|
$line =~ s/\s+$//;
|
||||||
|
|
||||||
if(not defined $command || not defined $enabled || not defined $owner || not defined $timestamp
|
if(not $line) {
|
||||||
|| not defined $type || not defined $ref_count
|
print "blank line at $i\n";
|
||||||
|| not defined $ref_user || not defined $value) {
|
next;
|
||||||
Carp::croak "Syntax error around line $i of $filename\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(exists ${ $self->factoids }{$command}) {
|
if($line eq '-') {
|
||||||
Carp::croak "Duplicate factoid $command found in $filename around line $i\n";
|
# 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
$type = lc $type;
|
my ($key, $value) = split /\:/, $line, 2;
|
||||||
|
|
||||||
${ $self->factoids }{$command}{enabled} = $enabled;
|
$key =~ s/^\s+//;
|
||||||
${ $self->factoids }{$command}{$type} = $value;
|
$key =~ s/\s+$//;
|
||||||
${ $self->factoids }{$command}{owner} = $owner;
|
$value =~ s/^\s+//;
|
||||||
${ $self->factoids }{$command}{timestamp} = $timestamp;
|
$value =~ s/\s+$//;
|
||||||
${ $self->factoids }{$command}{ref_count} = $ref_count;
|
|
||||||
${ $self->factoids }{$command}{ref_user} = $ref_user;
|
|
||||||
|
|
||||||
|
$factoid->{$key} = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(FILE);
|
||||||
|
|
||||||
|
if($factoid) {
|
||||||
|
my $type = lc $factoid->{type};
|
||||||
if($type eq "text") {
|
if($type eq "text") {
|
||||||
$text++;
|
$text++;
|
||||||
} elsif($type eq "regex") {
|
} elsif($type eq "regex") {
|
||||||
@ -105,9 +161,10 @@ sub load_factoids {
|
|||||||
} else {
|
} else {
|
||||||
Carp::croak "Unknown type '$type' in $filename around line $i\n";
|
Carp::croak "Unknown type '$type' in $filename around line $i\n";
|
||||||
}
|
}
|
||||||
|
$self->load_factoids_add($factoid, $i, $filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
$self->{pbot}->logger->log(" $i factoids loaded ($text factoids, $regex regexs, $modules modules).\n");
|
$self->{pbot}->logger->log(" " . ($text + $regex + $modules) . " factoids loaded ($text text, $regex regexs, $modules modules).\n");
|
||||||
$self->{pbot}->logger->log("Done.\n");
|
$self->{pbot}->logger->log("Done.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,32 +181,32 @@ sub save_factoids {
|
|||||||
|
|
||||||
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
|
open(FILE, "> $filename") or die "Couldn't open $filename: $!\n";
|
||||||
|
|
||||||
foreach my $command (sort keys %{ $self->factoids }) {
|
foreach my $trigger (sort keys %{ $self->factoids }) {
|
||||||
next if $command eq "version";
|
next if $trigger eq "version";
|
||||||
if(defined ${ $self->factoids }{$command}{module} || defined ${ $self->factoids }{$command}{text} || defined ${ $self->factoids }{$command}{regex}) {
|
|
||||||
print FILE "$command ";
|
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 {
|
} else {
|
||||||
$self->{pbot}->logger->log("save_commands: unknown command type $command\n");
|
$self->{pbot}->logger->log("WARNING: save_factoids: skipping unknown trigger type for $trigger\n");
|
||||||
|
#todo -- /msg logged in admins greater than level X
|
||||||
next;
|
next;
|
||||||
}
|
}
|
||||||
#bleh, this is ugly - duplicated
|
|
||||||
if(defined ${ $self->factoids }{$command}{module}) {
|
print FILE "type: $type\n";
|
||||||
print FILE "module ";
|
print FILE "action: ${ $self->factoids }{$trigger}{$type}\n";
|
||||||
print FILE "${ $self->factoids }{$command}{enabled} ${ $self->factoids }{$command}{owner} ${ $self->factoids }{$command}{timestamp} ";
|
|
||||||
print FILE "${ $self->factoids }{$command}{ref_count} ${ $self->factoids }{$command}{ref_user} ";
|
foreach my $key (sort keys %{ ${ $self->factoids }{$trigger} }) {
|
||||||
print FILE "${ $self->factoids }{$command}{module}\n";
|
next if $key eq 'text' or $key eq 'module' or $key eq 'regex';
|
||||||
} elsif(defined ${ $self->factoids }{$command}{text}) {
|
print FILE "$key: ${ $self->factoids }{$trigger}{$key}\n";
|
||||||
print FILE "text ";
|
|
||||||
print FILE "${ $self->factoids }{$command}{enabled} ${ $self->factoids }{$command}{owner} ${ $self->factoids }{$command}{timestamp} ";
|
|
||||||
print FILE "${ $self->factoids }{$command}{ref_count} ${ $self->factoids }{$command}{ref_user} ";
|
|
||||||
print FILE "${ $self->factoids }{$command}{text}\n";
|
|
||||||
} elsif(defined ${ $self->factoids }{$command}{regex}) {
|
|
||||||
print FILE "regex ";
|
|
||||||
print FILE "${ $self->factoids }{$command}{enabled} ${ $self->factoids }{$command}{owner} ${ $self->factoids }{$command}{timestamp} ";
|
|
||||||
print FILE "${ $self->factoids }{$command}{ref_count} ${ $self->factoids }{$command}{ref_user} ";
|
|
||||||
print FILE "${ $self->factoids }{$command}{regex}\n";
|
|
||||||
} else {
|
|
||||||
$self->{pbot}->logger->log("save_commands: skipping unknown command type for $command\n");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(FILE);
|
close(FILE);
|
||||||
@ -169,7 +226,7 @@ sub add_factoid {
|
|||||||
${ $self->factoids }{$command}{$type} = $text;
|
${ $self->factoids }{$command}{$type} = $text;
|
||||||
${ $self->factoids }{$command}{owner} = $owner;
|
${ $self->factoids }{$command}{owner} = $owner;
|
||||||
${ $self->factoids }{$command}{channel} = $channel;
|
${ $self->factoids }{$command}{channel} = $channel;
|
||||||
${ $self->factoids }{$command}{timestamp} = gettimeofday;
|
${ $self->factoids }{$command}{created_on} = gettimeofday;
|
||||||
${ $self->factoids }{$command}{ref_count} = 0;
|
${ $self->factoids }{$command}{ref_count} = 0;
|
||||||
${ $self->factoids }{$command}{ref_user} = "nobody";
|
${ $self->factoids }{$command}{ref_user} = "nobody";
|
||||||
}
|
}
|
||||||
@ -197,7 +254,7 @@ sub export_factoids {
|
|||||||
}
|
}
|
||||||
$text = "<td><b>$command</b> is " . encode_entities(${ $self->factoids }{$command}{text}) . "</td>\n";
|
$text = "<td><b>$command</b> is " . encode_entities(${ $self->factoids }{$command}{text}) . "</td>\n";
|
||||||
print FILE $text;
|
print FILE $text;
|
||||||
my ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yday, $isdst) = localtime(${ $self->factoids }{$command}{timestamp});
|
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",
|
my $t = sprintf("%02d:%02d:%02d-%04d/%02d/%02d\n",
|
||||||
$hours, $minutes, $seconds, $year+1900, $month+1, $day_of_month);
|
$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 align=\"right\">- submitted by<br> ${ $self->factoids }{$command}{owner}<br><i>$t</i>\n";
|
||||||
|
@ -9,7 +9,7 @@ use strict;
|
|||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
use vars qw($VERSION);
|
use vars qw($VERSION);
|
||||||
$VERSION = "0.6.3-beta";
|
$VERSION = "0.7.0";
|
||||||
|
|
||||||
# unbuffer stdout
|
# unbuffer stdout
|
||||||
STDOUT->autoflush(1);
|
STDOUT->autoflush(1);
|
||||||
|
Loading…
Reference in New Issue
Block a user