From 1d19bac8ad46601a9833afd6b93e5af54487063b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Sat, 11 Oct 2014 17:36:13 +0200 Subject: [PATCH] Refactor chansaver (#674) CChan now keeps track of the state. The module no longer needs to rely on incoming messages, but can just mark whether each channel should be kept in config or not. Cleanup things and use C++11 for loops while we're at it... --- modules/chansaver.cpp | 66 +++++++++++-------------------------------- 1 file changed, 16 insertions(+), 50 deletions(-) diff --git a/modules/chansaver.cpp b/modules/chansaver.cpp index ffd03f24..d0975aef 100644 --- a/modules/chansaver.cpp +++ b/modules/chansaver.cpp @@ -18,22 +18,18 @@ #include #include -using std::vector; - class CChanSaverMod : public CModule { public: MODCONSTRUCTOR(CChanSaverMod) { - m_bWriteConf = false; - switch (GetType()) { case CModInfo::GlobalModule: LoadUsers(); break; case CModInfo::UserModule: - LoadUser(*GetUser()); + LoadUser(GetUser()); break; case CModInfo::NetworkModule: - LoadNetwork(*GetNetwork()); + LoadNetwork(GetNetwork()); break; } } @@ -42,71 +38,41 @@ public: } void LoadUsers() { - std::map vUsers = CZNC::Get().GetUserMap(); - for (std::map::iterator it = vUsers.begin(); it != vUsers.end(); ++it) { - LoadUser(*it->second); + const std::map& vUsers = CZNC::Get().GetUserMap(); + for (const auto& user : vUsers) { + LoadUser(user.second); } } - void LoadUser(CUser &user) { - vector vNetworks = user.GetNetworks(); - for (vector::iterator it = vNetworks.begin(); it != vNetworks.end(); ++it) { - CIRCNetwork &network = **it; - LoadNetwork(network); + void LoadUser(CUser* pUser) { + const std::vector& vNetworks = pUser->GetNetworks(); + for (const CIRCNetwork* pNetwork : vNetworks) { + LoadNetwork(pNetwork); } } - void LoadNetwork(CIRCNetwork &network) { - const vector& vChans = network.GetChans(); - - for (vector::const_iterator it = vChans.begin(); it != vChans.end(); ++it) { - CChan &chan = **it; - + void LoadNetwork(const CIRCNetwork* pNetwork) { + const std::vector& vChans = pNetwork->GetChans(); + for (CChan* pChan : vChans) { // If that channel isn't yet in the config, // we'll have to add it... - if (!chan.InConfig()) { - chan.SetInConfig(true); - m_bWriteConf = true; + if (!pChan->InConfig()) { + pChan->SetInConfig(true); } } } - virtual EModRet OnRaw(CString& sLine) { - if (m_bWriteConf) { - CZNC::Get().WriteConfig(); - m_bWriteConf = false; - } - - return CONTINUE; - } - - virtual void OnMode2(const CNick* pOpNick, CChan& Channel, char uMode, const CString& sArg, bool bAdded, bool bNoChange) { - // This is called when we join (ZNC requests the channel modes - // on join) *and* when someone changes the channel keys. - // We ignore channel key "*" because of some broken nets. - if (uMode != 'k' || bNoChange || !bAdded || sArg == "*") - return; - - Channel.SetKey(sArg); - m_bWriteConf = true; - } - virtual void OnJoin(const CNick& Nick, CChan& Channel) { - if (Nick.GetNick() == GetNetwork()->GetIRCNick().GetNick() && !Channel.InConfig()) { + if (!Channel.InConfig() && GetNetwork()->GetIRCNick().NickEquals(Nick.GetNick())) { Channel.SetInConfig(true); - CZNC::Get().WriteConfig(); } } virtual void OnPart(const CNick& Nick, CChan& Channel, const CString& sMessage) { - if (Nick.GetNick() == GetNetwork()->GetIRCNick().GetNick() && Channel.InConfig()) { + if (Channel.InConfig() && GetNetwork()->GetIRCNick().NickEquals(Nick.GetNick())) { Channel.SetInConfig(false); - CZNC::Get().WriteConfig(); } } - -private: - bool m_bWriteConf; }; template<> void TModInfo(CModInfo& Info) {