~vanhoof/+junk/znc

« back to all changes in this revision

Viewing changes to modules/webadmin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2011-02-06 17:41:38 UTC
  • mfrom: (21.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110206174138-ush4l5mkr4wg738n
Tags: 0.096-2
* Merge 0.092-3~bpo50+1 changelog.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
using std::stringstream;
22
22
using std::make_pair;
23
23
 
 
24
/* Stuff to be able to write this:
 
25
   // i will be name of local variable, see below
 
26
   // pUser can be NULL if only global modules are needed
 
27
   FOR_EACH_MODULE(i, pUser) {
 
28
       // i is local variable of type CModules::iterator,
 
29
           // so *i has type CModule*
 
30
   }
 
31
*/
 
32
struct FOR_EACH_MODULE_Type {
 
33
        bool bOnCMuser;
 
34
        CModules CMtemp;
 
35
        CModules& CMuser;
 
36
        FOR_EACH_MODULE_Type(CUser* pUser) : CMuser(pUser ? pUser->GetModules() : CMtemp) {
 
37
                bOnCMuser = false;
 
38
        }
 
39
        operator bool() { return false; }
 
40
};
 
41
 
 
42
inline bool FOR_EACH_MODULE_CanContinue(FOR_EACH_MODULE_Type& state, CModules::iterator& i) {
 
43
        if (!state.bOnCMuser && i == CZNC::Get().GetModules().end()) {
 
44
                i = state.CMuser.begin();
 
45
                state.bOnCMuser = true;
 
46
        }
 
47
        if (state.bOnCMuser && i == state.CMuser.end()) {
 
48
                return false;
 
49
        }
 
50
        return true;
 
51
}
 
52
 
 
53
#define FOR_EACH_MODULE(I, pUser)\
 
54
        if (FOR_EACH_MODULE_Type FOR_EACH_MODULE_Var = pUser) {} else\
 
55
        for (CModules::iterator I = CZNC::Get().GetModules().begin(); FOR_EACH_MODULE_CanContinue(FOR_EACH_MODULE_Var, I); ++I)
24
56
 
25
57
class CWebAdminMod : public CGlobalModule {
26
58
public:
198
230
                sArg = WebSock.GetParam("chanmodes"); if (!sArg.empty()) { pNewUser->SetDefaultChanModes(sArg); }
199
231
                sArg = WebSock.GetParam("timestampformat"); if (!sArg.empty()) { pNewUser->SetTimestampFormat(sArg); }
200
232
 
201
 
                sArg = WebSock.GetParam("vhost");
202
 
                // To change VHosts be admin or don't have DenySetVHost
203
 
                if (spSession->IsAdmin() || !spSession->GetUser()->DenySetVHost()) {
 
233
                sArg = WebSock.GetParam("bindhost");
 
234
                // To change BindHosts be admin or don't have DenySetBindHost
 
235
                if (spSession->IsAdmin() || !spSession->GetUser()->DenySetBindHost()) {
204
236
                        if (!sArg.empty()) {
205
 
                                pNewUser->SetVHost(sArg);
 
237
                                pNewUser->SetBindHost(sArg);
206
238
                        }
207
239
                } else if (pUser){
208
 
                        pNewUser->SetVHost(pUser->GetVHost());
 
240
                        pNewUser->SetBindHost(pUser->GetBindHost());
209
241
                }
210
242
 
 
243
                // First apply the old limit in case the new one is too high
 
244
                if (pUser)
 
245
                        pNewUser->SetBufferCount(pUser->GetBufferCount(), true);
 
246
                pNewUser->SetBufferCount(WebSock.GetParam("bufsize").ToUInt());
211
247
                pNewUser->SetSkinName(WebSock.GetParam("skin"));
212
 
                pNewUser->SetBufferCount(WebSock.GetParam("bufsize").ToUInt());
213
248
                pNewUser->SetKeepBuffer(WebSock.GetParam("keepbuffer").ToBool());
214
249
                pNewUser->SetMultiClients(WebSock.GetParam("multiclients").ToBool());
215
250
                pNewUser->SetBounceDCCs(WebSock.GetParam("bouncedccs").ToBool());
223
258
 
224
259
                if (spSession->IsAdmin()) {
225
260
                        pNewUser->SetDenyLoadMod(WebSock.GetParam("denyloadmod").ToBool());
226
 
                        pNewUser->SetDenySetVHost(WebSock.GetParam("denysetvhost").ToBool());
 
261
                        pNewUser->SetDenySetBindHost(WebSock.GetParam("denysetbindhost").ToBool());
227
262
                } else if (pUser) {
228
263
                        pNewUser->SetDenyLoadMod(pUser->DenyLoadMod());
229
 
                        pNewUser->SetDenySetVHost(pUser->DenySetVHost());
 
264
                        pNewUser->SetDenySetBindHost(pUser->DenySetBindHost());
230
265
                }
231
266
 
232
267
                // If pUser is not NULL, we are editing an existing user.
255
290
                                        CString sArgs = WebSock.GetParam("modargs_" + sModName);
256
291
 
257
292
                                        try {
258
 
                                                if (!pNewUser->GetModules().LoadModule(sModName, sArgs, pNewUser, sModRet, (pUser != NULL))) {
 
293
                                                if (!pNewUser->GetModules().LoadModule(sModName, sArgs, pNewUser, sModRet)) {
259
294
                                                        sModLoadError = "Unable to load module [" + sModName + "] [" + sModRet + "]";
260
295
                                                }
261
296
                                        } catch (...) {
278
313
                                CString sModLoadError;
279
314
 
280
315
                                try {
281
 
                                        if (!pNewUser->GetModules().LoadModule(sModName, sArgs, pNewUser, sModRet, (pUser != NULL))) {
 
316
                                        if (!pNewUser->GetModules().LoadModule(sModName, sArgs, pNewUser, sModRet)) {
282
317
                                                sModLoadError = "Unable to load module [" + sModName + "] [" + sModRet + "]";
283
318
                                        }
284
319
                                } catch (...) {
490
525
                        o3["DisplayName"] = "Detached";
491
526
                        if (pChan && pChan->IsDetached()) { o3["Checked"] = "true"; }
492
527
 
 
528
                        FOR_EACH_MODULE(i, pUser) {
 
529
                                CTemplate& mod = Tmpl.AddRow("EmbeddedModuleLoop");
 
530
                                mod.insert(Tmpl.begin(), Tmpl.end());
 
531
                                mod["WebadminAction"] = "display";
 
532
                                if ((*i)->OnEmbeddedWebRequest(WebSock, "webadmin/channel", mod)) {
 
533
                                        mod["Embed"] = WebSock.FindTmpl(*i, "WebadminChan.tmpl");
 
534
                                        mod["ModName"] = (*i)->GetModName();
 
535
                                }
 
536
                        }
 
537
 
493
538
                        return true;
494
539
                }
495
540
 
510
555
                        pUser->AddChan(pChan);
511
556
                }
512
557
 
 
558
                pChan->SetBufferCount(WebSock.GetParam("buffercount").ToUInt());
513
559
                pChan->SetDefaultModes(WebSock.GetParam("defmodes"));
514
 
                pChan->SetBufferCount(WebSock.GetParam("buffercount").ToUInt());
515
560
                pChan->SetInConfig(WebSock.GetParam("save").ToBool());
516
561
                pChan->SetKeepBuffer(WebSock.GetParam("keepbuffer").ToBool());
517
562
 
525
570
                        }
526
571
                }
527
572
 
 
573
                CTemplate TmplMod;
 
574
                TmplMod["User"] = pUser->GetUserName();
 
575
                TmplMod["ChanName"] = sChanName;
 
576
                TmplMod["WebadminAction"] = "change";
 
577
                FOR_EACH_MODULE(it, pUser) {
 
578
                        (*it)->OnEmbeddedWebRequest(WebSock, "webadmin/channel", TmplMod);
 
579
                }
 
580
        
528
581
                if (!CZNC::Get().WriteConfig()) {
529
582
                        WebSock.PrintErrorPage("Channel added/modified, but config was not written");
530
583
                        return true;
627
680
                                Tmpl["IRCConnectEnabled"] = "true";
628
681
                        }
629
682
 
630
 
                        // To change VHosts be admin or don't have DenySetVHost
631
 
                        const VCString& vsVHosts = CZNC::Get().GetVHosts();
632
 
                        bool bFoundVHost = false;
633
 
                        if (spSession->IsAdmin() || !spSession->GetUser()->DenySetVHost()) {
634
 
                                for (unsigned int b = 0; b < vsVHosts.size(); b++) {
635
 
                                        const CString& sVHost = vsVHosts[b];
636
 
                                        CTemplate& l = Tmpl.AddRow("VHostLoop");
637
 
 
638
 
                                        l["VHost"] = sVHost;
639
 
 
640
 
                                        if (pUser && pUser->GetVHost() == sVHost) {
 
683
                        // To change BindHosts be admin or don't have DenySetBindHost
 
684
                        const VCString& vsBindHosts = CZNC::Get().GetBindHosts();
 
685
                        bool bFoundBindHost = false;
 
686
                        if (spSession->IsAdmin() || !spSession->GetUser()->DenySetBindHost()) {
 
687
                                for (unsigned int b = 0; b < vsBindHosts.size(); b++) {
 
688
                                        const CString& sBindHost = vsBindHosts[b];
 
689
                                        CTemplate& l = Tmpl.AddRow("BindHostLoop");
 
690
 
 
691
                                        l["BindHost"] = sBindHost;
 
692
 
 
693
                                        if (pUser && pUser->GetBindHost() == sBindHost) {
641
694
                                                l["Checked"] = "true";
642
 
                                                bFoundVHost = true;
 
695
                                                bFoundBindHost = true;
643
696
                                        }
644
697
                                }
645
698
 
646
 
                                // If our current vhost is not in the global list...
647
 
                                if (pUser && !bFoundVHost && !pUser->GetVHost().empty()) {
648
 
                                        CTemplate& l = Tmpl.AddRow("VHostLoop");
 
699
                                // If our current bindhost is not in the global list...
 
700
                                if (pUser && !bFoundBindHost && !pUser->GetBindHost().empty()) {
 
701
                                        CTemplate& l = Tmpl.AddRow("BindHostLoop");
649
702
 
650
 
                                        l["VHost"] = pUser->GetVHost();
 
703
                                        l["BindHost"] = pUser->GetBindHost();
651
704
                                        l["Checked"] = "true";
652
705
                                }
653
706
                        }
730
783
                                if (pUser && pUser == CZNC::Get().FindUser(WebSock.GetUser())) { o10["Disabled"] = "true"; }
731
784
 
732
785
                                CTemplate& o11 = Tmpl.AddRow("OptionLoop");
733
 
                                o11["Name"] = "denysetvhost";
734
 
                                o11["DisplayName"] = "Deny SetVHost";
735
 
                                if (pUser && pUser->DenySetVHost()) { o11["Checked"] = "true"; }
 
786
                                o11["Name"] = "denysetbindhost";
 
787
                                o11["DisplayName"] = "Deny SetBindHost";
 
788
                                if (pUser && pUser->DenySetBindHost()) { o11["Checked"] = "true"; }
 
789
                        }
 
790
 
 
791
                        FOR_EACH_MODULE(i, pUser) {
 
792
                                CTemplate& mod = Tmpl.AddRow("EmbeddedModuleLoop");
 
793
                                mod.insert(Tmpl.begin(), Tmpl.end());
 
794
                                mod["WebadminAction"] = "display";
 
795
                                if ((*i)->OnEmbeddedWebRequest(WebSock, "webadmin/user", mod)) {
 
796
                                        mod["Embed"] = WebSock.FindTmpl(*i, "WebadminUser.tmpl");
 
797
                                        mod["ModName"] = (*i)->GetModName();
 
798
                                }
736
799
                        }
737
800
 
738
801
                        return true;
752
815
                }
753
816
 
754
817
                CString sErr;
 
818
                CString sAction;
755
819
 
756
820
                if (!pUser) {
757
821
                        // Add User Submission
761
825
                                return true;
762
826
                        }
763
827
 
764
 
                        if (!CZNC::Get().WriteConfig()) {
765
 
                                WebSock.PrintErrorPage("User added, but config was not written");
766
 
                                return true;
767
 
                        }
 
828
                        pUser = pNewUser;
 
829
                        sAction = "added";
768
830
                } else {
769
831
                        // Edit User Submission
770
832
                        if (!pUser->Clone(*pNewUser, sErr, false)) {
774
836
                        }
775
837
 
776
838
                        delete pNewUser;
777
 
                        if (!CZNC::Get().WriteConfig()) {
778
 
                                WebSock.PrintErrorPage("User edited, but config was not written");
779
 
                                return true;
780
 
                        }
 
839
                        sAction = "edited";
 
840
                }
 
841
 
 
842
                CTemplate TmplMod;
 
843
                TmplMod["Username"] = sUsername;
 
844
                TmplMod["WebadminAction"] = "change";
 
845
                FOR_EACH_MODULE(it, pUser) {
 
846
                        (*it)->OnEmbeddedWebRequest(WebSock, "webadmin/user", TmplMod);
 
847
                }
 
848
        
 
849
                if (!CZNC::Get().WriteConfig()) {
 
850
                        WebSock.PrintErrorPage("User " + sAction + ", but config was not written");
 
851
                        return true;
781
852
                }
782
853
 
783
854
                if (!spSession->IsAdmin()) {
873
944
 
874
945
        bool SettingsPage(CWebSock& WebSock, CTemplate& Tmpl) {
875
946
                if (!WebSock.GetParam("submitted").ToUInt()) {
876
 
                        CString sVHosts, sMotd;
 
947
                        CString sBindHosts, sMotd;
877
948
                        Tmpl["Action"] = "settings";
878
949
                        Tmpl["Title"] = "Settings";
879
950
                        Tmpl["StatusPrefix"] = CZNC::Get().GetStatusPrefix();
880
951
                        Tmpl["ISpoofFile"] = CZNC::Get().GetISpoofFile();
881
952
                        Tmpl["ISpoofFormat"] = CZNC::Get().GetISpoofFormat();
882
953
 
883
 
                        const VCString& vsVHosts = CZNC::Get().GetVHosts();
884
 
                        for (unsigned int a = 0; a < vsVHosts.size(); a++) {
885
 
                                CTemplate& l = Tmpl.AddRow("VHostLoop");
886
 
                                l["VHost"] = vsVHosts[a];
 
954
                        const VCString& vsBindHosts = CZNC::Get().GetBindHosts();
 
955
                        for (unsigned int a = 0; a < vsBindHosts.size(); a++) {
 
956
                                CTemplate& l = Tmpl.AddRow("BindHostLoop");
 
957
                                l["BindHost"] = vsBindHosts[a];
887
958
                        }
888
959
 
889
960
                        const VCString& vsMotd = CZNC::Get().GetMotd();
978
1049
                        CZNC::Get().AddMotd(vsArgs[a].TrimRight_n());
979
1050
                }
980
1051
 
981
 
                WebSock.GetRawParam("vhosts").Split("\n", vsArgs);
982
 
                CZNC::Get().ClearVHosts();
 
1052
                WebSock.GetRawParam("bindhosts").Split("\n", vsArgs);
 
1053
                CZNC::Get().ClearBindHosts();
983
1054
 
984
1055
                for (a = 0; a < vsArgs.size(); a++) {
985
 
                        CZNC::Get().AddVHost(vsArgs[a].Trim_n());
 
1056
                        CZNC::Get().AddBindHost(vsArgs[a].Trim_n());
986
1057
                }
987
1058
 
988
1059
                CZNC::Get().SetSkinName(WebSock.GetParam("skin"));
1039
1110
                /* we don't want the template to be printed while we redirect */
1040
1111
                return false;
1041
1112
        }
1042
 
 
1043
 
private:
1044
 
        map<CString, unsigned int>  m_suSwitchCounters;
1045
1113
};
1046
1114
 
1047
1115
GLOBALMODULEDEFS(CWebAdminMod, "Web based administration module")