From cf8fc5e52f53abda2b2372fb8c337710155f9525 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Fri, 16 Aug 2019 19:19:38 +0200 Subject: [PATCH] Move quotes to database --- Commands/Quotes.pm | 116 +++++++++++++++++---------------------------- 1 file changed, 43 insertions(+), 73 deletions(-) diff --git a/Commands/Quotes.pm b/Commands/Quotes.pm index 0e50dd4..7c0f669 100644 --- a/Commands/Quotes.pm +++ b/Commands/Quotes.pm @@ -24,80 +24,54 @@ use feature ':5.10'; use MooseX::Declare; use NDIRC::Dispatcher; -use Tie::File; -use File::Temp (); - -tie my @FILE, 'Tie::File', "quote.txt"; -tie my @OLDFILE, 'Tie::File',"oldquotes.txt" or die "test"; command quote => { - help => q(syntax .quote [number] | if number isn't given, then a random quote is used), + help => q(syntax .quote [number|search term] | if number/search term isn't given, then a random quote is used), alias => q(lastquote), }, class extends NDIRC::Command { method execute($c,$msg) { - my ($n) = $msg =~ /(\d+)/; - - $n = $n-1 if defined $n; - $n //= int(rand($#FILE)); - $n = $#FILE if $self->name eq 'lastquote'; - - my $text = $FILE[$n]; - $text =~ s/(.*?)[\r\n]*$/$1/; - $n++; - my $num = $#FILE+1; - $c->reply("Quote $n of $num: $text"); + my $dbh = $c->model; + my $quote; + if ($msg =~ /(\d+)/) { + my $quote = $dbh->selectrow_hashref(q{SELECT qid,quote FROM quotes WHERE qid = ?}, undef, $1); + if ($quote) { + $c->reply("Quote $quote->{qid}: $quote->{quote}"); + } else { + $c->reply("No quote with id $1"); + } + } elsif ($msg =~ /^\s*(.+?)\s*$/) { + say $1; + my $quote = $dbh->selectrow_hashref(q{ +SELECT qid,quote FROM quotes +WHERE quote ILIKE '%' || $1 || '%' ORDER BY random() limit 1 + }, undef, $1); + if ($quote) { + $c->reply("Quote $quote->{qid}: $quote->{quote}"); + } else { + $c->reply("No quote matching $1"); + } + } else { + say "default"; + my $quote = $dbh->selectrow_hashref(q{ +SELECT qid,quote FROM quotes tablesample system(5) ORDER BY random() LIMIT 1 + }); + $c->reply("Quote $quote->{qid}: $quote->{quote}"); + } } }; command addquote => { help => q(syntax .addquote quote), }, class extends NDIRC::Command { - method execute($c,$quote) { - - die 'ARGS' unless $quote; - - push @FILE, $quote; - my $num = $#FILE+1; - $c->reply("Quote $num added"); - } -}; + method execute($c,$msg) { -command findquote => { - help => q(syntax .findquote pattern | findqre lets you use a regex pattern), - alias => q(findqre), -}, class extends NDIRC::Command { - my $file; - method execute($c,$pattern) { - die 'ARGS' unless $pattern; + my ($quote) = $msg =~ /^\s*(.+?)\s*$/ or die 'ARGS'; - my $matcher; - if ($self->name eq 'findqre'){ - if (defined (eval 'm/$pattern/ix')){ - $matcher = 'm/$pattern/ix'; - }else { - $c->reply("bad regexp"); - return; - } - }else{ - $matcher = '(index uc($_), uc($pattern)) != -1'; - } - $file = new File::Temp( SUFFIX => '.txt' ); - my $n = 1; - my $match = 0; - for (@FILE){ - chomp; - if (eval $matcher){ - $match = 1; - print $file "$n: $_\n"; - } - $n++; - } - if ($match){ - $file->flush; - $c->command(dcc => $c->nick => SEND => $file); - }else{ - $c->reply("No quotes matching $pattern."); - } + my $dbh = $c->model; + my ($qid) = $dbh->selectrow_array(q{ +INSERT INTO quotes (uid,quote) VALUES($1,$2) RETURNING qid + }, undef, $c->uid, $quote); + $c->reply("Quote $qid '$quote' added"); } }; @@ -107,18 +81,14 @@ command delquote => { }, class extends NDIRC::Command { method execute($c,$msg) { my ($n) = $msg =~ /(\d+)/ or die 'ARGS'; - $n = $n-1; - if (exists $FILE[$n]){ - my ($uid,$username) = $c->model->selectrow_array(q{ -SELECT uid,username FROM users where uid = ? - },undef,$c->uid); - - my $text = $FILE[$n]; - push @OLDFILE,"Removed by $username ($uid): $text"; - splice @FILE,$n,1; - $n++; - my $num = $#FILE+1; - $c->reply("Quote $n {$text} removed, number of quotes now: $num"); + my $dbh = $c->model; + my $quote = $dbh->selectrow_hashref(q{ +WITH q AS (DELETE FROM quotes WHERE qid = $1 RETURNING qid,time,uid,quote) +INSERT INTO removed_quotes (qid,time,uid,quote,removed_by) + SELECT *, $2 AS removed_by FROM q RETURNING quote; + }, undef, $n, $c->uid); + if ($quote){ + $c->reply("Quote $n {$quote->{quote}} removed"); }else{ $c->reply("No such quote."); } -- 2.39.2