Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions src/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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<int>(length);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to cast to int? For loops work fine with size_t

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Size_t conversions to int produce compiler warnings.

"warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://sprunge.us/iaNL?diff
This compiles 100% fine, no compile warnings. You'll get a warning if you define i as an int instead of size_t, but it's perfectly valid to use size_t.


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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-should- be const qualified, if you'd done this then you'd've noticed that you're not actually returning a copy! - wait, I'm wrong here- this'll take by-copy, nevermind! Been doing too much rust lately.
also same as above re: int/size_t.

int intLength;
const size_t length = str.length();

if (length > INT_MAX) {
LOG(WARNING) << "String size_t const exceeds maximum signed integer size. Overflow immenent.";
}

intLength = static_cast<int>(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) {
Expand Down Expand Up @@ -210,28 +253,31 @@ void Channel::addMod(ConnectionPtr src, string& dest) {
mod.modder = src->characterName;
mod.time = time(0);
moderators[dest] = mod;
moderatorsMap[getLower(dest)] = dest;
}

void Channel::addMod(string& dest) {
ModRecord mod;
mod.modder = "[System]";
mod.time = time(0);
moderators[dest] = mod;
moderatorsMap[getLower(dest)] = dest;
}

void Channel::remMod(string& dest) {
moderators.erase(dest);
moderatorsMap.erase(getLower(dest));
}

bool Channel::isMod(ConnectionPtr con) {
if (con->globalModerator || con->admin || (owner == con->characterName) || moderators.find(con->characterName) != moderators.end())
if (con->globalModerator || con->admin || (owner == con->characterName) || moderatorsMap.find(con->characterNameLower) != moderatorsMap.end())
return true;

return false;
}

bool Channel::isMod(string& name) {
if ((owner == name) || moderators.find(name) != moderators.end())
if ((owner == name) || moderatorsMap.find(getLower(name)) != moderatorsMap.end())
return true;

return false;
Expand Down Expand Up @@ -509,10 +555,11 @@ 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);
if (namestring) {
moderatorsMap[getLower(namestring)] = namestring;
moderators[namestring] = m;
}
} else {
LOG(WARNING) << "Mod json for channel " << name << " contains no name item.";
continue;
Expand Down
2 changes: 2 additions & 0 deletions src/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ typedef list<ConnectionPtr> chconlist_t;
typedef unordered_set<string> chstringset_t;
typedef unordered_map<string, BanRecord> chbanmap_t;
typedef unordered_map<string, ModRecord> chmodmap_t;
typedef unordered_map<string, string> chmodnamemap_t;
typedef unordered_map<string, double> chtimermap_t;

class Channel : public LBase {
Expand Down Expand Up @@ -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;
Expand Down