-
Notifications
You must be signed in to change notification settings - Fork 1
/
znc.cpp
1891 lines (1533 loc) · 49.8 KB
/
znc.cpp
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
/*
* Copyright (C) 2004-2011 See the AUTHORS file for details.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/
#include "znc.h"
#include "Chan.h"
#include "FileUtils.h"
#include "IRCSock.h"
#include "Server.h"
#include "User.h"
#include "IRCNetwork.h"
#include "Listener.h"
#include "Config.h"
#include <list>
static inline CString FormatBindError() {
CString sError = (errno == 0 ? CString("unknown error, check the host name") : CString(strerror(errno)));
return "Unable to bind [" + sError + "]";
}
CZNC::CZNC() {
if (!InitCsocket()) {
CUtils::PrintError("Could not initialize Csocket!");
exit(-1);
}
m_pModules = new CModules();
m_uiConnectDelay = 5;
m_uiAnonIPLimit = 10;
m_uBytesRead = 0;
m_uBytesWritten = 0;
m_uiMaxBufferSize = 500;
m_pConnectUserTimer = NULL;
m_eConfigState = ECONFIG_NOTHING;
m_TimeStarted = time(NULL);
m_sConnectThrottle.SetTTL(30000);
m_pLockFile = NULL;
m_bProtectWebSessions = true;
}
CZNC::~CZNC() {
m_pModules->UnloadAll();
for (map<CString,CUser*>::iterator a = m_msUsers.begin(); a != m_msUsers.end(); ++a) {
a->second->GetModules().UnloadAll();
}
for (size_t b = 0; b < m_vpListeners.size(); b++) {
delete m_vpListeners[b];
}
for (map<CString,CUser*>::iterator a = m_msUsers.begin(); a != m_msUsers.end(); ++a) {
a->second->SetBeingDeleted(true);
}
m_pConnectUserTimer = NULL;
// This deletes m_pConnectUserTimer
m_Manager.Cleanup();
DeleteUsers();
delete m_pModules;
delete m_pLockFile;
ShutdownCsocket();
DeletePidFile();
}
CString CZNC::GetVersion() {
char szBuf[128];
snprintf(szBuf, sizeof(szBuf), "%1.3f"VERSION_EXTRA, VERSION);
// If snprintf overflows (which I doubt), we want to be on the safe side
szBuf[sizeof(szBuf) - 1] = '\0';
return szBuf;
}
CString CZNC::GetTag(bool bIncludeVersion) {
if (!bIncludeVersion) {
return "ZNC - http://znc.in";
}
char szBuf[128];
snprintf(szBuf, sizeof(szBuf), "ZNC %1.3f"VERSION_EXTRA" - http://znc.in", VERSION);
// If snprintf overflows (which I doubt), we want to be on the safe side
szBuf[sizeof(szBuf) - 1] = '\0';
return szBuf;
}
CString CZNC::GetUptime() const {
time_t now = time(NULL);
return CString::ToTimeStr(now - TimeStarted());
}
bool CZNC::OnBoot() {
ALLMODULECALL(OnBoot(), return false);
return true;
}
bool CZNC::ConnectNetwork(CIRCNetwork *pNetwork) {
CUser *pUser = pNetwork->GetUser();
CString sSockName = "IRC::" + pUser->GetUserName() + "::" + pNetwork->GetName();
CIRCSock* pIRCSock = pNetwork->GetIRCSock();
if (!pUser->GetIRCConnectEnabled())
return false;
if (pIRCSock || !pNetwork->HasServers())
return false;
CServer* pServer = pNetwork->GetNextServer();
if (!pServer)
return false;
if (m_sConnectThrottle.GetItem(pServer->GetName()))
return false;
m_sConnectThrottle.AddItem(pServer->GetName());
DEBUG("User [" << pUser->GetUserName() << "] is connecting to [" << pServer->GetString(false) << "] on network [" << pNetwork->GetName() << "]");
pNetwork->PutStatus("Attempting to connect to [" + pServer->GetString(false) + "] ...");
pIRCSock = new CIRCSock(pNetwork);
pIRCSock->SetPass(pServer->GetPass());
bool bSSL = false;
#ifdef HAVE_LIBSSL
if (pServer->IsSSL()) {
bSSL = true;
}
#endif
NETWORKMODULECALL(OnIRCConnecting(pIRCSock), pUser, pNetwork, NULL,
DEBUG("Some module aborted the connection attempt");
pUser->PutStatus("Some module aborted the connection attempt");
delete pIRCSock;
return false;
);
if (!m_Manager.Connect(pServer->GetName(), pServer->GetPort(), sSockName, 120, bSSL, pUser->GetBindHost(), pIRCSock)) {
pNetwork->PutStatus("Unable to connect. (Bad host?)");
}
return true;
}
bool CZNC::HandleUserDeletion()
{
map<CString, CUser*>::iterator it;
map<CString, CUser*>::iterator end;
if (m_msDelUsers.empty())
return false;
end = m_msDelUsers.end();
for (it = m_msDelUsers.begin(); it != end; ++it) {
CUser* pUser = it->second;
pUser->SetBeingDeleted(true);
if (GetModules().OnDeleteUser(*pUser)) {
pUser->SetBeingDeleted(false);
continue;
}
m_msUsers.erase(pUser->GetUserName());
CWebSock::FinishUserSessions(*pUser);
delete pUser;
}
m_msDelUsers.clear();
return true;
}
void CZNC::Loop() {
while (true) {
CString sError;
switch (GetConfigState()) {
case ECONFIG_NEED_REHASH:
SetConfigState(ECONFIG_NOTHING);
if (RehashConfig(sError)) {
Broadcast("Rehashing succeeded", true);
} else {
Broadcast("Rehashing failed: " + sError, true);
Broadcast("ZNC is in some possibly inconsistent state!", true);
}
break;
case ECONFIG_NEED_WRITE:
SetConfigState(ECONFIG_NOTHING);
if (WriteConfig()) {
Broadcast("Writing the config succeeded", true);
} else {
Broadcast("Writing the config file failed", true);
}
break;
case ECONFIG_NOTHING:
break;
}
// Check for users that need to be deleted
if (HandleUserDeletion()) {
// Also remove those user(s) from the config file
WriteConfig();
}
// Csocket wants micro seconds
// 500 msec to 600 sec
m_Manager.DynamicSelectLoop(500 * 1000, 600 * 1000 * 1000);
}
}
CFile* CZNC::InitPidFile() {
if (!m_sPidFile.empty()) {
CString sFile;
// absolute path or relative to the data dir?
if (m_sPidFile[0] != '/')
sFile = GetZNCPath() + "/" + m_sPidFile;
else
sFile = m_sPidFile;
return new CFile(sFile);
}
return NULL;
}
bool CZNC::WritePidFile(int iPid) {
CFile* File = InitPidFile();
if (File == NULL)
return false;
CUtils::PrintAction("Writing pid file [" + File->GetLongName() + "]");
bool bRet = false;
if (File->Open(O_WRONLY | O_TRUNC | O_CREAT)) {
File->Write(CString(iPid) + "\n");
File->Close();
bRet = true;
}
delete File;
CUtils::PrintStatus(bRet);
return bRet;
}
bool CZNC::DeletePidFile() {
CFile* File = InitPidFile();
if (File == NULL)
return false;
CUtils::PrintAction("Deleting pid file [" + File->GetLongName() + "]");
bool bRet = File->Delete();
delete File;
CUtils::PrintStatus(bRet);
return bRet;
}
bool CZNC::WritePemFile() {
#ifndef HAVE_LIBSSL
CUtils::PrintError("ZNC was not compiled with ssl support.");
return false;
#else
CString sPemFile = GetPemLocation();
CUtils::PrintAction("Writing Pem file [" + sPemFile + "]");
FILE *f = fopen(sPemFile.c_str(), "w");
if (!f) {
CUtils::PrintStatus(false, "Unable to open");
return false;
}
CUtils::GenerateCert(f, "");
fclose(f);
CUtils::PrintStatus(true);
return true;
#endif
}
void CZNC::DeleteUsers() {
for (map<CString,CUser*>::iterator a = m_msUsers.begin(); a != m_msUsers.end(); ++a) {
a->second->SetBeingDeleted(true);
delete a->second;
}
m_msUsers.clear();
DisableConnectUser();
}
bool CZNC::IsHostAllowed(const CString& sHostMask) const {
for (map<CString,CUser*>::const_iterator a = m_msUsers.begin(); a != m_msUsers.end(); ++a) {
if (a->second->IsHostAllowed(sHostMask)) {
return true;
}
}
return false;
}
bool CZNC::AllowConnectionFrom(const CString& sIP) const {
if (m_uiAnonIPLimit == 0)
return true;
return (GetManager().GetAnonConnectionCount(sIP) < m_uiAnonIPLimit);
}
void CZNC::InitDirs(const CString& sArgvPath, const CString& sDataDir) {
// If the bin was not ran from the current directory, we need to add that dir onto our cwd
CString::size_type uPos = sArgvPath.rfind('/');
if (uPos == CString::npos)
m_sCurPath = "./";
else
m_sCurPath = CDir::ChangeDir("./", sArgvPath.Left(uPos), "");
// Try to set the user's home dir, default to binpath on failure
CFile::InitHomePath(m_sCurPath);
if (sDataDir.empty()) {
m_sZNCPath = CFile::GetHomePath() + "/.znc";
} else {
m_sZNCPath = sDataDir;
}
m_sSSLCertFile = m_sZNCPath + "/znc.pem";
}
CString CZNC::GetConfPath(bool bAllowMkDir) const {
CString sConfPath = m_sZNCPath + "/configs";
if (bAllowMkDir && !CFile::Exists(sConfPath)) {
CDir::MakeDir(sConfPath);
}
return sConfPath;
}
CString CZNC::GetUserPath() const {
CString sUserPath = m_sZNCPath + "/users";
if (!CFile::Exists(sUserPath)) {
CDir::MakeDir(sUserPath);
}
return sUserPath;
}
CString CZNC::GetModPath() const {
CString sModPath = m_sZNCPath + "/modules";
if (!CFile::Exists(sModPath)) {
CDir::MakeDir(sModPath);
}
return sModPath;
}
const CString& CZNC::GetCurPath() const {
if (!CFile::Exists(m_sCurPath)) {
CDir::MakeDir(m_sCurPath);
}
return m_sCurPath;
}
const CString& CZNC::GetHomePath() const {
return CFile::GetHomePath();
}
const CString& CZNC::GetZNCPath() const {
if (!CFile::Exists(m_sZNCPath)) {
CDir::MakeDir(m_sZNCPath);
}
return m_sZNCPath;
}
CString CZNC::GetPemLocation() const {
return CDir::ChangeDir("", m_sSSLCertFile);
}
CString CZNC::ExpandConfigPath(const CString& sConfigFile, bool bAllowMkDir) {
CString sRetPath;
if (sConfigFile.empty()) {
sRetPath = GetConfPath(bAllowMkDir) + "/znc.conf";
} else {
if (sConfigFile.Left(2) == "./" || sConfigFile.Left(3) == "../") {
sRetPath = GetCurPath() + "/" + sConfigFile;
} else if (sConfigFile.Left(1) != "/") {
sRetPath = GetConfPath(bAllowMkDir) + "/" + sConfigFile;
} else {
sRetPath = sConfigFile;
}
}
return sRetPath;
}
bool CZNC::WriteConfig() {
if (GetConfigFile().empty()) {
return false;
}
// We first write to a temporary file and then move it to the right place
CFile *pFile = new CFile(GetConfigFile() + "~");
if (!pFile->Open(O_WRONLY | O_CREAT | O_TRUNC, 0600)) {
delete pFile;
return false;
}
// We have to "transfer" our lock on the config to the new file.
// The old file (= inode) is going away and thus a lock on it would be
// useless. These lock should always succeed (races, anyone?).
if (!pFile->TryExLock()) {
pFile->Delete();
delete pFile;
return false;
}
pFile->Write(MakeConfigHeader() + "\n");
CConfig config;
config.AddKeyValuePair("AnonIPLimit", CString(m_uiAnonIPLimit));
config.AddKeyValuePair("MaxBufferSize", CString(m_uiMaxBufferSize));
config.AddKeyValuePair("SSLCertFile", CString(m_sSSLCertFile));
config.AddKeyValuePair("ProtectWebSessions", CString(m_bProtectWebSessions));
config.AddKeyValuePair("Version", CString(VERSION, 3));
for (size_t l = 0; l < m_vpListeners.size(); l++) {
CListener* pListener = m_vpListeners[l];
CConfig listenerConfig;
listenerConfig.AddKeyValuePair("Host", pListener->GetBindHost());
listenerConfig.AddKeyValuePair("Port", CString(pListener->GetPort()));
listenerConfig.AddKeyValuePair("IPv4", CString(pListener->GetAddrType() != ADDR_IPV6ONLY));
listenerConfig.AddKeyValuePair("IPv6", CString(pListener->GetAddrType() != ADDR_IPV4ONLY));
listenerConfig.AddKeyValuePair("SSL", CString(pListener->IsSSL()));
listenerConfig.AddKeyValuePair("AllowIRC", CString(pListener->GetAcceptType() != CListener::ACCEPT_HTTP));
listenerConfig.AddKeyValuePair("AllowWeb", CString(pListener->GetAcceptType() != CListener::ACCEPT_IRC));
config.AddSubConfig("Listener", "listener" + CString(l), listenerConfig);
}
config.AddKeyValuePair("ConnectDelay", CString(m_uiConnectDelay));
config.AddKeyValuePair("ServerThrottle", CString(m_sConnectThrottle.GetTTL()/1000));
if (!m_sPidFile.empty()) {
config.AddKeyValuePair("PidFile", m_sPidFile.FirstLine());
}
if (!m_sSkinName.empty()) {
config.AddKeyValuePair("Skin", m_sSkinName.FirstLine());
}
if (!m_sStatusPrefix.empty()) {
config.AddKeyValuePair("StatusPrefix", m_sStatusPrefix.FirstLine());
}
for (unsigned int m = 0; m < m_vsMotd.size(); m++) {
config.AddKeyValuePair("Motd", m_vsMotd[m].FirstLine());
}
for (unsigned int v = 0; v < m_vsBindHosts.size(); v++) {
config.AddKeyValuePair("BindHost", m_vsBindHosts[v].FirstLine());
}
CModules& Mods = GetModules();
for (unsigned int a = 0; a < Mods.size(); a++) {
CString sName = Mods[a]->GetModName();
CString sArgs = Mods[a]->GetArgs();
if (!sArgs.empty()) {
sArgs = " " + sArgs.FirstLine();
}
config.AddKeyValuePair("LoadModule", sName.FirstLine() + sArgs);
}
for (map<CString,CUser*>::iterator it = m_msUsers.begin(); it != m_msUsers.end(); ++it) {
CString sErr;
if (!it->second->IsValid(sErr)) {
DEBUG("** Error writing config for user [" << it->first << "] [" << sErr << "]");
continue;
}
config.AddSubConfig("User", it->second->GetUserName(), it->second->ToConfig());
}
config.Write(*pFile);
// If Sync() fails... well, let's hope nothing important breaks..
pFile->Sync();
if (pFile->HadError()) {
DEBUG("Error while writing the config, errno says: " + CString(strerror(errno)));
pFile->Delete();
delete pFile;
return false;
}
// We wrote to a temporary name, move it to the right place
if (!pFile->Move(GetConfigFile(), true)) {
DEBUG("Error while replacing the config file with a new version");
pFile->Delete();
delete pFile;
return false;
}
// Everything went fine, just need to update the saved path.
pFile->SetFileName(GetConfigFile());
// Make sure the lock is kept alive as long as we need it.
delete m_pLockFile;
m_pLockFile = pFile;
return true;
}
CString CZNC::MakeConfigHeader() {
return
"// WARNING\n"
"//\n"
"// Do NOT edit this file while ZNC is running!\n"
"// Use webadmin or *admin instead.\n"
"//\n"
"// Buf if you feel risky, you might want to read help on /znc saveconfig and /znc rehash.\n"
"// Also check http://en.znc.in/wiki/Configuration\n";
}
bool CZNC::WriteNewConfig(const CString& sConfigFile) {
CString sAnswer, sUser, sNetwork;
VCString vsLines;
vsLines.push_back(MakeConfigHeader());
m_sConfigFile = ExpandConfigPath(sConfigFile);
CUtils::PrintMessage("Building new config");
CUtils::PrintMessage("");
CUtils::PrintMessage("First let's start with some global settings...");
CUtils::PrintMessage("");
// Listen
#ifdef HAVE_IPV6
bool b6 = true;
#else
bool b6 = false;
#endif
CString sListenHost;
bool bListenSSL = false;
unsigned int uListenPort = 0;
bool bSuccess;
do {
bSuccess = true;
while (!CUtils::GetNumInput("What port would you like ZNC to listen on?", uListenPort, 1, 65535)) ;
#ifdef HAVE_LIBSSL
if (CUtils::GetBoolInput("Would you like ZNC to listen using SSL?", bListenSSL)) {
bListenSSL = true;
CString sPemFile = GetPemLocation();
if (!CFile::Exists(sPemFile)) {
CUtils::PrintError("Unable to locate pem file: [" + sPemFile + "]");
if (CUtils::GetBoolInput("Would you like to create a new pem file now?",
true)) {
WritePemFile();
}
}
} else
bListenSSL = false;
#endif
#ifdef HAVE_IPV6
b6 = CUtils::GetBoolInput("Would you like ZNC to listen using ipv6?", b6);
#endif
CUtils::GetInput("Listen Host", sListenHost, sListenHost, "Blank for all ips");
CUtils::PrintAction("Verifying the listener");
CListener* pListener = new CListener(uListenPort, sListenHost, bListenSSL,
b6 ? ADDR_ALL : ADDR_IPV4ONLY, CListener::ACCEPT_ALL);
if (!pListener->Listen()) {
CUtils::PrintStatus(false, FormatBindError());
bSuccess = false;
} else
CUtils::PrintStatus(true);
delete pListener;
} while (!bSuccess);
vsLines.push_back("<Listener l>");
vsLines.push_back("\tPort = " + CString(uListenPort));
vsLines.push_back("\tIPv4 = true");
vsLines.push_back("\tIPv6 = " + CString(b6));
vsLines.push_back("\tSSL = " + CString(bListenSSL));
if (!sListenHost.empty()) {
vsLines.push_back("\tHost = " + sListenHost);
}
vsLines.push_back("</Listener>");
// !Listen
set<CModInfo> ssGlobalMods;
GetModules().GetAvailableMods(ssGlobalMods, CModInfo::GlobalModule);
size_t uNrOtherGlobalMods = FilterUncommonModules(ssGlobalMods);
if (!ssGlobalMods.empty()) {
CUtils::PrintMessage("");
CUtils::PrintMessage("-- Global Modules --");
CUtils::PrintMessage("");
CTable Table;
Table.AddColumn("Name");
Table.AddColumn("Description");
set<CModInfo>::iterator it;
for (it = ssGlobalMods.begin(); it != ssGlobalMods.end(); ++it) {
const CModInfo& Info = *it;
Table.AddRow();
Table.SetCell("Name", Info.GetName());
Table.SetCell("Description", Info.GetDescription().Ellipsize(128));
}
unsigned int uTableIdx = 0; CString sLine;
while (Table.GetLine(uTableIdx++, sLine)) {
CUtils::PrintMessage(sLine);
}
if (uNrOtherGlobalMods > 0) {
CUtils::PrintMessage("And " + CString(uNrOtherGlobalMods) + " other (uncommon) modules. You can enable those later.");
}
CUtils::PrintMessage("");
for (it = ssGlobalMods.begin(); it != ssGlobalMods.end(); ++it) {
const CModInfo& Info = *it;
CString sName = Info.GetName();
if (CDebug::StdoutIsTTY()) {
if (CUtils::GetBoolInput("Load global module <\033[1m" + sName + "\033[22m>?", false))
vsLines.push_back("LoadModule = " + sName);
} else {
if (CUtils::GetBoolInput("Load global module <" + sName + ">?", false))
vsLines.push_back("LoadModule = " + sName);
}
}
}
// User
CUtils::PrintMessage("");
CUtils::PrintMessage("Now we need to set up a user...");
CUtils::PrintMessage("");
bool bFirstUser = true;
do {
vsLines.push_back("");
CString sNick;
do {
CUtils::GetInput("Username", sUser, "", "AlphaNumeric");
} while (!CUser::IsValidUserName(sUser));
vsLines.push_back("<User " + sUser + ">");
CString sSalt;
sAnswer = CUtils::GetSaltedHashPass(sSalt);
vsLines.push_back("\tPass = " + CUtils::sDefaultHash + "#" + sAnswer + "#" + sSalt + "#");
if (CUtils::GetBoolInput("Would you like this user to be an admin?", bFirstUser)) {
vsLines.push_back("\tAdmin = true");
} else {
vsLines.push_back("\tAdmin = false");
}
CUtils::GetInput("Nick", sNick, CUser::MakeCleanUserName(sUser));
vsLines.push_back("\tNick = " + sNick);
CUtils::GetInput("Alt Nick", sAnswer, sNick + "_");
if (!sAnswer.empty()) {
vsLines.push_back("\tAltNick = " + sAnswer);
}
CUtils::GetInput("Ident", sAnswer, sNick);
vsLines.push_back("\tIdent = " + sAnswer);
CUtils::GetInput("Real Name", sAnswer, "Got ZNC?");
vsLines.push_back("\tRealName = " + sAnswer);
CUtils::GetInput("Bind Host", sAnswer, "", "optional");
if (!sAnswer.empty()) {
vsLines.push_back("\tBindHost = " + sAnswer);
}
// todo: Possibly add motd
unsigned int uBufferCount = 0;
CUtils::GetNumInput("Number of lines to buffer per channel", uBufferCount, 0, ~0, 50);
if (uBufferCount) {
vsLines.push_back("\tBuffer = " + CString(uBufferCount));
}
if (CUtils::GetBoolInput("Would you like to keep buffers after replay?", false)) {
vsLines.push_back("\tKeepBuffer = true");
} else {
vsLines.push_back("\tKeepBuffer = false");
}
CUtils::GetInput("Default channel modes", sAnswer, "+stn");
if (!sAnswer.empty()) {
vsLines.push_back("\tChanModes = " + sAnswer);
}
set<CModInfo> ssUserMods;
GetModules().GetAvailableMods(ssUserMods);
size_t uNrOtherUserMods = FilterUncommonModules(ssUserMods);
if (ssUserMods.size()) {
vsLines.push_back("");
CUtils::PrintMessage("");
CUtils::PrintMessage("-- User Modules --");
CUtils::PrintMessage("");
CTable Table;
Table.AddColumn("Name");
Table.AddColumn("Description");
set<CModInfo>::iterator it;
for (it = ssUserMods.begin(); it != ssUserMods.end(); ++it) {
const CModInfo& Info = *it;
Table.AddRow();
Table.SetCell("Name", Info.GetName());
Table.SetCell("Description", Info.GetDescription().Ellipsize(128));
}
unsigned int uTableIdx = 0; CString sLine;
while (Table.GetLine(uTableIdx++, sLine)) {
CUtils::PrintMessage(sLine);
}
if (uNrOtherUserMods > 0) {
CUtils::PrintMessage("And " + CString(uNrOtherUserMods) + " other (uncommon) modules. You can enable those later.");
}
CUtils::PrintMessage("");
for (it = ssUserMods.begin(); it != ssUserMods.end(); ++it) {
const CModInfo& Info = *it;
CString sName = Info.GetName();
if (CDebug::StdoutIsTTY()) {
if (CUtils::GetBoolInput("Load module <\033[1m" + sName + "\033[22m>?", false))
vsLines.push_back("\tLoadModule = " + sName);
} else {
if (CUtils::GetBoolInput("Load module <" + sName + ">?", false))
vsLines.push_back("\tLoadModule = " + sName);
}
}
}
CUtils::PrintMessage("");
CString sAAnother = "a";
while (CUtils::GetBoolInput("Would you like to set up " + sAAnother + " network?", false)) {
sAAnother = "another";
vsLines.push_back("");
do {
CUtils::GetInput("Network", sNetwork, "");
} while (!CIRCNetwork::IsValidNetwork(sNetwork));
vsLines.push_back("\t<Network " + sNetwork + ">");
set<CModInfo> ssNetworkMods;
GetModules().GetAvailableMods(ssNetworkMods, CModInfo::NetworkModule);
size_t uNrOtherNetworkMods = FilterUncommonModules(ssNetworkMods);
if (ssNetworkMods.size()) {
CUtils::PrintMessage("");
CUtils::PrintMessage("-- Network Modules --");
CUtils::PrintMessage("");
CTable Table;
Table.AddColumn("Name");
Table.AddColumn("Description");
set<CModInfo>::iterator it;
for (it = ssNetworkMods.begin(); it != ssNetworkMods.end(); ++it) {
const CModInfo& Info = *it;
Table.AddRow();
Table.SetCell("Name", Info.GetName());
Table.SetCell("Description", Info.GetDescription().Ellipsize(128));
}
unsigned int uTableIdx = 0; CString sLine;
while (Table.GetLine(uTableIdx++, sLine)) {
CUtils::PrintMessage(sLine);
}
if (uNrOtherNetworkMods > 0) {
CUtils::PrintMessage("And " + CString(uNrOtherNetworkMods) + " other (uncommon) modules. You can enable those later.");
}
CUtils::PrintMessage("");
for (it = ssNetworkMods.begin(); it != ssNetworkMods.end(); ++it) {
const CModInfo& Info = *it;
CString sName = Info.GetName();
if (CDebug::StdoutIsTTY()) {
if (CUtils::GetBoolInput("Load module <\033[1m" + sName + "\033[22m>?", false))
vsLines.push_back("\t\tLoadModule = " + sName);
} else {
if (CUtils::GetBoolInput("Load module <" + sName + ">?", false))
vsLines.push_back("\t\tLoadModule = " + sName);
}
}
}
vsLines.push_back("");
CUtils::PrintMessage("");
CUtils::PrintMessage("-- IRC Servers --");
CUtils::PrintMessage("Only add servers from the same IRC network.");
CUtils::PrintMessage("If a server from the list can't be reached, another server will be used.");
CUtils::PrintMessage("");
do {
CString sHost, sPass;
bool bSSL = false;
unsigned int uServerPort = 0;
while (!CUtils::GetInput("IRC server", sHost, "", "host only") || !CServer::IsValidHostName(sHost)) ;
while (!CUtils::GetNumInput("[" + sHost + "] Port", uServerPort, 1, 65535, 6667)) ;
CUtils::GetInput("[" + sHost + "] Password (probably empty)", sPass);
#ifdef HAVE_LIBSSL
bSSL = CUtils::GetBoolInput("Does this server use SSL?", false);
#endif
vsLines.push_back("\t\tServer = " + sHost + ((bSSL) ? " +" : " ") + CString(uServerPort) + " " + sPass);
CUtils::PrintMessage("");
} while (CUtils::GetBoolInput("Would you like to add another server for this IRC network?", false));
vsLines.push_back("");
CUtils::PrintMessage("");
CUtils::PrintMessage("-- Channels --");
CUtils::PrintMessage("");
CString sArg = "a";
CString sPost = " for ZNC to automatically join?";
bool bDefault = true;
while (CUtils::GetBoolInput("Would you like to add " + sArg + " channel" + sPost, bDefault)) {
while (!CUtils::GetInput("Channel name", sAnswer)) ;
vsLines.push_back("\t\t<Chan " + sAnswer + ">");
vsLines.push_back("\t\t</Chan>");
sArg = "another";
sPost = "?";
bDefault = false;
}
vsLines.push_back("\t</Network>");
}
vsLines.push_back("</User>");
CUtils::PrintMessage("");
bFirstUser = false;
} while (CUtils::GetBoolInput("Would you like to set up another user?", false));
// !User
CFile File;
bool bFileOK, bFileOpen = false;
do {
CUtils::PrintAction("Writing config [" + m_sConfigFile + "]");
bFileOK = true;
if (CFile::Exists(m_sConfigFile)) {
if (!File.TryExLock(m_sConfigFile)) {
CUtils::PrintStatus(false, "ZNC is currently running on this config.");
bFileOK = false;
} else {
File.Close();
CUtils::PrintStatus(false, "This config already exists.");
if (CUtils::GetBoolInput("Would you like to overwrite it?", false))
CUtils::PrintAction("Overwriting config [" + m_sConfigFile + "]");
else
bFileOK = false;
}
}
if (bFileOK) {
File.SetFileName(m_sConfigFile);
if (File.Open(O_WRONLY | O_CREAT | O_TRUNC, 0600)) {
bFileOpen = true;
} else {
CUtils::PrintStatus(false, "Unable to open file");
bFileOK = false;
}
}
if (!bFileOK) {
CUtils::GetInput("Please specify an alternate location (or \"stdout\" for displaying the config)", m_sConfigFile, m_sConfigFile);
if (m_sConfigFile.Equals("stdout"))
bFileOK = true;
else
m_sConfigFile = ExpandConfigPath(m_sConfigFile);
}
} while (!bFileOK);
if (!bFileOpen) {
CUtils::PrintMessage("");
CUtils::PrintMessage("Printing the new config to stdout:");
CUtils::PrintMessage("");
cout << endl << "----------------------------------------------------------------------------" << endl << endl;
}
for (unsigned int a = 0; a < vsLines.size(); a++) {
if (bFileOpen) {
File.Write(vsLines[a] + "\n");
} else {
cout << vsLines[a] << endl;
}
}
if (bFileOpen) {
File.Close();
if (File.HadError())
CUtils::PrintStatus(false, "There was an error while writing the config");
else
CUtils::PrintStatus(true);
} else {
cout << endl << "----------------------------------------------------------------------------" << endl << endl;
}
if (File.HadError()) {
bFileOpen = false;
CUtils::PrintMessage("Printing the new config to stdout instead:");
cout << endl << "----------------------------------------------------------------------------" << endl << endl;
for (unsigned int a = 0; a < vsLines.size(); a++) {
cout << vsLines[a] << endl;
}
cout << endl << "----------------------------------------------------------------------------" << endl << endl;
}
const CString sProtocol(bListenSSL ? "https" : "http");
const CString sSSL(bListenSSL ? "+" : "");
CUtils::PrintMessage("");
CUtils::PrintMessage("To connect to this ZNC you need to connect to it as your IRC server", true);
CUtils::PrintMessage("using the port that you supplied. You have to supply your login info", true);
CUtils::PrintMessage("as the IRC server password like this: user:pass.", true);
CUtils::PrintMessage("");
CUtils::PrintMessage("Try something like this in your IRC client...", true);
CUtils::PrintMessage("/server <znc_server_ip> " + sSSL + CString(uListenPort) + " " + sUser + ":<pass>", true);
CUtils::PrintMessage("And this in your browser...", true);
CUtils::PrintMessage(sProtocol + "://<znc_server_ip>:" + CString(uListenPort) + "/", true);
CUtils::PrintMessage("");
File.UnLock();
return bFileOpen && CUtils::GetBoolInput("Launch ZNC now?", true);
}
size_t CZNC::FilterUncommonModules(set<CModInfo>& ssModules) {
const char* ns[] = { "webadmin", "admin",
"chansaver", "keepnick", "simple_away", "partyline",
"kickrejoin", "nickserv", "perform" };
const set<CString> ssNames(ns, ns + sizeof(ns) / sizeof(ns[0]));
size_t uNrRemoved = 0;
for(set<CModInfo>::iterator it = ssModules.begin(); it != ssModules.end(); ) {
if(ssNames.count(it->GetName()) > 0) {
it++;
} else {
set<CModInfo>::iterator it2 = it++;
ssModules.erase(it2);
uNrRemoved++;
}
}
return uNrRemoved;
}
void CZNC::BackupConfigOnce(const CString& sSuffix) {
static bool didBackup = false;
if (didBackup)
return;
didBackup = true;
CUtils::PrintAction("Creating a config backup");
CString sBackup = CDir::ChangeDir(m_sConfigFile, "../znc.conf." + sSuffix);
if (CFile::Copy(m_sConfigFile, sBackup))
CUtils::PrintStatus(true, sBackup);
else
CUtils::PrintStatus(false, strerror(errno));