Read MPD connection data from environment.
authorOle Christian Tvedt <olechrt@stud.ntnu.no>
Sun, 14 Jun 2009 23:15:04 +0000 (01:15 +0200)
committerBrenden Matthews <brenden@rty.ca>
Mon, 15 Jun 2009 02:07:59 +0000 (20:07 -0600)
Most MPD clients read the MPD_HOST and MPD_PORT
environment variables. Now, conky will too.

MPD_HOST can be either "hostname" or "password@hostname".

If a user specifies a host in the configuration, the
password set in MPD_HOST will be ignored. This is to
prevent the password from being sent to the wrong host.

In other words, if the host is specified in the conky
configuration, the password must be too (if there is
one).

Signed-off-by: Brenden Matthews <brenden@rty.ca>

src/conky.c
src/mpd.c
src/mpd.h

index ed24d92..af8db82 100644 (file)
@@ -7540,6 +7540,10 @@ static void set_default_configurations_for_x(void)
 static void set_default_configurations(void)
 {
        int i;
+#ifdef MPD
+       char *mpd_env_host;
+       char *mpd_env_port;
+#endif
        update_uname();
        fork_to_background = 0;
        total_run_times = 0;
@@ -7557,8 +7561,36 @@ static void set_default_configurations(void)
        top_io = 0;
 #endif
 #ifdef MPD
-       mpd_set_host("localhost");
-       mpd_set_port("6600");
+       mpd_env_host = getenv("MPD_HOST");
+       mpd_env_port = getenv("MPD_PORT");
+
+       if (!mpd_env_host || !strlen(mpd_env_host)) {
+               mpd_set_host("localhost");
+       } else {
+               /* MPD_HOST environment variable is set */
+               char *mpd_hostpart = strchr(mpd_env_host, '@');
+               if (!mpd_hostpart) {
+                       mpd_set_host(mpd_env_host);
+               } else {
+                       /* MPD_HOST contains a password */
+                       char mpd_password[mpd_hostpart - mpd_env_host + 1];
+                       snprintf(mpd_password, mpd_hostpart - mpd_env_host + 1, "%s", mpd_env_host);
+
+                       if (!strlen(mpd_hostpart + 1)) {
+                               mpd_set_host("localhost");
+                       } else {
+                               mpd_set_host(mpd_hostpart + 1);
+                       }
+
+                       mpd_set_password(mpd_password, 1);
+               }
+       }
+
+
+       if (!mpd_env_port || mpd_set_port(mpd_env_port)) {
+               /* failed to set port from environment variable */
+               mpd_set_port("6600");
+       }
 #endif
 #ifdef XMMS2
        info.xmms2.artist = NULL;
@@ -8034,7 +8066,7 @@ static void load_config_file(const char *f)
                }
                CONF("mpd_password") {
                        if (value) {
-                               mpd_set_password(value);
+                               mpd_set_password(value, 0);
                        } else {
                                CONF_ERR;
                        }
index 63b127d..39fb1b5 100644 (file)
--- a/src/mpd.c
+++ b/src/mpd.c
@@ -35,6 +35,9 @@ static char mpd_host[128];
 static char mpd_password[128];
 static int mpd_port;
 
+/* this is >0 if the current password was set from MPD_HOST */
+static int mpd_environment_password = 0;
+
 /* global mpd information */
 static struct mpd_s mpd_info;
 
@@ -44,14 +47,21 @@ static int refcount = 0;
 void mpd_set_host(const char *host)
 {
        snprintf(mpd_host, 128, "%s", host);
+
+       if (mpd_environment_password) {
+               /* for security, dont use environment password when user specifies host in config */
+               mpd_clear_password();
+       }
 }
-void mpd_set_password(const char *password)
+void mpd_set_password(const char *password, int from_environment)
 {
        snprintf(mpd_password, 128, "%s", password);
+       mpd_environment_password = from_environment;
 }
 void mpd_clear_password(void)
 {
        *mpd_password = '\0';
+       mpd_environment_password = 0;
 }
 int mpd_set_port(const char *port)
 {
index 65047fc..6505b46 100644 (file)
--- a/src/mpd.h
+++ b/src/mpd.h
@@ -23,7 +23,7 @@ struct mpd_s {
 
 /* functions for setting the configuration values */
 void mpd_set_host(const char *);
-void mpd_set_password(const char *);
+void mpd_set_password(const char *, int);
 void mpd_clear_password(void);
 int mpd_set_port(const char *);