X-Git-Url: https://git.draconx.ca/gitweb/mpdhacks.git/blobdiff_plain/adba02b582138375bfd3adc9f68c0cd398827e7a..e49c1f846176562de0978ba1cd445b50b1094a2d:/mpdreload.pl diff --git a/mpdreload.pl b/mpdreload.pl index 4c77e83..fcbbcc3 100755 --- a/mpdreload.pl +++ b/mpdreload.pl @@ -1,6 +1,6 @@ #!/usr/bin/env perl # -# Copyright © 2019 Nick Bowler +# Copyright © 2019-2020 Nick Bowler # # Replace the current MPD play queue with a saved playlist, by rearranging # existing queue entries when possible. This avoids losing the current @@ -22,33 +22,18 @@ use IO::Socket::UNIX; use Getopt::Long qw(:config gnu_getopt); +use FindBin; +use lib "$FindBin::Bin"; +use MPDHacks; + my $host = $ENV{MPD_HOST} // "localhost"; my $port = $ENV{MPD_PORT} // 6600; my $sock; -# Quotes the argument so that it is presented as a single argument to MPD -# at the protocol level. -sub escape { - my $s = @_[0] // $_; - - # No way to encode literal newlines in the protocol, so we - # convert any newlines in the arguments into a space, which - # can help with quoting. - $s =~ s/\n/ /g; - - if (/[ \t\\"]/) { - $s =~ s/[\\"]/\\$&/g; - return "\"$s\""; - } - - $s =~ s/^\s*$/"$&"/; - return $s; -} - # Submit a command to the MPD server; each argument to this function # is quoted and sent as a single argument to MPD. sub mpd_exec { - my $cmd = join(' ', map { escape } @_); + my $cmd = join(' ', map { MPD::escape } @_); print $sock "$cmd\n"; } @@ -63,7 +48,7 @@ sub get_tracks_in_play_queue { while (<$sock>) { last if /^OK/; die($_) if /^ACK/; - + if (/^(\w+): (.*)$/) { if ($1 eq "file") { if (exists($matches{$2})) {