- 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 <b>$n</b> of <b>$num</b>: $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 <b>$quote->{qid}</b>: $quote->{quote}");
+ } else {
+ $c->reply("No quote with id <b>$1</b>");
+ }
+ } 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 <b>$quote->{qid}</b>: $quote->{quote}");
+ } else {
+ $c->reply("No quote matching <b>$1</b>");
+ }
+ } 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 <b>$quote->{qid}</b>: $quote->{quote}");
+ }