From c17911ba268a4a784c57baed1540d08ba8741e04 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sun, 15 Jun 2008 21:23:37 +0200 Subject: client: Config file added, not yet documented Some restructuring was necessary too. --- libpiano/main.c | 4 --- src/Makefile.am | 2 +- src/main.c | 34 ++++++++++++++++----- src/settings.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/settings.h | 39 +++++++++++++++++++++++ src/terminal.c | 50 ++++++++++++++++++++++++++++++ src/terminal.h | 29 ++++++++++++++++++ 7 files changed, 240 insertions(+), 13 deletions(-) create mode 100644 src/settings.c create mode 100644 src/settings.h create mode 100644 src/terminal.c create mode 100644 src/terminal.h diff --git a/libpiano/main.c b/libpiano/main.c index 9754e73..faf5317 100644 --- a/libpiano/main.c +++ b/libpiano/main.c @@ -47,10 +47,6 @@ void PianoInit (PianoHandle_t *ph) { snprintf (ph->routeId, sizeof (ph->routeId), "%07liP", time (NULL)>>8); /* at the moment we don't need publicity */ curl_easy_setopt (ph->curlHandle, CURLOPT_USERAGENT, PIANO_USERAGENT); - /* set tor as control connection proxy */ - curl_easy_setopt (ph->curlHandle, CURLOPT_PROXY, "localhost:9050"); - curl_easy_setopt (ph->curlHandle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A); - curl_easy_setopt (ph->curlHandle, CURLOPT_CONNECTTIMEOUT, 60); } /* free complete search result diff --git a/src/Makefile.am b/src/Makefile.am index 20b5b40..1a8f3cf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ bin_PROGRAMS = pianobar -pianobar_SOURCES = main.c +pianobar_SOURCES = main.c terminal.c terminal.h settings.c settings.h pianobar_CPPFLAGS = ${LIBCURL_CFLAGS} ${LIBAO_CFLAGS} ${LIBXML_CFLAGS} -I../libpiano pianobar_LDADD = ${LIBCURL_LIBS} ${LIBAO_LIBS} ${LIBFAAD_LIBS} ${LIBREADLINE_LIBS} ${LIBXML_LIBS} -lpthread ../libpiano/libpiano.la diff --git a/src/main.c b/src/main.c index 05d1f4e..a7df2aa 100644 --- a/src/main.c +++ b/src/main.c @@ -31,10 +31,12 @@ THE SOFTWARE. #include #include #include -#include #include #include +#include "terminal.h" +#include "settings.h" + struct aacPlayer { /* buffer */ char *buffer; @@ -278,23 +280,38 @@ int main (int argc, char **argv) { char doQuit = 0; PianoSong_t *curSong = NULL; PianoStation_t *curStation; - struct termios termopts; + BarSettings_t bsettings; /* init some things */ curl_global_init (CURL_GLOBAL_SSL); xmlInitParser (); ao_initialize(); + + BarSettingsInit (&bsettings); + readSettings (&bsettings); + + if (bsettings.username == NULL) { + bsettings.username = readline ("Username: "); + } + if (bsettings.password == NULL) { + termSetEcho (0); + bsettings.password = readline ("Password: "); + termSetEcho (1); + } + PianoInit (&ph); + /* setup control proxy */ + curl_easy_setopt (ph.curlHandle, CURLOPT_PROXY, bsettings.controlProxy); + curl_easy_setopt (ph.curlHandle, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A); + curl_easy_setopt (ph.curlHandle, CURLOPT_CONNECTTIMEOUT, 60); /* no buffering for stdin */ - tcgetattr (fileno (stdin), &termopts); - termopts.c_lflag &= ~ICANON; - tcsetattr(fileno (stdin), TCSANOW, &termopts); - setvbuf (stdin, NULL, _IONBF, 1); + termSetBuffer (0); - PianoConnect (&ph, argv[1], argv[2]); + printf ("Login...\n"); + PianoConnect (&ph, bsettings.username, bsettings.password); + printf ("Get stations...\n"); PianoGetStations (&ph); - printf ("webAuthToken: %s\nauthToken: %s\nlistenerId: %s\n", ph.user.webAuthToken, ph.user.authToken, ph.user.listenerId); /* select station */ curStation = selectStation (&ph); @@ -467,6 +484,7 @@ int main (int argc, char **argv) { curl_global_cleanup (); ao_shutdown(); xmlCleanupParser (); + BarSettingsDestroy (&bsettings); return 0; } diff --git a/src/settings.c b/src/settings.c new file mode 100644 index 0000000..2b1c313 --- /dev/null +++ b/src/settings.c @@ -0,0 +1,95 @@ +/* +Copyright (c) 2008 Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include +#include +#include + +#include "settings.h" + +/* tries to guess your config dir; somehow conforming to + * http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html + * @author PromyLOPh + * @added 2008-06-15 + * @param name of the config file (can contain subdirs too) + * @param store the whole path here + * @param but only up to this size + * @return nothing + */ +void getXdgConfigDir (char *filename, char *retDir, size_t retDirN) { + char *xdgConfigDir = NULL; + + if ((xdgConfigDir = getenv ("XDG_CONFIG_HOME")) != NULL && + strlen (xdgConfigDir) > 0) { + /* special dir: $xdg_config_home */ + snprintf (retDir, retDirN, "%s/%s", xdgConfigDir, filename); + } else { + if ((xdgConfigDir = getenv ("HOME")) != NULL && + strlen (xdgConfigDir) > 0) { + /* standard config dir: $home/.config */ + snprintf (retDir, retDirN, "%s/.config/%s", xdgConfigDir, + filename); + } else { + /* fallback: working dir */ + snprintf (retDir, retDirN, "%s", filename); + } + } +} + +void BarSettingsInit (BarSettings_t *settings) { + memset (settings, 0, sizeof (*settings)); +} + +void BarSettingsDestroy (BarSettings_t *settings) { + free (settings->controlProxy); + free (settings->username); + free (settings->password); +} + +void readSettings (BarSettings_t *settings) { + char configfile[1024], key[256], val[256]; + FILE *configfd; + + getXdgConfigDir (PACKAGE "/config", configfile, sizeof (configfile)); + if ((configfd = fopen (configfile, "r")) == NULL) { + printf ("config file at %s not found\n", configfile); + return; + } + + while (!feof (configfd)) { + memset (val, 0, sizeof (*val)); + memset (key, 0, sizeof (*key)); + if (fscanf (configfd, "%255s = %255[^\n]", key, val) < 2) { + /* invalid config line */ + continue; + } + if (strcmp ("control_proxy", key) == 0) { + settings->controlProxy = strdup (val); + } else if (strcmp ("user", key) == 0) { + settings->username = strdup (val); + } else if (strcmp ("password", key) == 0) { + settings->password = strdup (val); + } + } + + fclose (configfd); +} diff --git a/src/settings.h b/src/settings.h new file mode 100644 index 0000000..26ca527 --- /dev/null +++ b/src/settings.h @@ -0,0 +1,39 @@ +/* +Copyright (c) 2008 Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef _SETTINGS_H +#define _SETTINGS_H + +struct BarSettings { + char *username; + char *password; + char *controlProxy; /* non-american listeners need this */ +}; + +typedef struct BarSettings BarSettings_t; + +void BarSettingsInit (BarSettings_t *settings); +void BarSettingsDestroy (BarSettings_t *settings); + +void readSettings (BarSettings_t *settings); + +#endif /* _SETTINGS_H */ diff --git a/src/terminal.c b/src/terminal.c new file mode 100644 index 0000000..e3fd5d0 --- /dev/null +++ b/src/terminal.c @@ -0,0 +1,50 @@ +/* +Copyright (c) 2008 Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include +#include + +void termSetEcho (char enable) { + struct termios termopts; + + tcgetattr (fileno (stdin), &termopts); + if (enable == 1) { + termopts.c_lflag |= ECHO; + } else { + termopts.c_lflag &= ~ECHO; + } + tcsetattr(fileno (stdin), TCSANOW, &termopts); +} + +void termSetBuffer (char enable) { + struct termios termopts; + + tcgetattr (fileno (stdin), &termopts); + if (enable == 1) { + termopts.c_lflag |= ICANON; + setlinebuf (stdin); + } else { + termopts.c_lflag &= ~ICANON; + setvbuf (stdin, NULL, _IONBF, 1); + } + tcsetattr(fileno (stdin), TCSANOW, &termopts); +} diff --git a/src/terminal.h b/src/terminal.h new file mode 100644 index 0000000..397a5d6 --- /dev/null +++ b/src/terminal.h @@ -0,0 +1,29 @@ +/* +Copyright (c) 2008 Lars-Dominik Braun + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef _TERMINAL_H +#define _TERMINAL_H + +void termSetEcho (char enable); +void termSetBuffer (char enable); + +#endif /* _TERMINAL_H */ -- cgit v1.2.3