]> git.draconx.ca Git - mpdhacks.git/blobdiff - mpdexec.pl
Factor out MPD connection code.
[mpdhacks.git] / mpdexec.pl
index 8147f1d185c96fe4aec99a2399de5d63c6a1e279..496c5581ff53b9f9f411f75d22fb8aaebb88f00d 100755 (executable)
@@ -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,8 +27,6 @@ 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 {
@@ -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) = @_;