From a9d08422f5ee3aa99429b264e9424309615e813c Mon Sep 17 00:00:00 2001 From: prozacx Date: Sun, 17 Apr 2005 19:05:06 +0000 Subject: [PATCH] Added colorful printing functions git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@139 726aef4b-f618-498e-8847-2d620e286838 --- Utils.cpp | 29 ++++++++++++++++++++++++++++ Utils.h | 4 ++++ main.cpp | 57 +++++++++++++++++++++++++++++++++++-------------------- znc.cpp | 54 +++++++++++++++++++++++++++++----------------------- 4 files changed, 99 insertions(+), 45 deletions(-) diff --git a/Utils.cpp b/Utils.cpp index c3982598..19d16039 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -213,6 +213,35 @@ int CUtils::MakeDir(const string& sPath, mode_t iMode) { return -1; } +void CUtils::PrintError(const string& sMessage) { + fprintf(stdout, "\033[1m\033[31m*\033[39m\033[22m %s", sMessage.c_str()); + PrintStatus(false); +} + +void CUtils::PrintMessage(const string& sMessage) { + fprintf(stdout, "\033[1m\033[33m*\033[39m\033[22m %s\n", sMessage.c_str()); +} + +void CUtils::PrintAction(const string& sMessage) { + fprintf(stdout, "\033[1m\033[32m*\033[39m\033[22m %s... ", sMessage.c_str()); + fflush(stdout); +} + +void CUtils::PrintStatus(bool bSuccess, const string& sMessage) { + if (!sMessage.empty()) { + fprintf(stdout, "%s", sMessage.c_str()); + } + + fprintf(stdout, "\n\b\b\b\b\b\b"); + + if (bSuccess) { + fprintf(stdout, "\033[1m\033[34m[ \033[32mok \033[34m]\033[39m\033[22m\n"); + } else { + fprintf(stdout, "\033[1m\033[34m[ \033[31m!! \033[34m]\033[39m\033[22m"); + fprintf(stdout, "\r\033[1m\033[31m*\033[39m\033[22m\n"); + } +} + string CUtils::ToString(short i) { stringstream s; s << i; return s.str(); } string CUtils::ToString(unsigned short i) { stringstream s; s << i; return s.str(); } string CUtils::ToString(int i) { stringstream s; s << i; return s.str(); } diff --git a/Utils.h b/Utils.h index 4e35cc54..b7576d4b 100644 --- a/Utils.h +++ b/Utils.h @@ -29,6 +29,10 @@ public: static unsigned long GetLongIP(const string& sIP); static string ChangeDir(const string& sPath, const string& sAdd, const string& sHomeDir); static int MakeDir(const string& sPath, mode_t iMode = 0700); + static void PrintError(const string& sMessage); + static void PrintMessage(const string& sMessage); + static void PrintAction(const string& sMessage); + static void PrintStatus(bool bSuccess, const string& sMessage = ""); static string ToString(short i); static string ToString(unsigned short i); diff --git a/main.cpp b/main.cpp index b57e9004..8aa6130f 100644 --- a/main.cpp +++ b/main.cpp @@ -18,13 +18,13 @@ static struct option g_LongOpts[] = void GenerateHelp( const char *appname ) { - cerr << "USAGE: " << appname << " [options] [znc.conf]" << endl; - cerr << "Options are:" << endl; - cerr << " --help" << endl; - cerr << " --makepass Generates a password for use in config" << endl; + CUtils::PrintMessage("USAGE: " + string(appname) + " [options] [znc.conf]"); + CUtils::PrintMessage("Options are:"); + CUtils::PrintMessage("\t--help"); + CUtils::PrintMessage("\t--makepass Generates a password for use in config"); #ifdef HAVE_LIBSSL - cerr << " --makepem Generates a pemfile for use with SSL" << endl; - cerr << " --encrypt-pem Encrypts the pemfile" << endl; + CUtils::PrintMessage("\t--makepem Generates a pemfile for use with SSL"); + CUtils::PrintMessage("\t--encrypt-pem Encrypts the pemfile"); #endif /* HAVE_LIBSSL */ } @@ -34,7 +34,7 @@ void die(int sig) { signal( SIGPIPE, SIG_DFL ); #ifdef _DEBUG - cerr << "Exiting on SIG [" << sig << "]" << endl; + CUtils::PrintMessage("Exiting on SIG [" + CUtils::ToString(sig) + "]"); if ( ( sig == SIGABRT ) || ( sig == SIGSEGV ) ) abort(); #endif /* _DEBUG */ @@ -96,33 +96,42 @@ int main(int argc, char** argv) { sConfig = argv[optind]; #ifdef HAVE_LIBSSL - if ( bMakePem ) { + if (bMakePem) { CZNC* pZNC = CZNC::New(); pZNC->InitDirs(""); string sPemFile = pZNC->GetPemLocation(); + CUtils::PrintAction("Writing Pem file [" + sPemFile + "]"); + + if (CFile::Exists(sPemFile)) { + CUtils::PrintStatus(false, "File already exists"); + delete pZNC; + return 1; + } + FILE *f = fopen( sPemFile.c_str(), "w" ); - if ( !f ) { - cerr << "Unable to open pem file [" << sPemFile << "]" << endl; + if (!f) { + CUtils::PrintStatus(false, "Unable to open"); delete pZNC; - return( 1 ); + return 1 ; } CUtils::GenerateCert( f, bEncPem ); - fclose( f ); + fclose(f); - cout << "Wrote pem file to [" << sPemFile << "]" << endl; + CUtils::PrintStatus(true); delete pZNC; - return( 0 ); + return 0; } #endif /* HAVE_LIBSSL */ if ( bMakePass ) { char* pass = getpass( "Enter Password: " ); int iLen = strlen(pass); - cout << "Use this in the section of your config:" << endl << endl << "Pass = " << CMD5(pass, iLen) << " -" << endl << endl; - memset((char*) pass, 0, iLen); // Null out our pass so it doesn't sit in memory + CUtils::PrintMessage("Use this in the section of your config:"); + CUtils::PrintMessage("Pass = " + string((const char*) CMD5(pass, iLen)) + " -"); + memset((char*) pass, 0, iLen); // null out our pass so it doesn't sit in memory return 0; } @@ -130,33 +139,39 @@ int main(int argc, char** argv) { pZNC->InitDirs(((argc) ? argv[0] : "")); if (!pZNC->ParseConfig(sConfig)) { - cerr << endl << "*** Unrecoverable error while parsing config." << endl; + CUtils::PrintError("Unrecoverable error while parsing config."); delete pZNC; return 1; } if (!pZNC->GetListenPort()) { - cerr << "You must supply a ListenPort in your config." << endl; + CUtils::PrintError("You must supply a ListenPort in your config."); delete pZNC; return 1; } if (!pZNC->OnBoot()) { - cerr << "Exiting due to module boot errors." << endl; + CUtils::PrintError("Exiting due to module boot errors."); delete pZNC; return 1; } #ifndef _DEBUG + CUtils::PrintAction("Forking into the background"); + int iPid = fork(); + if (iPid == -1) { - cerr << "Failed to fork into background: [" << strerror(errno) << "]" << endl; + CUtils::PrintStatus(false, strerror(errno)); delete pZNC; exit(1); } if (iPid > 0) { - cout << "ZNC - by prozac [port: " << ((pZNC->IsSSL()) ? "+" : "") << pZNC->GetListenPort() << "] [pid: " << iPid << "]" << endl; + CUtils::PrintStatus(true, "[pid: " + CUtils::ToString(iPid) + "]"); + + CUtils::PrintMessage("ZNC - by prozac@gmail.com"); + // [port: " << ((pZNC->IsSSL()) ? "+" : "") << pZNC->GetListenPort() << "] [pid: " << iPid << "]" << endl; pZNC->WritePidFile(iPid); exit(0); } diff --git a/znc.cpp b/znc.cpp index 3abd3bfd..870eaebe 100644 --- a/znc.cpp +++ b/znc.cpp @@ -220,20 +220,24 @@ bool CZNC::ParseConfig(const string& sConfigFile) { if (sConfigFile.empty()) { sFilePath = GetZNCPath() + "/znc.conf"; } else { - if (CUtils::Left(sConfigFile, 1) != "/") { + if (CUtils::Left(sConfigFile, 2) == "./" || CUtils::Left(sConfigFile, 3) == "../") { sFilePath = GetCurPath() + "/" + sConfigFile; - } else { - sFilePath = sConfigFile; + } else if (CUtils::Left(sConfigFile, 1) != "/") { + sFilePath = GetZNCPath() + "/" + sConfigFile; } } + CUtils::PrintAction("Opening Config [" + sFilePath + "]"); + CFile File(sFilePath); if (!File.Open(O_RDONLY)) { - cerr << "Could not open config [" << sFilePath << "]" << endl; + CUtils::PrintStatus(false); return false; } + CUtils::PrintStatus(true); + string sLine; bool bCommented = false; // support for /**/ style comments bool bAutoCycle = true; @@ -293,17 +297,17 @@ bool CZNC::ParseConfig(const string& sConfigFile) { } } else if (strcasecmp(sTag.c_str(), "User") == 0) { if (pUser) { - cerr << "You may not nest tags inside of other tags." << endl; + CUtils::PrintStatus(false, "You may not nest tags inside of other tags."); return false; } if (sValue.empty()) { - cerr << "You must supply a username in the tag." << endl; + CUtils::PrintStatus(false, "You must supply a username in the tag."); return false; } if (m_msUsers.find(sValue) != m_msUsers.end()) { - cerr << "User [" << sValue << "] defined more than once." << endl; + CUtils::PrintStatus(false, "User [" + sValue + "] defined more than once."); return false; } @@ -312,19 +316,19 @@ bool CZNC::ParseConfig(const string& sConfigFile) { if (!sStatusPrefix.empty()) { if (!pUser->SetStatusPrefix(sStatusPrefix)) { - cerr << "Invalid StatusPrefix [" + sStatusPrefix + "] Must be 1-5 chars, no spaces." << endl; + CUtils::PrintStatus(false, "Invalid StatusPrefix [" + sStatusPrefix + "] Must be 1-5 chars, no spaces."); } } m_msUsers[sValue] = pUser; continue; } else if (strcasecmp(sTag.c_str(), "Chan") == 0) { if (!pUser) { - cerr << " tags must be nested inside of a tag." << endl; + CUtils::PrintStatus(false, " tags must be nested inside of a tag."); return false; } if (pChan) { - cerr << "You may not nest tags inside of other tags." << endl; + CUtils::PrintStatus(false, "You may not nest tags inside of other tags."); return false; } @@ -390,7 +394,7 @@ bool CZNC::ParseConfig(const string& sConfigFile) { continue; } else if (strcasecmp(sName.c_str(), "StatusPrefix") == 0) { if (!pUser->SetStatusPrefix(sValue)) { - cerr << "Invalid StatusPrefix [" + sValue + "] Must be 1-5 chars, no spaces." << endl; + CUtils::PrintStatus(false, "Invalid StatusPrefix [" + sValue + "] Must be 1-5 chars, no spaces."); } continue; } else if (strcasecmp(sName.c_str(), "DCCLookupMethod") == 0) { @@ -412,23 +416,23 @@ bool CZNC::ParseConfig(const string& sConfigFile) { pUser->AddAllowedHost(sValue); continue; } else if (strcasecmp(sName.c_str(), "Server") == 0) { - if (!pUser->AddServer(sValue)) { - cerr << "Unable to add server [" << sValue << "]" << endl; - } + CUtils::PrintAction("Adding Server [" + sValue + "]"); + CUtils::PrintStatus(pUser->AddServer(sValue)); continue; } else if (strcasecmp(sName.c_str(), "Chan") == 0) { pUser->AddChan(sValue); continue; } else if (strcasecmp(sName.c_str(), "LoadModule") == 0) { + string sModName = CUtils::Token(sValue, 0); + CUtils::PrintAction("Loading Module [" + sModName + "]"); #ifdef _MODULES string sModRet; - string sModName = CUtils::Token(sValue, 0); string sArgs = CUtils::Token(sValue, 1, true); - pUser->GetModules().LoadModule(sModName, sArgs, pUser, sModRet); - cout << sModRet << endl; + bool bModRet = pUser->GetModules().LoadModule(sModName, sArgs, pUser, sModRet); + CUtils::PrintStatus(bModRet, (bModRet) ? "" : sModRet); #else - cerr << "Unable to load [" << sValue << "] Modules are not enabled." << endl; + CUtils::PrintStatus(false, "Modules are not enabled."); #endif continue; } @@ -443,24 +447,27 @@ bool CZNC::ParseConfig(const string& sConfigFile) { } m_uListenPort = strtol(sPort.c_str(), NULL, 10); + CUtils::PrintAction("Binding to port [" + string((m_bSSL) ? "+" : "") + CUtils::ToString(m_uListenPort) + "]"); #ifndef HAVE_LIBSSL if (m_bSSL) { - cerr << "SSL is not enabled, could not bind to ssl port [" << m_uListenPort << "]" << endl; + CUtils::PrintStatus(false, "SSL is not enabled"); return false; } #endif if ((m_bSSL) && (!CFile::Exists(GetPemLocation()))) { - cerr << "Unable to locate pem file: [" << GetPemLocation() << "]" << endl; + CUtils::PrintStatus(false, "Unable to locate pem file: [" + GetPemLocation() + "]"); return false; } if (!Listen()) { - cerr << "Could not bind to port [" << m_uListenPort << "]" << endl; + CUtils::PrintStatus(false); return false; } + CUtils::PrintStatus(true); + continue; } else if (strcasecmp(sName.c_str(), "ISpoofFile") == 0) { m_sISpoofFile = sValue; @@ -480,7 +487,7 @@ bool CZNC::ParseConfig(const string& sConfigFile) { } } - cerr << "Unhandled line in config: [" << sLine << "]" << endl; + CUtils::PrintError("Unhandled line in config: [" + sLine + "]"); } if (pChan) { @@ -490,10 +497,9 @@ bool CZNC::ParseConfig(const string& sConfigFile) { File.Close(); if (!m_msUsers.size()) { - cerr << "You must define at least one user in your config." << endl; + CUtils::PrintStatus(false, "You must define at least one user in your config."); return false; } - cout << "Loaded config [" << sFilePath << "]" << endl; return true; }