play.pl: add -stop, fix bugs
This commit is contained in:
parent
9b06530604
commit
6ba1657fd4
@ -8,17 +8,18 @@ my $SCRIPT_AUTHOR = 'The Krusty Krab <wowaname@volatile.ch>';
|
|||||||
my $SCRIPT_VERSION = '1.0';
|
my $SCRIPT_VERSION = '1.0';
|
||||||
my $SCRIPT_LICENCE = 'Public domain';
|
my $SCRIPT_LICENCE = 'Public domain';
|
||||||
my $SCRIPT_DESC = 'Play ASCII art';
|
my $SCRIPT_DESC = 'Play ASCII art';
|
||||||
our ($pipe, @queue);
|
our (%queue, %timer);
|
||||||
|
|
||||||
if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION,
|
if (weechat::register($SCRIPT_NAME, $SCRIPT_AUTHOR, $SCRIPT_VERSION,
|
||||||
$SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) {
|
$SCRIPT_LICENCE, $SCRIPT_DESC, '', '')) {
|
||||||
weechat::hook_command('play', 'Play ASCII art',
|
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".
|
"-delay: delay in milliseconds between lines\n".
|
||||||
"-find: list matching files, don't play\n".
|
"-find: list matching files, don't play\n".
|
||||||
"-pipe: pipe output into command\n".
|
"-pipe: pipe output into command\n".
|
||||||
"filename: file to play. Supports wildcards. By default, searches ".
|
"filename: file to play. Supports wildcards. By default, searches\n".
|
||||||
"subdirectories as well unless '/' is found in the filename",
|
" subdirectories as well unless '/' is found in the filename\n".
|
||||||
|
"-stop: stop currently playing file in buffer",
|
||||||
'', 'cmd_play', '');
|
'', 'cmd_play', '');
|
||||||
|
|
||||||
my %OPTIONS = (
|
my %OPTIONS = (
|
||||||
@ -58,17 +59,24 @@ sub parse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sub play {
|
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;
|
return weechat::WEECHAT_RC_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub cmd_play {
|
sub cmd_play {
|
||||||
my $buffer = $_[1];
|
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 $server = weechat::info_get($buffer, 'localvar_server');
|
||||||
my ($prio_s, $prio_d) = (
|
my ($prio_s, $prio_d) = (
|
||||||
weechat::config_get("irc.server.$server.anti_flood_prio_high"),
|
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 :
|
if ($path and open FH, "<", (($file !~ m"/") ? $path->match :
|
||||||
weechat::config_get_plugin('dir')."/$file")) {
|
weechat::config_get_plugin('dir')."/$file")) {
|
||||||
@queue = <FH>;
|
while (<FH>) {
|
||||||
|
s/[\r\n]*$//;
|
||||||
|
push @{ $queue{$buffer} }, "$pipe \x0f$_\x0f";
|
||||||
|
}
|
||||||
close FH;
|
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 {
|
} else {
|
||||||
weechat::print($buffer, weechat::prefix('error').
|
weechat::print($buffer, weechat::prefix('error').
|
||||||
"Cannot open '$file': $!");
|
"Cannot open '$file': $!");
|
||||||
|
Loading…
Reference in New Issue
Block a user