diff options
author | Attila Molnar <attilamolnar@hush.com> | 2014-03-13 15:37:19 +0100 |
---|---|---|
committer | Attila Molnar <attilamolnar@hush.com> | 2014-03-13 15:37:19 +0100 |
commit | b18319769864542912bec9646986ef229a884bde (patch) | |
tree | ab69978f33e2bf395b4832e3196188a6f9c6abeb /src/channels.cpp | |
parent | 04779a0a0d82ab79036ca3d8e4ee0a4f21955baf (diff) |
Backport "Refactor Channel::UserList() to use std::string"
This gets rid of questionable snprintf() usage, reported by @hifi
Backported commit: 8a06d54076551387f83a29360478ee6605e241b6
Diffstat (limited to 'src/channels.cpp')
-rw-r--r-- | src/channels.cpp | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/src/channels.cpp b/src/channels.cpp index c546e68db..73d408fb8 100644 --- a/src/channels.cpp +++ b/src/channels.cpp @@ -758,9 +758,6 @@ char* Channel::ChanModes(bool showkey) */ void Channel::UserList(User *user) { - char list[MAXBUF]; - size_t dlen, curlen; - if (!IS_LOCAL(user)) return; @@ -772,17 +769,23 @@ void Channel::UserList(User *user) return; } - dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str()); + std::string list = user->nick; + list.push_back(' '); + list.push_back(this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '='); + list.push_back(' '); + list.append(this->name).append(" :"); + std::string::size_type pos = list.size(); - int numusers = 0; - char* ptr = list + dlen; + bool has_one = false; /* Improvement by Brain - this doesnt change in value, so why was it inside * the loop? */ bool has_user = this->HasUser(user); - for (UserMembIter i = userlist.begin(); i != userlist.end(); i++) + std::string prefixlist; + std::string nick; + for (UserMembIter i = userlist.begin(); i != userlist.end(); ++i) { if (i->first->quitting) continue; @@ -795,8 +798,8 @@ void Channel::UserList(User *user) continue; } - std::string prefixlist = this->GetPrefixChar(i->first); - std::string nick = i->first->nick; + prefixlist = this->GetPrefixChar(i->first); + nick = i->first->nick; FOREACH_MOD(I_OnNamesListItem, OnNamesListItem(user, i->second, prefixlist, nick)); @@ -804,32 +807,25 @@ void Channel::UserList(User *user) if (nick.empty()) continue; - size_t ptrlen = 0; - - if (curlen + prefixlist.length() + nick.length() + 1 > 480) + if (list.size() + prefixlist.length() + nick.length() + 1 > 480) { /* list overflowed into multiple numerics */ - user->WriteNumeric(RPL_NAMREPLY, std::string(list)); + user->WriteNumeric(RPL_NAMREPLY, list); - /* reset our lengths */ - dlen = curlen = snprintf(list,MAXBUF,"%s %c %s :", user->nick.c_str(), this->IsModeSet('s') ? '@' : this->IsModeSet('p') ? '*' : '=', this->name.c_str()); - ptr = list + dlen; - - numusers = 0; + // Erase all nicks, keep the constant part + list.erase(pos); + has_one = false; } - ptrlen = snprintf(ptr, MAXBUF, "%s%s ", prefixlist.c_str(), nick.c_str()); - - curlen += ptrlen; - ptr += ptrlen; + list.append(prefixlist).append(nick).push_back(' '); - numusers++; + has_one = true; } /* if whats left in the list isnt empty, send it */ - if (numusers) + if (has_one) { - user->WriteNumeric(RPL_NAMREPLY, std::string(list)); + user->WriteNumeric(RPL_NAMREPLY, list); } user->WriteNumeric(RPL_ENDOFNAMES, "%s %s :End of /NAMES list.", user->nick.c_str(), this->name.c_str()); |