diff --git a/scripts/play.pl b/scripts/play.pl new file mode 100644 index 0000000..d1475b0 --- /dev/null +++ b/scripts/play.pl @@ -0,0 +1,103 @@ +use strict; +use warnings; +use File::Find::Rule; +no strict 'subs'; + +my $SCRIPT_NAME = 'play'; +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); + +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: 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", + '', 'cmd_play', ''); + + my %OPTIONS = ( + delay => ['Default delay between lines', 0], + dir => ['Art directory', + weechat::info_get('weechat_dir', '').'/ascii'], + ); + + for my $option (keys %OPTIONS) { + weechat::config_set_plugin($option, $OPTIONS{$option}[1]) + unless weechat::config_is_set_plugin($option); + weechat::config_set_desc_plugin($option, $OPTIONS{$option}[0]); + } +} + +sub parse { + my ($input, $delay, $pipe, $find) = + (shift, weechat::config_get_plugin('delay'), '/msg *', 0); + + if ($input =~ / *-delay +([0-9]+) /) { + $delay = $1; + $input =~ s/ *-delay +[0-9]+ *//; + } + if ($input =~ / *-find /) { + $find = 1; + $input =~ s/ *-find *//; + } + # greedy match within quotes so we don't need to escape quotes. + # i don't think we will be affected negatively by this + if ($input =~ / *-pipe +(".+"|[^ ]+) /) { + $pipe = $1; + $pipe =~ s/^"(.+)"$/$1/ if $pipe =~ /^".+"$/; + $input =~ s/ *-pipe +(?:".+"|[^ ]+) *//; + } + + return ($delay, $pipe, $find, $input); +} + +sub play { + my ($buffer, $line) = (shift, (shift @queue) =~ s/[\r\n]*$//r); + + weechat::command($buffer, "$pipe \x0f$line\x0f"); + + return weechat::WEECHAT_RC_OK; +} + +sub cmd_play { + my $buffer = $_[1]; + my ($delay, $find, $file); + ($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"), + weechat::config_get("irc.server_default.anti_flood_prio_high"), + ); + $delay = ($delay or 1000 * ( + weechat::config_option_is_null($prio_s) + ? weechat::config_integer($prio_d) + : weechat::config_integer($prio_s) + ) or 10); + + my $path = File::Find::Rule->file->name($file) + ->start(weechat::config_get_plugin('dir')); + + if ($find) { + weechat::print($buffer, " \t$_") while defined( $_ = $path->match ); + return weechat::WEECHAT_RC_OK; + } + + if ($path and open FH, "<", (($file !~ m"/") ? $path->match : + weechat::config_get_plugin('dir')."/$file")) { + @queue = ; + close FH; + weechat::hook_timer($delay, 0, scalar @queue, 'play', $buffer); + } else { + weechat::print($buffer, weechat::prefix('error'). + "Cannot open '$file': $!"); + return weechat::WEECHAT_RC_ERROR; + } + + return weechat::WEECHAT_RC_OK; +}