]> ruin.nu Git - NDIRC.git/blob - Commands/Quotes.pm
608832f9d38094c40173c8d92c0800105acf7d10
[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 package NDIRC::Commands::Quotes;
21
22 use strict;
23 use warnings;
24 use feature ':5.10';
25
26 use Moose;
27 use MooseX::MethodAttributes;
28
29 use Tie::File;
30 use File::Temp ();
31
32 tie my @FILE, 'Tie::File', "quote.txt";
33 tie my @OLDFILE, 'Tie::File',"oldquotes.txt" or die "test";
34
35 sub quote
36         : Help(syntax .quote [number] | if number isn't given, then a random quote is used)
37         : Alias(lastquote)
38 {
39         my ($self,$c,$msg) = @_;
40         my ($n) = $msg =~ /(\d+)/;
41
42         $n = $n-1 if defined $n;
43         $n //= int(rand($#FILE));
44         $n = $#FILE if $self->name eq 'lastquote';
45
46         my $text = $FILE[$n];
47         $text =~ s/(.*?)[\r\n]*$/$1/;
48         $n++;
49         my $num = $#FILE+1;
50         $c->reply("Quote <b>$n</b> of <b>$num</b>: $text");
51 }
52
53 sub addquote
54         : Help(syntax .addquote quote)
55 {
56         my ($self,$c,$quote) = @_;
57
58         die 'ARGS' unless $quote;
59
60         push @FILE, $quote;
61         my $num = $#FILE+1;
62         $c->reply("Quote <b>$num</b> added");
63 }
64
65 sub findquote
66         : Help(syntax .findquote pattern | findqre lets you use a regex pattern)
67         : Alias(findqre)
68 {
69         my ($self,$c,$pattern) = @_;
70         die 'ARGS' unless $pattern;
71
72         my $matcher;
73         if ($self->name eq 'findqre'){
74                 if (defined (eval 'm/$pattern/ix')){
75                         $matcher = 'm/$pattern/ix';
76                 }else {
77                         $c->reply("bad regexp");
78                         return;
79                 }
80         }else{
81                 $matcher = '(index uc($_), uc($pattern)) != -1';
82         }
83         my $file = new File::Temp( SUFFIX => '.txt' );
84         my $n = 1;
85         my $match = 0;
86         for (@FILE){
87                 chomp;
88                 if (eval $matcher){
89                         $match = 1;
90                         print $file "$n: $_\n";
91                 }
92                 $n++;
93         }
94         if ($match){
95                 $file->flush;
96                 $c->command(dcc => $c->nick => SEND => $file);
97         }else{
98                 $c->reply("No quotes matching <b>$pattern.</b>");
99         }
100 }
101
102 sub delquote
103         : Help(syntax: .delquote number)
104         : ACL(irc_delquote)
105 {
106         my ($self,$c,$msg) = @_;
107         my ($n) = $msg =~ /(\d+)/ or die 'ARGS';
108         $n = $n-1;
109         if (exists $FILE[$n]){
110                 my ($uid,$username) = $c->model->selectrow_array(q{
111 SELECT uid,username FROM users where uid = ?
112                         },undef,$c->uid);
113
114                 my $text = $FILE[$n];
115                 push @OLDFILE,"Removed by $username ($uid): $text";
116                 splice @FILE,$n,1;
117                 $n++;
118                 my $num = $#FILE+1;
119                 $c->reply("Quote <b>$n</b> {$text} removed, number of quotes now: <b>$num</b>");
120         }else{
121                 $c->reply("No such quote.");
122         }
123 }
124
125 1;