]> git.draconx.ca Git - mpdhacks.git/blobdiff - mpdexec.pl
mpdexec: Add support for readpicture command in download mode.
[mpdhacks.git] / mpdexec.pl
index 8147f1d185c96fe4aec99a2399de5d63c6a1e279..72f81fe3b6336cbfcfa4f88d4299f7369eeb683a 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env perl
 #
-# Copyright © 2012,2019-2020 Nick Bowler
+# Copyright © 2012,2019-2021 Nick Bowler
 #
 # Send commands to MPD.  Each command-line argument is quoted as necessary
 # so it appears as a single argument at the protocol level.  The result is
@@ -20,7 +20,6 @@ decode_argv(Encode::FB_CROAK);
 
 binmode(STDOUT, ":utf8");
 binmode(STDIN, ":utf8");
-use IO::Socket::INET6;
 
 use Getopt::Long qw(:config gnu_getopt);
 
@@ -28,14 +27,12 @@ use FindBin;
 use lib "$FindBin::Bin";
 use MPDHacks;
 
-my $host = $ENV{MPD_HOST} // "localhost";
-my $port = $ENV{MPD_PORT} // 6600;
 my ($quiet, $binary, $ignore_errors, $download);
 
 sub print_version {
        print <<EOF
 mpdexec.pl 0.8
-Copyright © 2019 Nick Bowler
+Copyright © 2021 Nick Bowler
 License GPLv3+: GNU General Public License version 3 or any later version.
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.
@@ -43,10 +40,10 @@ EOF
 }
 
 sub print_usage {
-       my $fh = $_[1] // *STDERR;
+       my ($fh) = (@_, *STDERR);
 
        print $fh "Usage: $0 [options] [command ...]\n";
-       print "Try $0 --help for more information.\n" unless (@_ > 0);
+       print $fh "Try $0 --help for more information.\n" unless (@_ > 0);
 }
 
 sub print_help {
@@ -64,10 +61,10 @@ Options:
                     written.  If FILE is specified, the data is written there.
                     Otherwise, --quiet is automatically enabled and the data
                     goes to standard output.
-  --download        Enable automatic sequencing of albumart commands; if this
-                    option is specified, albumart commands without offsets will
-                    be expanded into multiple commands in order to download the
-                    entire file.
+  --download        Enable automatic sequencing of albumart and readpicture
+                    commands; if this option is specified, such commands
+                    without offsets will be expanded into multiple commands
+                    in order to download the entire file.
   --ignore-errors   In batch mode, continue submitting commands after errors.
   -V, --version     Print a version message and then exit.
   -H, --help        Print this message and then exit.
@@ -77,8 +74,8 @@ EOF
 }
 
 GetOptions(
-       'host|h=s'         => \$host,
-       'port|p=s'         => \$port,
+       'host|h=s'         => \$MPD::host,
+       'port|p=s'         => \$MPD::port,
 
        'quiet|q'          => \$quiet,
        'no-quiet'         => sub { $quiet = 0; },
@@ -104,17 +101,7 @@ if ($binary) {
 }
 $quiet = 1 if (defined($binary) && $binary eq "");
 
-my $sock = new IO::Socket::INET6(
-       PeerAddr => $host,
-       PeerPort => $port,
-       Proto    => 'tcp',
-) or die "failed to connect to MPD: $!";
-#binmode($sock, ":utf8");
-binmode($sock);
-
-if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
-       die "MPD failed to announce version: $!";
-}
+my $sock = MPD::connect(binmode => ":raw");
 
 sub read_binary {
        my ($count) = @_;
@@ -131,13 +118,14 @@ sub read_binary {
        return $rc;
 }
 
+my %downloadcmds = map { $_ => 1 } ( "albumart", "readpicture" );
 sub mpd_exec {
        my $downloadseq;
 
-       # special case for "albumart"; if no offset is specified
-       # (invalid command) we synthesize a sequence of albumart
+       # special case for "albumart" and "readpicture"; if no offset is
+       # specified (invalid command) we synthesize a sequence of albumart
        # commands to retrieve the entire file.
-       if ($download && $_[0] eq "albumart" && @_ == 2) {
+       if ($download && $downloadcmds{$_[0]} && @_ == 2) {
                $_[2] = 0;
                $downloadseq = 2;
        }