play.pl: add -stop, fix bugs

This commit is contained in:
wowaname 2016-10-02 10:44:45 +00:00
parent 9b06530604
commit 6ba1657fd4

View File

@ -8,17 +8,18 @@ my $SCRIPT_AUTHOR = 'The Krusty Krab <wowaname@volatile.ch>';
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 = <FH>;
weechat::config_get_plugin('dir')."/$file")) {
while (<FH>) {
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': $!");