3 # Copyright © 2008,2010,2012,2019-2020 Nick Bowler
5 # License GPLv3+: GNU General Public License version 3 or any later version.
6 # This is free software: you are free to change and redistribute it.
7 # There is NO WARRANTY, to the extent permitted by law.
14 our ($VERSION, @ISA, @EXPORT);
16 use IO::Socket::INET6;
23 our $host = $ENV{MPD_HOST} // "localhost";
24 our $port = $ENV{MPD_PORT} // 6600;
27 our ($major, $minor, $revision);
29 # MPD::connect([ARGS])
31 # Connect to MPD based on the current settings of $MPD::host and $MPD::port.
33 # The following key-value arguments may optionally be specified:
35 # binmode => socket binmode, e.g., :utf8 or :raw. The default is :utf8.
37 # Text in the MPD protocol is always UTF-8 encoded but some
38 # commands return raw binary data which can be easier to
39 # handle in :raw mode.
41 # On failure, an error message is printed and undef is returned.
45 if ($host =~ /^[@\/]/) {
47 $sock = new IO::Socket::UNIX(Type => SOCK_STREAM(),
50 $sock = new IO::Socket::INET6(PeerAddr => $host,
55 die "MPD connection failed: $!\n" unless $sock;
57 binmode($sock, $args{binmode} // ":utf8");
58 unless (<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/) {
60 die "MPD failed to announce version: $!\n";
63 ($major, $minor, $revision) = ($1, $2, $3);
67 # min_version(x, y, z)
69 # Returns true iff the MPD protocol version is at least x.y.z.
71 my ($maj, $min, $rev) = @_;
74 return 1 if $maj < $major;
75 return 0 if $maj > $major;
79 return 1 if $min < $minor;
80 return 0 if $min > $minor;
84 return 1 if $rev < $revision;
85 return 0 if $rev > $revision;
91 # Returns the argument (or $_ if no arguments are supplied) quoted so that it
92 # can be presented as a single command argument to MPD at the protocol level.
96 # No way to encode literal newlines in the protocol, so we convert
97 # any newlines in the arguments into a space, which can help with
101 if ($s =~ /[ \t\\"']/) {
102 $s =~ s/[\\"]/\\$&/g;