From c7135970d2d7936169a42953bca9a91817849adf Mon Sep 17 00:00:00 2001 From: Maw-Fox Date: Wed, 23 Mar 2016 20:51:49 -0600 Subject: [PATCH 1/7] Lowercase checking of channel mo privileges. --- src/channel.cpp | 42 +++++++++++++++++++++++++++++++++++++----- src/channel.hpp | 2 ++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index 1ff5d63..cd332c2 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -207,31 +207,57 @@ bool Channel::getBan(string& name, BanRecord& ban) { void Channel::addMod(ConnectionPtr src, string& dest) { ModRecord mod; + string lowername; mod.modder = src->characterName; mod.time = time(0); moderators[dest] = mod; + lowername = dest; + for (int i = 0, len = lowername.length(); i < len; ++i) { + lowername[i] = (char)tolower(lowername[i]); + } + moderatorsMap[lowername] = dest; } void Channel::addMod(string& dest) { ModRecord mod; + string lowername; mod.modder = "[System]"; mod.time = time(0); moderators[dest] = mod; + lowername = dest; + for (int i = 0, len = lowername.length(); i < len; ++i) { + lowername[i] = (char)tolower(lowername[i]); + } + moderatorsMap[lowername] = dest; } void Channel::remMod(string& dest) { + string lowername; moderators.erase(dest); + lowername = dest; + for (int i = 0, len = lowername.length(); i < len; ++i) { + lowername[i] = (char)tolower(lowername[i]); + } + moderatorsMap.erase(dest); } bool Channel::isMod(ConnectionPtr con) { - if (con->globalModerator || con->admin || (owner == con->characterName) || moderators.find(con->characterName) != moderators.end()) + lowername = con->characterName; + for (int i = 0, len = lowername.length(); i < len; ++i) { + lowername[i] = (char)tolower(lowername[i]); + } + if (con->globalModerator || con->admin || (owner == con->characterName) || moderatorsMap.find(lowername) != moderatorsMap.end()) return true; return false; } bool Channel::isMod(string& name) { - if ((owner == name) || moderators.find(name) != moderators.end()) + lowername = name; + for (int i = 0, len = lowername.length(); i < len; ++i) { + lowername[i] = (char)tolower(lowername[i]); + } + if ((owner == name) || moderatorsMap.find(lowername) != moderatorsMap.end()) return true; return false; @@ -509,10 +535,16 @@ void Channel::loadChannel(const json_t* channode) { { json_t* namenode = json_object_get(mod, "name"); if (namenode) { - const char* namestring = json_string_value(namenode); - if (namestring) { - moderators[namestring] = m; + const char* namestring = json_string_value(namenode); + string namestringLower; + if (namestring) { + string lowername = namestring; + for (int i = 0, len = lowername.length(); i < len; ++i) { + lowername[i] = (char)tolower(lowername[i]); } + moderatorsMap[lowername] = namestring; + moderators[namestring] = m; + } } else { LOG(WARNING) << "Mod json for channel " << name << " contains no name item."; continue; diff --git a/src/channel.hpp b/src/channel.hpp index 0faa3b7..03e71a7 100644 --- a/src/channel.hpp +++ b/src/channel.hpp @@ -70,6 +70,7 @@ typedef list chconlist_t; typedef unordered_set chstringset_t; typedef unordered_map chbanmap_t; typedef unordered_map chmodmap_t; +typedef unordered_map chmodnamemap_t; typedef unordered_map chtimermap_t; class Channel : public LBase { @@ -205,6 +206,7 @@ class Channel : public LBase { ChannelMessageMode chatMode; chconlist_t participants; chmodmap_t moderators; + chmodnamemap_t moderatorsMap; // lower -> regular string owner; chbanmap_t bans; int participantCount; From c2098b6285d04f7777ca7806fb39dd7160f1dbf6 Mon Sep 17 00:00:00 2001 From: Maw-Fox Date: Wed, 23 Mar 2016 21:01:55 -0600 Subject: [PATCH 2/7] Lowercase checking of channel mo privileges. --- src/channel.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index cd332c2..fca7ba3 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -242,7 +242,7 @@ void Channel::remMod(string& dest) { } bool Channel::isMod(ConnectionPtr con) { - lowername = con->characterName; + string lowername = con->characterName; for (int i = 0, len = lowername.length(); i < len; ++i) { lowername[i] = (char)tolower(lowername[i]); } @@ -253,7 +253,7 @@ bool Channel::isMod(ConnectionPtr con) { } bool Channel::isMod(string& name) { - lowername = name; + string lowername = name; for (int i = 0, len = lowername.length(); i < len; ++i) { lowername[i] = (char)tolower(lowername[i]); } From 4fde4c576550ca1d4821cb50c8fd08d152e2b584 Mon Sep 17 00:00:00 2001 From: Maw-Fox Date: Wed, 23 Mar 2016 21:06:11 -0600 Subject: [PATCH 3/7] Lowercase checking of channel mo privileges. --- src/channel.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/channel.cpp b/src/channel.cpp index fca7ba3..7121247 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -536,7 +536,6 @@ void Channel::loadChannel(const json_t* channode) { json_t* namenode = json_object_get(mod, "name"); if (namenode) { const char* namestring = json_string_value(namenode); - string namestringLower; if (namestring) { string lowername = namestring; for (int i = 0, len = lowername.length(); i < len; ++i) { From 1cdb707b8500034e63b3d10c6921cfc11688a89f Mon Sep 17 00:00:00 2001 From: Maw-Fox Date: Thu, 24 Mar 2016 18:16:48 -0600 Subject: [PATCH 4/7] Minor refactoring and addition of utils. --- src/channel.cpp | 82 +++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 33 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index 7121247..b2549f4 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -71,6 +71,49 @@ Channel::~Channel() { assert(participants.size() == 0); } +// Minor utils defined here until we have a utility function file. + +/** +* Change a string's pointer value to a lowercased version. +* @param string& str string pointer reference +* @returns void +*/ +void setLower(string& str) { + int intLength; + const size_t length = str.length(); + + if (length > INT_MAX) { + throw overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. + } + + intLength = static_cast(length); + + for (int i = 0; i < intLength; ++i) { + str[i] = (char)tolower(str[i]); + } +} + +/** +* Returns a copy of the string in lowercase. +* @param string str string +* @returns string +*/ +string getLower(string str) { + int intLength; + const size_t length = str.length(); + + if (length > INT_MAX) { + throw overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. + } + + intLength = static_cast(length); + + for (int i = 0; i < intLength; ++i) { + str[i] = (char)tolower(str[i]); + } + return str; +} + void Channel::sendToAll(string& message) { MessagePtr outMessage(MessageBuffer::fromString(message)); for (chconlist_t::iterator i = participants.begin(); i != participants.end(); ++i) { @@ -207,57 +250,34 @@ bool Channel::getBan(string& name, BanRecord& ban) { void Channel::addMod(ConnectionPtr src, string& dest) { ModRecord mod; - string lowername; mod.modder = src->characterName; mod.time = time(0); moderators[dest] = mod; - lowername = dest; - for (int i = 0, len = lowername.length(); i < len; ++i) { - lowername[i] = (char)tolower(lowername[i]); - } - moderatorsMap[lowername] = dest; + moderatorsMap[getLower(dest)] = dest; } void Channel::addMod(string& dest) { ModRecord mod; - string lowername; mod.modder = "[System]"; mod.time = time(0); moderators[dest] = mod; - lowername = dest; - for (int i = 0, len = lowername.length(); i < len; ++i) { - lowername[i] = (char)tolower(lowername[i]); - } - moderatorsMap[lowername] = dest; + moderatorsMap[getLower(lowername)] = dest; } void Channel::remMod(string& dest) { - string lowername; moderators.erase(dest); - lowername = dest; - for (int i = 0, len = lowername.length(); i < len; ++i) { - lowername[i] = (char)tolower(lowername[i]); - } - moderatorsMap.erase(dest); + moderatorsMap.erase(getLower(dest)); } bool Channel::isMod(ConnectionPtr con) { - string lowername = con->characterName; - for (int i = 0, len = lowername.length(); i < len; ++i) { - lowername[i] = (char)tolower(lowername[i]); - } - if (con->globalModerator || con->admin || (owner == con->characterName) || moderatorsMap.find(lowername) != moderatorsMap.end()) + if (con->globalModerator || con->admin || (owner == con->characterName) || moderatorsMap.find(con->characterNameLower) != moderatorsMap.end()) return true; return false; } bool Channel::isMod(string& name) { - string lowername = name; - for (int i = 0, len = lowername.length(); i < len; ++i) { - lowername[i] = (char)tolower(lowername[i]); - } - if ((owner == name) || moderatorsMap.find(lowername) != moderatorsMap.end()) + if ((owner == name) || moderatorsMap.find(getLower(name)) != moderatorsMap.end()) return true; return false; @@ -537,11 +557,7 @@ void Channel::loadChannel(const json_t* channode) { if (namenode) { const char* namestring = json_string_value(namenode); if (namestring) { - string lowername = namestring; - for (int i = 0, len = lowername.length(); i < len; ++i) { - lowername[i] = (char)tolower(lowername[i]); - } - moderatorsMap[lowername] = namestring; + moderatorsMap[getLower(namestring)] = namestring; moderators[namestring] = m; } } else { From 73874052f8afcf119e7841b65b8cb351d3f2000b Mon Sep 17 00:00:00 2001 From: Maw-Fox Date: Thu, 24 Mar 2016 18:24:37 -0600 Subject: [PATCH 5/7] Fix for compile. --- src/channel.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index b2549f4..a242233 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -83,7 +83,7 @@ void setLower(string& str) { const size_t length = str.length(); if (length > INT_MAX) { - throw overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. + throw std::overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. } intLength = static_cast(length); @@ -103,7 +103,7 @@ string getLower(string str) { const size_t length = str.length(); if (length > INT_MAX) { - throw overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. + throw std::overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. } intLength = static_cast(length); @@ -261,7 +261,7 @@ void Channel::addMod(string& dest) { mod.modder = "[System]"; mod.time = time(0); moderators[dest] = mod; - moderatorsMap[getLower(lowername)] = dest; + moderatorsMap[getLower(dest)] = dest; } void Channel::remMod(string& dest) { From 5976483f401dfe69d8925119bc20716372e242bf Mon Sep 17 00:00:00 2001 From: Maw-Fox Date: Thu, 24 Mar 2016 18:29:32 -0600 Subject: [PATCH 6/7] Fuck it, use log(warning)! --- src/channel.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index a242233..737d6e7 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -83,7 +83,7 @@ void setLower(string& str) { const size_t length = str.length(); if (length > INT_MAX) { - throw std::overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. + LOG(WARNING) << "String size_t const exceeds maxium signed integer size. Overflow immenent."; // For the rare, rare case that this may occur. } intLength = static_cast(length); @@ -103,7 +103,7 @@ string getLower(string str) { const size_t length = str.length(); if (length > INT_MAX) { - throw std::overflow_error("string exceeds maximum signed int length."); // For the rare, rare case that this may occur. + LOG(WARNING) << "String size_t const exceeds maxium signed integer size. Overflow immenent."; } intLength = static_cast(length); From 72347d67bd14508b9e9d0f0c7cf6e8fec3a6e6ec Mon Sep 17 00:00:00 2001 From: Maw-Fox Date: Thu, 24 Mar 2016 18:34:10 -0600 Subject: [PATCH 7/7] I can't spell. --- src/channel.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index 737d6e7..ae17efa 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -83,7 +83,7 @@ void setLower(string& str) { const size_t length = str.length(); if (length > INT_MAX) { - LOG(WARNING) << "String size_t const exceeds maxium signed integer size. Overflow immenent."; // For the rare, rare case that this may occur. + LOG(WARNING) << "String size_t const exceeds maximum signed integer size. Overflow immenent."; // For the rare, rare case that this may occur. } intLength = static_cast(length); @@ -103,7 +103,7 @@ string getLower(string str) { const size_t length = str.length(); if (length > INT_MAX) { - LOG(WARNING) << "String size_t const exceeds maxium signed integer size. Overflow immenent."; + LOG(WARNING) << "String size_t const exceeds maximum signed integer size. Overflow immenent."; } intLength = static_cast(length);