added long options

git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@88 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
imaginos
2005-04-03 20:56:41 +00:00
parent 46daeeba2c
commit de1f9e1bcf

105
main.cpp
View File

@@ -1,9 +1,33 @@
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <signal.h>
#include "znc.h"
#include "md5.h"
static struct option g_LongOpts[] =
{
{ "help", 0, NULL, 0 },
{ "makepass", 0, NULL, 0 },
#ifdef HAVE_LIBSSL
{ "makepem", 0, NULL, 0 },
{ "encrypt-pem", 0, NULL, 0 },
#endif /* HAVE_LIBSSL */
{ NULL }
};
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;
#ifdef HAVE_LIBSSL
cerr << " --makepem Generates a pemfile for use with SSL" << endl;
cerr << " --encrypt-pem Encrypts the pemfile" << endl;
#endif /* HAVE_LIBSSL */
}
void die(int sig) {
signal( SIGSEGV, SIG_DFL );
signal( SIGABRT, SIG_DFL );
@@ -24,22 +48,77 @@ int main(int argc, char** argv) {
// initialize ssl, allow client to have compression enabled if desired
InitSSL( CT_ZLIB );
if (argc > 1) {
if ((argc > 2) || (strcasecmp(argv[1], "--help") == 0) || (strcasecmp(argv[1], "-h") == 0)) {
cerr << "Usage: " << argv[0] << " [--makepass|--help|znc.conf]" << endl;
return 1;
int iArg, iOptIndex = -1;
#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 ) )
{
switch( iArg )
{
case 1:
{ // long options
if ( iOptIndex >= 0 )
{
string sOption = Lower( g_LongOpts[iOptIndex].name );
if ( sOption == "makepass" )
bMakePass = true;
#ifdef HAVE_LIBSSL
else if ( sOption == "makepem" )
bMakePem = true;
else if ( sOption == "encrypt-pem" )
bEncPem = true;
#endif /* HAVE_LIBSSL */
else if ( sOption == "help" )
{
GenerateHelp( argv[0] );
return( 0 );
}
}
else
{
GenerateHelp( argv[0] );
return( 1 );
}
break;
}
case 'h':
GenerateHelp( argv[0] );
return( 0 );
default :
{
GenerateHelp( argv[0] );
return( 1 );
}
}
}
if (strcasecmp(argv[1], "--makepass") == 0) {
char* pass = getpass( "Enter Password: " );
int iLen = strlen(pass);
cout << "Use this in the <User> 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
return 0;
} else {
sConfig = argv[1];
if ( optind < argc )
sConfig = argv[optind];
#ifdef HAVE_LIBSSL
if ( bMakePem )
{
FILE *f = fopen( "znc.pem", "w" );
if ( !f )
{
cerr << "Unable to open znc.pem!" << endl;
return( 1 );
}
CUtils::GenerateCert( f, bEncPem );
fclose( f );
return( 0 );
}
#endif /* HAVE_LIBSSL */
if ( bMakePass )
{
char* pass = getpass( "Enter Password: " );
int iLen = strlen(pass);
cout << "Use this in the <User> 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
return 0;
}
CZNC* pZNC = CZNC::New();