From: Kalle Wallin Date: Mon, 29 Mar 2004 18:09:38 +0000 (+0000) Subject: Added password support. X-Git-Tag: v0.12_alpha1~627 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=b4c27011d20196aeeb104edb3c2e3dd97861e8d8;p=ncmpc.git Added password support. git-svn-id: https://svn.musicpd.org/ncmpc/trunk@534 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- diff --git a/main.c b/main.c index 92e40b6..8538636 100644 --- a/main.c +++ b/main.c @@ -13,8 +13,11 @@ #include "screen.h" #include "conf.h" +/* time in seconds between mpd updates (double) */ +#define MPD_UPDATE_TIME 1.0 -#define MPD_UPDATE_TIME 1.0 +/* timout in seconds before trying to reconnect (int) */ +#define MPD_RECONNECT_TIMEOUT 3 static mpd_client_t *mpc = NULL; @@ -32,6 +35,7 @@ exit_and_cleanup(void) mpc_close(mpc); } g_free(options.host); + g_free(options.password); if( timer ) g_timer_destroy(timer); } @@ -48,7 +52,7 @@ main(int argc, const char *argv[]) { options_t *options; struct sigaction act; - int connected; + gboolean connected; /* initialize options */ options = options_init(); @@ -92,14 +96,14 @@ main(int argc, const char *argv[]) } /* set xterm title */ - if( getenv("DISPLAY") ) - printf("%c]0;%s%c", '\033', PACKAGE " v" VERSION, '\007'); + if( g_getenv("DISPLAY") ) + printf("%c]0;%s%c", '\033', PACKAGE " version " VERSION, '\007'); /* install exit function */ atexit(exit_and_cleanup); /* connect to our music player daemon */ - mpc = mpc_connect(options->host, options->port); + mpc = mpc_connect(options->host, options->port, options->password); if( mpc_error(mpc) ) exit(EXIT_FAILURE); @@ -109,7 +113,7 @@ main(int argc, const char *argv[]) /* initialize timer */ timer = g_timer_new(); - connected=1; + connected = TRUE; while( connected || options->reconnect ) { static gdouble t = G_MAXDOUBLE; @@ -117,20 +121,26 @@ main(int argc, const char *argv[]) if( connected && t>=MPD_UPDATE_TIME ) { mpc_update(mpc); - if( mpc_error(mpc) ) + if( mpc_error(mpc) == MPD_ERROR_ACK ) + { + screen_status_printf("%s", mpc_error_str(mpc)); + mpd_clearError(mpc->connection); + mpd_finishCommand(mpc->connection); + } + else if( mpc_error(mpc) ) { - connected=0; screen_status_printf("Lost connection to %s", options->host); + connected = FALSE; doupdate(); + mpd_clearError(mpc->connection); mpd_closeConnection(mpc->connection); mpc->connection = NULL; } - else + else mpd_finishCommand(mpc->connection); g_timer_start(timer); } - if( connected ) { command_t cmd; @@ -146,19 +156,22 @@ main(int argc, const char *argv[]) } else if( options->reconnect ) { - sleep(3); + sleep(MPD_RECONNECT_TIMEOUT); screen_status_printf("Connecting to %s... [Press Ctrl-C to abort]", options->host); - if( mpc_reconnect(mpc, options->host, options->port) == 0 ) + if( mpc_reconnect(mpc, + options->host, + options->port, + options->password) == 0 ) { screen_status_printf("Connected to %s!", options->host); - connected=1; + connected = TRUE; } doupdate(); } t = g_timer_elapsed(timer, NULL); - } + exit(EXIT_FAILURE); } diff --git a/mpc.c b/mpc.c index 785117f..00a0444 100644 --- a/mpc.c +++ b/mpc.c @@ -63,7 +63,7 @@ mpc_close(mpd_client_t *c) } mpd_client_t * -mpc_connect(char *host, int port) +mpc_connect(char *host, int port, char *password) { mpd_Connection *connection; mpd_client_t *c; @@ -80,11 +80,17 @@ mpc_connect(char *host, int port) c->connection = connection; c->cwd = g_strdup(""); + if( password ) + { + mpd_sendPasswordCommand(connection, password); + mpd_finishCommand(connection); + } + return c; } int -mpc_reconnect(mpd_client_t *c, char *host, int port) +mpc_reconnect(mpd_client_t *c, char *host, int port, char *password) { mpd_Connection *connection; @@ -99,6 +105,12 @@ mpc_reconnect(mpd_client_t *c, char *host, int port) c->connection = connection; + if( password ) + { + mpd_sendPasswordCommand(connection, password); + mpd_finishCommand(connection); + } + return 0; } @@ -109,7 +121,7 @@ mpc_error(mpd_client_t *c) if( c == NULL || c->connection == NULL ) return 1; if( c->connection->error ) - return 1; + return c->connection->error; return 0; } diff --git a/mpc.h b/mpc.h index 608d22c..b18974b 100644 --- a/mpc.h +++ b/mpc.h @@ -28,8 +28,8 @@ typedef struct int mpc_close(mpd_client_t *c); -mpd_client_t *mpc_connect(char *host, int port); -int mpc_reconnect(mpd_client_t *c, char *host, int port); +mpd_client_t *mpc_connect(char *host, int port, char *passwd); +int mpc_reconnect(mpd_client_t *c, char *host, int port, char *passwd); int mpc_update(mpd_client_t *c); int mpc_update_playlist(mpd_client_t *c); diff --git a/options.c b/options.c index cf3bbca..8556b0f 100644 --- a/options.c +++ b/options.c @@ -9,9 +9,13 @@ #include "config.h" #include "options.h" #include "command.h" +#include "support.h" options_t options; +static char *mpd_host = NULL; +static char *mpd_password = NULL; + static struct poptOption optionsTable[] = { #ifdef DEBUG { "debug", 'D', 0, 0, 'D', "Enable debug output." }, @@ -23,8 +27,10 @@ static struct poptOption optionsTable[] = { { "exit", 'e', 0, 0, 'e', "Exit on connection errors." }, { "port", 'p', POPT_ARG_INT, &options.port, 0, "Connect to server on port [" DEFAULT_PORT_STR "].", "PORT" }, - { "host", 'h', POPT_ARG_STRING, &options.host, 0, + { "host", 'h', POPT_ARG_STRING, &mpd_host, 0, "Connect to server [" DEFAULT_HOST "].", "HOSTNAME" }, + { "passwd", 'P', POPT_ARG_STRING, &mpd_password, 0, + "Connect with password.", "PASSWORD" }, POPT_AUTOHELP { NULL, 0, 0, NULL, 0 } }; @@ -44,6 +50,8 @@ options_parse( int argc, const char **argv) int c; poptContext optCon; /* context for parsing command-line options */ + mpd_host = NULL; + mpd_password = NULL; optCon = poptGetContext(NULL, argc, argv, optionsTable, 0); while ((c = poptGetNextOpt(optCon)) >= 0) { @@ -88,6 +96,16 @@ options_parse( int argc, const char **argv) exit(EXIT_FAILURE); } + if( mpd_host ) + { + g_free(options.host); + options.host = mpd_host; + } + if( mpd_password ) + { + g_free(options.password); + options.password = mpd_password; + } poptFreeContext(optCon); return &options; } @@ -95,14 +113,25 @@ options_parse( int argc, const char **argv) options_t * options_init( void ) { - char *value; + const char *value; + char *tmp; memset(&options, 0, sizeof(options_t)); - if( (value=getenv(MPD_HOST_ENV)) ) + + if( (value=g_getenv(MPD_HOST_ENV)) ) options.host = g_strdup(value); else options.host = g_strdup(DEFAULT_HOST); - if( (value=getenv(MPD_PORT_ENV)) ) + if( (tmp=g_strstr_len(options.host, strlen(options.host), "@")) ) + { + char *oldhost = options.host; + *tmp = '\0'; + options.password = locale_to_utf8(oldhost); + options.host = g_strdup(tmp+1); + g_free(oldhost); + } + + if( (value=g_getenv(MPD_PORT_ENV)) ) options.port = atoi(value); else options.port = DEFAULT_PORT; @@ -111,12 +140,12 @@ options_init( void ) options.find_wrap = 1; options.bg_color = COLOR_BLACK; - options.title_color = COLOR_BLUE; - options.line_color = COLOR_GREEN; - options.list_color = COLOR_YELLOW; - options.progress_color = COLOR_GREEN; - options.status_color = COLOR_RED; - options.alert_color = COLOR_MAGENTA;; + options.title_color = COLOR_YELLOW; + options.line_color = COLOR_WHITE; + options.list_color = COLOR_GREEN; + options.progress_color = COLOR_WHITE; + options.status_color = COLOR_YELLOW; + options.alert_color = COLOR_RED; return &options; } diff --git a/options.h b/options.h index cb34637..fb17485 100644 --- a/options.h +++ b/options.h @@ -8,6 +8,8 @@ typedef struct { char *host; + char *username; + char *password; int port; int reconnect; int debug;