From cda40aec0afd15b8b823fafba349c42a67450f92 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@gmail.com>
Date: Thu, 30 Oct 2014 15:59:23 +0100
Subject: [PATCH] =?UTF-8?q?getUsers=20:=20qui=20va=20=C3=A0=20la=20xxx=20?=
 =?UTF-8?q?=3F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/commands/getUsers.pm | 60 ++++++++++++++++++++++++++++++++++++++++
 lib/commands/parser.pm   | 11 ++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 lib/commands/getUsers.pm

diff --git a/lib/commands/getUsers.pm b/lib/commands/getUsers.pm
new file mode 100644
index 0000000..9d537cd
--- /dev/null
+++ b/lib/commands/getUsers.pm
@@ -0,0 +1,60 @@
+package commands::getUsers;
+
+our $dbh;
+our $log;
+our $irc;
+
+my @insultes = ("Ahahahah ! 23 à 0 !", "C'est la piquette, Jack !", "Tu sais pas jouer, Jack !", "T'es mauvais, Jack !");
+
+sub exec {
+    my ($chan, $id) = @_;
+    my $sth;
+
+    $sth = $dbh->prepare_cached('
+        SELECT sam.title, sam_users.nick
+        FROM sam LEFT OUTER JOIN sam_users
+        ON sam.id = sam_users.event
+        WHERE sam.id = ?
+    ');
+
+    unless (defined $sth) {
+    	$log->error("Couldn't prepare querie; aborting");
+        return;
+    }
+
+	$sth->execute($id)
+		or $log->error("Couldn't finish transaction: " . $dbh->errstr);
+
+    # FIXME
+    # mixsql specific trick
+    if (!$sth->rows) {
+        $irc->yield(privmsg => $chan => $insultes[rand @insultes]);
+        return;
+    }
+
+    my $row = $sth->fetch;
+    my $title = $row->[0];
+    my @users;
+
+    do {
+        push @users, $row->[1] if $row->[1];
+    } while ($row = $sth->fetch);
+
+    my $msg;
+    if (scalar @users) {
+        $msg = $title;
+        $msg .= ' : ';
+
+        foreach (@users) {
+            $msg .= $_;
+            $msg .= ' ';
+        }
+    } else {
+        $msg = "Personne ne va à la soirée $title :(";
+    }
+
+    $irc->yield(privmsg => $chan => $msg);
+    return $id;
+}
+
+1;
diff --git a/lib/commands/parser.pm b/lib/commands/parser.pm
index 7fac6c0..64dabda 100644
--- a/lib/commands/parser.pm
+++ b/lib/commands/parser.pm
@@ -11,6 +11,7 @@ use commands::add;
 use commands::list;
 use commands::addUser;
 use commands::edit;
+use commands::getUsers;
 
 my $nick;
 my $irc;
@@ -23,15 +24,18 @@ sub setConf {
     $commands::list::dbh = $dbh;
     $commands::addUser::dbh = $dbh;
     $commands::edit::dbh = $dbh;
+    $commands::getUsers::dbh = $dbh;
 
     $commands::add::log = $log;
     $commands::list::log = $log;
     $commands::addUser::log = $log;
     $commands::edit::log = $log;
+    $commands::getUsers::log = $log;
 
     $commands::list::irc = $ircNew;
     $commands::addUser::irc = $ircNew;
     $commands::edit::irc = $ircNew;
+    $commands::getUsers::irc = $ircNew;
 
     $nick = $nickNew;
     $irc = $ircNew;
@@ -83,6 +87,13 @@ sub exec {
     elsif ($msg =~ /^Sam(?:,|:) +je +vais +à +la +(\d+)/) {
         commands::addUser::exec($nick, $1, $chan);
     }
+    elsif ($msg =~ /^Sam(?:,|:) +qui +va +à +la +(\d+) +?/) {
+        my $id = commands::getUsers::exec($chan->[0], $1);
+
+        if ($id) {
+            $lastID->{$chan->[0]} = $id;
+        }
+    }
     elsif ($msg =~ /^Sam(?:,|:) +liste/) {
         commands::list::exec($chan);
     }
-- 
GitLab