Add support for an MPD password
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sijmen 2023-05-06 21:10:15 +02:00
parent 4455b9013b
commit ef46017ed8
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
3 changed files with 64 additions and 27 deletions

View file

@ -12,9 +12,10 @@
Empede is configured using environment variables: Empede is configured using environment variables:
| Name | Default | Description | | Name | Default | Description |
| --------------- | ------------ | --------------------------------- | | ---------------- | ------------ | --------------------------------- |
| **MPD_HOST** | localhost | MPD server host | | **MPD_HOST** | localhost | MPD server host |
| **MPD_PORT** | 6600 | MPD server port | | **MPD_PORT** | 6600 | MPD server port |
| **MPD_PASSWORD** | | MPD server password |
| **EMPEDE_BIND** | 0.0.0.0:8080 | Address for Empede to bind to | | **EMPEDE_BIND** | 0.0.0.0:8080 | Address for Empede to bind to |
## Running ## Running

View file

@ -165,8 +165,10 @@ async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result
sender.send("playlist", "", None).await?; sender.send("playlist", "", None).await?;
sender.send("player", "", None).await?; sender.send("player", "", None).await?;
let mut mpd = mpd::Mpd::connect().await?;
loop { loop {
let systems = mpd::idle(&["playlist", "player", "database"]).await?; let systems = mpd.idle(&["playlist", "player", "database"]).await?;
for system in systems { for system in systems {
sender.send(&system, "", None).await?; sender.send(&system, "", None).await?;
} }

View file

@ -14,7 +14,14 @@ pub(crate) fn host() -> String {
} }
pub(crate) fn connect() -> Result<mpdrs::Client, mpdrs::error::Error> { pub(crate) fn connect() -> Result<mpdrs::Client, mpdrs::error::Error> {
mpdrs::Client::connect(host()) let mut client = mpdrs::Client::connect(host())?;
let password = std::env::var("MPD_PASSWORD").unwrap_or(String::new());
if !password.is_empty() {
client.login(&password)?;
}
Ok(client)
} }
pub(crate) fn ls(path: &str) -> anyhow::Result<Vec<Entry>> { pub(crate) fn ls(path: &str) -> anyhow::Result<Vec<Entry>> {
@ -91,7 +98,17 @@ pub(crate) enum Entry {
}, },
} }
pub(crate) async fn idle(systems: &[&str]) -> anyhow::Result<Vec<String>> { pub(crate) struct Mpd {
stream: TcpStream,
reader: BufReader<TcpStream>,
}
impl Mpd {
fn escape_str(s: &str) -> String {
s.replace("\"", "\\\"").replace("'", "\\'")
}
pub async fn connect() -> anyhow::Result<Self> {
let mut stream = TcpStream::connect(host()).await?; let mut stream = TcpStream::connect(host()).await?;
let mut reader = BufReader::new(stream.clone()); let mut reader = BufReader::new(stream.clone());
@ -100,14 +117,30 @@ pub(crate) async fn idle(systems: &[&str]) -> anyhow::Result<Vec<String>> {
let mut buffer = String::new(); let mut buffer = String::new();
reader.read_line(&mut buffer).await?; reader.read_line(&mut buffer).await?;
let password = std::env::var("MPD_PASSWORD").unwrap_or(String::new());
if !password.is_empty() {
let password = Self::escape_str(&password);
let command = format!("password \"{password}\"\n");
stream.write_all(command.as_bytes()).await?;
buffer.clear();
reader.read_line(&mut buffer).await?;
}
Ok(Self { stream, reader })
}
pub(crate) async fn idle(&mut self, systems: &[&str]) -> anyhow::Result<Vec<String>> {
let mut buffer = String::new();
let systems = systems.join(" "); let systems = systems.join(" ");
let command = format!("idle {systems}\n"); let command = format!("idle {systems}\n");
stream.write_all(command.as_bytes()).await?; self.stream.write_all(command.as_bytes()).await?;
let mut updated = vec![]; let mut updated = vec![];
loop { loop {
buffer.clear(); buffer.clear();
reader.read_line(&mut buffer).await?; self.reader.read_line(&mut buffer).await?;
if buffer == "OK\n" { if buffer == "OK\n" {
break Ok(updated); break Ok(updated);
} }
@ -119,3 +152,4 @@ pub(crate) async fn idle(systems: &[&str]) -> anyhow::Result<Vec<String>> {
updated.push(changed.to_string()); updated.push(changed.to_string());
} }
} }
}