From 4d6be361624741bc3745e191445f63ffa12bd976 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 13 Aug 2015 13:48:08 +0200 Subject: [PATCH] autoop: C++11 range-based for loops --- modules/autoop.cpp | 84 ++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 44 deletions(-) diff --git a/modules/autoop.cpp b/modules/autoop.cpp index 18421afc..88c978f2 100644 --- a/modules/autoop.cpp +++ b/modules/autoop.cpp @@ -63,8 +63,8 @@ public: const CString& GetUserKey() const { return m_sUserKey; } bool ChannelMatches(const CString& sChan) const { - for (set::const_iterator it = m_ssChans.begin(); it != m_ssChans.end(); ++it) { - if (sChan.AsLower().WildCmp(*it, CString::CaseInsensitive)) { + for (const CString& s : m_ssChans) { + if (sChan.AsLower().WildCmp(s, CString::CaseInsensitive)) { return true; } } @@ -73,8 +73,8 @@ public: } bool HostMatches(const CString& sHostmask) { - for (set::const_iterator it = m_ssHostmasks.begin(); it != m_ssHostmasks.end(); ++it) { - if (sHostmask.WildCmp(*it, CString::CaseInsensitive)) { + for (const CString& s : m_ssHostmasks) { + if (sHostmask.WildCmp(s, CString::CaseInsensitive)) { return true; } } @@ -93,8 +93,8 @@ public: VCString vsHostmasks; sHostmasks.Split(",", vsHostmasks); - for (unsigned int a = 0; a < vsHostmasks.size(); a++) { - m_ssHostmasks.erase(vsHostmasks[a]); + for (const CString& s : vsHostmasks) { + m_ssHostmasks.erase(s); } return m_ssHostmasks.empty(); @@ -104,8 +104,8 @@ public: VCString vsHostmasks; sHostmasks.Split(",", vsHostmasks); - for (unsigned int a = 0; a < vsHostmasks.size(); a++) { - m_ssHostmasks.insert(vsHostmasks[a]); + for (const CString& s : vsHostmasks) { + m_ssHostmasks.insert(s); } } @@ -113,8 +113,8 @@ public: VCString vsChans; sChans.Split(" ", vsChans); - for (unsigned int a = 0; a < vsChans.size(); a++) { - m_ssChans.erase(vsChans[a].AsLower()); + for (const CString& sChan : vsChans) { + m_ssChans.erase(sChan.AsLower()); } } @@ -122,8 +122,8 @@ public: VCString vsChans; sChans.Split(" ", vsChans); - for (unsigned int a = 0; a < vsChans.size(); a++) { - m_ssChans.insert(vsChans[a].AsLower()); + for (const CString& sChan : vsChans) { + m_ssChans.insert(sChan.AsLower()); } } @@ -179,8 +179,8 @@ public: } virtual ~CAutoOpMod() { - for (map::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) { - delete it->second; + for (const auto& it : m_msUsers) { + delete it.second; } m_msUsers.clear(); } @@ -230,9 +230,9 @@ public: if (Nick.GetNick() == GetNetwork()->GetIRCNick().GetNick()) { const map& msNicks = Channel.GetNicks(); - for (map::const_iterator it = msNicks.begin(); it != msNicks.end(); ++it) { - if (!it->second.HasPerm(CChan::Op)) { - CheckAutoOp(it->second, Channel); + for (const auto& it : msNicks) { + if (!it.second.HasPerm(CChan::Op)) { + CheckAutoOp(it.second, Channel); } } } @@ -288,15 +288,15 @@ public: Table.AddColumn("Key"); Table.AddColumn("Channels"); - for (map::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) { + for (const auto& it : m_msUsers) { VCString vsHostmasks; - it->second->GetHostmasks().Split(",", vsHostmasks); + it.second->GetHostmasks().Split(",", vsHostmasks); for (unsigned int a = 0; a < vsHostmasks.size(); a++) { Table.AddRow(); if (a == 0) { - Table.SetCell("User", it->second->GetUsername()); - Table.SetCell("Key", it->second->GetUserKey()); - Table.SetCell("Channels", it->second->GetChannels()); + Table.SetCell("User", it.second->GetUsername()); + Table.SetCell("Key", it.second->GetUserKey()); + Table.SetCell("Channels", it.second->GetChannels()); } else if (a == vsHostmasks.size()-1) { Table.SetCell("User", "`-"); } else { @@ -405,8 +405,8 @@ public: } CAutoOpUser* FindUserByHost(const CString& sHostmask, const CString& sChannel = "") { - for (map::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) { - CAutoOpUser* pUser = it->second; + for (const auto& it : m_msUsers) { + CAutoOpUser* pUser = it.second; if (pUser->HostMatches(sHostmask) && (sChannel.empty() || pUser->ChannelMatches(sChannel))) { return pUser; @@ -467,8 +467,8 @@ public: bool bMatchedHost = false; CAutoOpUser* pUser = nullptr; - for (map::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) { - pUser = it->second; + for (const auto& it : m_msUsers) { + pUser = it.second; // First verify that the person who challenged us matches a user's host if (pUser->HostMatches(Nick.GetHostMask())) { @@ -476,13 +476,11 @@ public: bMatchedHost = true; // Also verify that they are opped in at least one of the user's chans - for (size_t a = 0; a < Chans.size(); a++) { - const CChan& Chan = *Chans[a]; - - const CNick* pNick = Chan.FindNick(Nick.GetNick()); + for (CChan* pChan : Chans) { + const CNick* pNick = pChan->FindNick(Nick.GetNick()); if (pNick) { - if (pNick->HasPerm(CChan::Op) && pUser->ChannelMatches(Chan.GetName())) { + if (pNick->HasPerm(CChan::Op) && pUser->ChannelMatches(pChan->GetName())) { bValid = true; break; } @@ -526,10 +524,10 @@ public: CString sChallenge = itQueue->second; m_msQueue.erase(itQueue); - for (map::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) { - if (it->second->HostMatches(Nick.GetHostMask())) { - if (sResponse == CString(it->second->GetUserKey() + "::" + sChallenge).MD5()) { - OpUser(Nick, *it->second); + for (const auto& it : m_msUsers) { + if (it.second->HostMatches(Nick.GetHostMask())) { + if (sResponse == CString(it.second->GetUserKey() + "::" + sChallenge).MD5()) { + OpUser(Nick, *it.second); return true; } else { PutModule("WARNING! [" + Nick.GetHostMask() + "] sent a bad response. Please verify that you have their correct password."); @@ -560,23 +558,21 @@ public: } // Now issue challenges for the new users in the queue - for (MCString::iterator it = m_msQueue.begin(); it != m_msQueue.end(); ++it) { - it->second = CString::RandomString(AUTOOP_CHALLENGE_LENGTH); - PutIRC("NOTICE " + it->first + " :!ZNCAO CHALLENGE " + it->second); + for (auto& it : m_msQueue) { + it.second = CString::RandomString(AUTOOP_CHALLENGE_LENGTH); + PutIRC("NOTICE " + it.first + " :!ZNCAO CHALLENGE " + it.second); } } void OpUser(const CNick& Nick, const CAutoOpUser& User) { const vector& Chans = GetNetwork()->GetChans(); - for (size_t a = 0; a < Chans.size(); a++) { - const CChan& Chan = *Chans[a]; - - if (Chan.HasPerm(CChan::Op) && User.ChannelMatches(Chan.GetName())) { - const CNick* pNick = Chan.FindNick(Nick.GetNick()); + for (CChan* pChan : Chans) { + if (pChan->HasPerm(CChan::Op) && User.ChannelMatches(pChan->GetName())) { + const CNick* pNick = pChan->FindNick(Nick.GetNick()); if (pNick && !pNick->HasPerm(CChan::Op)) { - PutIRC("MODE " + Chan.GetName() + " +o " + Nick.GetNick()); + PutIRC("MODE " + pChan->GetName() + " +o " + Nick.GetNick()); } } }