Skip to content

Commit

Permalink
Merge pull request #50 from Polochon-street/mpd-host-actual-hostname-…
Browse files Browse the repository at this point in the history
…support

Add support for actual hostnames in MPD_HOST
  • Loading branch information
Polochon-street authored Jan 28, 2024
2 parents 880d2c5 + 2717d0d commit 27f4a42
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## blissify 0.3.9
* Add support for hostnames and abstract sockets in MPD_HOST

## blissify 0.3.8
* Bump bliss to fix build on raspberry pis.

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blissify"
version = "0.3.8"
version = "0.3.9"
authors = ["Polochon-street <[email protected]>"]
edition = "2021"
license = "GPL-3.0-only"
Expand Down
35 changes: 20 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::sync::{Arc, Mutex};
use std::io;
use std::io::Write;
#[cfg(not(test))]
use std::{io::Read, net::SocketAddr, os::unix::net::UnixStream};
use std::{io::Read, os::unix::net::UnixStream};

use termion::input::TermRead;
use termion::raw::IntoRawMode;
Expand Down Expand Up @@ -131,11 +131,14 @@ impl MPDLibrary {
/// variables.
#[cfg(not(test))]
fn get_mpd_conn() -> Result<Client<MPDStream>> {
use std::net::IpAddr;
use std::os::linux::net::SocketAddrExt;
use std::os::unix::net::SocketAddr;

let (password, mpd_host) = match env::var("MPD_HOST") {
Ok(h) => match h.rsplit_once('@') {
Ok(h) => match h.split_once('@') {
None => (None, h),
// If it's a unix abstract socket, there will be nothing before the '@'
Some(("", _)) => (None, h),
Some((password, host)) => (Some(password.to_owned()), host.to_owned()),
},
Err(_) => {
Expand All @@ -152,21 +155,23 @@ impl MPDLibrary {
6600
}
};
let addr: Option<SocketAddr> = {
let tentative_parse: Option<SocketAddr> = mpd_host.parse().ok();
if tentative_parse.is_none() {
let tentative_parse = mpd_host.parse::<IpAddr>().ok();
tentative_parse.map(|ip| (ip, mpd_port).into())
} else {
tentative_parse
}
};

let mut client = {
if let Some(address) = addr {
Client::new(MPDStream::Tcp(TcpStream::connect(address)?))?
} else {
// TODO It is most likely a socket if it starts by "/", but maybe not necessarily?
// find a solution that doesn't depend on a url crate that pulls the entire internet
// with it
if mpd_host.starts_with('/') || mpd_host.starts_with('~') {
Client::new(MPDStream::Unix(UnixStream::connect(mpd_host)?))?
} else if mpd_host.starts_with('@') {
let addr = SocketAddr::from_abstract_name(mpd_host.split_once('@').unwrap().1)?;
Client::new(MPDStream::Unix(UnixStream::connect_addr(&addr)?))?
}
// It is a hostname or an IP address
else {
Client::new(MPDStream::Tcp(TcpStream::connect(format!(
"{}:{}",
mpd_host, mpd_port
))?))?
}
};
if let Some(pw) = password {
Expand Down

0 comments on commit 27f4a42

Please sign in to comment.