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