From 2aee9821e508b1d84b6b6bfc138fbdc54c7e8cca Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@gmail.com>
Date: Fri, 9 Aug 2013 00:14:36 +0200
Subject: [PATCH] Testing if the message is a command before searching url

---
 PlayBot.pl             | 103 ++++++++++++++++++++---------------------
 lib/commands/parser.pm |   5 ++
 2 files changed, 56 insertions(+), 52 deletions(-)

diff --git a/PlayBot.pl b/PlayBot.pl
index d2c6129..a17d8a8 100755
--- a/PlayBot.pl
+++ b/PlayBot.pl
@@ -266,61 +266,60 @@ sub on_speak
 	my ($nick,$mask) = split(/!/,$user);
 	my %content;
 
-	%content = sites::parser::parse($msg);
+    if (!commands::parser::exec(@args)) {
+	    %content = sites::parser::parse($msg);
 
-    if ($@) {
-        $log->warning ($@);
-        return;
-    }
-
-    if (%content) {
-	    if ($debug) {
-		    $log->debug($content{'url'});
-	    }
-	    else {
-		    # insertion de la vidéo dans la bdd
-		    my $sth = $dbh->prepare_cached('INSERT INTO playbot (date, type, url, sender_irc, sender, title, chan) VALUES (NOW(),?,?,?,?,?,?)');
-		    $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
-
-		    $sth->execute($content{'site'}, $content{'url'}, $nick, $content{'author'}, $content{'title'}, $chan->[0])
-			    or $log->error("Couldn't finish transaction: " . $dbh->errstr);
-	    }
-
-	    # sélection de l'id de la vidéo insérée
-	    my $id = $dbh->{mysql_insert_id};
-	    if (!$id) {
-		    my $sth = $dbh->prepare_cached('SELECT id FROM playbot WHERE url = ?');
-		    $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
-
-		    $sth->execute($content{'url'})
-			    or $log->error("Couldn't finish transaction: " . $dbh->errstr);
-
-		    $id = $sth->fetch->[0];
-	    }
-	    $lastID = $id;
-
-
-	    # insertion des éventuels tags
-	    while ($msg =~ /#([a-zA-Z0-9_-]+)/g) {
-		    if ($debug) {
-			    $log->debug($1);
-			    next;
-		    }
-
-            addTag ($lastID, $1);
+        if ($@) {
+            $log->warning ($@);
+            return;
         }
 
-
-	    # message sur irc
-	    if (defined $content{'author'}) {
-		    $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}.' | '.$content{'author'}) ;
-	    }
-	    else {
-		    $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}) ;
-	    }
-    }
-    else {
-        commands::parser::exec(@args);
+        if (%content) {
+	        if ($debug) {
+		        $log->debug($content{'url'});
+	        }
+	        else {
+		        # insertion de la vidéo dans la bdd
+		        my $sth = $dbh->prepare_cached('INSERT INTO playbot (date, type, url, sender_irc, sender, title, chan) VALUES (NOW(),?,?,?,?,?,?)');
+		        $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
+
+		        $sth->execute($content{'site'}, $content{'url'}, $nick, $content{'author'}, $content{'title'}, $chan->[0])
+			        or $log->error("Couldn't finish transaction: " . $dbh->errstr);
+	        }
+
+	        # sélection de l'id de la vidéo insérée
+	        my $id = $dbh->{mysql_insert_id};
+	        if (!$id) {
+		        my $sth = $dbh->prepare_cached('SELECT id FROM playbot WHERE url = ?');
+		        $log->error("Couldn't prepare querie; aborting") unless (defined $sth);
+
+		        $sth->execute($content{'url'})
+			        or $log->error("Couldn't finish transaction: " . $dbh->errstr);
+
+		        $id = $sth->fetch->[0];
+	        }
+	        $lastID = $id;
+
+
+	        # insertion des éventuels tags
+	        while ($msg =~ /#([a-zA-Z0-9_-]+)/g) {
+		        if ($debug) {
+			        $log->debug($1);
+			        next;
+		        }
+
+                addTag ($lastID, $1);
+            }
+
+
+	        # message sur irc
+	        if (defined $content{'author'}) {
+		        $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}.' | '.$content{'author'}) ;
+	        }
+	        else {
+		        $irc->yield(privmsg => $chan => '['.$id.'] '.$content{'title'}) ;
+	        }
+        }
     }	
 }
 
diff --git a/lib/commands/parser.pm b/lib/commands/parser.pm
index d18fa55..4194ff6 100644
--- a/lib/commands/parser.pm
+++ b/lib/commands/parser.pm
@@ -53,6 +53,11 @@ sub exec {
 		$irc->yield(privmsg => $chan => '!later [<id>] [in <x>[s|m|h]] : vidéo rappelée par query (par défaut temps de 6h)');
 		$irc->yield(privmsg => $chan => 'Sans id précisée, la dernière vidéo postée est utilisée.');
     }
+    else {
+        return 0;
+    }
+
+    return 1;
 }
 
 1;
-- 
GitLab