3.2.1 Quoting

The sendmessage command takes two string arguments: the channel & the message. Both must be quoted according to the rules of the MPD protocol. Per the MPD manual, “If arguments contain spaces, they should be surrounded by double quotation marks”.

More generally, in the MPD protocol:

  1. an un-quoted token may contain any printable ASCII character except space, tab, ' & "
  2. to include spaces, tabs, '-s or "-s, the token must be enclosed in "-s, and any "-s or \-s therein must be backslash escaped. NB. 's need not be escaped, if they occur within a quoted string (although doing so is harmless)

Since mppopmd commands generally take arguments, they will contain spaces, and hence need to be quoted. For instance, the “rate” command above takes at least one argument (the rating) and so the text “rate 255” had to be enclosed in double-quotes.

Commands that take filters as arguments will not only contain spaces, but also "s and/or 's and possible backslashes, so quoting will be more involved. Cf. libmpdclient for a reference implemenation of the quoting algorithm.

As a worked example, suppose we wanted to send the findadd command with the (filter) argument of:

(artist =~ 'foo\'bar')

The filter itself needs to be quoted to make it a single parameter:

"(artist =~ \'foo\\\'bar\')"

Giving us a command of

findadd "(artist =~ \'foo\\\'bar\')"

This contains spaces, so we wrap it in double-quotes and escape all special characters therein, giving us the final message as it would appear on the wire:

sendmessage unwoundstack.com:commands "findadd \"(artist =~ \\\'foo\\\\\\\'bar\\\')\""

See also here for the MPD parsing logic.