diff --git a/User.cpp b/User.cpp index 061e4b80..53315c07 100644 --- a/User.cpp +++ b/User.cpp @@ -770,10 +770,42 @@ void CUser::JoinChans() { } } - for (set::iterator it = sChans.begin(); - it != sChans.end(); ++it) { - PutIRC("JOIN " + (*it)->GetName() + " " + (*it)->GetKey()); + while (!sChans.empty()) + JoinChans(sChans); +} + +void CUser::JoinChans(set& sChans) { + CString sKeys, sJoin; + bool bHaveKey = false; + size_t uiJoinLength = strlen("JOIN "); + + while (!sChans.empty()) { + set::iterator it = sChans.begin(); + const CString& sName = (*it)->GetName(); + const CString& sKey = (*it)->GetKey(); + size_t len = sName.length() + sKey.length(); + len += 2; // two comma + + if (!sKeys.empty() && uiJoinLength + len >= 512) + break; + + if (!sJoin.empty()) { + sJoin += ","; + sKeys += ","; + } + uiJoinLength += len; + sJoin += sName; + if (!sKey.empty()) { + sKeys += sKey; + bHaveKey = true; + } + sChans.erase(it); } + + if (bHaveKey) + PutIRC("JOIN " + sJoin + " " + sKeys); + else + PutIRC("JOIN " + sJoin); } bool CUser::JoinChan(CChan* pChan) { diff --git a/User.h b/User.h index c18e847a..1a0cc33e 100644 --- a/User.h +++ b/User.h @@ -224,6 +224,7 @@ public: // !Getters private: bool JoinChan(CChan* pChan); + void JoinChans(set& sChans); protected: CString m_sUserName;