]> ruin.nu Git - NDIRC.git/blob - Commands/Quotes.pm
Move quotes to database
[NDIRC.git] / Commands / Quotes.pm
1 #**************************************************************************
2 #   Copyright (C) 2009 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 use strict;
21 use warnings;
22 use feature ':5.10';
23
24 use MooseX::Declare;
25 use NDIRC::Dispatcher;
26
27
28 command quote => {
29         help => q(syntax .quote [number|search term] | if number/search term isn't given, then a random quote is used),
30         alias => q(lastquote),
31 }, class extends NDIRC::Command {
32         method execute($c,$msg) {
33                 my $dbh = $c->model;
34                 my $quote;
35                 if ($msg =~ /(\d+)/) {
36                         my $quote = $dbh->selectrow_hashref(q{SELECT qid,quote FROM quotes WHERE qid = ?},  undef, $1);
37                         if ($quote) {
38                                 $c->reply("Quote <b>$quote->{qid}</b>: $quote->{quote}");
39                         } else {
40                                 $c->reply("No quote with id <b>$1</b>");
41                         }
42                 } elsif ($msg =~ /^\s*(.+?)\s*$/) {
43                         say $1;
44                         my $quote = $dbh->selectrow_hashref(q{
45 SELECT qid,quote FROM quotes
46 WHERE quote ILIKE '%' || $1 || '%' ORDER BY random() limit 1
47                                 },  undef, $1);
48                         if ($quote) {
49                                 $c->reply("Quote <b>$quote->{qid}</b>: $quote->{quote}");
50                         } else {
51                                 $c->reply("No quote matching <b>$1</b>");
52                         }
53                 } else {
54                         say "default";
55                         my $quote = $dbh->selectrow_hashref(q{
56 SELECT qid,quote FROM quotes tablesample system(5) ORDER BY random() LIMIT 1
57                                 });
58                         $c->reply("Quote <b>$quote->{qid}</b>: $quote->{quote}");
59                 }
60         }
61 };
62
63 command addquote => {
64         help => q(syntax .addquote quote),
65 }, class extends NDIRC::Command {
66         method execute($c,$msg) {
67
68                 my ($quote) = $msg =~ /^\s*(.+?)\s*$/ or die 'ARGS';
69
70                 my $dbh = $c->model;
71                 my ($qid) = $dbh->selectrow_array(q{
72 INSERT INTO quotes (uid,quote) VALUES($1,$2) RETURNING qid
73                         },  undef, $c->uid, $quote);
74                 $c->reply("Quote <b>$qid</b> '$quote' added");
75         }
76 };
77
78 command delquote => {
79         help => q(syntax: .delquote number),
80         acl => q(irc_delquote),
81 }, class extends NDIRC::Command {
82         method execute($c,$msg) {
83                 my ($n) = $msg =~ /(\d+)/ or die 'ARGS';
84                 my $dbh = $c->model;
85                 my $quote = $dbh->selectrow_hashref(q{
86 WITH q AS (DELETE FROM quotes WHERE qid = $1 RETURNING qid,time,uid,quote)
87 INSERT INTO removed_quotes (qid,time,uid,quote,removed_by)
88         SELECT *, $2 AS removed_by FROM q RETURNING quote;
89                         },  undef, $n, $c->uid);
90                 if ($quote){
91                         $c->reply("Quote <b>$n</b> {$quote->{quote}} removed");
92                 }else{
93                         $c->reply("No such quote.");
94                 }
95         }
96 };
97
98 1;