diff --git a/main.cpp b/main.cpp index 1e9759c8..6a603914 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,7 @@ static struct option g_LongOpts[] = { { "help", 0, NULL, 0 }, { "version", 0, NULL, 0 }, + { "makeconf", 0, NULL, 0 }, { "makepass", 0, NULL, 0 }, #ifdef HAVE_LIBSSL { "makepem", 0, NULL, 0 }, @@ -20,12 +21,13 @@ static struct option g_LongOpts[] = { void GenerateHelp(const char *appname) { CUtils::PrintMessage("USAGE: " + CString(appname) + " [options] [config]"); CUtils::PrintMessage("Options are:"); - CUtils::PrintMessage("\t--help"); - CUtils::PrintMessage("\t--version Output version information and exit"); - CUtils::PrintMessage("\t--makepass Generates a password for use in config"); + CUtils::PrintMessage("\t--help)"); + CUtils::PrintMessage("\t--version Output version information and exit"); + CUtils::PrintMessage("\t--makeconf Interactively create a new config"); + CUtils::PrintMessage("\t--makepass Generates a password for use in config"); #ifdef HAVE_LIBSSL - CUtils::PrintMessage("\t--makepem Generates a pemfile for use with SSL"); - CUtils::PrintMessage("\t--encrypt-pem Encrypts the pemfile"); + CUtils::PrintMessage("\t--makepem Generates a pemfile for use with SSL"); + CUtils::PrintMessage("\t--encrypt-pem Encrypts the pemfile"); #endif /* HAVE_LIBSSL */ } @@ -54,12 +56,13 @@ int main(int argc, char** argv) { #endif /* HAVE_LIBSSL */ int iArg, iOptIndex = -1; + bool bMakeConf = false; + bool bMakePass = false; #ifdef HAVE_LIBSSL bool bMakePem = false; bool bEncPem = false; #endif /* HAVE_LIBSSL */ - bool bMakePass = false; - while ((iArg = getopt_long(argc, argv, "h", g_LongOpts, &iOptIndex) != -1)) { + while ((iArg = getopt_long(argc, argv, "c|h", g_LongOpts, &iOptIndex) != -1)) { switch (iArg) { case 1: { // long options if (iOptIndex >= 0) { @@ -67,6 +70,8 @@ int main(int argc, char** argv) { if (sOption == "version") { cout << CZNC::GetTag() << endl; return 0; + } else if (sOption == "makeconf") { + bMakeConf = true; } else if (sOption == "makepass") { bMakePass = true; #ifdef HAVE_LIBSSL @@ -99,6 +104,13 @@ int main(int argc, char** argv) { sConfig = argv[optind]; } + if (bMakeConf) { + CZNC* pZNC = CZNC::New(); + pZNC->InitDirs(""); + pZNC->WriteNewConfig(sConfig); + return 0; + } + #ifdef HAVE_LIBSSL if (bMakePem) { CZNC* pZNC = CZNC::New(); diff --git a/znc.cpp b/znc.cpp index c56a351f..f535a395 100644 --- a/znc.cpp +++ b/znc.cpp @@ -270,10 +270,19 @@ CString CZNC::ExpandConfigPath(const CString& sConfigFile) { } bool CZNC::WriteNewConfig(const CString& sConfig) { + CString sConfigFile = ExpandConfigPath((sConfig.empty()) ? "znc.conf" : sConfig); CString sAnswer, sUser; vector vsLines; bool bAnswer = false; + if (CFile::Exists(sConfigFile)) { + if (!CUtils::GetBoolInput("This config already exists. Would you like to overwrite it?", false)) { + return false; + } + } + + CUtils::PrintMessage("Writing new config [" + sConfigFile + "]"); + CUtils::PrintMessage(""); CUtils::PrintMessage("First lets start with some global settings..."); CUtils::PrintMessage(""); @@ -460,21 +469,51 @@ bool CZNC::WriteNewConfig(const CString& sConfig) { } while (CUtils::GetBoolInput("Would you like to setup another user?", false)); // !User - CString sConfigFile = ExpandConfigPath(sConfig); CUtils::PrintAction("Writing config [" + sConfigFile + "]"); CFile File(sConfigFile); - if (!File.Open(O_WRONLY | O_CREAT, 0600)) { + bool bFileOpen = false; + + if (File.Open(O_WRONLY | O_CREAT, 0600)) { + bFileOpen = true; + } else { CUtils::PrintStatus(false, "Unable to open file"); - return false; + CUtils::GetInput("Alternate location", sConfigFile, "/tmp/" + sConfig); + + if (!CFile::Exists(sConfigFile) || CUtils::GetBoolInput("Would you like to overwrite the existing alt file", false)) { + CUtils::PrintAction("Writing to alt location [" + sConfigFile + "]"); + File.SetFileName(sConfigFile); + + if (File.Open(O_WRONLY | O_CREAT, 0600)) { + bFileOpen = true; + } else { + CUtils::PrintStatus(false, "Unable to open alt file"); + } + } + } + + if (!bFileOpen) { + CUtils::PrintMessage(""); + CUtils::PrintMessage("Printing new config to stdout since we were unable to open a file"); + CUtils::PrintMessage(""); + cout << endl << "----------------------------------------------------------------------------" << endl << endl; } for (unsigned int a = 0; a < vsLines.size(); a++) { - File.Write(vsLines[a] + "\n"); + if (bFileOpen) { + File.Write(vsLines[a] + "\n"); + } else { + cout << vsLines[a] << endl; + } } - File.Close(); - CUtils::PrintStatus(true); + if (bFileOpen) { + File.Close(); + + CUtils::PrintStatus(true); + } else { + cout << endl << "----------------------------------------------------------------------------" << endl << endl; + } CUtils::PrintMessage(""); CUtils::PrintMessage("To connect to this znc you need to connect to it as your irc server", true); @@ -496,12 +535,8 @@ bool CZNC::ParseConfig(const CString& sConfig) { if (!CFile::Exists(sConfigFile)) { CUtils::PrintStatus(false, "No such file"); - if (!CUtils::GetBoolInput("Would you like to create this config now?", true)) { - return false; - } - - WriteNewConfig(sConfigFile); - CUtils::PrintAction("Opening Config [" + sConfigFile + "]"); + CUtils::PrintMessage("Restart znc with the --makeconf option if you wish to create this config."); + return false; } if (!CFile::IsReg(sConfigFile)) {