Add support for an MPD password
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
4455b9013b
commit
ef46017ed8
|
@ -12,9 +12,10 @@
|
|||
Empede is configured using environment variables:
|
||||
|
||||
| Name | Default | Description |
|
||||
| --------------- | ------------ | --------------------------------- |
|
||||
| ---------------- | ------------ | --------------------------------- |
|
||||
| **MPD_HOST** | localhost | MPD server host |
|
||||
| **MPD_PORT** | 6600 | MPD server port |
|
||||
| **MPD_PASSWORD** | | MPD server password |
|
||||
| **EMPEDE_BIND** | 0.0.0.0:8080 | Address for Empede to bind to |
|
||||
|
||||
## Running
|
||||
|
|
|
@ -165,8 +165,10 @@ async fn sse(_req: tide::Request<()>, sender: tide::sse::Sender) -> tide::Result
|
|||
sender.send("playlist", "", None).await?;
|
||||
sender.send("player", "", None).await?;
|
||||
|
||||
let mut mpd = mpd::Mpd::connect().await?;
|
||||
|
||||
loop {
|
||||
let systems = mpd::idle(&["playlist", "player", "database"]).await?;
|
||||
let systems = mpd.idle(&["playlist", "player", "database"]).await?;
|
||||
for system in systems {
|
||||
sender.send(&system, "", None).await?;
|
||||
}
|
||||
|
|
42
src/mpd.rs
42
src/mpd.rs
|
@ -14,7 +14,14 @@ pub(crate) fn host() -> String {
|
|||
}
|
||||
|
||||
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>> {
|
||||
|
@ -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 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();
|
||||
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 command = format!("idle {systems}\n");
|
||||
stream.write_all(command.as_bytes()).await?;
|
||||
self.stream.write_all(command.as_bytes()).await?;
|
||||
|
||||
let mut updated = vec![];
|
||||
loop {
|
||||
buffer.clear();
|
||||
reader.read_line(&mut buffer).await?;
|
||||
self.reader.read_line(&mut buffer).await?;
|
||||
if buffer == "OK\n" {
|
||||
break Ok(updated);
|
||||
}
|
||||
|
@ -119,3 +152,4 @@ pub(crate) async fn idle(systems: &[&str]) -> anyhow::Result<Vec<String>> {
|
|||
updated.push(changed.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue