From f325c3a3710293c7f4afdbd8e1c663da2a07f6f7 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Fri, 2 Aug 2019 22:44:42 +0200 Subject: [PATCH] outsourced main method of ip refresh and added some doc --- .idea/.gitignore | 2 ++ CMakeLists.txt | 2 +- inc/IPRefresher.h | 14 ++++++++++++ inc/Logger.h | 1 + inc/api/TelegramAPI.h | 11 ++++++++-- src/IPRefresher.cpp | 47 +++++++++++++++++++++++++++++++++++++++++ src/api/TelegramAPI.cpp | 5 +++++ src/main.cpp | 43 +++---------------------------------- 8 files changed, 82 insertions(+), 43 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 inc/IPRefresher.h create mode 100644 src/IPRefresher.cpp diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e8e690..f7ee912 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,7 @@ add_library(logger set(SOURCE src/main.cpp - ) + src/IPRefresher.cpp inc/IPRefresher.h) add_executable(iprefresher ${SOURCE}) diff --git a/inc/IPRefresher.h b/inc/IPRefresher.h new file mode 100644 index 0000000..2c5f93f --- /dev/null +++ b/inc/IPRefresher.h @@ -0,0 +1,14 @@ +// +// Created by lukas on 02.08.19. +// + +#pragma once + + +class IPRefresher { +public: + /** + * refresh ip address on Dynu server + */ + void checkIPAdress(); +}; diff --git a/inc/Logger.h b/inc/Logger.h index 32ab87f..465dffb 100644 --- a/inc/Logger.h +++ b/inc/Logger.h @@ -3,6 +3,7 @@ // #pragma once +#include class Logger { public: /** diff --git a/inc/api/TelegramAPI.h b/inc/api/TelegramAPI.h index 921ed93..dc09035 100644 --- a/inc/api/TelegramAPI.h +++ b/inc/api/TelegramAPI.h @@ -16,7 +16,14 @@ public: */ void sendMessage(std::string text); + /** + * init Telegram api with apikey and chatid + * @param apikey recieved API key + * @param chatid chatid where bot should post into + */ + void init(std::string apikey, std::string chatid); + private: - const std::string apikey = "717213769:AAHan1nSXhUsxLJAN1Dv8Oc0z8wqwDdYPn4"; - const std::string chatid = "618154204"; + std::string apikey; + std::string chatid; }; diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp new file mode 100644 index 0000000..3249991 --- /dev/null +++ b/src/IPRefresher.cpp @@ -0,0 +1,47 @@ +// +// Created by lukas on 02.08.19. +// + +#include + +#include +#include +#include +#include +#include "IPRefresher.h" + +void IPRefresher::checkIPAdress() { + Logger logger; + + IPAPI ipapi; + std::string ip = ipapi.getGlobalIp(); + + if (ip.empty()) { + //no internet connection + logger.logToLogfile("[WARNING] no internet connection"); + std::cout << "[WARNING] no internet connection" << std::endl; + } else { + std::string oldip = logger.readip(); + + if (oldip == ip) { + std::cout << "[INFO] no change -- ip: " << ip << std::endl; + logger.logToLogfile(" [INFO] no change -- ip: " + ip); + } else { + logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip); + std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl; + + DynuAPI dynu; + + if (dynu.refreshIp(ip)) { + TelegramAPI tele; + tele.init("717213769:AAHan1nSXhUsxLJAN1Dv8Oc0z8wqwDdYPn4","618154204"); + tele.sendMessage(oldip + " moved to " + ip); + } else { + //error + logger.logToLogfile(" [ERROR] failed to write ip to dynu api!"); + } + + logger.safeip(ip); + } + } +} diff --git a/src/api/TelegramAPI.cpp b/src/api/TelegramAPI.cpp index d70f9d1..f44d589 100644 --- a/src/api/TelegramAPI.cpp +++ b/src/api/TelegramAPI.cpp @@ -14,3 +14,8 @@ void TelegramAPI::sendMessage(std::string text) { std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); // std::cout << "[DEBUG] " << reply << std::endl; } + +void TelegramAPI::init(std::string apikey, std::string chatid) { + this->apikey = apikey; + this->chatid = chatid; +} diff --git a/src/main.cpp b/src/main.cpp index a99b777..acdb55f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,5 @@ #include -#include -#include - -#include "api/API.h" -#include "Logger.h" -#include "api/TelegramAPI.h" -#include "api/DynuAPI.h" -#include "api/IPAPI.h" +#include int main(int argc, char *argv[]) { @@ -22,38 +15,8 @@ int main(int argc, char *argv[]) { std::cout << "wrong arguments! -h for help" << std::endl; } } else { - Logger logger; - - IPAPI ipapi; - std::string ip = ipapi.getGlobalIp(); - - if (ip.empty()) { - //no internet connection - logger.logToLogfile("[WARNING] no internet connection"); - std::cout << "[WARNING] no internet connection" << std::endl; - } else { - std::string oldip = logger.readip(); - - if (oldip == ip) { - std::cout << "[INFO] no change -- ip: " << ip << std::endl; - logger.logToLogfile(" [INFO] no change -- ip: " + ip); - } else { - logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip); - std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl; - - DynuAPI dynu; - - if (dynu.refreshIp(ip)) { - TelegramAPI tele; - tele.sendMessage(oldip + " moved to " + ip); - } else { - //error - logger.logToLogfile(" [ERROR] failed to write ip to dynu api!"); - } - - logger.safeip(ip); - } - } + IPRefresher ipr; + ipr.checkIPAdress(); } return 0;