add conan file rm existing configparser
This commit is contained in:
		
							
								
								
									
										182
									
								
								src/Config.cpp
									
									
									
									
									
								
							
							
						
						
									
										182
									
								
								src/Config.cpp
									
									
									
									
									
								
							@@ -1,182 +0,0 @@
 | 
			
		||||
#include "Config.h"
 | 
			
		||||
#include "Logger.h"
 | 
			
		||||
#include "StaticData.h"
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <libconfig.h++>
 | 
			
		||||
#include <sys/stat.h>
 | 
			
		||||
 | 
			
		||||
std::string Config::dynuapikey;
 | 
			
		||||
std::string Config::domainid; //id of the dynu domain
 | 
			
		||||
std::string Config::domainname;
 | 
			
		||||
 | 
			
		||||
std::string Config::telegramApiKey;
 | 
			
		||||
std::string Config::chatId;
 | 
			
		||||
 | 
			
		||||
bool Config::telegramSupport;
 | 
			
		||||
 | 
			
		||||
bool Config::readConfig() {
 | 
			
		||||
    libconfig::Config cfg;
 | 
			
		||||
    try {
 | 
			
		||||
        cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str());
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::FileIOException &fioex) {
 | 
			
		||||
        std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl;
 | 
			
		||||
 | 
			
		||||
        // check if config folder exists
 | 
			
		||||
        struct stat info{};
 | 
			
		||||
 | 
			
		||||
        if (stat(StaticData::ConfigDir.c_str(), &info) != 0) {
 | 
			
		||||
            Logger::warning("The config folder doesn't exist. Trying to create it.");
 | 
			
		||||
 | 
			
		||||
// mkdir command is different defined for windows
 | 
			
		||||
#ifdef __unix
 | 
			
		||||
            int check = mkdir(StaticData::ConfigDir.c_str(), 777);
 | 
			
		||||
#else
 | 
			
		||||
            int check = mkdir(StaticData::ConfigDir.c_str());
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
            // check if directory is created or not
 | 
			
		||||
            if (!check)
 | 
			
		||||
                Logger::message("config directory successfully created. ");
 | 
			
		||||
            else
 | 
			
		||||
                Logger::error("unable to create config directory.");
 | 
			
		||||
 | 
			
		||||
        } else if (info.st_mode & S_IFDIR) {
 | 
			
		||||
            Logger::debug("config directory exists already");
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger::error("A file exists with the same name as the config dir should be");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        std::ofstream myfile;
 | 
			
		||||
        myfile.open(StaticData::ConfigDir + StaticData::ConfName);
 | 
			
		||||
        if (myfile.is_open()) {
 | 
			
		||||
            myfile << StaticData::SAMPLECONFIG;
 | 
			
		||||
            myfile.close();
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger::error("error creating file");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::ParseException &pex) {
 | 
			
		||||
        std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
 | 
			
		||||
                  << " - " << pex.getError() << std::endl;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        // needed parameters
 | 
			
		||||
        dynuapikey = (std::string) cfg.lookup("dynuapikey");
 | 
			
		||||
        domainid = (std::string) cfg.lookup("domainid");
 | 
			
		||||
        domainname = (std::string) cfg.lookup("domainname");
 | 
			
		||||
        // optional parameters
 | 
			
		||||
        telegramApiKey = (std::string) cfg.lookup("telegramApiKey");
 | 
			
		||||
        chatId = (std::string) cfg.lookup("chatId");
 | 
			
		||||
        telegramSupport = true;
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::SettingNotFoundException &nfex) {
 | 
			
		||||
        // triggered if setting is missing in config
 | 
			
		||||
        if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) {
 | 
			
		||||
            std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl;
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger::message("no Telegram support - fields in config not set");
 | 
			
		||||
            telegramSupport = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // check if needed values aren't empty
 | 
			
		||||
    return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Config::saveConfig() {
 | 
			
		||||
    // todo save config
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Config::validateConfig() {
 | 
			
		||||
    libconfig::Config cfg;
 | 
			
		||||
    try {
 | 
			
		||||
        Logger::message("reading config file");
 | 
			
		||||
        cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str());
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::FileIOException &fioex) {
 | 
			
		||||
        Logger::warning("config file doesn't exist or permission denied!");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::ParseException &pex) {
 | 
			
		||||
        std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
 | 
			
		||||
                  << " - " << pex.getError() << std::endl;
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    Logger::message("Syntax and Permission is OK");
 | 
			
		||||
 | 
			
		||||
    try {
 | 
			
		||||
        // needed parameters
 | 
			
		||||
        if (((std::string) cfg.lookup("dynuapikey")).empty()) {
 | 
			
		||||
            Logger::warning("required parameter \"dynuapikey\" seems to be empty.");
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        if (((std::string) cfg.lookup("domainid")).empty()) {
 | 
			
		||||
            Logger::warning("required parameter \"domainid\" seems to be empty.");
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        if (((std::string) cfg.lookup("domainname")).empty()) {
 | 
			
		||||
            Logger::warning("required parameter \"domainname\" seems to be empty.");
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        // optional parameters
 | 
			
		||||
        cfg.lookup("telegramApiKey");
 | 
			
		||||
        cfg.lookup("chatId");
 | 
			
		||||
        telegramSupport = true;
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::SettingNotFoundException &nfex) {
 | 
			
		||||
        // triggered if setting is missing in config
 | 
			
		||||
        if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) {
 | 
			
		||||
            std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl;
 | 
			
		||||
            return false;
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger::message("no Telegram support - fields in config not set");
 | 
			
		||||
            telegramSupport = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Config::isTelegramSupported() {
 | 
			
		||||
    return telegramSupport;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getDynuapikey() {
 | 
			
		||||
    return dynuapikey;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getDomainid() {
 | 
			
		||||
    return domainid;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getDomainname() {
 | 
			
		||||
    return domainname;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getTelegramApiKey() {
 | 
			
		||||
    return telegramApiKey;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getChatId() {
 | 
			
		||||
    return chatId;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Config::setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid) {
 | 
			
		||||
    Config::domainname = domainname;
 | 
			
		||||
    Config::dynuapikey = dynuapikey;
 | 
			
		||||
    Config::domainid = domainid;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Config::setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid, const std::string &telegramApiKey, const std::string &chatId) {
 | 
			
		||||
    setValues(domainname, dynuapikey, domainid);
 | 
			
		||||
    Config::telegramApiKey = telegramApiKey;
 | 
			
		||||
    Config::chatId = chatId;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										81
									
								
								src/ConfigParser.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								src/ConfigParser.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,81 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 09.10.20.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <inc/StaticData.h>
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <regex>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "inc/ConfigParser.h"
 | 
			
		||||
 | 
			
		||||
bool ConfigParser::loadConfig() {
 | 
			
		||||
    const std::string config = StaticData::ConfigDir + StaticData::ConfName;
 | 
			
		||||
 | 
			
		||||
    const std::regex matchcomment(R"(^\s*#)"); // match hash to be a comment line
 | 
			
		||||
    const std::regex matchkey(R"(.+(?=\=.+))");
 | 
			
		||||
    const std::regex matchvalue(R"((?:=(.+)(?=\s*#*)))");
 | 
			
		||||
 | 
			
		||||
    std::map<std::string, std::string> entries;
 | 
			
		||||
 | 
			
		||||
    std::ifstream myfile(config);
 | 
			
		||||
    if (myfile.is_open()) {
 | 
			
		||||
        std::string line;
 | 
			
		||||
 | 
			
		||||
        while (getline(myfile, line)) {
 | 
			
		||||
            if (std::regex_search(line, matchcomment) || line == "") {
 | 
			
		||||
                // comment line
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // parse a key value pair
 | 
			
		||||
            std::smatch mk, mv;
 | 
			
		||||
            std::regex_search(line, mk, matchkey);
 | 
			
		||||
            std::regex_search(line, mv, matchvalue);
 | 
			
		||||
 | 
			
		||||
            if (!mk.empty() && !mv.empty()){
 | 
			
		||||
                entries.insert(std::pair<std::string, std::string>(mk[0], mv[0]));
 | 
			
		||||
                std::cout << mk[0] << "--" << mv[0] << std::endl;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        myfile.close();
 | 
			
		||||
    } else return false;
 | 
			
		||||
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ConfigParser::saveConfig() {
 | 
			
		||||
    // todo
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ConfigParser::validateConfig() {
 | 
			
		||||
    // todo
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ConfigParser::isTelegramSupported() {
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &ConfigParser::getDynuapikey() {
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &ConfigParser::getDomainid() {
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &ConfigParser::getDomainname() {
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &ConfigParser::getTelegramApiKey() {
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &ConfigParser::getChatId() {
 | 
			
		||||
    return "";
 | 
			
		||||
}
 | 
			
		||||
@@ -3,13 +3,13 @@
 | 
			
		||||
#include "api/IPAPI.h"
 | 
			
		||||
#include "api/DynuAPI.h"
 | 
			
		||||
#include "api/TelegramAPI.h"
 | 
			
		||||
#include "Config.h"
 | 
			
		||||
#include "StaticData.h"
 | 
			
		||||
#include "IpHelper.h"
 | 
			
		||||
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#include <thread>
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
#include <inc/ConfigParser.h>
 | 
			
		||||
 | 
			
		||||
bool IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
    FileLogger logger;
 | 
			
		||||
@@ -35,13 +35,13 @@ bool IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
            Logger::message("ip changed! -- from :" + oldip + "to: " + ip);
 | 
			
		||||
 | 
			
		||||
            DynuAPI dynu;
 | 
			
		||||
            dynu.init(Config::getDynuapikey(), Config::getDomainid(), Config::getDomainname());
 | 
			
		||||
            dynu.init(ConfigParser::getDynuapikey(), ConfigParser::getDomainid(), ConfigParser::getDomainname());
 | 
			
		||||
            // actual refresh of IP in api - here
 | 
			
		||||
            bool result = dynu.refreshIp(ip);
 | 
			
		||||
 | 
			
		||||
            if (result && Config::isTelegramSupported()) {
 | 
			
		||||
            if (result && ConfigParser::isTelegramSupported()) {
 | 
			
		||||
                TelegramAPI tele;
 | 
			
		||||
                tele.init(Config::getTelegramApiKey(), Config::getChatId());
 | 
			
		||||
                tele.init(ConfigParser::getTelegramApiKey(), ConfigParser::getChatId());
 | 
			
		||||
                tele.sendMessage(oldip + " moved to " + ip);
 | 
			
		||||
            } else if (!result) {
 | 
			
		||||
                //error
 | 
			
		||||
@@ -61,7 +61,7 @@ void IPRefresher::startUpService(int interval) {
 | 
			
		||||
 | 
			
		||||
    while (true) {
 | 
			
		||||
        Logger::message("starting check");
 | 
			
		||||
        if (Config::readConfig()) {
 | 
			
		||||
        if (ConfigParser::loadConfig()) {
 | 
			
		||||
            checkIPAdress(false);
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -1,8 +1,8 @@
 | 
			
		||||
#include "StaticData.h"
 | 
			
		||||
#include "IPRefresher.h"
 | 
			
		||||
#include "Logger.h"
 | 
			
		||||
#include "Config.h"
 | 
			
		||||
#include "api/IPAPI.h"
 | 
			
		||||
#include "ConfigParser.h"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * application entry point
 | 
			
		||||
@@ -21,7 +21,7 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
        } else if (firstarg == "-v" || firstarg == "--version") {
 | 
			
		||||
            std::cout << "Version " << StaticData::VERSION << std::endl;
 | 
			
		||||
        } else if (firstarg == "-f" || firstarg == "--force") {
 | 
			
		||||
            if (Config::readConfig()) {
 | 
			
		||||
            if (ConfigParser::loadConfig()) {
 | 
			
		||||
                IPRefresher::checkIPAdress(true);
 | 
			
		||||
            } else {
 | 
			
		||||
                std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
@@ -30,7 +30,7 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
        } else if (firstarg == "-l" || firstarg == "--loop") {
 | 
			
		||||
            IPRefresher::startUpService(true);
 | 
			
		||||
        } else if (firstarg == "-c" || firstarg == "--checkconfig") {
 | 
			
		||||
            if (Config::validateConfig()) {
 | 
			
		||||
            if (ConfigParser::validateConfig()) {
 | 
			
		||||
                Logger::message("Config file is OK");
 | 
			
		||||
            } else {
 | 
			
		||||
                Logger::error("There are errors in config file!");
 | 
			
		||||
@@ -43,8 +43,11 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
            Logger::message("wrong arguments!  -h for help");
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        ConfigParser::loadConfig();
 | 
			
		||||
 | 
			
		||||
        return 0;
 | 
			
		||||
        Logger::message("starting check");
 | 
			
		||||
        if (Config::readConfig()) {
 | 
			
		||||
        if (ConfigParser::loadConfig()) {
 | 
			
		||||
            IPRefresher::checkIPAdress(false);
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user