diff options
author | Kalle Wallin <kaw@linux.se> | 2004-03-29 18:09:38 +0000 |
---|---|---|
committer | Kalle Wallin <kaw@linux.se> | 2004-03-29 18:09:38 +0000 |
commit | b4c27011d20196aeeb104edb3c2e3dd97861e8d8 (patch) | |
tree | df2492d66f86e4470da2b12ae4425ee093b5677c | |
parent | 30327ee6f3bc929ae9f8ceca73a6b0ef8e5a0282 (diff) | |
download | mpd-b4c27011d20196aeeb104edb3c2e3dd97861e8d8.tar.gz mpd-b4c27011d20196aeeb104edb3c2e3dd97861e8d8.tar.xz mpd-b4c27011d20196aeeb104edb3c2e3dd97861e8d8.zip |
Added password support.
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@534 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to '')
-rw-r--r-- | main.c | 41 | ||||
-rw-r--r-- | mpc.c | 18 | ||||
-rw-r--r-- | mpc.h | 4 | ||||
-rw-r--r-- | options.c | 49 | ||||
-rw-r--r-- | options.h | 2 |
5 files changed, 85 insertions, 29 deletions
@@ -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); } @@ -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; } @@ -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); @@ -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; } @@ -8,6 +8,8 @@ typedef struct { char *host; + char *username; + char *password; int port; int reconnect; int debug; |