From 24e74445453d50f24dac9dd6432bc4bc83702a29 Mon Sep 17 00:00:00 2001 From: Spencer Williams Date: Thu, 10 Jul 2025 21:38:17 -0400 Subject: Add password support --- README.md | 1 + mpc-bar.m | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d965f66..d643c90 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ information. [connection] host = localhost port = 6600 +# password = qwerty123 [display] format = [%name%: &[[%artist%|%performer%|%composer%|%albumartist%] - ]%title%]|%name%|[[%artist%|%performer%|%composer%|%albumartist%] - ]%title%|%file% diff --git a/mpc-bar.m b/mpc-bar.m index 3892e9b..c9decd4 100644 --- a/mpc-bar.m +++ b/mpc-bar.m @@ -29,7 +29,7 @@ #include "ini.h" #include "mpc/song_format.h" -#define VERSION "0.3" +#define VERSION "0.4" #define TITLE_MAX_LENGTH 96 #define SLEEP_INTERVAL 0.2 @@ -48,7 +48,7 @@ static NSString *formatTime(unsigned int t) { } struct config { - const char *host, *format, *idle_message; + const char *host, *password, *format, *idle_message; int show_queue, show_queue_idle; unsigned port; }; @@ -61,6 +61,8 @@ static int handler(void *userdata, const char *section, const char *name, c->host = strdup(value); } else if (MATCH("connection", "port")) { c->port = atoi(value); + } else if (MATCH("connection", "password")) { + c->password = strdup(value); } else if (MATCH("display", "format")) { c->format = strdup(value); } else if (MATCH("display", "idle_message")) { @@ -84,6 +86,7 @@ static int handler(void *userdata, const char *section, const char *name, struct mpd_connection *connection; BOOL songMenuNeedsUpdate; + NSString *errorMessage; NSMenu *controlMenu; NSMenuItem *timeItem, *timeSeparator, *playPauseItem, *stopItem, *nextItem, *previousItem, *singleItem, *clearItem, *updateDatabaseItem, @@ -123,6 +126,16 @@ static int handler(void *userdata, const char *section, const char *name, exit(1); } + errorMessage = @"Failed to get status (is MPD running?)"; + + if (mpd_connection_get_error(connection) == MPD_ERROR_SUCCESS) { + if (config.password != NULL) { + if (!mpd_run_password(connection, config.password)) { + errorMessage = @"Auth failed (please fix password and restart service)"; + } + } + } + songMenuNeedsUpdate = YES; } - (void)disconnect { @@ -146,7 +159,7 @@ static int handler(void *userdata, const char *section, const char *name, [NSThread sleepForTimeInterval:SLEEP_INTERVAL]; if (!connection) { [self disableAllItems]; - [self showError:@"Failed to get status (is MPD running?)"]; + [self showError:errorMessage]; [self connect]; } if (!mpd_send_idle(connection)) { -- cgit v1.2.3