2019-07-17 19:37:18 +02:00
//
// Created by wolverindev on 16.11.17.
//
# include <algorithm>
# include <sqlite3.h>
# include <iostream>
# include <random>
# include <misc/rnd.h>
# include <log/LogUtils.h>
# include "TokeManager.h"
2020-01-26 18:04:38 +01:00
# include "src/VirtualServer.h"
2019-07-17 19:37:18 +02:00
using namespace std ;
using namespace std : : chrono ;
using namespace ts ;
using namespace ts : : server ;
using namespace ts : : token ;
2020-01-26 18:04:38 +01:00
TokenManager : : TokenManager ( server : : VirtualServer * handle ) : handle ( handle ) {
2019-07-17 19:37:18 +02:00
}
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 < TokenType > ( 0xFF ) ;
GroupId group = 0 ;
ChannelId chId = 0 ;
time_point < system_clock > created ;
for ( int i = 0 ; i < length ; i + + ) {
if ( strcmp ( columns [ i ] , " type " ) = = 0 )
type = static_cast < TokenType > ( stoi ( values [ i ] ) ) ;
else if ( strcmp ( columns [ i ] , " token " ) = = 0 )
token = values [ i ] ;
else if ( strcmp ( columns [ i ] , " targetGroup " ) = = 0 )
group = static_cast < GroupId > ( stoll ( values [ i ] ) ) ;
else if ( strcmp ( columns [ i ] , " targetChannel " ) = = 0 )
chId = static_cast < ChannelId > ( stoll ( values [ i ] ) ) ;
else if ( strcmp ( columns [ i ] , " description " ) = = 0 )
description = values [ i ] ;
else if ( strcmp ( columns [ i ] , " created " ) = = 0 )
created = time_point < system_clock > ( ) + 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 < TokenEntry > ( token , type , group , chId , system_clock : : now ( ) , description ) ) ;
return 0 ;
}
std : : shared_ptr < TokenEntry > TokenManager : : createToken ( TokenType type , GroupId group , std : : string description , ChannelId chid , std : : string token ) {
token = token . empty ( ) ? rnd_string ( 20 ) : token ;
shared_ptr < TokenEntry > entry = make_shared < TokenEntry > ( 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 < seconds > ( 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 < TokenEntry > 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 < system_clock > & created , const string & description ) : token ( token ) , type ( type ) , groupId ( groupId ) , channelId ( channelId ) ,
created ( created ) , description ( description ) { }