]> ruin.nu Git - NDIRC.git/blob - Quotes.pm
Use http, which is redirected to the right url
[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         if (defined $n && $n =~ /(\d+)/){
37                 $n = $1;
38         }else{
39                 $n = undef;
40         }
41                 
42         $n = $n-1 if defined $n;
43         $n = int(rand($#FILE)) unless defined $n;
44         my $text = $FILE[$n];
45         $text =~ s/(.*?)[\r\n]*$/$1/;
46         $n++;
47         my $num = $#FILE+1;
48         $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$num:$ND::B $text");
49 }
50
51 sub addQuote {
52         my ($quote) = @_;
53         unless (defined $quote){
54                 $ND::server->command("notice $ND::nick Usage: .addquote quote");
55                 return;
56         }
57         push @FILE, $quote;
58         my $num = $#FILE+1;
59         $ND::server->command("msg $ND::target Quote $ND::B$num$ND::B added");
60 }
61 sub lastQuote {
62         my $n = $#FILE;
63         my $text = $FILE[$n];
64         $text =~ s/(.*?)[\r\n]*$/$1/;
65         $n++;
66         $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$n:$ND::B $text");
67 }
68 sub findQuote {
69         my ($pattern,$type) = @_;
70         my $matcher;
71         unless (defined $pattern){
72                 $ND::server->command("notice $ND::nick $type query | findqre uses regexes as query, findquote a substring match. Both are case insensitive");
73                 return;
74         }
75         if ($type eq 'findqre'){
76                 if (defined (eval 'm/$pattern/ix')){
77                         $matcher = 'm/$pattern/ix';
78                 }else {
79                         $ND::server->command("msg $ND::target bad regexp");
80                         close FILE;
81                         return;
82                 }
83         }else{
84                 $matcher = '(index uc($_), uc($pattern)) != -1';
85         }
86         #mkdir "/tmp/quotes";
87         #my $file = "/tmp/quotes/$ND::address.txt";
88         #open(FILE,'>',"$file");
89         my $file = new File::Temp( SUFFIX => '.txt' );
90         my $n = 1;
91         my $match = 0;
92         for $_ (@FILE){
93                 chomp;
94                 if (eval $matcher){
95                         $match = 1;
96                         print $file "$n: $_\n";
97                 }
98                 $n++;
99         }
100         if ($match){
101                 $ND::server->command("dcc send $ND::nick $file");
102         }else{
103                 $ND::server->command("msg $ND::target $ND::nick: No quotes matching that.");
104         }
105 }
106 sub delQuote {
107         my ($n) = @_;
108         if (hc){
109                 if ($n =~ /^(\d+)$/){
110                 $n = $1;
111                 }else{
112                         return
113                 }
114                 $n = $n-1;
115                 if (exists $FILE[$n]){
116                         my ($uid,$username) = $ND::DBH->selectrow_array(q{SELECT uid,username FROM users where hostmask ILIKE ?}
117                                 ,undef,$ND::address);
118                         my $text = $FILE[$n];
119                         push @OLDFILE,"Removed by $username ($uid): $text";
120                         splice @FILE,$n,1;
121                         $n++;
122                         my $num = $#FILE+1;
123                         $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B {$text} removed, number of quotes now: $ND::B$num$ND::B");
124                 }else{
125                         $ND::server->command("msg $ND::target No such quote.");
126                 }
127         }else{
128                 $ND::server->command("msg $ND::target You don't have access to that!");
129         }
130 }
131
132 1;