From 6ba1657fd47faf3efb93006abcaa68462b994f59 Mon Sep 17 00:00:00 2001 From: wowaname Date: Sun, 2 Oct 2016 10:44:45 +0000 Subject: [PATCH] play.pl: add -stop, fix bugs --- scripts/play.pl | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/scripts/play.pl b/scripts/play.pl index d1475b0..e283709 100644 --- a/scripts/play.pl +++ b/scripts/play.pl @@ -8,17 +8,18 @@ my $SCRIPT_AUTHOR = 'The Krusty Krab '; my $SCRIPT_VERSION = '1.0'; my $SCRIPT_LICENCE = 'Public domain'; my $SCRIPT_DESC = 'Play ASCII art'; -our ($pipe, @queue); +our (%queue, %timer); if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION, $SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) { weechat::hook_command('play', 'Play ASCII art', - '[-delay ms] [-find] [-pipe "command"] filename', + "[-delay ms] [-find] [-pipe \"command\"] filename\n-stop", "-delay: delay in milliseconds between lines\n". "-find: list matching files, don't play\n". "-pipe: pipe output into command\n". - "filename: file to play. Supports wildcards. By default, searches ". - "subdirectories as well unless '/' is found in the filename", + "filename: file to play. Supports wildcards. By default, searches\n". + " subdirectories as well unless '/' is found in the filename\n". + "-stop: stop currently playing file in buffer", '', 'cmd_play', ''); my %OPTIONS = ( @@ -58,17 +59,24 @@ sub parse { } sub play { - my ($buffer, $line) = (shift, (shift @queue) =~ s/[\r\n]*$//r); + my $buffer = shift; - weechat::command($buffer, "$pipe \x0f$line\x0f"); + weechat::command($buffer, shift @{ $queue{$buffer} }); + delete $queue{$buffer} unless @{ $queue{$buffer} }; return weechat::WEECHAT_RC_OK; } sub cmd_play { my $buffer = $_[1]; - my ($delay, $find, $file); - ($delay, $pipe, $find, $file) = parse($_[2]); + + if ($_[2] eq '-stop' and exists $timer{$buffer}) { + weechat::unhook($timer{$buffer}); + delete $queue{$buffer}; + return weechat::WEECHAT_RC_OK; + } + + my ($delay, $pipe, $find, $file) = parse($_[2]); my $server = weechat::info_get($buffer, 'localvar_server'); my ($prio_s, $prio_d) = ( weechat::config_get("irc.server.$server.anti_flood_prio_high"), @@ -89,10 +97,17 @@ sub cmd_play { } if ($path and open FH, "<", (($file !~ m"/") ? $path->match : - weechat::config_get_plugin('dir')."/$file")) { - @queue = ; + weechat::config_get_plugin('dir')."/$file")) { + while () { + s/[\r\n]*$//; + push @{ $queue{$buffer} }, "$pipe \x0f$_\x0f"; + } close FH; - weechat::hook_timer($delay, 0, scalar @queue, 'play', $buffer); + + weechat::unhook($timer{$buffer}) if exists $timer{$buffer}; + $timer{$buffer} = + weechat::hook_timer($delay, 0, scalar @{ $queue{$buffer} }, + 'play', $buffer); } else { weechat::print($buffer, weechat::prefix('error'). "Cannot open '$file': $!");