From 7b9afb0c7b44815e0dff1baa9c9d4b3627b3399f Mon Sep 17 00:00:00 2001
From: Aorimn <aorimn@gmail.com>
Date: Sun, 13 Dec 2015 16:12:06 +0100
Subject: [PATCH] Ajout de commandes au module Kaamelott
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

kaamelott count: compte le nombre de quotes
kaamelott get: récupère une quote particulière
---
 Modules/Kaamelott.pm | 179 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 173 insertions(+), 6 deletions(-)

diff --git a/Modules/Kaamelott.pm b/Modules/Kaamelott.pm
index 6071479..abc5068 100644
--- a/Modules/Kaamelott.pm
+++ b/Modules/Kaamelott.pm
@@ -39,6 +39,8 @@ my $people = undef;
 #
 # Usage :
 #  !kaamelott [<personnage>] [<regex>]
+#  !kaamelott count [<personnage>] [<regex>]
+#  !kaamelott get [<personnage>] [<regex>] <id>
 # ###
 sub kaamelott_main
 {
@@ -51,11 +53,32 @@ sub kaamelott_main
 
 	if(defined($ref_params->[0]))
 	{
-		my $ret = kaamelott_quote_from_person_or_regex(
-			$reply_to,
-			$ref_params
-		);
-		return 0 unless $ret;
+		if($ref_params->[0] =~ /count/i)
+		{
+			shift @{$ref_params};
+			my $ret = kaamelott_count_quote_from_person_or_regex(
+				$reply_to,
+				$ref_params
+			);
+			return 0 unless $ret;
+		}
+		elsif($ref_params->[0] =~ /get/i)
+		{
+			shift @{$ref_params};
+			my $ret = kaamelott_get_quote_from_person_or_regex(
+				$reply_to,
+				$ref_params
+			);
+			return 0 unless $ret;
+		}
+		else
+		{
+			my $ret = kaamelott_quote_from_person_or_regex(
+				$reply_to,
+				$ref_params
+			);
+			return 0 unless $ret;
+		}
 	}
 	else
 	{
@@ -228,6 +251,147 @@ sub kaamelott_quote_from_person_or_regex
 } # Fin kaamelott_quote_from_person_or_regex
 
 
+# ###
+#  kaamelott_count_quote_from_person_or_regex
+# Récupération et comptage de quotes
+# ###
+sub kaamelott_count_quote_from_person_or_regex
+{
+	my ($reply_to, $ref_params) = @_;
+
+	my $count = 0;
+	if(!defined($ref_params->[0]))
+	{
+		$count = scalar(grep {defined $_} keys %{$quote_people});
+		$::displayer->sendto($reply_to, "There are currently $count quotes loaded");
+		return 1;
+	}
+
+	my $person = undef;
+	my $regex = qr/.*/;
+	my @quotes = keys %{$quote_people};
+
+	my $sanitized = kaamelott_sanitize_from_user($ref_params->[0]);
+	if(defined($people->{$sanitized}) and defined($person_quotes->{$people->{$sanitized}}))
+	{
+		shift @{$ref_params};
+		$person = $people->{$sanitized};
+		@quotes = keys %{$person_quotes->{$person}};
+	}
+
+	if(defined($ref_params->[0]))
+	{
+		my $tmp = join('\s+', @{$ref_params});
+		if(!eval{ $regex = qr/$tmp/i })
+		{
+			$::displayer->sendto($reply_to, "'$tmp' doesn't seem to be a correct regex");
+			return 1;
+		}
+	}
+
+	my @choices = ();
+	foreach my $quote (@quotes)
+	{
+		if($quote =~ $regex)
+		{
+			push @choices, $quote;
+		}
+	}
+
+	if(@choices)
+	{
+		$count = scalar(grep {defined $_} @choices);
+		my $quote = 'quote';
+		$quote .= 's' if($count > 1);
+		$::displayer->sendto($reply_to, "$count $quote found");
+	}
+	else
+	{
+		$::displayer->sendto($reply_to, 'No quote found');
+	}
+
+	return 1;
+} # Fin kaamelott_count_quote_from_person_or_regex
+
+
+# ###
+#  kaamelott_get_quote_from_person_or_regex
+# Récupération et affichage de quotes
+# ###
+sub kaamelott_get_quote_from_person_or_regex
+{
+	my ($reply_to, $ref_params) = @_;
+
+	return 0 unless defined($ref_params->[0]);
+
+	my $id = pop @{$ref_params};
+	return 0 unless $id =~ /^\d+$/;
+
+	my $person = undef;
+	my $regex = qr/.*/;
+	my @quotes = keys %{$quote_people};
+
+	if(!defined($ref_params->[0]))
+	{
+		if(defined($quotes[$id]))
+		{
+			kaamelott_display_quote($reply_to, $quotes[$id]);
+		}
+		else
+		{
+			$::displayer->sendto($reply_to, 'Quote not found');
+		}
+
+		return 1;
+	}
+
+	my $sanitized = kaamelott_sanitize_from_user($ref_params->[0]);
+	if(defined($people->{$sanitized}) and defined($person_quotes->{$people->{$sanitized}}))
+	{
+		shift @{$ref_params};
+		$person = $people->{$sanitized};
+		@quotes = keys %{$person_quotes->{$person}};
+	}
+
+	if(defined($ref_params->[0]))
+	{
+		my $tmp = join('\s+', @{$ref_params});
+		if(!eval{ $regex = qr/$tmp/i })
+		{
+			$::displayer->sendto($reply_to, "'$tmp' doesn't seem to be a correct regex");
+			return 1;
+		}
+	}
+
+	my @choices = ();
+	foreach my $quote (@quotes)
+	{
+		if($quote =~ $regex)
+		{
+			push @choices, $quote;
+		}
+	}
+
+	if(@choices)
+	{
+		if(defined($choices[$id]))
+		{
+			kaamelott_display_quote($reply_to, $choices[$id]);
+		}
+		else
+		{
+			$::displayer->sendto($reply_to, 'Quote not found');
+		}
+	}
+	else
+	{
+		$::displayer->sendto($reply_to, 'Quote not found');
+	}
+
+	return 1;
+} # Fin kaamelott_get_quote_from_person_or_regex
+
+
 # ###
 #  kaamelott_help
 # Affiche l'aide de la commande '!kaamelott'
@@ -239,7 +403,10 @@ sub kaamelott_help
 	$conn->privmsg($reply_to, "`".$Config::command_sign."kaamelott [<personnage>] [<regex>]` => "
 	                             ."Affiche une citation de Kaamelott, dans les citation de <personnage> si fourni, "
 	                             ."contenant <regex> si fourni (https://fr.wikiquote.org/wiki/Kaamelott)");
-
+	$conn->privmsg($reply_to, "`".$Config::command_sign."kaamelott count [<personnage>] [<regex>]` => "
+	                             ."Compte le nombre de citations de <personnage> contenant <regex>");
+	$conn->privmsg($reply_to, "`".$Config::command_sign."kaamelott get [<personnage>] [<regex>] <id>` => "
+	                             ."Affiche la citation numéro <id> de <personnage> contenant <regex>");
 	return 1;
 } # Fin join_help
 
-- 
GitLab