// // Created by wolverindev on 16.11.17. // #include #include #include #include #include #include #include "TokeManager.h" #include "src/VirtualServer.h" using namespace std; using namespace std::chrono; using namespace ts; using namespace ts::server; using namespace ts::token; TokenManager::TokenManager(server::VirtualServer* handle) : handle(handle) { } TokenManager::~TokenManager() {} bool TokenManager::loadTokens() { auto fn = LOG_SQL_CMD; fn(sql::command(handle->getSql(), "SELECT * FROM `tokens` WHERE `serverId` = :sid", variable{":sid", handle->getServerId()}).query(&TokenManager::loadTokenFromDb, this)); return true; } int TokenManager::loadTokenFromDb(int length, char **values, char **columns) { std::string token, description = ""; auto type = static_cast(0xFF); GroupId group = 0; ChannelId chId = 0; time_point created; for(int i = 0; i < length; i++){ if(strcmp(columns[i], "type") == 0) type = static_cast(stoi(values[i])); else if(strcmp(columns[i], "token") == 0) token = values[i]; else if(strcmp(columns[i], "targetGroup") == 0) group = static_cast(stoll(values[i])); else if(strcmp(columns[i], "targetChannel") == 0) chId = static_cast(stoll(values[i])); else if(strcmp(columns[i], "description") == 0) description = values[i]; else if(strcmp(columns[i], "created") == 0) created = time_point() + seconds(stoll(values[i])); else if(strcmp(columns[i], "serverId") == 0); else cerr << "Invalid column id `" << columns[i] << "`" << endl; } assert(!token.empty()); //assert(!description.empty()); assert(type != 0xFF); assert(group != 0); assert(created.time_since_epoch().count() != 0); this->tokens.push_back(make_shared(token, type, group, chId, system_clock::now(), description)); return 0; } std::shared_ptr TokenManager::createToken(TokenType type, GroupId group, std::string description, ChannelId chid, std::string token) { token = token.empty() ? rnd_string(20) : token; shared_ptr entry = make_shared(token, type, group, chid, system_clock::now(), description); this->tokens.push_back(entry); auto res = sql::command(handle->getSql(), "INSERT INTO `tokens` (`serverId`, `type`, `token`, `targetGroup`, `targetChannel`, `description`, `created`) VALUES (:sid, :type, :token, :targetGroup, :targetChannel, :desc, :created);", variable{":sid", this->handle->getServerId()}, variable{":type", entry->type}, variable{":token", entry->token}, variable{":targetGroup", entry->groupId}, variable{":targetChannel", entry->channelId}, variable{":desc", entry->description}, variable{":created", duration_cast(entry->created.time_since_epoch()).count()} ).execute(); auto pf = LOG_SQL_CMD; pf(res); return entry; } bool TokenManager::deleteToke(const std::string& str) { auto token = this->findToken(str); if(!token) return false; this->tokens.erase(std::find(this->tokens.begin(), this->tokens.end(), token)); auto res = sql::command(handle->getSql(), "DELETE FROM `tokens` WHERE `token` = :token", variable{":token", token->token}).execute(); auto pf = LOG_SQL_CMD; pf(res); return true; } std::shared_ptr TokenManager::findToken(std::string str) { for(auto& elm : this->tokens) if(elm->token == str) return elm; return nullptr; } TokenEntry::TokenEntry(const string &token, TokenType type, GroupId groupId, ChannelId channelId, const time_point &created, const string &description) : token(token), type(type), groupId(groupId), channelId(channelId), created(created), description(description) {}