# File: Exporter.pm # # Purpose: Exports factoids to HTML. # SPDX-FileCopyrightText: 2021 Pragmatic Software # SPDX-License-Identifier: MIT package PBot::Core::Factoids::Exporter; use parent 'PBot::Core::Class'; use PBot::Imports; use HTML::Entities; use POSIX qw(strftime); sub initialize { } sub export { my $self = shift; my $filename; if (@_) { $filename = shift; } else { $filename = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/factoids.html'; } if (not defined $filename) { $self->{pbot}->{logger}->log("Factoids: export: no filename, skipping export.\n"); return "No file to export to."; } if (not defined $self->{pbot}->{factoids}->{data}->{storage}->{dbh}) { $self->{pbot}->{logger}->log("Factoids: export: database closed, skipping export.\n"); return "Factoids database closed; can't export."; } $self->{pbot}->{logger}->log("Exporting factoids to $filename\n"); if (not open FILE, "> $filename") { $self->{pbot}->{logger}->log("Could not open export file: $!\n"); return "Could not open export file: $!"; } my $botnick = $self->{pbot}->{registry}->get_value('irc', 'botnick'); my $factoids = $self->{pbot}->{factoids}->{data}->{storage}; my $time = localtime; print FILE "\n\n"; print FILE '' . "\n"; print FILE '' . "\n"; print FILE '' . "\n"; print FILE "\nLast updated at $time\n"; print FILE "

$botnick\'s factoids

\n"; my $i = 0; my $table_id = 1; foreach my $channel (sort $factoids->get_keys) { next if not $factoids->get_keys($channel); my $chan = $factoids->get_data($channel, '_name'); $chan = 'global' if $chan eq '.*'; print FILE "" . encode_entities($chan) . "
\n"; } foreach my $channel (sort $factoids->get_keys) { next if not $factoids->get_keys($channel); my $chan = $factoids->get_data($channel, '_name'); $chan = 'global' if $chan eq '.*'; print FILE "\n"; print FILE "
\n

" . encode_entities($chan) . "

\n
\n"; print FILE "\n"; print FILE "\n\n"; print FILE "\n"; print FILE "\n"; print FILE "\n"; print FILE "\n"; print FILE "\n"; print FILE "\n"; print FILE "\n"; print FILE "\n"; print FILE "\n\n\n"; $table_id++; my $iter = $factoids->get_each("index1 = $channel", '_everything', '_sort = index1'); while (defined (my $factoid = $factoids->get_next($iter))) { my $trigger_name = $factoids->get_data($factoid->{index1}, $factoid->{index2}, '_name'); if ($factoid->{type} eq 'text') { $i++; if ($i % 2) { print FILE "\n"; } else { print FILE "\n"; } print FILE "\n"; print FILE "\n"; print FILE "\n"; my $action = $factoid->{'action'}; if ($action =~ m/https?:\/\/[^ ]+/) { $action =~ s/(.*?)http(s?:\/\/[^ ]+)/encode_entities($1) . "http" . encode_entities($2) . "<\/a>"/ge; $action =~ s/(.*)<\/a>(.*$)/"$1<\/a>" . encode_entities($2)/e; } else { $action = encode_entities($action); } if (defined $factoid->{'action_with_args'}) { my $with_args = $factoid->{'action_with_args'}; $with_args =~ s/(.*?)http(s?:\/\/[^ ]+)/encode_entities($1) . "http" . encode_entities($2) . "<\/a>"/ge; $with_args =~ s/(.*)<\/a>(.*$)/"$1<\/a>" . encode_entities($2)/e; print FILE "\n"; } else { print FILE "\n"; } if (defined $factoid->{'edited_by'}) { print FILE "\n"; print FILE "\n"; } else { print FILE "\n"; print FILE "\n"; } print FILE "\n"; if (defined $factoid->{'last_referenced_on'}) { print FILE "\n"; } else { print FILE "\n"; } print FILE "\n"; } } print FILE "\n
ownercreated ontimes referencedfactoidlast edited byedited datelast referenced bylast referenced date
" . encode_entities($factoid->{'owner'}) . "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $factoid->{'created_on'}) . "" . $factoid->{'ref_count'} . "" . encode_entities($trigger_name) . " is $action

with_args: " . encode_entities($with_args) . "
" . encode_entities($trigger_name) . " is $action" . $factoid->{'edited_by'} . "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $factoid->{'edited_on'}) . "" . encode_entities($factoid->{'ref_user'}) . "" . encode_entities(strftime "%Y/%m/%d %H:%M:%S", localtime $factoid->{'last_referenced_on'}) . "
\n"; } print FILE "
$i factoids memorized.
"; print FILE "
Last updated at $time\n"; print FILE "\n"; print FILE "\n\n"; close FILE; return "/say $i factoids exported."; } 1;