File: controlpanel.cpp

package info (click to toggle)
znc 1.7.2-3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 8,520 kB
  • sloc: cpp: 45,532; sh: 3,577; perl: 1,182; python: 867; makefile: 392; tcl: 218; ansic: 188; pascal: 65
file content (1658 lines) | stat: -rw-r--r-- 62,739 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
/*
 * Copyright (C) 2004-2018 ZNC, see the NOTICE file for details.
 * Copyright (C) 2008 by Stefan Rado
 * based on admin.cpp by Sebastian Ramacher
 * based on admin.cpp in crox branch
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <znc/User.h>
#include <znc/IRCNetwork.h>
#include <znc/Chan.h>
#include <znc/IRCSock.h>

using std::map;
using std::vector;

template <std::size_t N>
struct array_size_helper {
    char __place_holder[N];
};

template <class T, std::size_t N>
static array_size_helper<N> array_size(T (&)[N]) {
    return array_size_helper<N>();
}

#define ARRAY_SIZE(array) sizeof(array_size((array)))

class CAdminMod : public CModule {
    using CModule::PutModule;

    struct Setting {
        const char* name;
        CString type;
    };

    void PrintVarsHelp(const CString& sFilter, const Setting vars[],
                       unsigned int uSize, const CString& sDescription) {
        CTable VarTable;
        VarTable.AddColumn(t_s("Type", "helptable"));
        VarTable.AddColumn(t_s("Variables", "helptable"));
        std::map<CString, VCString> mvsTypedVariables;
        for (unsigned int i = 0; i != uSize; ++i) {
            CString sVar = CString(vars[i].name).AsLower();
            if (sFilter.empty() || sVar.StartsWith(sFilter) ||
                sVar.WildCmp(sFilter)) {
                mvsTypedVariables[vars[i].type].emplace_back(vars[i].name);
            }
        }
        for (const auto& i : mvsTypedVariables) {
            VarTable.AddRow();
            VarTable.SetCell(t_s("Type", "helptable"), i.first);
            VarTable.SetCell(
                t_s("Variables", "helptable"),
                CString(", ").Join(i.second.cbegin(), i.second.cend()));
        }
        if (!VarTable.empty()) {
            PutModule(sDescription);
            PutModule(VarTable);
        }
    }

    void PrintHelp(const CString& sLine) {
        HandleHelpCommand(sLine);

        const CString str = t_s("String");
        const CString boolean = t_s("Boolean (true/false)");
        const CString integer = t_s("Integer");
        const CString number = t_s("Number");

        const CString sCmdFilter = sLine.Token(1, false);
        const CString sVarFilter = sLine.Token(2, true).AsLower();

        if (sCmdFilter.empty() || sCmdFilter.StartsWith("Set") ||
            sCmdFilter.StartsWith("Get")) {
            Setting vars[] = {
                {"Nick", str},
                {"Altnick", str},
                {"Ident", str},
                {"RealName", str},
                {"BindHost", str},
                {"MultiClients", boolean},
                {"DenyLoadMod", boolean},
                {"DenySetBindHost", boolean},
                {"DefaultChanModes", str},
                {"QuitMsg", str},
                {"ChanBufferSize", integer},
                {"QueryBufferSize", integer},
                {"AutoClearChanBuffer", boolean},
                {"AutoClearQueryBuffer", boolean},
                {"Password", str},
                {"JoinTries", integer},
                {"MaxJoins", integer},
                {"MaxNetworks", integer},
                {"MaxQueryBuffers", integer},
                {"Timezone", str},
                {"Admin", boolean},
                {"AppendTimestamp", boolean},
                {"PrependTimestamp", boolean},
                {"AuthOnlyViaModule", boolean},
                {"TimestampFormat", str},
                {"DCCBindHost", str},
                {"StatusPrefix", str},
#ifdef HAVE_I18N
                {"Language", str},
#endif
#ifdef HAVE_ICU
                {"ClientEncoding", str},
#endif
            };
            PrintVarsHelp(sVarFilter, vars, ARRAY_SIZE(vars),
                          t_s("The following variables are available when "
                              "using the Set/Get commands:"));
        }

        if (sCmdFilter.empty() || sCmdFilter.StartsWith("SetNetwork") ||
            sCmdFilter.StartsWith("GetNetwork")) {
            Setting nvars[] = {
                {"Nick", str},
                {"Altnick", str},
                {"Ident", str},
                {"RealName", str},
                {"BindHost", str},
                {"FloodRate", number},
                {"FloodBurst", integer},
                {"JoinDelay", integer},
#ifdef HAVE_ICU
                {"Encoding", str},
#endif
                {"QuitMsg", str},
                {"TrustAllCerts", boolean},
                {"TrustPKI", boolean},
            };
            PrintVarsHelp(sVarFilter, nvars, ARRAY_SIZE(nvars),
                          t_s("The following variables are available when "
                              "using the SetNetwork/GetNetwork commands:"));
        }

        if (sCmdFilter.empty() || sCmdFilter.StartsWith("SetChan") ||
            sCmdFilter.StartsWith("GetChan")) {
            Setting cvars[] = {{"DefModes", str},
                               {"Key", str},
                               {"BufferSize", integer},
                               {"InConfig", boolean},
                               {"AutoClearChanBuffer", boolean},
                               {"Detached", boolean}};
            PrintVarsHelp(sVarFilter, cvars, ARRAY_SIZE(cvars),
                          t_s("The following variables are available when "
                              "using the SetChan/GetChan commands:"));
        }

        if (sCmdFilter.empty())
            PutModule(
                t_s("You can use $user as the user name and $network as the "
                    "network name for modifying your own user and network."));
    }

    CUser* FindUser(const CString& sUsername) {
        if (sUsername.Equals("$me") || sUsername.Equals("$user"))
            return GetUser();
        CUser* pUser = CZNC::Get().FindUser(sUsername);
        if (!pUser) {
            PutModule(t_f("Error: User [{1}] does not exist!")(sUsername));
            return nullptr;
        }
        if (pUser != GetUser() && !GetUser()->IsAdmin()) {
            PutModule(t_s(
                "Error: You need to have admin rights to modify other users!"));
            return nullptr;
        }
        return pUser;
    }

    CIRCNetwork* FindNetwork(CUser* pUser, const CString& sNetwork) {
        if (sNetwork.Equals("$net") || sNetwork.Equals("$network")) {
            if (pUser != GetUser()) {
                PutModule(t_s(
                    "Error: You cannot use $network to modify other users!"));
                return nullptr;
            }
            return CModule::GetNetwork();
        }
        CIRCNetwork* pNetwork = pUser->FindNetwork(sNetwork);
        if (!pNetwork) {
            PutModule(
                t_f("Error: User {1} does not have a network named [{2}].")(
                    pUser->GetUserName(), sNetwork));
        }
        return pNetwork;
    }

    void Get(const CString& sLine) {
        const CString sVar = sLine.Token(1).AsLower();
        CString sUsername = sLine.Token(2, true);
        CUser* pUser;

        if (sVar.empty()) {
            PutModule(t_s("Usage: Get <variable> [username]"));
            return;
        }

        if (sUsername.empty()) {
            pUser = GetUser();
        } else {
            pUser = FindUser(sUsername);
        }

        if (!pUser) return;

        if (sVar == "nick")
            PutModule("Nick = " + pUser->GetNick());
        else if (sVar == "altnick")
            PutModule("AltNick = " + pUser->GetAltNick());
        else if (sVar == "ident")
            PutModule("Ident = " + pUser->GetIdent());
        else if (sVar == "realname")
            PutModule("RealName = " + pUser->GetRealName());
        else if (sVar == "bindhost")
            PutModule("BindHost = " + pUser->GetBindHost());
        else if (sVar == "multiclients")
            PutModule("MultiClients = " + CString(pUser->MultiClients()));
        else if (sVar == "denyloadmod")
            PutModule("DenyLoadMod = " + CString(pUser->DenyLoadMod()));
        else if (sVar == "denysetbindhost")
            PutModule("DenySetBindHost = " + CString(pUser->DenySetBindHost()));
        else if (sVar == "defaultchanmodes")
            PutModule("DefaultChanModes = " + pUser->GetDefaultChanModes());
        else if (sVar == "quitmsg")
            PutModule("QuitMsg = " + pUser->GetQuitMsg());
        else if (sVar == "buffercount")
            PutModule("BufferCount = " + CString(pUser->GetBufferCount()));
        else if (sVar == "chanbuffersize")
            PutModule("ChanBufferSize = " +
                      CString(pUser->GetChanBufferSize()));
        else if (sVar == "querybuffersize")
            PutModule("QueryBufferSize = " +
                      CString(pUser->GetQueryBufferSize()));
        else if (sVar == "keepbuffer")
            // XXX compatibility crap, added in 0.207
            PutModule("KeepBuffer = " + CString(!pUser->AutoClearChanBuffer()));
        else if (sVar == "autoclearchanbuffer")
            PutModule("AutoClearChanBuffer = " +
                      CString(pUser->AutoClearChanBuffer()));
        else if (sVar == "autoclearquerybuffer")
            PutModule("AutoClearQueryBuffer = " +
                      CString(pUser->AutoClearQueryBuffer()));
        else if (sVar == "maxjoins")
            PutModule("MaxJoins = " + CString(pUser->MaxJoins()));
        else if (sVar == "notraffictimeout")
            PutModule("NoTrafficTimeout = " +
                      CString(pUser->GetNoTrafficTimeout()));
        else if (sVar == "maxnetworks")
            PutModule("MaxNetworks = " + CString(pUser->MaxNetworks()));
        else if (sVar == "maxquerybuffers")
            PutModule("MaxQueryBuffers = " + CString(pUser->MaxQueryBuffers()));
        else if (sVar == "jointries")
            PutModule("JoinTries = " + CString(pUser->JoinTries()));
        else if (sVar == "timezone")
            PutModule("Timezone = " + pUser->GetTimezone());
        else if (sVar == "appendtimestamp")
            PutModule("AppendTimestamp = " +
                      CString(pUser->GetTimestampAppend()));
        else if (sVar == "prependtimestamp")
            PutModule("PrependTimestamp = " +
                      CString(pUser->GetTimestampPrepend()));
        else if (sVar == "authonlyviamodule")
            PutModule("AuthOnlyViaModule = " +
                      CString(pUser->AuthOnlyViaModule()));
        else if (sVar == "timestampformat")
            PutModule("TimestampFormat = " + pUser->GetTimestampFormat());
        else if (sVar == "dccbindhost")
            PutModule("DCCBindHost = " + CString(pUser->GetDCCBindHost()));
        else if (sVar == "admin")
            PutModule("Admin = " + CString(pUser->IsAdmin()));
        else if (sVar == "statusprefix")
            PutModule("StatusPrefix = " + pUser->GetStatusPrefix());
#ifdef HAVE_I18N
        else if (sVar == "language")
            PutModule("Language = " + (pUser->GetLanguage().empty()
                                           ? "en"
                                           : pUser->GetLanguage()));
#endif
#ifdef HAVE_ICU
        else if (sVar == "clientencoding")
            PutModule("ClientEncoding = " + pUser->GetClientEncoding());
#endif
        else
            PutModule(t_s("Error: Unknown variable"));
    }

    void Set(const CString& sLine) {
        const CString sVar = sLine.Token(1).AsLower();
        CString sUserName = sLine.Token(2);
        CString sValue = sLine.Token(3, true);

        if (sValue.empty()) {
            PutModule(t_s("Usage: Set <variable> <username> <value>"));
            return;
        }

        CUser* pUser = FindUser(sUserName);
        if (!pUser) return;

        if (sVar == "nick") {
            pUser->SetNick(sValue);
            PutModule("Nick = " + sValue);
        } else if (sVar == "altnick") {
            pUser->SetAltNick(sValue);
            PutModule("AltNick = " + sValue);
        } else if (sVar == "ident") {
            pUser->SetIdent(sValue);
            PutModule("Ident = " + sValue);
        } else if (sVar == "realname") {
            pUser->SetRealName(sValue);
            PutModule("RealName = " + sValue);
        } else if (sVar == "bindhost") {
            if (!pUser->DenySetBindHost() || GetUser()->IsAdmin()) {
                if (sValue.Equals(pUser->GetBindHost())) {
                    PutModule(t_s("This bind host is already set!"));
                    return;
                }

                pUser->SetBindHost(sValue);
                PutModule("BindHost = " + sValue);
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar == "multiclients") {
            bool b = sValue.ToBool();
            pUser->SetMultiClients(b);
            PutModule("MultiClients = " + CString(b));
        } else if (sVar == "denyloadmod") {
            if (GetUser()->IsAdmin()) {
                bool b = sValue.ToBool();
                pUser->SetDenyLoadMod(b);
                PutModule("DenyLoadMod = " + CString(b));
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar == "denysetbindhost") {
            if (GetUser()->IsAdmin()) {
                bool b = sValue.ToBool();
                pUser->SetDenySetBindHost(b);
                PutModule("DenySetBindHost = " + CString(b));
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar == "defaultchanmodes") {
            pUser->SetDefaultChanModes(sValue);
            PutModule("DefaultChanModes = " + sValue);
        } else if (sVar == "quitmsg") {
            pUser->SetQuitMsg(sValue);
            PutModule("QuitMsg = " + sValue);
        } else if (sVar == "chanbuffersize" || sVar == "buffercount") {
            unsigned int i = sValue.ToUInt();
            // Admins don't have to honour the buffer limit
            if (pUser->SetChanBufferSize(i, GetUser()->IsAdmin())) {
                PutModule("ChanBufferSize = " + sValue);
            } else {
                PutModule(t_f("Setting failed, limit for buffer size is {1}")(
                    CString(CZNC::Get().GetMaxBufferSize())));
            }
        } else if (sVar == "querybuffersize") {
            unsigned int i = sValue.ToUInt();
            // Admins don't have to honour the buffer limit
            if (pUser->SetQueryBufferSize(i, GetUser()->IsAdmin())) {
                PutModule("QueryBufferSize = " + sValue);
            } else {
                PutModule(t_f("Setting failed, limit for buffer size is {1}")(
                    CString(CZNC::Get().GetMaxBufferSize())));
            }
        } else if (sVar == "keepbuffer") {
            // XXX compatibility crap, added in 0.207
            bool b = !sValue.ToBool();
            pUser->SetAutoClearChanBuffer(b);
            PutModule("AutoClearChanBuffer = " + CString(b));
        } else if (sVar == "autoclearchanbuffer") {
            bool b = sValue.ToBool();
            pUser->SetAutoClearChanBuffer(b);
            PutModule("AutoClearChanBuffer = " + CString(b));
        } else if (sVar == "autoclearquerybuffer") {
            bool b = sValue.ToBool();
            pUser->SetAutoClearQueryBuffer(b);
            PutModule("AutoClearQueryBuffer = " + CString(b));
        } else if (sVar == "password") {
            const CString sSalt = CUtils::GetSalt();
            const CString sHash = CUser::SaltedHash(sValue, sSalt);
            pUser->SetPass(sHash, CUser::HASH_DEFAULT, sSalt);
            PutModule(t_s("Password has been changed!"));
        } else if (sVar == "maxjoins") {
            unsigned int i = sValue.ToUInt();
            pUser->SetMaxJoins(i);
            PutModule("MaxJoins = " + CString(pUser->MaxJoins()));
        } else if (sVar == "notraffictimeout") {
            unsigned int i = sValue.ToUInt();
            if (i < 30) {
                PutModule(t_s("Timeout can't be less than 30 seconds!"));
            } else {
                pUser->SetNoTrafficTimeout(i);
                PutModule("NoTrafficTimeout = " +
                          CString(pUser->GetNoTrafficTimeout()));
            }
        } else if (sVar == "maxnetworks") {
            if (GetUser()->IsAdmin()) {
                unsigned int i = sValue.ToUInt();
                pUser->SetMaxNetworks(i);
                PutModule("MaxNetworks = " + sValue);
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar == "maxquerybuffers") {
            unsigned int i = sValue.ToUInt();
            pUser->SetMaxQueryBuffers(i);
            PutModule("MaxQueryBuffers = " + sValue);
        } else if (sVar == "jointries") {
            unsigned int i = sValue.ToUInt();
            pUser->SetJoinTries(i);
            PutModule("JoinTries = " + CString(pUser->JoinTries()));
        } else if (sVar == "timezone") {
            pUser->SetTimezone(sValue);
            PutModule("Timezone = " + pUser->GetTimezone());
        } else if (sVar == "admin") {
            if (GetUser()->IsAdmin() && pUser != GetUser()) {
                bool b = sValue.ToBool();
                pUser->SetAdmin(b);
                PutModule("Admin = " + CString(pUser->IsAdmin()));
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar == "prependtimestamp") {
            bool b = sValue.ToBool();
            pUser->SetTimestampPrepend(b);
            PutModule("PrependTimestamp = " + CString(b));
        } else if (sVar == "appendtimestamp") {
            bool b = sValue.ToBool();
            pUser->SetTimestampAppend(b);
            PutModule("AppendTimestamp = " + CString(b));
        } else if (sVar == "authonlyviamodule") {
            if (GetUser()->IsAdmin()) {
                bool b = sValue.ToBool();
                pUser->SetAuthOnlyViaModule(b);
                PutModule("AuthOnlyViaModule = " + CString(b));
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar == "timestampformat") {
            pUser->SetTimestampFormat(sValue);
            PutModule("TimestampFormat = " + sValue);
        } else if (sVar == "dccbindhost") {
            if (!pUser->DenySetBindHost() || GetUser()->IsAdmin()) {
                pUser->SetDCCBindHost(sValue);
                PutModule("DCCBindHost = " + sValue);
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar == "statusprefix") {
            if (sVar.find_first_of(" \t\n") == CString::npos) {
                pUser->SetStatusPrefix(sValue);
                PutModule("StatusPrefix = " + sValue);
            } else {
                PutModule(t_s("That would be a bad idea!"));
            }
        }
#ifdef HAVE_I18N
        else if (sVar == "language") {
            auto mTranslations = CTranslationInfo::GetTranslations();
            // TODO: maybe stop special-casing English
            if (sValue == "en") {
                pUser->SetLanguage("");
                PutModule("Language is set to English");
            } else if (mTranslations.count(sValue)) {
                pUser->SetLanguage(sValue);
                PutModule("Language = " + sValue);
            } else {
                VCString vsCodes = {"en"};
                for (const auto it : mTranslations) {
                    vsCodes.push_back(it.first);
                }
                PutModule(t_f("Supported languages: {1}")(
                    CString(", ").Join(vsCodes.begin(), vsCodes.end())));
            }
        }
#endif
#ifdef HAVE_ICU
        else if (sVar == "clientencoding") {
            pUser->SetClientEncoding(sValue);
            PutModule("ClientEncoding = " + pUser->GetClientEncoding());
        }
#endif
        else
            PutModule(t_s("Error: Unknown variable"));
    }

    void GetNetwork(const CString& sLine) {
        const CString sVar = sLine.Token(1).AsLower();
        const CString sUsername = sLine.Token(2);
        const CString sNetwork = sLine.Token(3);

        CIRCNetwork* pNetwork = nullptr;
        CUser* pUser;

        if (sVar.empty()) {
            PutModule(t_s("Usage: GetNetwork <variable> [username] [network]"));
            return;
        }

        if (sUsername.empty()) {
            pUser = GetUser();
        } else {
            pUser = FindUser(sUsername);
        }

        if (!pUser) {
            return;
        }

        if (sNetwork.empty()) {
            if (pUser == GetUser()) {
                pNetwork = CModule::GetNetwork();
            } else {
                PutModule(
                    t_s("Error: A network must be specified to get another "
                        "users settings."));
                return;
            }

            if (!pNetwork) {
                PutModule(t_s("You are not currently attached to a network."));
                return;
            }
        } else {
            pNetwork = FindNetwork(pUser, sNetwork);
            if (!pNetwork) {
                PutModule(t_s("Error: Invalid network."));
                return;
            }
        }

        if (sVar.Equals("nick")) {
            PutModule("Nick = " + pNetwork->GetNick());
        } else if (sVar.Equals("altnick")) {
            PutModule("AltNick = " + pNetwork->GetAltNick());
        } else if (sVar.Equals("ident")) {
            PutModule("Ident = " + pNetwork->GetIdent());
        } else if (sVar.Equals("realname")) {
            PutModule("RealName = " + pNetwork->GetRealName());
        } else if (sVar.Equals("bindhost")) {
            PutModule("BindHost = " + pNetwork->GetBindHost());
        } else if (sVar.Equals("floodrate")) {
            PutModule("FloodRate = " + CString(pNetwork->GetFloodRate()));
        } else if (sVar.Equals("floodburst")) {
            PutModule("FloodBurst = " + CString(pNetwork->GetFloodBurst()));
        } else if (sVar.Equals("joindelay")) {
            PutModule("JoinDelay = " + CString(pNetwork->GetJoinDelay()));
#ifdef HAVE_ICU
        } else if (sVar.Equals("encoding")) {
            PutModule("Encoding = " + pNetwork->GetEncoding());
#endif
        } else if (sVar.Equals("quitmsg")) {
            PutModule("QuitMsg = " + pNetwork->GetQuitMsg());
        } else if (sVar.Equals("trustallcerts")) {
            PutModule("TrustAllCerts = " + CString(pNetwork->GetTrustAllCerts()));
        } else if (sVar.Equals("trustpki")) {
            PutModule("TrustPKI = " + CString(pNetwork->GetTrustPKI()));
        } else {
            PutModule(t_s("Error: Unknown variable"));
        }
    }

    void SetNetwork(const CString& sLine) {
        const CString sVar = sLine.Token(1).AsLower();
        const CString sUsername = sLine.Token(2);
        const CString sNetwork = sLine.Token(3);
        const CString sValue = sLine.Token(4, true);

        if (sValue.empty()) {
            PutModule(t_s(
                "Usage: SetNetwork <variable> <username> <network> <value>"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) {
            return;
        }

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        if (sVar.Equals("nick")) {
            pNetwork->SetNick(sValue);
            PutModule("Nick = " + pNetwork->GetNick());
        } else if (sVar.Equals("altnick")) {
            pNetwork->SetAltNick(sValue);
            PutModule("AltNick = " + pNetwork->GetAltNick());
        } else if (sVar.Equals("ident")) {
            pNetwork->SetIdent(sValue);
            PutModule("Ident = " + pNetwork->GetIdent());
        } else if (sVar.Equals("realname")) {
            pNetwork->SetRealName(sValue);
            PutModule("RealName = " + pNetwork->GetRealName());
        } else if (sVar.Equals("bindhost")) {
            if (!pUser->DenySetBindHost() || GetUser()->IsAdmin()) {
                if (sValue.Equals(pNetwork->GetBindHost())) {
                    PutModule(t_s("This bind host is already set!"));
                    return;
                }

                pNetwork->SetBindHost(sValue);
                PutModule("BindHost = " + sValue);
            } else {
                PutModule(t_s("Access denied!"));
            }
        } else if (sVar.Equals("floodrate")) {
            pNetwork->SetFloodRate(sValue.ToDouble());
            PutModule("FloodRate = " + CString(pNetwork->GetFloodRate()));
        } else if (sVar.Equals("floodburst")) {
            pNetwork->SetFloodBurst(sValue.ToUShort());
            PutModule("FloodBurst = " + CString(pNetwork->GetFloodBurst()));
        } else if (sVar.Equals("joindelay")) {
            pNetwork->SetJoinDelay(sValue.ToUShort());
            PutModule("JoinDelay = " + CString(pNetwork->GetJoinDelay()));
#ifdef HAVE_ICU
        } else if (sVar.Equals("encoding")) {
            pNetwork->SetEncoding(sValue);
            PutModule("Encoding = " + pNetwork->GetEncoding());
#endif
        } else if (sVar.Equals("quitmsg")) {
            pNetwork->SetQuitMsg(sValue);
            PutModule("QuitMsg = " + pNetwork->GetQuitMsg());
        } else if (sVar.Equals("trustallcerts")) {
            bool b = sValue.ToBool();
            pNetwork->SetTrustAllCerts(b);
            PutModule("TrustAllCerts = " + CString(b));
        } else if (sVar.Equals("trustpki")) {
            bool b = sValue.ToBool();
            pNetwork->SetTrustPKI(b);
            PutModule("TrustPKI = " + CString(b));
        } else {
            PutModule(t_s("Error: Unknown variable"));
        }
    }

    void AddChan(const CString& sLine) {
        const CString sUsername = sLine.Token(1);
        const CString sNetwork = sLine.Token(2);
        const CString sChan = sLine.Token(3);

        if (sChan.empty()) {
            PutModule(t_s("Usage: AddChan <username> <network> <channel>"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        if (pNetwork->FindChan(sChan)) {
            PutModule(t_f("Error: User {1} already has a channel named {2}.")(
                sUsername, sChan));
            return;
        }

        CChan* pChan = new CChan(sChan, pNetwork, true);
        if (pNetwork->AddChan(pChan))
            PutModule(t_f("Channel {1} for user {2} added to network {3}.")(
                pChan->GetName(), sUsername, pNetwork->GetName()));
        else
            PutModule(t_f(
                "Could not add channel {1} for user {2} to network {3}, does "
                "it already exist?")(sChan, sUsername, pNetwork->GetName()));
    }

    void DelChan(const CString& sLine) {
        const CString sUsername = sLine.Token(1);
        const CString sNetwork = sLine.Token(2);
        const CString sChan = sLine.Token(3);

        if (sChan.empty()) {
            PutModule(t_s("Usage: DelChan <username> <network> <channel>"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        std::vector<CChan*> vChans = pNetwork->FindChans(sChan);
        if (vChans.empty()) {
            PutModule(
                t_f("Error: User {1} does not have any channel matching [{2}] "
                    "in network {3}")(sUsername, sChan, pNetwork->GetName()));
            return;
        }

        VCString vsNames;
        for (const CChan* pChan : vChans) {
            const CString& sName = pChan->GetName();
            vsNames.push_back(sName);
            pNetwork->PutIRC("PART " + sName);
            pNetwork->DelChan(sName);
        }

        PutModule(t_p("Channel {1} is deleted from network {2} of user {3}",
                      "Channels {1} are deleted from network {2} of user {3}",
                      vsNames.size())(
            CString(", ").Join(vsNames.begin(), vsNames.end()),
            pNetwork->GetName(), sUsername));
    }

    void GetChan(const CString& sLine) {
        const CString sVar = sLine.Token(1).AsLower();
        CString sUsername = sLine.Token(2);
        CString sNetwork = sLine.Token(3);
        CString sChan = sLine.Token(4, true);

        if (sChan.empty()) {
            PutModule(
                t_s("Usage: GetChan <variable> <username> <network> <chan>"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        std::vector<CChan*> vChans = pNetwork->FindChans(sChan);
        if (vChans.empty()) {
            PutModule(t_f("Error: No channels matching [{1}] found.")(sChan));
            return;
        }

        for (CChan* pChan : vChans) {
            if (sVar == "defmodes") {
                PutModule(pChan->GetName() + ": DefModes = " +
                          pChan->GetDefaultModes());
            } else if (sVar == "buffersize" || sVar == "buffer") {
                CString sValue(pChan->GetBufferCount());
                if (!pChan->HasBufferCountSet()) {
                    sValue += " (default)";
                }
                PutModule(pChan->GetName() + ": BufferSize = " + sValue);
            } else if (sVar == "inconfig") {
                PutModule(pChan->GetName() + ": InConfig = " +
                          CString(pChan->InConfig()));
            } else if (sVar == "keepbuffer") {
                // XXX compatibility crap, added in 0.207
                PutModule(pChan->GetName() + ": KeepBuffer = " +
                          CString(!pChan->AutoClearChanBuffer()));
            } else if (sVar == "autoclearchanbuffer") {
                CString sValue(pChan->AutoClearChanBuffer());
                if (!pChan->HasAutoClearChanBufferSet()) {
                    sValue += " (default)";
                }
                PutModule(pChan->GetName() + ": AutoClearChanBuffer = " +
                          sValue);
            } else if (sVar == "detached") {
                PutModule(pChan->GetName() + ": Detached = " +
                          CString(pChan->IsDetached()));
            } else if (sVar == "key") {
                PutModule(pChan->GetName() + ": Key = " + pChan->GetKey());
            } else {
                PutModule(t_s("Error: Unknown variable"));
                return;
            }
        }
    }

    void SetChan(const CString& sLine) {
        const CString sVar = sLine.Token(1).AsLower();
        CString sUsername = sLine.Token(2);
        CString sNetwork = sLine.Token(3);
        CString sChan = sLine.Token(4);
        CString sValue = sLine.Token(5, true);

        if (sValue.empty()) {
            PutModule(
                t_s("Usage: SetChan <variable> <username> <network> <chan> "
                    "<value>"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        std::vector<CChan*> vChans = pNetwork->FindChans(sChan);
        if (vChans.empty()) {
            PutModule(t_f("Error: No channels matching [{1}] found.")(sChan));
            return;
        }

        for (CChan* pChan : vChans) {
            if (sVar == "defmodes") {
                pChan->SetDefaultModes(sValue);
                PutModule(pChan->GetName() + ": DefModes = " + sValue);
            } else if (sVar == "buffersize" || sVar == "buffer") {
                unsigned int i = sValue.ToUInt();
                if (sValue.Equals("-")) {
                    pChan->ResetBufferCount();
                    PutModule(pChan->GetName() + ": BufferSize = " +
                              CString(pChan->GetBufferCount()));
                } else if (pChan->SetBufferCount(i, GetUser()->IsAdmin())) {
                    // Admins don't have to honour the buffer limit
                    PutModule(pChan->GetName() + ": BufferSize = " + sValue);
                } else {
                    PutModule(
                        t_f("Setting failed, limit for buffer size is {1}")(
                            CString(CZNC::Get().GetMaxBufferSize())));
                    return;
                }
            } else if (sVar == "inconfig") {
                bool b = sValue.ToBool();
                pChan->SetInConfig(b);
                PutModule(pChan->GetName() + ": InConfig = " + CString(b));
            } else if (sVar == "keepbuffer") {
                // XXX compatibility crap, added in 0.207
                bool b = !sValue.ToBool();
                pChan->SetAutoClearChanBuffer(b);
                PutModule(pChan->GetName() + ": AutoClearChanBuffer = " +
                          CString(b));
            } else if (sVar == "autoclearchanbuffer") {
                if (sValue.Equals("-")) {
                    pChan->ResetAutoClearChanBuffer();
                } else {
                    bool b = sValue.ToBool();
                    pChan->SetAutoClearChanBuffer(b);
                }
                PutModule(pChan->GetName() + ": AutoClearChanBuffer = " +
                          CString(pChan->AutoClearChanBuffer()));
            } else if (sVar == "detached") {
                bool b = sValue.ToBool();
                if (pChan->IsDetached() != b) {
                    if (b)
                        pChan->DetachUser();
                    else
                        pChan->AttachUser();
                }
                PutModule(pChan->GetName() + ": Detached = " + CString(b));
            } else if (sVar == "key") {
                pChan->SetKey(sValue);
                PutModule(pChan->GetName() + ": Key = " + sValue);
            } else {
                PutModule(t_s("Error: Unknown variable"));
                return;
            }
        }
    }

    void ListUsers(const CString&) {
        if (!GetUser()->IsAdmin()) return;

        const map<CString, CUser*>& msUsers = CZNC::Get().GetUserMap();
        CTable Table;
        Table.AddColumn(t_s("Username", "listusers"));
        Table.AddColumn(t_s("Realname", "listusers"));
        Table.AddColumn(t_s("IsAdmin", "listusers"));
        Table.AddColumn(t_s("Nick", "listusers"));
        Table.AddColumn(t_s("AltNick", "listusers"));
        Table.AddColumn(t_s("Ident", "listusers"));
        Table.AddColumn(t_s("BindHost", "listusers"));

        for (const auto& it : msUsers) {
            Table.AddRow();
            Table.SetCell(t_s("Username", "listusers"), it.first);
            Table.SetCell(t_s("Realname", "listusers"),
                          it.second->GetRealName());
            if (!it.second->IsAdmin())
                Table.SetCell(t_s("IsAdmin", "listusers"), t_s("No"));
            else
                Table.SetCell(t_s("IsAdmin", "listusers"), t_s("Yes"));
            Table.SetCell(t_s("Nick", "listusers"), it.second->GetNick());
            Table.SetCell(t_s("AltNick", "listusers"), it.second->GetAltNick());
            Table.SetCell(t_s("Ident", "listusers"), it.second->GetIdent());
            Table.SetCell(t_s("BindHost", "listusers"),
                          it.second->GetBindHost());
        }

        PutModule(Table);
    }

    void AddUser(const CString& sLine) {
        if (!GetUser()->IsAdmin()) {
            PutModule(
                t_s("Error: You need to have admin rights to add new users!"));
            return;
        }

        const CString sUsername = sLine.Token(1), sPassword = sLine.Token(2);
        if (sPassword.empty()) {
            PutModule(t_s("Usage: AddUser <username> <password>"));
            return;
        }

        if (CZNC::Get().FindUser(sUsername)) {
            PutModule(t_f("Error: User {1} already exists!")(sUsername));
            return;
        }

        CUser* pNewUser = new CUser(sUsername);
        CString sSalt = CUtils::GetSalt();
        pNewUser->SetPass(CUser::SaltedHash(sPassword, sSalt),
                          CUser::HASH_DEFAULT, sSalt);

        CString sErr;
        if (!CZNC::Get().AddUser(pNewUser, sErr)) {
            delete pNewUser;
            PutModule(t_f("Error: User not added: {1}")(sErr));
            return;
        }

        PutModule(t_f("User {1} added!")(sUsername));
        return;
    }

    void DelUser(const CString& sLine) {
        if (!GetUser()->IsAdmin()) {
            PutModule(
                t_s("Error: You need to have admin rights to delete users!"));
            return;
        }

        const CString sUsername = sLine.Token(1, true);
        if (sUsername.empty()) {
            PutModule(t_s("Usage: DelUser <username>"));
            return;
        }

        CUser* pUser = CZNC::Get().FindUser(sUsername);

        if (!pUser) {
            PutModule(t_f("Error: User [{1}] does not exist!")(sUsername));
            return;
        }

        if (pUser == GetUser()) {
            PutModule(t_s("Error: You can't delete yourself!"));
            return;
        }

        if (!CZNC::Get().DeleteUser(pUser->GetUserName())) {
            // This can't happen, because we got the user from FindUser()
            PutModule(t_s("Error: Internal error!"));
            return;
        }

        PutModule(t_f("User {1} deleted!")(sUsername));
        return;
    }

    void CloneUser(const CString& sLine) {
        if (!GetUser()->IsAdmin()) {
            PutModule(
                t_s("Error: You need to have admin rights to add new users!"));
            return;
        }

        const CString sOldUsername = sLine.Token(1),
                      sNewUsername = sLine.Token(2, true);

        if (sOldUsername.empty() || sNewUsername.empty()) {
            PutModule(t_s("Usage: CloneUser <old username> <new username>"));
            return;
        }

        CUser* pOldUser = CZNC::Get().FindUser(sOldUsername);

        if (!pOldUser) {
            PutModule(t_f("Error: User [{1}] does not exist!")(sOldUsername));
            return;
        }

        CUser* pNewUser = new CUser(sNewUsername);
        CString sError;
        if (!pNewUser->Clone(*pOldUser, sError)) {
            delete pNewUser;
            PutModule(t_f("Error: Cloning failed: {1}")(sError));
            return;
        }

        if (!CZNC::Get().AddUser(pNewUser, sError)) {
            delete pNewUser;
            PutModule(t_f("Error: User not added: {1}")(sError));
            return;
        }

        PutModule(t_f("User {1} added!")(sNewUsername));
        return;
    }

    void AddNetwork(const CString& sLine) {
        CString sUser = sLine.Token(1);
        CString sNetwork = sLine.Token(2);
        CUser* pUser = GetUser();

        if (sNetwork.empty()) {
            sNetwork = sUser;
        } else {
            pUser = FindUser(sUser);
            if (!pUser) {
                return;
            }
        }

        if (sNetwork.empty()) {
            PutModule(t_s("Usage: AddNetwork [user] network"));
            return;
        }

        if (!GetUser()->IsAdmin() && !pUser->HasSpaceForNewNetwork()) {
            PutStatus(
                t_s("Network number limit reached. Ask an admin to increase "
                    "the limit for you, or delete unneeded networks using /znc "
                    "DelNetwork <name>"));
            return;
        }

        if (pUser->FindNetwork(sNetwork)) {
            PutModule(
                t_f("Error: User {1} already has a network with the name {2}")(
                    pUser->GetUserName(), sNetwork));
            return;
        }

        CString sNetworkAddError;
        if (pUser->AddNetwork(sNetwork, sNetworkAddError)) {
            PutModule(t_f("Network {1} added to user {2}.")(
                sNetwork, pUser->GetUserName()));
        } else {
            PutModule(t_f(
                "Error: Network [{1}] could not be added for user {2}: {3}")(
                sNetwork, pUser->GetUserName(), sNetworkAddError));
        }
    }

    void DelNetwork(const CString& sLine) {
        CString sUser = sLine.Token(1);
        CString sNetwork = sLine.Token(2);
        CUser* pUser = GetUser();

        if (sNetwork.empty()) {
            sNetwork = sUser;
        } else {
            pUser = FindUser(sUser);
            if (!pUser) {
                return;
            }
        }

        if (sNetwork.empty()) {
            PutModule(t_s("Usage: DelNetwork [user] network"));
            return;
        }

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        if (pNetwork == CModule::GetNetwork()) {
            PutModule(t_f(
                "The currently active network can be deleted via {1}status")(
                GetUser()->GetStatusPrefix()));
            return;
        }

        if (pUser->DeleteNetwork(sNetwork)) {
            PutModule(t_f("Network {1} deleted for user {2}.")(
                sNetwork, pUser->GetUserName()));
        } else {
            PutModule(
                t_f("Error: Network {1} could not be deleted for user {2}.")(
                    sNetwork, pUser->GetUserName()));
        }
    }

    void ListNetworks(const CString& sLine) {
        CString sUser = sLine.Token(1);
        CUser* pUser = GetUser();

        if (!sUser.empty()) {
            pUser = FindUser(sUser);
            if (!pUser) {
                return;
            }
        }

        const vector<CIRCNetwork*>& vNetworks = pUser->GetNetworks();

        CTable Table;
        Table.AddColumn(t_s("Network", "listnetworks"));
        Table.AddColumn(t_s("OnIRC", "listnetworks"));
        Table.AddColumn(t_s("IRC Server", "listnetworks"));
        Table.AddColumn(t_s("IRC User", "listnetworks"));
        Table.AddColumn(t_s("Channels", "listnetworks"));

        for (const CIRCNetwork* pNetwork : vNetworks) {
            Table.AddRow();
            Table.SetCell(t_s("Network", "listnetworks"), pNetwork->GetName());
            if (pNetwork->IsIRCConnected()) {
                Table.SetCell(t_s("OnIRC", "listnetworks"), t_s("Yes"));
                Table.SetCell(t_s("IRC Server", "listnetworks"),
                              pNetwork->GetIRCServer());
                Table.SetCell(t_s("IRC User", "listnetworks"),
                              pNetwork->GetIRCNick().GetNickMask());
                Table.SetCell(t_s("Channels", "listnetworks"),
                              CString(pNetwork->GetChans().size()));
            } else {
                Table.SetCell(t_s("OnIRC", "listnetworks"), t_s("No"));
            }
        }

        if (PutModule(Table) == 0) {
            PutModule(t_s("No networks"));
        }
    }

    void AddServer(const CString& sLine) {
        CString sUsername = sLine.Token(1);
        CString sNetwork = sLine.Token(2);
        CString sServer = sLine.Token(3, true);

        if (sServer.empty()) {
            PutModule(
                t_s("Usage: AddServer <username> <network> <server> [[+]port] "
                    "[password]"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        if (pNetwork->AddServer(sServer))
            PutModule(t_f("Added IRC Server {1} to network {2} for user {3}.")(
                sServer, pNetwork->GetName(), pUser->GetUserName()));
        else
            PutModule(t_f(
                "Error: Could not add IRC server {1} to network {2} for user "
                "{3}.")(sServer, pNetwork->GetName(), pUser->GetUserName()));
    }

    void DelServer(const CString& sLine) {
        CString sUsername = sLine.Token(1);
        CString sNetwork = sLine.Token(2);
        CString sServer = sLine.Token(3, true);
        unsigned short uPort = sLine.Token(4).ToUShort();
        CString sPass = sLine.Token(5);

        if (sServer.empty()) {
            PutModule(
                t_s("Usage: DelServer <username> <network> <server> [[+]port] "
                    "[password]"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        if (pNetwork->DelServer(sServer, uPort, sPass))
            PutModule(
                t_f("Deleted IRC Server {1} from network {2} for user {3}.")(
                    sServer, pNetwork->GetName(), pUser->GetUserName()));
        else
            PutModule(
                t_f("Error: Could not delete IRC server {1} from network {2} "
                    "for user {3}.")(sServer, pNetwork->GetName(),
                                     pUser->GetUserName()));
    }

    void ReconnectUser(const CString& sLine) {
        CString sUserName = sLine.Token(1);
        CString sNetwork = sLine.Token(2);

        if (sNetwork.empty()) {
            PutModule(t_s("Usage: Reconnect <username> <network>"));
            return;
        }

        CUser* pUser = FindUser(sUserName);
        if (!pUser) {
            return;
        }

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        CIRCSock* pIRCSock = pNetwork->GetIRCSock();
        // cancel connection attempt:
        if (pIRCSock && !pIRCSock->IsConnected()) {
            pIRCSock->Close();
        }
        // or close existing connection:
        else if (pIRCSock) {
            pIRCSock->Quit();
        }

        // then reconnect
        pNetwork->SetIRCConnectEnabled(true);

        PutModule(t_f("Queued network {1} of user {2} for a reconnect.")(
            pNetwork->GetName(), pUser->GetUserName()));
    }

    void DisconnectUser(const CString& sLine) {
        CString sUserName = sLine.Token(1);
        CString sNetwork = sLine.Token(2);

        if (sNetwork.empty()) {
            PutModule(t_s("Usage: Disconnect <username> <network>"));
            return;
        }

        CUser* pUser = FindUser(sUserName);
        if (!pUser) {
            return;
        }

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        pNetwork->SetIRCConnectEnabled(false);
        PutModule(t_f("Closed IRC connection for network {1} of user {2}.")(
            pNetwork->GetName(), pUser->GetUserName()));
    }

    void ListCTCP(const CString& sLine) {
        CString sUserName = sLine.Token(1, true);

        if (sUserName.empty()) {
            sUserName = GetUser()->GetUserName();
        }
        CUser* pUser = FindUser(sUserName);
        if (!pUser) return;

        const MCString& msCTCPReplies = pUser->GetCTCPReplies();
        CTable Table;
        Table.AddColumn(t_s("Request", "listctcp"));
        Table.AddColumn(t_s("Reply", "listctcp"));
        for (const auto& it : msCTCPReplies) {
            Table.AddRow();
            Table.SetCell(t_s("Request", "listctcp"), it.first);
            Table.SetCell(t_s("Reply", "listctcp"), it.second);
        }

        if (Table.empty()) {
            PutModule(t_f("No CTCP replies for user {1} are configured")(
                pUser->GetUserName()));
        } else {
            PutModule(t_f("CTCP replies for user {1}:")(pUser->GetUserName()));
            PutModule(Table);
        }
    }

    void AddCTCP(const CString& sLine) {
        CString sUserName = sLine.Token(1);
        CString sCTCPRequest = sLine.Token(2);
        CString sCTCPReply = sLine.Token(3, true);

        if (sCTCPRequest.empty()) {
            sCTCPRequest = sUserName;
            sCTCPReply = sLine.Token(2, true);
            sUserName = GetUser()->GetUserName();
        }
        if (sCTCPRequest.empty()) {
            PutModule(t_s("Usage: AddCTCP [user] [request] [reply]"));
            PutModule(
                t_s("This will cause ZNC to reply to the CTCP instead of "
                    "forwarding it to clients."));
            PutModule(t_s(
                "An empty reply will cause the CTCP request to be blocked."));
            return;
        }

        CUser* pUser = FindUser(sUserName);
        if (!pUser) return;

        pUser->AddCTCPReply(sCTCPRequest, sCTCPReply);
        if (sCTCPReply.empty()) {
            PutModule(t_f("CTCP requests {1} to user {2} will now be blocked.")(
                sCTCPRequest.AsUpper(), pUser->GetUserName()));
        } else {
            PutModule(
                t_f("CTCP requests {1} to user {2} will now get reply: {3}")(
                    sCTCPRequest.AsUpper(), pUser->GetUserName(), sCTCPReply));
        }
    }

    void DelCTCP(const CString& sLine) {
        CString sUserName = sLine.Token(1);
        CString sCTCPRequest = sLine.Token(2, true);

        if (sCTCPRequest.empty()) {
            sCTCPRequest = sUserName;
            sUserName = GetUser()->GetUserName();
        }
        CUser* pUser = FindUser(sUserName);
        if (!pUser) return;

        if (sCTCPRequest.empty()) {
            PutModule(t_s("Usage: DelCTCP [user] [request]"));
            return;
        }

        if (pUser->DelCTCPReply(sCTCPRequest)) {
            PutModule(t_f(
                "CTCP requests {1} to user {2} will now be sent to IRC clients")(
                sCTCPRequest.AsUpper(), pUser->GetUserName()));
        } else {
            PutModule(
                t_f("CTCP requests {1} to user {2} will be sent to IRC clients "
                    "(nothing has changed)")(sCTCPRequest.AsUpper(),
                                             pUser->GetUserName()));
        }
    }

    void LoadModuleFor(CModules& Modules, const CString& sModName,
                       const CString& sArgs, CModInfo::EModuleType eType,
                       CUser* pUser, CIRCNetwork* pNetwork) {
        if (pUser->DenyLoadMod() && !GetUser()->IsAdmin()) {
            PutModule(t_s("Loading modules has been disabled."));
            return;
        }

        CString sModRet;
        CModule* pMod = Modules.FindModule(sModName);
        if (!pMod) {
            if (!Modules.LoadModule(sModName, sArgs, eType, pUser, pNetwork,
                                    sModRet)) {
                PutModule(t_f("Error: Unable to load module {1}: {2}")(
                    sModName, sModRet));
            } else {
                PutModule(t_f("Loaded module {1}")(sModName));
            }
        } else if (pMod->GetArgs() != sArgs) {
            if (!Modules.ReloadModule(sModName, sArgs, pUser, pNetwork,
                                      sModRet)) {
                PutModule(t_f("Error: Unable to reload module {1}: {2}")(
                    sModName, sModRet));
            } else {
                PutModule(t_f("Reloaded module {1}")(sModName));
            }
        } else {
            PutModule(
                t_f("Error: Unable to load module {1} because it is already "
                    "loaded")(sModName));
        }
    }

    void LoadModuleForUser(const CString& sLine) {
        CString sUsername = sLine.Token(1);
        CString sModName = sLine.Token(2);
        CString sArgs = sLine.Token(3, true);

        if (sModName.empty()) {
            PutModule(t_s("Usage: LoadModule <username> <modulename> [args]"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        LoadModuleFor(pUser->GetModules(), sModName, sArgs,
                      CModInfo::UserModule, pUser, nullptr);
    }

    void LoadModuleForNetwork(const CString& sLine) {
        CString sUsername = sLine.Token(1);
        CString sNetwork = sLine.Token(2);
        CString sModName = sLine.Token(3);
        CString sArgs = sLine.Token(4, true);

        if (sModName.empty()) {
            PutModule(
                t_s("Usage: LoadNetModule <username> <network> <modulename> "
                    "[args]"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        LoadModuleFor(pNetwork->GetModules(), sModName, sArgs,
                      CModInfo::NetworkModule, pUser, pNetwork);
    }

    void UnLoadModuleFor(CModules& Modules, const CString& sModName,
                         CUser* pUser) {
        if (pUser->DenyLoadMod() && !GetUser()->IsAdmin()) {
            PutModule(t_s("Loading modules has been disabled."));
            return;
        }

        if (Modules.FindModule(sModName) == this) {
            PutModule(t_f("Please use /znc unloadmod {1}")(sModName));
            return;
        }

        CString sModRet;
        if (!Modules.UnloadModule(sModName, sModRet)) {
            PutModule(t_f("Error: Unable to unload module {1}: {2}")(sModName,
                                                                     sModRet));
        } else {
            PutModule(t_f("Unloaded module {1}")(sModName));
        }
    }

    void UnLoadModuleForUser(const CString& sLine) {
        CString sUsername = sLine.Token(1);
        CString sModName = sLine.Token(2);

        if (sModName.empty()) {
            PutModule(t_s("Usage: UnloadModule <username> <modulename>"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        UnLoadModuleFor(pUser->GetModules(), sModName, pUser);
    }

    void UnLoadModuleForNetwork(const CString& sLine) {
        CString sUsername = sLine.Token(1);
        CString sNetwork = sLine.Token(2);
        CString sModName = sLine.Token(3);

        if (sModName.empty()) {
            PutModule(t_s(
                "Usage: UnloadNetModule <username> <network> <modulename>"));
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) {
            return;
        }

        UnLoadModuleFor(pNetwork->GetModules(), sModName, pUser);
    }

    void ListModulesFor(CModules& Modules) {
        CTable Table;
        Table.AddColumn(t_s("Name", "listmodules"));
        Table.AddColumn(t_s("Arguments", "listmodules"));

        for (const CModule* pMod : Modules) {
            Table.AddRow();
            Table.SetCell(t_s("Name", "listmodules"), pMod->GetModName());
            Table.SetCell(t_s("Arguments", "listmodules"), pMod->GetArgs());
        }

        PutModule(Table);
    }

    void ListModulesForUser(const CString& sLine) {
        CString sUsername = sLine.Token(1);

        if (sUsername.empty()) {
            PutModule("Usage: ListMods <username>");
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        if (pUser->GetModules().empty()) {
            PutModule(
                t_f("User {1} has no modules loaded.")(pUser->GetUserName()));
            return;
        }

        PutModule(t_f("Modules loaded for user {1}:")(pUser->GetUserName()));
        ListModulesFor(pUser->GetModules());
    }

    void ListModulesForNetwork(const CString& sLine) {
        CString sUsername = sLine.Token(1);
        CString sNetwork = sLine.Token(2);

        if (sNetwork.empty()) {
            PutModule("Usage: ListNetMods <username> <network>");
            return;
        }

        CUser* pUser = FindUser(sUsername);
        if (!pUser) return;

        CIRCNetwork* pNetwork = FindNetwork(pUser, sNetwork);
        if (!pNetwork) return;

        if (pNetwork->GetModules().empty()) {
            PutModule(t_f("Network {1} of user {2} has no modules loaded.")(
                pNetwork->GetName(), pUser->GetUserName()));
            return;
        }

        PutModule(t_f("Modules loaded for network {1} of user {2}:")(
            pNetwork->GetName(), pUser->GetUserName()));
        ListModulesFor(pNetwork->GetModules());
    }

  public:
    MODCONSTRUCTOR(CAdminMod) {
        AddCommand("Help", t_d("[command] [variable]"),
                   t_d("Prints help for matching commands and variables"),
                   [=](const CString& sLine) { PrintHelp(sLine); });
        AddCommand(
            "Get", t_d("<variable> [username]"),
            t_d("Prints the variable's value for the given or current user"),
            [=](const CString& sLine) { Get(sLine); });
        AddCommand("Set", t_d("<variable> <username> <value>"),
                   t_d("Sets the variable's value for the given user"),
                   [=](const CString& sLine) { Set(sLine); });
        AddCommand("GetNetwork", t_d("<variable> [username] [network]"),
                   t_d("Prints the variable's value for the given network"),
                   [=](const CString& sLine) { GetNetwork(sLine); });
        AddCommand("SetNetwork", t_d("<variable> <username> <network> <value>"),
                   t_d("Sets the variable's value for the given network"),
                   [=](const CString& sLine) { SetNetwork(sLine); });
        AddCommand("GetChan", t_d("<variable> [username] <network> <chan>"),
                   t_d("Prints the variable's value for the given channel"),
                   [=](const CString& sLine) { GetChan(sLine); });
        AddCommand("SetChan",
                   t_d("<variable> <username> <network> <chan> <value>"),
                   t_d("Sets the variable's value for the given channel"),
                   [=](const CString& sLine) { SetChan(sLine); });
        AddCommand("AddChan", t_d("<username> <network> <chan>"),
                   t_d("Adds a new channel"),
                   [=](const CString& sLine) { AddChan(sLine); });
        AddCommand("DelChan", t_d("<username> <network> <chan>"),
                   t_d("Deletes a channel"),
                   [=](const CString& sLine) { DelChan(sLine); });
        AddCommand("ListUsers", "", t_d("Lists users"),
                   [=](const CString& sLine) { ListUsers(sLine); });
        AddCommand("AddUser", t_d("<username> <password>"),
                   t_d("Adds a new user"),
                   [=](const CString& sLine) { AddUser(sLine); });
        AddCommand("DelUser", t_d("<username>"), t_d("Deletes a user"),
                   [=](const CString& sLine) { DelUser(sLine); });
        AddCommand("CloneUser", t_d("<old username> <new username>"),
                   t_d("Clones a user"),
                   [=](const CString& sLine) { CloneUser(sLine); });
        AddCommand("AddServer", t_d("<username> <network> <server>"),
                   t_d("Adds a new IRC server for the given or current user"),
                   [=](const CString& sLine) { AddServer(sLine); });
        AddCommand("DelServer", t_d("<username> <network> <server>"),
                   t_d("Deletes an IRC server from the given or current user"),
                   [=](const CString& sLine) { DelServer(sLine); });
        AddCommand("Reconnect", t_d("<username> <network>"),
                   t_d("Cycles the user's IRC server connection"),
                   [=](const CString& sLine) { ReconnectUser(sLine); });
        AddCommand("Disconnect", t_d("<username> <network>"),
                   t_d("Disconnects the user from their IRC server"),
                   [=](const CString& sLine) { DisconnectUser(sLine); });
        AddCommand("LoadModule", t_d("<username> <modulename> [args]"),
                   t_d("Loads a Module for a user"),
                   [=](const CString& sLine) { LoadModuleForUser(sLine); });
        AddCommand("UnLoadModule", t_d("<username> <modulename>"),
                   t_d("Removes a Module of a user"),
                   [=](const CString& sLine) { UnLoadModuleForUser(sLine); });
        AddCommand("ListMods", t_d("<username>"),
                   t_d("Get the list of modules for a user"),
                   [=](const CString& sLine) { ListModulesForUser(sLine); });
        AddCommand("LoadNetModule",
                   t_d("<username> <network> <modulename> [args]"),
                   t_d("Loads a Module for a network"),
                   [=](const CString& sLine) { LoadModuleForNetwork(sLine); });
        AddCommand(
            "UnLoadNetModule", t_d("<username> <network> <modulename>"),
            t_d("Removes a Module of a network"),
            [=](const CString& sLine) { UnLoadModuleForNetwork(sLine); });
        AddCommand("ListNetMods", t_d("<username> <network>"),
                   t_d("Get the list of modules for a network"),
                   [=](const CString& sLine) { ListModulesForNetwork(sLine); });
        AddCommand("ListCTCPs", t_d("<username>"),
                   t_d("List the configured CTCP replies"),
                   [=](const CString& sLine) { ListCTCP(sLine); });
        AddCommand("AddCTCP", t_d("<username> <ctcp> [reply]"),
                   t_d("Configure a new CTCP reply"),
                   [=](const CString& sLine) { AddCTCP(sLine); });
        AddCommand("DelCTCP", t_d("<username> <ctcp>"),
                   t_d("Remove a CTCP reply"),
                   [=](const CString& sLine) { DelCTCP(sLine); });

        // Network commands
        AddCommand("AddNetwork", t_d("[username] <network>"),
                   t_d("Add a network for a user"),
                   [=](const CString& sLine) { AddNetwork(sLine); });
        AddCommand("DelNetwork", t_d("[username] <network>"),
                   t_d("Delete a network for a user"),
                   [=](const CString& sLine) { DelNetwork(sLine); });
        AddCommand("ListNetworks", t_d("[username]"),
                   t_d("List all networks for a user"),
                   [=](const CString& sLine) { ListNetworks(sLine); });
    }

    ~CAdminMod() override {}
};

template <>
void TModInfo<CAdminMod>(CModInfo& Info) {
    Info.SetWikiPage("controlpanel");
}

USERMODULEDEFS(CAdminMod,
               t_s("Dynamic configuration through IRC. Allows editing only "
               "yourself if you're not ZNC admin."))