]> git.draconx.ca Git - mpdhacks.git/blobdiff - mpdreload.pl
Factor out MPD connection code.
[mpdhacks.git] / mpdreload.pl
index fcbbcc3e5cb122fcbe28edacfb75460d2b7238d6..ba822ae9de8cf05946038d43472726e59b5bc4d5 100755 (executable)
@@ -17,8 +17,6 @@ use Encode::Locale qw(decode_argv);
 decode_argv(Encode::FB_CROAK);
 
 binmode(STDOUT, ":utf8");
-use IO::Socket::INET6;
-use IO::Socket::UNIX;
 
 use Getopt::Long qw(:config gnu_getopt);
 
@@ -26,8 +24,6 @@ use FindBin;
 use lib "$FindBin::Bin";
 use MPDHacks;
 
-my $host = $ENV{MPD_HOST} // "localhost";
-my $port = $ENV{MPD_PORT} // 6600;
 my $sock;
 
 # Submit a command to the MPD server; each argument to this function
@@ -124,8 +120,8 @@ EOF
 }
 
 GetOptions(
-       'host|h=s' => \$host,
-       'port|p=s' => \$port,
+       'host|h=s' => \$MPD::host,
+       'port|p=s' => \$MPD::port,
 
        'V|version' => sub { print_version(); exit },
        'H|help'    => sub { print_help(); exit },
@@ -138,21 +134,7 @@ if (@ARGV != 1) {
        print_usage(); exit 1
 };
 
-# Connect to MPD.
-if ($host =~ /^[@\/]/) {
-       $host =~ s/^@/\0/;
-       $sock = new IO::Socket::UNIX(Type => SOCK_STREAM(), Peer => $host);
-} else {
-       $sock = new IO::Socket::INET6(PeerAddr => $host,
-                                     PeerPort => $port,
-                                     Proto    => 'tcp');
-}
-$sock or die "failed to connect to MPD: $!";
-binmode($sock, ":utf8");
-
-if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
-       die "MPD failed to announce version: $!";
-}
+$sock = MPD::connect();
 
 # Retrieve the current play queue and target play queue.
 my $current = get_tracks_in_play_queue();