From: Michael Andreen Date: Fri, 15 May 2009 20:43:10 +0000 (+0200) Subject: Converted the quote commands X-Git-Url: https://ruin.nu/git/?a=commitdiff_plain;ds=sidebyside;h=fb91afb326b44a3e46fb92b060696aabc343e1cc;p=NDIRC.git Converted the quote commands --- diff --git a/Commands/Quotes.pm b/Commands/Quotes.pm new file mode 100644 index 0000000..c2625a8 --- /dev/null +++ b/Commands/Quotes.pm @@ -0,0 +1,124 @@ +#************************************************************************** +# Copyright (C) 2009 by Michael Andreen * +# * +# This program is free software; you can redistribute it and/or modify * +# it under the terms of the GNU General Public License as published by * +# the Free Software Foundation; either version 2 of the License, or * +# (at your option) any later version. * +# * +# This program is distributed in the hope that it will be useful, * +# but WITHOUT ANY WARRANTY; without even the implied warranty of * +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# GNU General Public License for more details. * +# * +# You should have received a copy of the GNU General Public License * +# along with this program; if not, write to the * +# Free Software Foundation, Inc., * +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * +#**************************************************************************/ + +package NDIRC::Commands::Quotes; + +use strict; +use warnings; +use feature ':5.10'; + +use Moose; +use MooseX::MethodAttributes; + +use Tie::File; +use File::Temp (); + +tie my @FILE, 'Tie::File', "quote.txt"; +tie my @OLDFILE, 'Tie::File',"oldquotes.txt" or die "test"; + +sub quote + : Help(syntax .quote [number] | if number isn't given, then a random quote is used) + : Alias(lastquote) +{ + my ($self,$c,$msg) = @_; + my ($n) = $msg =~ /(\d+)/; + + $n = $n-1 if defined $n; + $n //= int(rand($#FILE)); + $n = $#FILE if $self->name eq 'lastquote'; + + my $text = $FILE[$n]; + $text =~ s/(.*?)[\r\n]*$/$1/; + $n++; + my $num = $#FILE+1; + $c->reply("Quote $n of $num: $text"); +} + +sub addquote + : Help(syntax .addquote quote) +{ + my ($self,$c,$quote) = @_; + + die 'ARGS' unless $quote; + + push @FILE, $quote; + my $num = $#FILE+1; + $c->reply("Quote $num added"); +} + +sub findquote + : Help(syntax .findquote pattern | findqre lets you use a regex pattern) + : Alias(findqre) +{ + my ($self,$c,$pattern) = @_; + die 'ARGS' unless $pattern; + my $matcher; + if ($self->name eq 'findqre'){ + if (defined (eval 'm/$pattern/ix')){ + $matcher = 'm/$pattern/ix'; + }else { + $c->reply("bad regexp"); + return; + } + }else{ + $matcher = '(index uc($_), uc($pattern)) != -1'; + } + my $file = new File::Temp( SUFFIX => '.txt' ); + my $n = 1; + my $match = 0; + for (@FILE){ + chomp; + if (eval $matcher){ + $match = 1; + print $file "$n: $_\n"; + } + $n++; + } + if ($match){ + close $file; + $c->message("dcc send ".$c->nick, $file); + }else{ + $c->reply("No quotes matching $pattern."); + } +} + +sub delquote + : Help(syntax: .delquote number) + : ACL(irc_delquote) +{ + my ($self,$c,$msg) = @_; + my ($n) = $msg =~ /(\d+)/ or die 'ARGS'; + $n = $n-1; + if (exists $FILE[$n]){ + my ($uid,$username) = $c->model->selectrow_array(q{ +SELECT uid,username FROM users where hostmask ILIKE ? + },undef,$c->host); + + my $text = $FILE[$n]; + push @OLDFILE,"Removed by $username ($uid): $text"; + splice @FILE,$n,1; + $n++; + my $num = $#FILE+1; + $c->reply("Quote $n {$text} removed, number of quotes now: $num"); + }else{ + $c->reply("No such quote."); + } +} + +1; diff --git a/Quotes.pm b/Quotes.pm deleted file mode 100644 index 97aa231..0000000 --- a/Quotes.pm +++ /dev/null @@ -1,132 +0,0 @@ -#************************************************************************** -# Copyright (C) 2006 by Michael Andreen * -# * -# This program is free software; you can redistribute it and/or modify * -# it under the terms of the GNU General Public License as published by * -# the Free Software Foundation; either version 2 of the License, or * -# (at your option) any later version. * -# * -# This program is distributed in the hope that it will be useful, * -# but WITHOUT ANY WARRANTY; without even the implied warranty of * -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * -# GNU General Public License for more details. * -# * -# You should have received a copy of the GNU General Public License * -# along with this program; if not, write to the * -# Free Software Foundation, Inc., * -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * -#**************************************************************************/ -package NDIRC::Quotes; -use strict; -use warnings; -use NDIRC::Access; -use Tie::File; -use File::Temp (); -require Exporter; - -our @ISA = qw/Exporter/; - -our @EXPORT = qw/quote addQuote lastQuote findQuote delQuote/; - -tie our @FILE, 'Tie::File', "/home/ndawn/.eos/scripts/quote.txt"; -tie our @OLDFILE, 'Tie::File',"/home/ndawn/.eos/scripts/oldquotes.txt" or die "test"; - -sub quote { - my ($n) = @_; - if (defined $n && $n =~ /(\d+)/){ - $n = $1; - }else{ - $n = undef; - } - - $n = $n-1 if defined $n; - $n = int(rand($#FILE)) unless defined $n; - my $text = $FILE[$n]; - $text =~ s/(.*?)[\r\n]*$/$1/; - $n++; - my $num = $#FILE+1; - $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$num:$ND::B $text"); -} - -sub addQuote { - my ($quote) = @_; - unless (defined $quote){ - $ND::server->command("notice $ND::nick Usage: .addquote quote"); - return; - } - push @FILE, $quote; - my $num = $#FILE+1; - $ND::server->command("msg $ND::target Quote $ND::B$num$ND::B added"); -} -sub lastQuote { - my $n = $#FILE; - my $text = $FILE[$n]; - $text =~ s/(.*?)[\r\n]*$/$1/; - $n++; - $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B of $ND::B$n:$ND::B $text"); -} -sub findQuote { - my ($pattern,$type) = @_; - my $matcher; - unless (defined $pattern){ - $ND::server->command("notice $ND::nick $type query | findqre uses regexes as query, findquote a substring match. Both are case insensitive"); - return; - } - if ($type eq 'findqre'){ - if (defined (eval 'm/$pattern/ix')){ - $matcher = 'm/$pattern/ix'; - }else { - $ND::server->command("msg $ND::target bad regexp"); - close FILE; - return; - } - }else{ - $matcher = '(index uc($_), uc($pattern)) != -1'; - } - #mkdir "/tmp/quotes"; - #my $file = "/tmp/quotes/$ND::address.txt"; - #open(FILE,'>',"$file"); - my $file = new File::Temp( SUFFIX => '.txt' ); - my $n = 1; - my $match = 0; - for $_ (@FILE){ - chomp; - if (eval $matcher){ - $match = 1; - print $file "$n: $_\n"; - } - $n++; - } - if ($match){ - $ND::server->command("dcc send $ND::nick $file"); - }else{ - $ND::server->command("msg $ND::target $ND::nick: No quotes matching that."); - } -} -sub delQuote { - my ($n) = @_; - if (hc){ - if ($n =~ /^(\d+)$/){ - $n = $1; - }else{ - return - } - $n = $n-1; - if (exists $FILE[$n]){ - my ($uid,$username) = $ND::DBH->selectrow_array(q{SELECT uid,username FROM users where hostmask ILIKE ?} - ,undef,$ND::address); - my $text = $FILE[$n]; - push @OLDFILE,"Removed by $username ($uid): $text"; - splice @FILE,$n,1; - $n++; - my $num = $#FILE+1; - $ND::server->command("msg $ND::target Quote $ND::B$n$ND::B {$text} removed, number of quotes now: $ND::B$num$ND::B"); - }else{ - $ND::server->command("msg $ND::target No such quote."); - } - }else{ - $ND::server->command("msg $ND::target You don't have access to that!"); - } -} - -1; diff --git a/database/roles.sql b/database/roles.sql index 87dd813..9b0bbd2 100644 --- a/database/roles.sql +++ b/database/roles.sql @@ -6,6 +6,7 @@ INSERT INTO roles VALUES('irc_gs'); INSERT INTO roles VALUES('irc_scanreqs'); INSERT INTO roles VALUES('irc_scan'); INSERT INTO roles VALUES('irc_anonscan'); +INSERT INTO roles VALUES('irc_delquote'); INSERT INTO group_roles (gid,role) VALUES(1,'irc_p_nick'); INSERT INTO group_roles (gid,role) VALUES(1,'irc_p_intel'); @@ -13,6 +14,7 @@ INSERT INTO group_roles (gid,role) VALUES(1,'irc_masterop'); INSERT INTO group_roles (gid,role) VALUES(1,'irc_masterinvite'); INSERT INTO group_roles (gid,role) VALUES(1,'irc_scanreqs'); INSERT INTO group_roles (gid,role) VALUES(1,'irc_anonscan'); +INSERT INTO group_roles (gid,role) VALUES(1,'irc_delquote'); INSERT INTO group_roles (gid,role) VALUES(2,'irc_gs'); INSERT INTO group_roles (gid,role) VALUES(2,'irc_scan'); @@ -22,6 +24,7 @@ INSERT INTO group_roles (gid,role) VALUES(3,'irc_masterop'); INSERT INTO group_roles (gid,role) VALUES(3,'irc_masterinvite'); INSERT INTO group_roles (gid,role) VALUES(3,'irc_scanreqs'); INSERT INTO group_roles (gid,role) VALUES(3,'irc_anonscan'); +INSERT INTO group_roles (gid,role) VALUES(3,'irc_delquote'); INSERT INTO group_roles (gid,role) VALUES(5,'irc_p_nick'); diff --git a/ndawn.pl b/ndawn.pl index 62ec96d..d61640d 100644 --- a/ndawn.pl +++ b/ndawn.pl @@ -55,7 +55,7 @@ my $TICK = $DBH->selectrow_array('SELECT tick()'); my $disp = new NDIRC::Dispatcher; -$disp->load('Basic','PA','Channel','Scans'); +$disp->load('Basic','PA','Channel','Scans','Quotes'); $ND::scanchan = '#testarmer'; $disp->add_channel('#testarlite', ['pub','help','channel']);