1 /*
2  * Copyright (C) 2004-2020 ZNC, see the NOTICE file for details.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <znc/Chan.h>
18 #include <znc/User.h>
19 #include <znc/IRCNetwork.h>
20 
21 class CChanSaverMod : public CModule {
22   public:
MODCONSTRUCTOR(CChanSaverMod)23     MODCONSTRUCTOR(CChanSaverMod) {}
24 
~CChanSaverMod()25     ~CChanSaverMod() override {}
26 
OnLoad(const CString & sArgsi,CString & sMessage)27     bool OnLoad(const CString& sArgsi, CString& sMessage) override {
28         switch (GetType()) {
29             case CModInfo::GlobalModule:
30                 LoadUsers();
31                 break;
32             case CModInfo::UserModule:
33                 LoadUser(GetUser());
34                 break;
35             case CModInfo::NetworkModule:
36                 LoadNetwork(GetNetwork());
37                 break;
38         }
39         return true;
40     }
41 
LoadUsers()42     void LoadUsers() {
43         const std::map<CString, CUser*>& vUsers = CZNC::Get().GetUserMap();
44         for (const auto& user : vUsers) {
45             LoadUser(user.second);
46         }
47     }
48 
LoadUser(CUser * pUser)49     void LoadUser(CUser* pUser) {
50         const std::vector<CIRCNetwork*>& vNetworks = pUser->GetNetworks();
51         for (const CIRCNetwork* pNetwork : vNetworks) {
52             LoadNetwork(pNetwork);
53         }
54     }
55 
LoadNetwork(const CIRCNetwork * pNetwork)56     void LoadNetwork(const CIRCNetwork* pNetwork) {
57         const std::vector<CChan*>& vChans = pNetwork->GetChans();
58         for (CChan* pChan : vChans) {
59             // If that channel isn't yet in the config,
60             // we'll have to add it...
61             if (!pChan->InConfig()) {
62                 pChan->SetInConfig(true);
63             }
64         }
65     }
66 
OnJoin(const CNick & Nick,CChan & Channel)67     void OnJoin(const CNick& Nick, CChan& Channel) override {
68         if (!Channel.InConfig() &&
69             GetNetwork()->GetIRCNick().NickEquals(Nick.GetNick())) {
70             Channel.SetInConfig(true);
71         }
72     }
73 
OnPart(const CNick & Nick,CChan & Channel,const CString & sMessage)74     void OnPart(const CNick& Nick, CChan& Channel,
75                 const CString& sMessage) override {
76         if (Channel.InConfig() &&
77             GetNetwork()->GetIRCNick().NickEquals(Nick.GetNick())) {
78             Channel.SetInConfig(false);
79         }
80     }
81 };
82 
83 template <>
TModInfo(CModInfo & Info)84 void TModInfo<CChanSaverMod>(CModInfo& Info) {
85     Info.SetWikiPage("chansaver");
86     Info.AddType(CModInfo::NetworkModule);
87     Info.AddType(CModInfo::GlobalModule);
88 }
89 
90 USERMODULEDEFS(CChanSaverMod,
91                t_s("Keeps config up-to-date when user joins/parts."))
92