From 71c1c21d0e2bd84556f69e610874a1f5d06edfa4 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@gmail.com>
Date: Sun, 11 Aug 2013 14:18:14 +0200
Subject: [PATCH] New module commands::tag

---
 PlayBot.pl             | 23 +----------------------
 lib/commands/parser.pm | 15 ++++++++++-----
 lib/commands/tag.pm    | 28 ++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 27 deletions(-)
 create mode 100644 lib/commands/tag.pm

diff --git a/PlayBot.pl b/PlayBot.pl
index 7433934..13d5b70 100755
--- a/PlayBot.pl
+++ b/PlayBot.pl
@@ -101,18 +101,6 @@ sub flux
 }
 
 
-sub addTag
-{
-    my ($id, $tag) = @_;
-
-    my $sth = $dbh->prepare_cached('INSERT INTO playbot_tags (id, tag) VALUES (?, ?)');
-	$log->error("Couldn't prepare querie; aborting") unless (defined $sth);
-
-	$sth->execute($id, $tag)
-		or $log->error("Couldn't finish transaction: " . $dbh->errstr);
-}
-
-
 sub later
 {
 	my ($nick, $id) = @_[ARG0,ARG1];
@@ -303,17 +291,8 @@ sub on_speak
 	        $lastID = $id;
             $commands::parser::lastID = $id;
 
-
 	        # insertion des éventuels tags
-	        while ($msg =~ /#([a-zA-Z0-9_-]+)/g) {
-		        if ($debug) {
-			        $log->debug($1);
-			        next;
-		        }
-
-                addTag ($lastID, $1);
-            }
-
+            commands::parser::tag($msg);
 
 	        # message sur irc
 	        if (defined $content{'author'}) {
diff --git a/lib/commands/parser.pm b/lib/commands/parser.pm
index f170976..3a2f3bb 100644
--- a/lib/commands/parser.pm
+++ b/lib/commands/parser.pm
@@ -38,11 +38,10 @@ sub exec {
 
         commands::later::exec($id, $time, $unit);
 	}
-    elsif ($msg =~ /^!tag( +([0-9]+))?/) {
-        my $id = ($2) ? $2 : $lastID;
-        while ($msg =~ /#([a-zA-Z0-9_-]+)/g) {
-            addTag($id, $1);
-        }
+    elsif ($msg =~ /^!tag(?: +([0-9]+))?/) {
+        my $id = ($1) ? $1 : $lastID;
+
+        commands::tag($id, $msg);
     }
     elsif ($msg =~ /^!help/) {
 		$irc->yield(privmsg => $chan => '!fav [<id>] : enregistre la vidéo dans les favoris');
@@ -57,4 +56,10 @@ sub exec {
     return 1;
 }
 
+sub tag {
+    my ($msg) = @_;
+
+    commands::tag($lastID, $msg);
+}
+
 1;
diff --git a/lib/commands/tag.pm b/lib/commands/tag.pm
new file mode 100644
index 0000000..c9ce28b
--- /dev/null
+++ b/lib/commands/tag.pm
@@ -0,0 +1,28 @@
+package commands::later;
+
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw(exec);
+
+our $kernel;
+our $dbh;
+
+sub exec {
+    my ($id, $msg) = @_;
+    while ($msg =~ /#([a-zA-Z0-9_-]+)/g) {
+        addTag($id, $1);
+    }
+}
+
+sub addTag
+{
+    my ($id, $tag) = @_;
+
+    my $sth = $dbh->prepare_cached('INSERT INTO playbot_tags (id, tag) VALUES (?, ?)');
+	$log->error("Couldn't prepare querie; aborting") unless (defined $sth);
+
+	$sth->execute($id, $tag)
+		or $log->error("Couldn't finish transaction: " . $dbh->errstr);
+}
+
+1;
-- 
GitLab