mirror of
https://github.com/perlbot/perlbuut
synced 2025-06-07 21:25:42 -04:00
more work on the factoid test query, fixes a bug where channels without config dont work
This commit is contained in:
parent
8e67d51e8a
commit
d620a8e4bb
3 changed files with 49 additions and 105 deletions
|
@ -1,13 +1,18 @@
|
||||||
WITH RECURSIVE factoid_lookup_order (depth, namespace, server, alias_namespace, alias_server, parent_namespace, parent_server, recursive) AS (
|
WITH RECURSIVE factoid_lookup_order_inner (depth, namespace, server, alias_namespace, alias_server, parent_namespace, parent_server, recursive) AS (
|
||||||
SELECT 0 AS depth, namespace, server, alias_namespace, alias_server, parent_namespace, parent_server, recursive
|
SELECT 0 AS depth, namespace, server, alias_namespace, alias_server, parent_namespace, parent_server, recursive
|
||||||
FROM factoid_config
|
FROM factoid_config
|
||||||
WHERE namespace = '#perlbot' AND server = 'freenode.net' -- PLACEHOLDER TARGET
|
WHERE namespace = '#perlbot' AND server = 'notfreenode.net' -- PLACEHOLDER TARGET
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT p.depth+1 AS depth, m.namespace, m.server, m.alias_namespace, m.alias_server, m.parent_namespace, m.parent_server, m.recursive
|
SELECT p.depth+1 AS depth, m.namespace, m.server, m.alias_namespace, m.alias_server, m.parent_namespace, m.parent_server, m.recursive
|
||||||
FROM factoid_config m
|
FROM factoid_config m
|
||||||
INNER JOIN factoid_lookup_order p
|
INNER JOIN factoid_lookup_order_inner p
|
||||||
ON m.namespace = p.parent_namespace AND m.server = p.parent_server AND p.recursive
|
ON m.namespace = p.parent_namespace AND m.server = p.parent_server AND p.recursive
|
||||||
),
|
),
|
||||||
|
factoid_lookup_order (depth, namespace, server, alias_namespace, alias_server, parent_namespace, parent_server, recursive) AS (
|
||||||
|
SELECT * FROM factoid_lookup_order_inner
|
||||||
|
UNION ALL
|
||||||
|
SELECT 0, '', '', NULL, NULL, NULL, NULL, false WHERE NOT EXISTS (table factoid_lookup_order_inner)
|
||||||
|
),
|
||||||
get_latest_factoid (depth, factoid_id, subject, copula, predicate, author, modified_time, compose_macro, protected, original_subject, deleted, server, namespace) AS (
|
get_latest_factoid (depth, factoid_id, subject, copula, predicate, author, modified_time, compose_macro, protected, original_subject, deleted, server, namespace) AS (
|
||||||
SELECT DISTINCT ON(lo.depth) lo.depth, factoid_id, subject, copula, predicate, author, modified_time, compose_macro, protected, original_subject, f.deleted, f.server, f.namespace
|
SELECT DISTINCT ON(lo.depth) lo.depth, factoid_id, subject, copula, predicate, author, modified_time, compose_macro, protected, original_subject, f.deleted, f.server, f.namespace
|
||||||
FROM factoid f
|
FROM factoid f
|
||||||
|
@ -17,7 +22,8 @@ get_latest_factoid (depth, factoid_id, subject, copula, predicate, author, modif
|
||||||
WHERE original_subject = 'hi' -- PLACEHOLDER TARGET
|
WHERE original_subject = 'hi' -- PLACEHOLDER TARGET
|
||||||
ORDER BY depth ASC, factoid_id DESC
|
ORDER BY depth ASC, factoid_id DESC
|
||||||
)
|
)
|
||||||
SELECT * FROM get_latest_factoid WHERE NOT deleted ORDER BY depth ASC, factoid_id DESC LIMIT 1;
|
SELECT * FROM get_latest_factoid WHERE NOT deleted ORDER BY depth ASC, factoid_id DESC;
|
||||||
|
--SELECT * FROM factoid_lookup_order;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,6 @@ use experimental 'signatures';
|
||||||
use feature 'postderef', 'fc';
|
use feature 'postderef', 'fc';
|
||||||
|
|
||||||
use DBI;
|
use DBI;
|
||||||
use DBD::SQLite;
|
|
||||||
use DBD::SQLite::BundledExtensions;
|
|
||||||
use IRC::Utils qw/lc_irc strip_color strip_formatting/;
|
use IRC::Utils qw/lc_irc strip_color strip_formatting/;
|
||||||
use Text::Metaphone;
|
use Text::Metaphone;
|
||||||
use strict;
|
use strict;
|
||||||
|
@ -82,61 +80,35 @@ sub dbh($self) {
|
||||||
return $dbh;
|
return $dbh;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_conf_for_channel ($self, $pm, $server, $channel) {
|
sub get_namespace($self, $said) {
|
||||||
|
my ($server, $channel) = $said->@{qw/server channel/};
|
||||||
|
|
||||||
# TODO this needs to use the tables now
|
$server = s/^.*?([^\.]\.[^\.]+)$/$1/;
|
||||||
my $gc = sub {$pm->plugin_conf($_[0], $server, $channel)};
|
|
||||||
|
return ($server, $channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub get_alias_namespace($self, $said) {
|
||||||
|
my $conf = $self->get_conf_for_channel($said);
|
||||||
|
|
||||||
|
my $server = $conf->{alias_server} // $conf->{server};
|
||||||
|
my $namespace = $conf->{alias_namespace} // $conf->{namespace};
|
||||||
|
|
||||||
|
return ($server, $namespace);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub get_conf_for_channel ($self, $said) {
|
||||||
|
my ($server, $namespace) = $self->get_namespace($said);
|
||||||
|
|
||||||
|
my $dbh = $self->{dbh};
|
||||||
|
|
||||||
|
my $result = $dbh->selectrow_hashref(qq{
|
||||||
|
SELECT * FROM factoid_config WHERE server = ? AND namespace = ? LIMIT 1
|
||||||
|
}, {}, $server, $name);
|
||||||
|
|
||||||
# Load factoids if it exists, otherwise grab the old nfacts setup
|
|
||||||
my $conf = $gc->("factoids");
|
|
||||||
return $conf;
|
return $conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
# This must go away instead
|
|
||||||
# TODO
|
|
||||||
sub __get_namespaced_factoid {
|
|
||||||
my ($self, $pm, $body, $said, $forcechan, $forceserver) = @_;
|
|
||||||
my $command;
|
|
||||||
|
|
||||||
my ($channel, $server) = @{$said}{qw/channel server/};
|
|
||||||
$server =~ s/^.*?([^.]+\.(?:com|net|co.uk|org|bot|info))$/$1/; # grab just the domain and tld, will expand to more domains later
|
|
||||||
|
|
||||||
$channel = $forcechan // $channel;
|
|
||||||
$server = $forceserver // $server;
|
|
||||||
|
|
||||||
return $body if $channel eq '*irc_msg' or $channel eq '##NULL';
|
|
||||||
|
|
||||||
if ($body =~ /^(?:\s*(?<command>$commands_re|macro)\s+)?(?<body>.*)$/) {
|
|
||||||
|
|
||||||
#my ($command, $body);
|
|
||||||
($command, $body) = @+{qw/command body/};
|
|
||||||
}
|
|
||||||
|
|
||||||
open(my $fh, ">/tmp/notwut");
|
|
||||||
|
|
||||||
print $fh "NAMESPACE: [ $channel , $server ]";
|
|
||||||
|
|
||||||
my $conf = $self->get_conf_for_channel($pm, $said->{server}, $channel);
|
|
||||||
|
|
||||||
print $fh Dumper($conf);
|
|
||||||
|
|
||||||
my $realserver = $conf->{serverspace} // $server;
|
|
||||||
my $realchannel = $conf->{chanspace} // $channel;
|
|
||||||
|
|
||||||
print $fh Dumper($realserver, $realchannel);
|
|
||||||
|
|
||||||
return ($realserver, $realchannel, $body);
|
|
||||||
}
|
|
||||||
|
|
||||||
# TODO remove this
|
|
||||||
sub __namespace_filter {
|
|
||||||
my ($self, $body, $enabled) = @_;
|
|
||||||
|
|
||||||
return $body =~ s|$fsep[^$fsep]*?$fsep[^$fsep]*?$fsep(\S+)|$1|rg
|
|
||||||
if $enabled;
|
|
||||||
$body;
|
|
||||||
}
|
|
||||||
|
|
||||||
# TODO update this to use the new table layout once it's ready
|
# TODO update this to use the new table layout once it's ready
|
||||||
sub postload {
|
sub postload {
|
||||||
my ($self, $pm) = @_;
|
my ($self, $pm) = @_;
|
||||||
|
@ -173,60 +145,20 @@ sub postload {
|
||||||
sub command ($self, $_said, $pm) {
|
sub command ($self, $_said, $pm) {
|
||||||
my $said = +{ $_said->%* };
|
my $said = +{ $_said->%* };
|
||||||
|
|
||||||
my $conf = $self->get_conf_for_channel($pm, $said->{server}, $said->{channel});
|
if ($said->{channel} eq '*irc_msg') {
|
||||||
|
|
||||||
open(my $fh, ">/tmp/wut");
|
|
||||||
print $fh "COMMAND INCOMING\n";
|
|
||||||
print $fh Dumper($conf);
|
|
||||||
print $fh Dumper($said);
|
|
||||||
|
|
||||||
my $response; #namespaced factoids have no fallback
|
|
||||||
my ($realchannel, $realserver);
|
|
||||||
|
|
||||||
# I want to rework this TODO
|
|
||||||
|
|
||||||
if ($conf->{namespaced} || $said->{channel} eq '*irc_msg') {
|
|
||||||
|
|
||||||
# Parse body here
|
# Parse body here
|
||||||
my $body = $said->{body};
|
my $body = $said->{body};
|
||||||
$said->{channel} = "##NULL" if $said->{channel} eq '*irc_msg';
|
$said->{channel} = "##NULL" if $said->{channel} eq '*irc_msg';
|
||||||
|
|
||||||
if ($body =~ /^(?<command>$commands_re)\s+(?<fact>.*)$/) {
|
|
||||||
my ($command, $fact) = @+{qw/command fact/};
|
|
||||||
|
|
||||||
print $fh "Got command $command :: $fact\n";
|
|
||||||
|
|
||||||
# handle a channel prefix on everything
|
|
||||||
if ($fact =~ /^\s*(?<channel>#\S+)\s+(?<fact>.*)$/) {
|
|
||||||
$said->{channel} = $+{channel};
|
|
||||||
$body = $+{fact};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($said->{channel} ne '##NULL') { # fuck ##NULL, they don't get factoids
|
|
||||||
($realserver, $realchannel) = $self->get_namespaced_factoid($pm, $fact, $said);
|
|
||||||
print $fh "New body is $body\n";
|
|
||||||
} else {
|
|
||||||
$body = $command . " " . $body;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
|
|
||||||
# handle a channel prefix on everything
|
|
||||||
if ($body =~ /^\s*(?<channel>#\S+)\s+(?<fact>.*)$/) {
|
if ($body =~ /^\s*(?<channel>#\S+)\s+(?<fact>.*)$/) {
|
||||||
$said->{channel} = $+{channel};
|
$said->{channel} = $+{channel};
|
||||||
$body = $+{fact};
|
$said->{body} = $+{fact};
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($said->{channel} ne '##NULL') { # fuck ##NULL, they don't get factoids
|
# TODO does this need to support parsing the command out again?
|
||||||
($realserver, $realchannel) = $self->get_namespaced_factoid($pm, $body, $said);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
print $fh Dumper($said);
|
my ($handled, $fact_out) = $self->sub_command($said, $pm);
|
||||||
|
|
||||||
my ($handled, $fact_out) = $self->sub_command($said, $pm, $realchannel, $realserver);
|
|
||||||
|
|
||||||
$fact_out = $self->namespace_filter($fact_out, $conf->{filtersep});
|
|
||||||
|
|
||||||
return ($handled, $fact_out);
|
return ($handled, $fact_out);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
||||||
|
|
||||||
BEGIN;
|
BEGIN;
|
||||||
DROP TABLE IF EXISTS public.factoid;
|
DROP TABLE IF EXISTS public.factoid;
|
||||||
CREATE TABLE public.factoid AS (SELECT * FROM sqlite.factoid);
|
CREATE TABLE public.factoid AS (SELECT * FROM sqlite.factoid);
|
||||||
|
@ -42,4 +44,8 @@ INSERT INTO public.factoid_config (server, namespace, alias_server, alias_namesp
|
||||||
('freenode.net', '#regex', 'freenode.net', '#regex', false, '!'),
|
('freenode.net', '#regex', 'freenode.net', '#regex', false, '!'),
|
||||||
('freenode.net', '#regexen', 'freenode.net', '#regex', false, '!');
|
('freenode.net', '#regexen', 'freenode.net', '#regex', false, '!');
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS factoid_original_subject_lookup_idx ON public.factoid (original_subject);
|
||||||
|
CREATE INDEX IF NOT EXISTS factoid_original_subject_trigram_idx ON public.factoid USING GIN(original_subject gin_trgm_ops);
|
||||||
|
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|
Loading…
Add table
Reference in a new issue