code reformat

This commit is contained in:
max mustermann 2019-05-08 19:31:49 +02:00
parent 31819abcaf
commit c738a01ce4
9 changed files with 35 additions and 28 deletions

1
.gitignore vendored
View File

@ -20,6 +20,7 @@
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/*.xml
# Gradle
.idea/**/gradle.xml

View File

@ -11,7 +11,7 @@
void Logger::safeip(std::string ip) {
std::ofstream out;
out.open("ip.txt",std::ios::out);
out.open("ip.txt", std::ios::out);
out << ip;
@ -20,7 +20,7 @@ void Logger::safeip(std::string ip) {
std::string Logger::readip() {
std::ifstream in;
in.open("ip.txt",std::ios::in);
in.open("ip.txt", std::ios::in);
std::string ip;
@ -31,16 +31,16 @@ std::string Logger::readip() {
void Logger::logToLogfile(std::string text) {
std::ofstream out;
out.open("dynurefresher.log",std::ios::out | std::ios::app);
out.open("dynurefresher.log", std::ios::out | std::ios::app);
std::time_t t = std::time(0); // get time now
std::tm* now = std::localtime(&t);
std::tm *now = std::localtime(&t);
std::stringstream logline;
logline << "[ "<< (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday
<< "_" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << " ] " << '\t' << text << std::endl;
logline << "[ " << (now->tm_year + 1900) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday
<< "_" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << " ] " << '\t' << text << std::endl;
out << logline.str();

View File

@ -9,7 +9,9 @@
class Logger {
public:
void logToLogfile(std::string text);
void safeip(std::string ip);
std::string readip();
};

View File

@ -12,13 +12,14 @@
std::string API::request(std::string myurl) {
Hashmap<std::string,std::string> map;
Hashmap<std::string, std::string> map;
std::vector<std::string> str;
return request(std::move(myurl),false,map,str);
return request(std::move(myurl), false, map, str);
}
std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::string> &map, std::vector<std::string> &headers) {
std::string
API::request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers) {
CURL *curl;
CURLcode res;
@ -40,12 +41,12 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
if(post){
if (post) {
std::stringstream poststring;
poststring << "{";
poststring << "{";
for (int i = 0; i < map.size(); i++) {
poststring << "\""+map.getKey(i) << "\": \""+map.getValue(i) << "\"";
if(i < map.size()-1){
poststring << "\"" + map.getKey(i) << "\": \"" + map.getValue(i) << "\"";
if (i < map.size() - 1) {
poststring << ", ";
}
}
@ -53,20 +54,19 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, poststring.str().c_str());
}else{
} else {
std::string getstring;
for(int i =0; i< map.size();i++){
getstring+=map.getKey(i)+"="+map.getValue(i);
if(i < map.size()-1){
getstring+="&";
for (int i = 0; i < map.size(); i++) {
getstring += map.getKey(i) + "=" + map.getValue(i);
if (i < map.size() - 1) {
getstring += "&";
}
}
myurl+="?"+getstring;
myurl += "?" + getstring;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString);
@ -78,6 +78,6 @@ std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::
}
size_t API::write_data(void *contents, size_t size, size_t nmemb, FILE *stream) {
((std::string*)stream)->append((char*)contents, size * nmemb);
((std::string *) stream)->append((char *) contents, size * nmemb);
return size * nmemb;
}

View File

@ -12,7 +12,8 @@
class API {
public:
std::string request(std::string myurl);
std::string request(std::string myurl, bool post, Hashmap<std::string,std::string> &map,std::vector<std::string> &headers);
std::string request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers);
private:
static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream);

View File

@ -9,10 +9,13 @@
#include <iostream>
#include <vector>
template <class keytype,class valuetype> class Hashmap {
template<class keytype, class valuetype>
class Hashmap {
public:
void add(keytype key,keytype value);
void add(keytype key, keytype value);
keytype getKey(int position);
valuetype getValue(int position);
int size();

View File

@ -5,6 +5,6 @@
#include "TelegramAPI.h"
void TelegramAPI::sendMessage(std::string text) {
std::string reply = request("https://api.telegram.org/bot"+ apikey + "/sendmessage?chat_id="+chatid+"&text="+text);
std::cout << "[DEBUG] " <<reply << std::endl;
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage?chat_id=" + chatid + "&text=" + text);
std::cout << "[DEBUG] " << reply << std::endl;
}

View File

@ -9,7 +9,7 @@
#include <string>
#include "API.h"
class TelegramAPI : API{
class TelegramAPI : API {
public:
void sendMessage(std::string text);

View File

@ -58,7 +58,7 @@ int main(int argc, char *argv[]) {
if (dynurepl != "{\"statusCode\":200}") {
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
} else{
} else {
TelegramAPI tele;
tele.sendMessage(oldip + " moved to " + ip);
}