Skip to content

Commit

Permalink
rework configuration system
Browse files Browse the repository at this point in the history
closes #42

Squashed commit of the following:

commit 8b6019789cb38f221a3925f0a69c791ed0b99653
Author: Henrik Friedrichsen <[email protected]>
Date:   Sat Aug 26 14:08:58 2017 +0200

    don't print config values

commit 537faad
Author: Henrik Friedrichsen <[email protected]>
Date:   Fri Aug 25 15:51:58 2017 +0200

    use reference passed to inihandler

commit b6736ab
Author: Henrik Friedrichsen <[email protected]>
Date:   Fri Aug 25 15:18:51 2017 +0200

    Revert "don't print config values"

    This reverts commit faeaf02.

commit 272346b
Author: Henrik Friedrichsen <[email protected]>
Date:   Fri Aug 25 15:18:12 2017 +0200

    add inih files to project instead of using submodule

commit faeaf02
Author: Henrik Friedrichsen <[email protected]>
Date:   Fri Aug 25 14:32:20 2017 +0200

    don't print config values

commit 0b3a131
Author: Henrik Friedrichsen <[email protected]>
Date:   Fri Aug 25 14:31:12 2017 +0200

    formatting/indentation

commit 50f4e40
Author: Henrik Friedrichsen <[email protected]>
Date:   Fri Aug 25 14:25:00 2017 +0200

    further implementation of new config system

commit 31ac8ce
Author: Henrik Friedrichsen <[email protected]>
Date:   Thu Aug 24 19:51:59 2017 +0200

    more wip code, won't link yet

commit 721cec0
Author: Henrik Friedrichsen <[email protected]>
Date:   Thu Aug 24 19:28:34 2017 +0200

    wip: better config handling

commit 3391769
Author: Henrik Friedrichsen <[email protected]>
Date:   Thu Aug 24 19:12:25 2017 +0200

    switch to verbose compilation output
  • Loading branch information
hrkfdn committed Aug 26, 2017
1 parent b8a189d commit 7c0d266
Show file tree
Hide file tree
Showing 11 changed files with 443 additions and 106 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "inih"]
path = inih
url = https://github.com/benhoyt/inih
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VERSION = 0.4.3
VERSION = 0.4.4

CXX ?= g++
OBJ = main.o md5.o utils.o mpd.o audioscrobbler.o cache.o config.o
OBJ = main.o md5.o utils.o mpd.o audioscrobbler.o cache.o config.o ini.o
OUT = mpdas
PREFIX ?= /usr/local
MANPREFIX ?= ${PREFIX}/man/man1
Expand Down
38 changes: 19 additions & 19 deletions audioscrobbler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ void CAudioScrobbler::OpenURL(std::string url, const char* postfields = 0, char*
// Sometimes last.fm likes to just timeout for no reason, leaving us hanging.
// If this happens, retry a few times with a small delay.
if (res != CURLE_OK) {
eprintf("libcurl error (%d): %s", res, curl_easy_strerror(res));
eprintf("Will retry %d times with a %d second delay.", CURL_MAX_RETRIES, CURL_RETRY_DELAY);

int retries = 0;
do {
sleep(CURL_RETRY_DELAY);
retries++;
eprintf("Retry %d/%d", retries, CURL_MAX_RETRIES);

res = curl_easy_perform(_handle);
if (res != CURLE_OK) {
eprintf("Failed: %s", curl_easy_strerror(res));
}
} while (res != CURLE_OK && retries < CURL_MAX_RETRIES);
eprintf("libcurl error (%d): %s", res, curl_easy_strerror(res));
eprintf("Will retry %d times with a %d second delay.", CURL_MAX_RETRIES, CURL_RETRY_DELAY);

int retries = 0;
do {
sleep(CURL_RETRY_DELAY);
retries++;
eprintf("Retry %d/%d", retries, CURL_MAX_RETRIES);

res = curl_easy_perform(_handle);
if (res != CURLE_OK) {
eprintf("Failed: %s", curl_easy_strerror(res));
}
} while (res != CURLE_OK && retries < CURL_MAX_RETRIES);
}
}

Expand Down Expand Up @@ -304,15 +304,15 @@ bool CAudioScrobbler::SendNowPlaying(const Song& song)
void CAudioScrobbler::Handshake()
{
std::string username="";
for(unsigned int i = 0; i < Config->getLUsername().length(); i++) {
username.append(1, tolower(Config->getLUsername().c_str()[i]));
for(unsigned int i = 0; i < Config->Get("username").length(); i++) {
username.append(1, tolower(Config->Get("username").c_str()[i]));
}
std::string authtoken(md5sum((char*)"%s%s", username.c_str(), Config->getLPassword().c_str()));
std::string authtoken(md5sum((char*)"%s%s", username.c_str(), Config->Get("password").c_str()));

std::ostringstream query, sig;
query << "method=auth.getMobileSession&username=" << username << "&password=" << Config->getLPassword().c_str() << "&api_key=" << APIKEY;
query << "method=auth.getMobileSession&username=" << username << "&password=" << Config->Get("password").c_str() << "&api_key=" << APIKEY;

sig << "api_key" << APIKEY << "methodauth.getMobileSession" << "password" << Config->getLPassword().c_str() << "username" << username << SECRET;
sig << "api_key" << APIKEY << "methodauth.getMobileSession" << "password" << Config->Get("password").c_str() << "username" << username << SECRET;
std::string sighash(md5sum((char*)"%s", sig.str().c_str()));

query << "&api_sig=" << sighash;
Expand Down
107 changes: 45 additions & 62 deletions config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,62 @@

CConfig* Config = 0;

void CConfig::ParseLine(std::string line)
int IniHandler(void* param, const char* section, const char* name, const char* value)
{
std::vector<std::string> tokens;
char* pstr = 0;
char* szline = new char[line.size()+1];

strncpy(szline, line.c_str(), line.size()+1);

pstr = strtok(szline, " =\t");
while(pstr) {
tokens.push_back(pstr);
pstr = strtok(NULL, " =\t");
}
delete[] szline;

if(tokens.size() > 1) {
if(tokens[0] == "username")
_lusername = tokens[1];
else if(tokens[0] == "password")
_lpassword = tokens[1];
else if(tokens[0] == "host")
_mhost = tokens[1];
else if(tokens[0] == "mpdpassword")
_mpassword = tokens[1];
else if(tokens[0] == "service" && tokens[1] == "librefm")
_service = LibreFm;
else if(tokens[0] == "port")
_mport = atoi(tokens[1].c_str());
else if(tokens[0] == "runas")
_runninguser = tokens[1];
else if(tokens[0] == "debug") {
if(tokens[1] == "1" || tokens[1] == "true")
_debug = true;
}

}
CConfig *config = (CConfig*)param;
config->Set(name, value);
return 1;
}

void CConfig::LoadConfig(std::string path)
{
std::string line = "";
if(ini_parse(path.c_str(), &IniHandler, this) < 0) {
iprintf("Cannot parse config file (%s).", path.c_str());
return;
}
}
std::string CConfig::Get(std::string name)
{
if(_configuration.find(name) == _configuration.end()) {
return "";
}

std::ifstream ifs(path.c_str(), std::ios::in);
return _configuration.find(name)->second;
}

if(!ifs.good()) {
iprintf("Config file (%s) does not exist or is not readable.", path.c_str());
return;
}
bool CConfig::GetBool(std::string name)
{
std::string value = Get(name);
return value == "1" || value == "true";
}

while(ifs.good()) {
getline(ifs, line);
ParseLine(line);
}
int CConfig::GetInt(std::string name)
{
return atoi(Get(name).c_str());
}

ScrobblingService CConfig::getService()
{
return Get("service") == "librefm" ? LibreFm : LastFm;
}

CConfig::CConfig(char* cfg)
{
/* Set optional settings to default */
_mhost = "localhost";
_service = LastFm;
_mport = 6600;
_debug = false;

std::string path = "";

if(!cfg) {
path = CONFDIR;
path.append("/mpdasrc");
}
else {
path = cfg;
}

LoadConfig(path);
/* Set optional settings to default */
Set("host", "localhost");
Set("port", "6600");
Set("debug", "false");
Set("service", "lastfm");

std::string path = "";

if(!cfg) {
path = CONFDIR;
path.append("/mpdasrc");
}
else {
path = cfg;
}

LoadConfig(path);
}
28 changes: 11 additions & 17 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,23 @@ class CConfig
public:
CConfig(char* cfg);

std::string getLUsername() { return _lusername; }
std::string getLPassword() { return _lpassword; }
std::string getMHost() { return _mhost; }
std::string getMPassword() { return _mpassword; }
std::string getRUser() { return _runninguser; }
ScrobblingService getService() { return _service; }
bool getDebug() { return (_debug == true); }
int getMPort() { return _mport; }

bool gotNecessaryData() {
if(!_lusername.size() || !_lpassword.size())
ScrobblingService getService();

std::string Get(std::string name);
bool GetBool(std::string name);
int GetInt(std::string name);
void Set(std::string name, std::string value) { _configuration[name] = value; };

bool gotNecessaryData() {
if(!Get("username").size() || !Get("password").size())
return false;
return true;
}

void LoadConfig(std::string path);
private:
void ParseLine(std::string line);
std::string _lusername, _lpassword;
std::string _mhost, _mpassword;
std::string _runninguser;
ScrobblingService _service;
int _mport;
bool _debug;
std::map<std::string, std::string> _configuration;
};

extern CConfig* Config;
Expand Down
Loading

0 comments on commit 7c0d266

Please sign in to comment.