Some code refactoring

This commit is contained in:
WolverinDEV 2021-04-14 14:57:03 +02:00
parent 8dde5b1c23
commit 8fb93d1d9d
8 changed files with 75 additions and 24 deletions

View File

@ -16,7 +16,6 @@ find_package(DataPipes REQUIRED)
include_directories(${DataPipes_INCLUDE_DIR}) include_directories(${DataPipes_INCLUDE_DIR})
find_package(Ed25519 REQUIRED) find_package(Ed25519 REQUIRED)
include_directories(${ed25519_INCLUDE_DIR})
set(TARGET_LIBRARIES) set(TARGET_LIBRARIES)
set(FEATURE_LOGGING ON) set(FEATURE_LOGGING ON)
@ -236,6 +235,7 @@ add_library(TeaSpeak STATIC ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(TeaSpeak PUBLIC target_link_libraries(TeaSpeak PUBLIC
tomcrypt::static tomcrypt::static
tommath::static tommath::static
ed25519::static
${OPENSSL_LIBRARIES} ${OPENSSL_LIBRARIES}
${TARGET_LIBRARIES} ${TARGET_LIBRARIES}
dl dl

View File

@ -125,15 +125,26 @@ void BasicChannel::setChannelType(ChannelType::ChannelType type) {
properties()[property::CHANNEL_FLAG_SEMI_PERMANENT] = type == ChannelType::semipermanent; properties()[property::CHANNEL_FLAG_SEMI_PERMANENT] = type == ChannelType::semipermanent;
} }
bool BasicChannel::passwordMatch(std::string password, bool hashed) { bool BasicChannel::verify_password(const std::optional<std::string> &password, bool password_hashed) {
if (!this->properties()[property::CHANNEL_FLAG_PASSWORD].as_unchecked<bool>() || this->properties()[property::CHANNEL_PASSWORD].value().empty()) return true; if(!this->properties()[property::CHANNEL_FLAG_PASSWORD].as_unchecked<bool>()) {
if (password.empty()) return false;
if(this->properties()[property::CHANNEL_PASSWORD].as_unchecked<string>() == password)
return true; return true;
}
password = base64::encode(digest::sha1(password)); auto channel_password = this->properties()[property::CHANNEL_PASSWORD].value();
return this->properties()[property::CHANNEL_PASSWORD].as_unchecked<string>() == password; if(channel_password.empty()) {
return true;
}
if(!password.has_value()) {
return false;
}
if(password_hashed) {
return *password == channel_password;
}
/* We might have supplied the raw password */
return base64::encode(digest::sha1(*password)) == channel_password;
} }
int64_t BasicChannel::emptySince() { int64_t BasicChannel::emptySince() {

View File

@ -52,7 +52,8 @@ namespace ts {
ChannelType::ChannelType channelType(); ChannelType::ChannelType channelType();
void setChannelType(ChannelType::ChannelType); void setChannelType(ChannelType::ChannelType);
bool passwordMatch(std::string password, bool hashed = false); [[nodiscard]] bool verify_password(const std::optional<std::string>&, bool /* password already hashed */);
bool defaultChannel() { return (*this->_properties)[property::CHANNEL_FLAG_DEFAULT]; } bool defaultChannel() { return (*this->_properties)[property::CHANNEL_FLAG_DEFAULT]; }
int64_t emptySince(); int64_t emptySince();

View File

@ -825,6 +825,13 @@ namespace ts {
[[nodiscard]] constexpr bool has_power() const { return this->has_value && (this->value > 0 || this->value == -1); } [[nodiscard]] constexpr bool has_power() const { return this->has_value && (this->value > 0 || this->value == -1); }
[[nodiscard]] constexpr bool has_infinite_power() const { return this->has_value && this->value == -1; } [[nodiscard]] constexpr bool has_infinite_power() const { return this->has_value && this->value == -1; }
constexpr bool clear_flag_on_zero() {
if(this->has_value && this->value == 0) {
this->has_value = false;
return true;
}
return false;
}
inline bool operator==(const PermissionFlaggedValue& other) const { return other.value == this->value && other.has_value == this->has_value; } inline bool operator==(const PermissionFlaggedValue& other) const { return other.value == this->value && other.has_value == this->has_value; }
inline bool operator!=(const PermissionFlaggedValue& other) const { return !(*this == other); } inline bool operator!=(const PermissionFlaggedValue& other) const { return !(*this == other); }
@ -832,9 +839,10 @@ namespace ts {
static constexpr PermissionFlaggedValue empty_permission_flagged_value{0, false}; static constexpr PermissionFlaggedValue empty_permission_flagged_value{0, false};
static constexpr bool permission_granted(const PermissionFlaggedValue& required, const PermissionFlaggedValue& given, bool requires_given = true) { static constexpr bool permission_granted(const PermissionFlaggedValue& required, const PermissionFlaggedValue& given) {
if(!required.has_value) { if(!required.has_value) {
return !requires_given || given.has_power(); /* The target permission hasn't been set so just check if we've not negated the target */
return !given.has_value || given.value >= 0;
} else if(!given.has_power()) { } else if(!given.has_power()) {
return false; return false;
} else if(given.has_infinite_power()) { } else if(given.has_infinite_power()) {
@ -845,8 +853,8 @@ namespace ts {
return given.value >= required.value; return given.value >= required.value;
} }
} }
static constexpr bool permission_granted(const PermissionValue& required, const PermissionFlaggedValue& given, bool requires_given = true) { static constexpr bool permission_granted(const PermissionValue& required, const PermissionFlaggedValue& given) {
return permission_granted({required, true}, given, requires_given); return permission_granted({required, true}, given);
} }
class PermissionManager { class PermissionManager {

View File

@ -16,21 +16,26 @@ namespace logger {
#ifdef HAVE_CXX_TERMINAL #ifdef HAVE_CXX_TERMINAL
if (terminal::active()) { if (terminal::active()) {
//Split the string at new lines //Split the string at new lines
size_t index{0}, found{0}; size_t index{0}, found;
do { do {
found = message.find('\n', index); found = message.find('\n', index);
const auto length = (found == -1 ? message.length() : found) - index; const auto length = (found == -1 ? message.length() : found) - index;
const auto line = message.substr(index, length); const auto line = message.substr(index, length);
index = found; index = found;
if(length == 0) continue; if(length == 0) {
continue;
}
terminal::instance()->writeMessage(std::string{line}); terminal::instance()->writeMessage(std::to_string(length) + "_" + std::to_string(index) + ": " + std::string{line});
} while(++index); } while(++index);
} else } else {
#endif
cout << message << std::flush; cout << message << std::flush;
} }
#else
cout << message << std::flush;
#endif
}
void TerminalSink::flush_() { void TerminalSink::flush_() {
#ifdef HAVE_CXX_TERMINAL #ifdef HAVE_CXX_TERMINAL

View File

@ -1,6 +1,5 @@
#include "LogUtils.h" #include "LogUtils.h"
#include "LogSinks.h" #include "LogSinks.h"
#include <iomanip>
#include <fstream> #include <fstream>
#include <map> #include <map>
#ifdef WIN32 #ifdef WIN32

View File

@ -15,4 +15,5 @@
__FILE__, __LINE__, #exp); \ __FILE__, __LINE__, #exp); \
} \ } \
} while(0) } while(0)
#undef S
#endif #endif

View File

@ -96,19 +96,36 @@ operator type() {
ValueList(const ValueList& ref) : key(ref.key), bulkList(ref.bulkList) {} ValueList(const ValueList& ref) : key(ref.key), bulkList(ref.bulkList) {}
variable operator[](int index){ variable operator[](int index){
while(index >= bulkList.size()) bulkList.push_back(ParameterBulk()); while(index >= bulkList.size()) {
bulkList.push_back(ParameterBulk{});
}
return bulkList[index][key]; return bulkList[index][key];
} }
variable first() const { variable first() const {
int index = 0; auto value = this->optional_first();
while(index < bulkList.size() && !bulkList[index].has(key)) index++; if(value.has_value()) {
if(index < bulkList.size()) return bulkList[index][key]; return std::move(*value);
}
return variable{this->key, "", VariableType::VARTYPE_NULL}; return variable{this->key, "", VariableType::VARTYPE_NULL};
throw std::invalid_argument("could not find key [" + key + "]"); throw std::invalid_argument("could not find key [" + key + "]");
} }
std::optional<variable> optional_first() const {
int index = 0;
while(index < bulkList.size() && !bulkList[index].has(key)) {
index++;
}
if(index < bulkList.size()) {
return std::make_optional(bulkList[index][key]);
}
return std::nullopt;
}
size_t size(){ size_t size(){
size_t count = 0; size_t count = 0;
for(const auto& blk : this->bulkList) for(const auto& blk : this->bulkList)
@ -134,6 +151,15 @@ operator type() {
std::string string() const { return as<std::string>(); } std::string string() const { return as<std::string>(); }
std::optional<std::string> optional_string() const {
auto value = this->optional_first();
if(value.has_value()) {
return std::make_optional(value->string());
}
return std::nullopt;
}
friend std::ostream& operator<<(std::ostream&, const ValueList&); friend std::ostream& operator<<(std::ostream&, const ValueList&);
private: private:
ValueList(std::string key, std::deque<ParameterBulk>& bulkList) : key{std::move(key)}, bulkList(bulkList) {} ValueList(std::string key, std::deque<ParameterBulk>& bulkList) : key{std::move(key)}, bulkList(bulkList) {}