3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-27 06:19:25 +01:00
pbot/misc/greybot/import-greybot.pl

139 lines
4.1 KiB
Perl
Raw Normal View History

2021-05-29 23:03:26 +02:00
#!/usr/bin/env perl
# quick-and-dirty script to import greybot factoids.
#
# http://wooledge.org/~greybot/
#
# Disclaimer: this import script really IS quick-and-dirty. some bits
# of code are copied out of the PBot tree. ideally, we would instead update
# the code in the PBot tree to be more library-like.
#
# but that's a task for another day. right now i just want to get these
# greybot factoids imported As-Soon-As-Possible. isn't technical debt fun?
#
# if this script is used again in the future, ensure the PBot snippets
# that were copied over are not out-dated. time-permitting, implement
# all the TODOs in this script.
2021-05-29 23:03:26 +02:00
use warnings;
use strict;
use File::Basename;
# TODO: DualIndexSQLiteObject is the current factoids database API. It expects
# a PBot instance. We have "temporarily" copied it out of the PBot source tree
# and modified it for this import script. Ideally we should instead modify the
# file in the PBot tree.
use lib '.';
2021-05-29 23:03:26 +02:00
use PBot::DualIndexSQLiteObject;
2021-06-08 07:13:42 +02:00
# skip these factoids since they are used by
# candide for other purposes.
my @skip = qw/bash ksh check bashfaq bashpf faq pf/;
2021-05-29 23:03:26 +02:00
# dirtily copied from PBot/Factoids.pm for now.
# TODO: we should use PBot::Factoids instead.
2021-05-29 23:03:26 +02:00
my %factoid_metadata = (
'action' => 'TEXT',
'action_with_args' => 'TEXT',
'add_nick' => 'INTEGER',
'allow_empty_args' => 'INTEGER',
'background-process' => 'INTEGER',
'cap-override' => 'TEXT',
'created_on' => 'NUMERIC',
'dont-protect-self' => 'INTEGER',
'dont-replace-pronouns' => 'INTEGER',
'edited_by' => 'TEXT',
'edited_on' => 'NUMERIC',
'enabled' => 'INTEGER',
'help' => 'TEXT',
'interpolate' => 'INTEGER',
'keyword_override' => 'TEXT',
'last_referenced_in' => 'TEXT',
'last_referenced_on' => 'NUMERIC',
'locked' => 'INTEGER',
'locked_to_channel' => 'INTEGER',
'no_keyword_override' => 'INTEGER',
'noembed' => 'INTEGER',
'nooverride' => 'INTEGER',
'owner' => 'TEXT',
'persist-key' => 'INTEGER',
'preserve_whitespace' => 'INTEGER',
'process-timeout' => 'INTEGER',
'rate_limit' => 'INTEGER',
'ref_count' => 'INTEGER',
'ref_user' => 'TEXT',
'require_explicit_args' => 'INTEGER',
'requires_arguments' => 'INTEGER',
'type' => 'TEXT',
'unquote_spaces' => 'INTEGER',
'usage' => 'TEXT',
'use_output_queue' => 'INTEGER',
'workdir' => 'TEXT',
);
my $factoids = PBot::DualIndexSQLiteObject->new(name => 'Factoids', filename => './greybot.sqlite3');
$factoids->load;
$factoids->create_metadata(\%factoid_metadata);
2021-05-29 23:03:26 +02:00
$factoids->begin_work;
2021-05-29 23:03:26 +02:00
my @files = glob 'meta/*';
foreach my $file (sort @files) {
my $factoid = basename $file;
if (grep { $_ eq $factoid } @skip) {
print "$factoid: skipping factoid.\n";
2021-05-29 23:03:26 +02:00
next;
}
open my $fh, '< :encoding(UTF-8)', $file or die "Couldn't open $file: $!\n";
2021-05-29 23:03:26 +02:00
my @lines = <$fh>;
my $data = $lines[$#lines];
chomp $data;
2021-05-29 23:03:26 +02:00
my ($nick, $timestamp, $command, $text) = split /\s/, $data, 4;
2021-05-29 23:03:26 +02:00
if (not defined $nick or not defined $timestamp or not defined $command) {
print "$factoid: could not parse: $data\n";
die;
}
2021-05-29 23:03:26 +02:00
if (lc $command eq 'forget') {
print "$factoid: skipping deleted factoid\n";
2021-05-29 23:03:26 +02:00
next;
}
if (not defined $text) {
print "$factoid: missing text\n";
die;
}
print "nick: $nick, timestamp: $timestamp, cmd: $command, text: [$text]\n" if $ENV{DEBUG};
2021-05-29 23:03:26 +02:00
if ($text =~ m/^#redirect (.*)/i) {
$text = "/call $1";
} else {
$text = "/say $text";
}
print "adding factoid: $factoid is \"$text\"\n" if $ENV{DEBUG};
2021-05-29 23:03:26 +02:00
$data = {
2021-05-29 23:03:26 +02:00
enabled => 1,
type => 'text',
action => $text,
owner => $nick,
created_on => $timestamp,
ref_count => 0,
ref_user => "nobody",
};
$factoids->add('#bash', $factoid, $data);
2021-05-29 23:03:26 +02:00
}
$factoids->end;