Skip to content
This page is for the development version of rmpc. Make sure your version matches the selected documentation.

Lua plugin API

Rmpcd provides a set of functions available on the global scope to help with plugin development. Each of the plugins runs on a fully separate Lua VM and as such they are completely isolated from each other which means that they do not share state and cannot block each other.

You also should not use any of the Lua built in blocking APIs such as io library as that could starve rmpcd’s async executor.

These modules are available on the global scope

  • mpd - Functions to interact with MPD directly. These are relatively low level primitives mirroring the MPD protocol
  • fs - A set of functions used to interact with the filesystem
  • http - A simple HTTP client
  • log - Logging facilities
  • process - To spawn external programs
  • sync - Utilities to for asynchronous execution
  • util - Set of random utility functions that do not fit anywhere else

These functions correspond to the MPD protocol, see its documentation for more details.

They are available on global scope by accessing the mpd table.

FunctionDescription
get_songGet song by its URI
Parameters
uri string Song URI
Returns
Song | nil The song object if found
string | nil Error message if any
get_song_by_idGet song by its ID in the current queue
Parameters
id number Song ID
Returns
Song | nil The song object if found
string | nil Error message if any
get_current_songGet the currently playing song
Parameters
Returns
Song | nil The song object if found
string | nil Error message if any
set_song_stickerSet a song sticker
Parameters
uri string Song URI
name string Sticker name
value string Sticker value
Returns
boolean Whether the call was successful
string | nil Error message if any
get_song_stickerGet a song sticker value
Parameters
uri string Song URI
name string Sticker name
Returns
string | nil The sticker value if success, nil otherwise
string | nil Error message if any
set_consumeSet the consume state
Parameters
value "on", "off" or "oneshot" The consume state
Returns
boolean Whether the call was successful
string | nil Error message if any
set_crossfadeSet crossfade duration
Parameters
seconds number Crossfade duration in seconds
Returns
boolean Whether the call was successful
string | nil Error message if any
set_randomSet the random state
Parameters
value boolean Whether to enable or disable random
Returns
boolean Whether the call was successful
string | nil Error message if any
set_repeatSet the repeat state
Parameters
value boolean Whether to enable or disable repeat
Returns
boolean Whether the call was successful
string | nil Error message if any
set_singleSet the single state
Parameters
value "on", "off" or "oneshot" The single state
Returns
boolean Whether the call was successful
string | nil Error message if any
set_volumeSet MPD's volume
Parameters
volume number Volume between 0 and 100
Returns
boolean Whether the call was successful
string | nil Error message if any
get_volumeGet MPD's current volume
Returns
number | nil The volume value if successful
string | nil Error message if any
volumeSet MPD's volume, supports relative change
Parameters
value string +5 to increase volume by 5, -5 to decrease by 5 and 5 to set to 5
Returns
boolean Whether the call was successful
string | nil Error message if any
prevSwitch to the previous song
Returns
boolean Whether the call was successful
string | nil Error message if any
nextSwitch to the next song
Returns
boolean Whether the call was successful
string | nil Error message if any
playStart playback
Returns
boolean Whether the call was successful
string | nil Error message if any
pausePause playback
Returns
boolean Whether the call was successful
string | nil Error message if any
stopStop playback
Returns
boolean Whether the call was successful
string | nil Error message if any
toggle_pauseToggle pause state on/off
Returns
boolean Whether the call was successful
string | nil Error message if any
seek_currentSeek the current song
Parameters
value string +5 to seek forward by 5 seconds, -5 to seek backwards by 5 seconds and 5 to seek to the 5th second directly
Returns
boolean Whether the call was successful
string | nil Error message if any
get_statusReturns MPD's current status info
Returns
MpdStatus MPD's current status
string | nil Error message if any
album_artGet song's album art by searching for cover.jpg, cover.png or cover.webp in the song's directory
Parameters
uri string The song URI
Returns
number[] | nil Raw bytes of the image if successful
string | nil Error message if any
read_pictureGet song's embedded image
Parameters
uri string The song URI
Returns
number[] | nil Raw bytes of the image if successful
string | nil Error message if any
subscribeSubscribe to a channel
Parameters
channel string Channel name
Returns
boolean Whether the call was successful
string | nil Error message if any
unsubscribeUnsubscribe from a channel
Parameters
channel string Channel name
Returns
boolean Whether the call was successful
string | nil Error message if any
channelsList of currently active channels
Returns
string[] | nil List of active channels if successful
string | nil Error message if any
send_messageSend message to a channel
Parameters
channel string Channel name
message string Message to send
Returns
boolean Whether the call was successful
string | nil Error message if any
read_messagesRead messages from all channels that rmpcd is currently subscribed to, this probably should not be used directly unless you know what you are doing
Returns
table Messages if successful
string | nil Error message if any

Filesystem API. These are available on global scope by accessing the fs table.

FunctionDescription
existsCheck whether a file exists
Parameters
path string File path
Returns
boolean true if the file exists
string | nil Error message if any
create_dir_allCreate all directories in the path if they do not exist already
Parameters
path string Directory path
Returns
boolean Whether the call was successful
string | nil Error message if any
create_dirCreate a directory
Parameters
path string Directory path
Returns
boolean Whether the call was successful
string | nil Error message if any
writeWrite raw bytes to a file
Parameters
path string File path
contents number[] Raw bytes
Returns
boolean Whether the call was successful
string | nil Error message if any
write_strWrite a string to a file
Parameters
path string File path
contents string Content to write
Returns
boolean Whether the call was successful
string | nil Error message if any
readRead raw bytes from a file
Parameters
path string File path
Returns
number[] | nil Raw bytes if successful
string | nil Error message if any
read_strRead file as string
Parameters
path string File path
Returns
string | nil String content if successful
string | nil Error message if any
deleteDelete a file
Parameters
path string File path
Returns
boolean Whether the file was deleted
string | nil Error message if any
remove_dirRemove a directory
Parameters
path string Directory path
Returns
boolean Whether the directory was deleted
string | nil Error message if any
remove_dirRemove a directory recursively
Parameters
path string Directory path
Returns
boolean Whether the directory was deleted
string | nil Error message if any

A simple HTTP client. Available on the global scope by accessing the http table.

FunctionDescription
callDo an HTTP call
Parameters
url string URL to call to
opts HttpRequestOpts Options for the HTTP request
Returns
HttpResponse The response, see below for more details
getDo a GET request
Parameters
url string URL to call to
opts HttpGetOpts Options for the GET HTTP request
Returns
HttpResponse The response, see below for more details
postDo a POST request
Parameters
url string URL to call to
opts HttpGetOpts Options for the POST HTTP request
Returns
HttpResponse The response, see below for more details

Each of the http functions takes an opts table.

PropertyDescription
methodHTTP method string
headerstable of HTTP headers
bodyString body
paramstable to send as query parameters

The get function only takes headers and params in opts. The post function only takes headers, params and body in opts.

The response is a table with following properties and functions:

Property or functionDescription
codeHttp Response code
errorString error if any
bodyRaw string body of the response
textGets the body text
Returns
string The body
jsonGets the body json as a table
Returns
table The json value

Logging facilities. Available on the global scope by accessing the log table.

FunctionDescription
infoLog at the info level
Parameters
content string String content to log
warnLog at the warn level
Parameters
content string String content to log
errorLog at the error level
Parameters
content string String content to log
debugLog at the debug level
Parameters
content string String content to log
traceLog at the trace level
Parameters
content string String content to log

Spawn external processes. Available on the global scope by accessing the process table.

FunctionDescription
spawnSpawn an external process
Parameters
cmd string[] First element is the program to spawn, rest are passed in as arguments
Returns
number | nil The child process PID
string | nil Error message if any

Execute things asynchronously. Available on the global scope by accessing the sync table.

FunctionDescription
set_timeoutSchedule a function to run at some later time
Parameters
timeout_ms number Timeout in milliseconds
callback function A function to run when the timeout expires
Returns
TimeoutHandle A handle to the timeout, call handle.cancel() to cancel the timeout
set_intervalSchedule a function to run repeatedly with an interval
Parameters
interval_ms number Interval in milliseconds
callback function A function to run every interval tick
Returns
TimeoutHandle A handle to the interval, call handle.cancel() to cancel the interval
debounceCreate a debounced version of a function
Parameters
interval_ms number Interval in milliseconds
callback function A function to run when the debounce timeout expires
Returns
function The debounced function

Set of random utilities that do not fit anywhere else. Available on the global scope by accessing the util table.

FunctionDescription
dump_tableDump table to the logger
Parameters
tbl table Dump the table to the logger
md5Compute an md5 hash of a value
Parameters
data string Data to hash
Returns
string The md5 hash
whichCheck whether a program exists in path
Parameters
prog string The program
Returns
boolean Whether the program exists
nil_or_nullCheck whether a value is nil or null, useful to for example validate HTTP responses
Parameters
value any The value
Returns
boolean Whether the value is nil or nul
deserialize_ron Deserialize a ron value
Parameters
data number[] Input bytes
Returns
any | nil Deserialized value if success
string | nil Error message if any