]> ruin.nu Git - NDIRC.git/blob - Quotes.pm
84514801af0a2667571765e7d19233be5a3b1157
[NDIRC.git] / Quotes.pm
1 #**************************************************************************
2 #   Copyright (C) 2006 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 package NDIRC::Quotes;
20 use strict;
21 use warnings;
22 use NDIRC::Access;
23 use Tie::File;
24 use File::Temp ();
25 require Exporter;
26
27 our @ISA = qw/Exporter/;
28
29 our @EXPORT = qw/quote addQuote lastQuote findQuote delQuote/;
30
31 tie our @FILE, 'Tie::File', "/home/ndawn/.eos/scripts/quote.txt";
32 tie our @OLDFILE, 'Tie::File',"/home/ndawn/.eos/scripts/oldquotes.txt" or die "test";
33
34 sub quote {
35         my ($n) = @_;
36         $n = $n-1 if defined $n;
37         $n = int(rand($#FILE)) unless defined $n;
38         my $text = $FILE[$n];
39         $text =~ s/(.*?)[\r\n]*$/$1/;
40         $n++;
41         my $num = $#FILE+1;
42         $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$num:$ND::B $text");
43 }
44
45 sub addQuote {
46         my ($quote) = @_;
47         push @FILE, $quote;
48         my $num = $#FILE+1;
49         $ND::server->command("msg $ND::target Quote $ND::B$num$ND::B added");
50 }
51 sub lastQuote {
52         my $n = $#FILE;
53         my $text = $FILE[$n];
54         $text =~ s/(.*?)[\r\n]*$/$1/;
55         $n++;
56         $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$n:$ND::B $text");
57 }
58 sub findQuote {
59         my ($type,$pattern) = @_;
60         my $matcher;
61         if ($type eq 'qre'){
62                 if (defined (eval 'm/$pattern/ix')){
63                         $matcher = 'm/$pattern/ix';
64                 }else {
65                         $ND::server->command("msg $ND::target bad regexp");
66                         close FILE;
67                         return;
68                 }
69         }else{
70                 $matcher = '(index uc($_), uc($pattern)) != -1';
71         }
72         #mkdir "/tmp/quotes";
73         #my $file = "/tmp/quotes/$ND::address.txt";
74         #open(FILE,'>',"$file");
75         my $file = new File::Temp( SUFFIX => '.txt' );
76         my $n = 1;
77         my $match = 0;
78         for $_ (@FILE){
79                 chomp;
80                 if (eval $matcher){
81                         $match = 1;
82                         print $file "$n: $_\n";
83                 }
84                 $n++;
85         }
86         if ($match){
87                 $ND::server->command("dcc send $ND::nick $file");
88         }else{
89                 $ND::server->command("msg $ND::target $ND::nick: No quotes matching that.");
90         }
91 }
92 sub delQuote {
93         my ($n) = @_;
94         if (hc){
95                 $n = $n-1;
96                 if (exists $FILE[$n]){
97                         my ($uid,$username) = $ND::DBH->selectrow_array(q{SELECT uid,username FROM users where hostmask ILIKE ?}
98                                 ,undef,$ND::address);
99                         my $text = $FILE[$n];
100                         push @OLDFILE,"Removed by $username ($uid): $text";
101                         splice @FILE,$n,1;
102                         $n++;
103                         my $num = $#FILE+1;
104                         $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B {$text} removed, number of quotes now: $ND::B$num$ND::B");
105                 }else{
106                         $ND::server->command("msg $ND::target No such quote.");
107                 }
108         }else{
109                 $ND::server->command("msg $ND::target You don't have access to that!");
110         }
111 }
112
113 1;