diff --git a/CMakeLists.txt b/CMakeLists.txt index 6502211..8ba8683 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,15 +66,19 @@ if (${WinBuild}) # or set(CMAKE_CXX_STANDARD_LIBRARIES -lcurl -lpthread -static-libgcc -static-libstdc++ -lcrypto -lssl -lws2_32 -static -DCURL_STATICLIB) # or add_definitions(-DCURL_STATICLIB) - # windows config path is same as executable + # windows config path is in %appdata% folder of user set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") + # temp file is also stored in appdata folder + set(TempFilePath "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") else () message(STATUS "using nativ gcc toolchain.") set(LIBSUFFIX .so) set(SUFFIX "") - # set /etc/ config path + # set linux config path set(CONFIG_PATH "\"/etc/\"") + # set path of temp file + set(TempFilePath "\"/var/tmp/\"") endif () # test compiler settings and enable languages here @@ -146,10 +150,10 @@ message("") #read sample config FILE(READ ${CMAKE_SOURCE_DIR}/config/dynuiprefresher.cfg SAMPLECONFIG) -#add version header -FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h +#add StaticData header +FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/StaticData.h "/** - * Version header to store Version, Config dir and a Sample config + * StaticData header to store Version, Config dir and a Sample config * Do not edit this file manually, it is generated by the cmake script! * * @author Lukas Heiligenbrunner @@ -160,8 +164,9 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h #include -namespace Version { +namespace StaticData { const std::string VERSION = \"${PROJECT_VERSION}\"; + const std::string TempFilePath = ${TempFilePath}; const std::string ConfigDir = ${CONFIG_PATH}; const std::string ConfName = \"${Application_Name}.cfg\"; const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\"; diff --git a/src/Config.cpp b/src/Config.cpp index 217bdc7..26ad6ba 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,6 +1,6 @@ #include "Config.h" #include "Logger.h" -#include "Version.h" +#include "StaticData.h" #include #include @@ -20,7 +20,7 @@ bool Config::telegramSupport; bool Config::readConfig() { libconfig::Config cfg; try { - cfg.readFile(std::string(Version::ConfigDir + Version::ConfName).c_str()); + 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; @@ -28,13 +28,14 @@ bool Config::readConfig() { // check if config folder exists struct stat info{}; - if (stat(Version::ConfigDir.c_str(), &info) != 0) { + 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(Version::ConfigDir.c_str(), 777); + int check = mkdir(StaticData::ConfigDir.c_str(), 777); #else - int check = mkdir(Version::ConfigDir.c_str()); + int check = mkdir(StaticData::ConfigDir.c_str()); #endif // check if directory is created or not @@ -51,9 +52,9 @@ bool Config::readConfig() { std::ofstream myfile; - myfile.open(Version::ConfigDir + Version::ConfName); + myfile.open(StaticData::ConfigDir + StaticData::ConfName); if (myfile.is_open()) { - myfile << Version::SAMPLECONFIG; + myfile << StaticData::SAMPLECONFIG; myfile.close(); } else { Logger::error("error creating file"); @@ -94,7 +95,7 @@ bool Config::validateConfig() { libconfig::Config cfg; try { Logger::message("reading config file"); - cfg.readFile(Version::ConfigDir.c_str()); + cfg.readFile(StaticData::ConfigDir.c_str()); } catch (const libconfig::FileIOException &fioex) { Logger::warning("config file doesn't exist or permission denied!"); diff --git a/src/FileLogger.cpp b/src/FileLogger.cpp index 78d670c..0e9caf0 100644 --- a/src/FileLogger.cpp +++ b/src/FileLogger.cpp @@ -1,12 +1,13 @@ #include "FileLogger.h" #include "IpHelper.h" +#include "StaticData.h" #include #include void FileLogger::safeip(std::string ip) { std::ofstream out; - out.open("ip.txt", std::ios::out); + out.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::out); out << ip; @@ -15,7 +16,7 @@ void FileLogger::safeip(std::string ip) { std::string FileLogger::readip() { std::ifstream in; - in.open("ip.txt", std::ios::in); + in.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::in); std::string ip; diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 092b577..af66491 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -4,7 +4,7 @@ #include "api/DynuAPI.h" #include "api/TelegramAPI.h" #include "Config.h" -#include "Version.h" +#include "StaticData.h" #include "IpHelper.h" #include @@ -53,7 +53,7 @@ void IPRefresher::checkIPAdress(bool force) { IPRefresher::IPRefresher(bool loop) { if (loop) { Logger::message("startup of service"); - Logger::message("Version: " + Version::VERSION); + Logger::message("Version: " + StaticData::VERSION); while (true) { Logger::message("starting check"); diff --git a/src/IpHelper.cpp b/src/IpHelper.cpp index 67c5384..5be0bf0 100644 --- a/src/IpHelper.cpp +++ b/src/IpHelper.cpp @@ -1,7 +1,5 @@ #include "IpHelper.h" -#include - bool IpHelper::isIpValid(std::string ip) { - return (ip.find('.') != ULONG_MAX); + return (ip.find('.') != SIZE_MAX); } diff --git a/src/api/TelegramAPI.cpp b/src/api/TelegramAPI.cpp index c89a247..4cc3acf 100644 --- a/src/api/TelegramAPI.cpp +++ b/src/api/TelegramAPI.cpp @@ -1,8 +1,6 @@ #include "api/TelegramAPI.h" #include "Logger.h" -#include - int TelegramAPI::sendMessage(const std::string &text) { Hashmap args; args.add("chat_id", chatid); @@ -12,8 +10,8 @@ int TelegramAPI::sendMessage(const std::string &text) { std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); - if (reply.find("\"error_code\"") != ULONG_MAX) { - Logger::error("failed to refresh the ip (Dynu API)"); + if (reply.find("\"error_code\"") != SIZE_MAX) { + Logger::error("failed to send the Telegram Message"); return -1; } return 1; diff --git a/src/main.cpp b/src/main.cpp index 6518f92..5f830a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "Version.h" +#include "StaticData.h" #include "IPRefresher.h" #include "Logger.h" #include "Config.h" @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) { << "[-ip] [--currentip] get current global ip" << std::endl << "[no argument] normal ip check and refresh" << std::endl; } else if (firstarg == "-v" || firstarg == "--version") { - std::cout << "Version " << Version::VERSION << std::endl; + std::cout << "Version " << StaticData::VERSION << std::endl; } else if (firstarg == "-f" || firstarg == "--force") { IPRefresher ipr; if (Config::readConfig()) { diff --git a/tests/UnitTest.cpp b/tests/UnitTest.cpp index e1d5bcc..2c97a9e 100644 --- a/tests/UnitTest.cpp +++ b/tests/UnitTest.cpp @@ -4,7 +4,6 @@ #include #include -#include #include "gtest/gtest.h" /** @@ -21,7 +20,7 @@ TEST(ReadIp, testzeroIpIfNotExists) { TEST(IPAPI, testIpAPIcheckIPSyntax) { IPAPI ipapi; std::string ip = ipapi.getGlobalIp(); - if (ip.find('.') == ULONG_MAX) { + if (ip.find('.') == SIZE_MAX) { // error when ip doesn't contain a . ASSERT_TRUE(false); } else {