Ejemplo n.º 1
0
int main(int argc, char **argv)
{
	int ret= 0;
	int ok= parseCommandLine(argc, argv);

#if defined(DEBUG_MEM)
	atexit(_CheckMemLeaks);
#endif

#if defined(__FreeBSD__)

	//set_terminate( term_exception );
	setlocale(LC_TIME, "fr_FR.ISO8859-1");
	//signal(SIGINT, sigINT);
	signal(SIGPIPE, SIG_IGN);

#elif defined(WIN32)
	setlocale(LC_TIME, "French");
	WSAData wsaData;
	main_thread= GetCurrentThread();
	console.run();
	sleep(1);

#	if defined(_DEBUG) && defined(_MSC_VER)
		// add breakpoint when memory block n is allocated
		// _crtBreakAlloc
		//_CrtSetBreakAlloc(n);
		//_CrtSetBreakAlloc(10193);
		//_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF |
		//	_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
#	endif

if( WSAStartup(MAKEWORD(1,1), &wsaData) != 0)
{
	Error("Unable to initialise network: %d\n", WSAGetLastError());
}
else
{
#endif
	Output("Dryon v" BOT_VERSION " compiled on " __DATE__ "\n");
	Output("Embedded AMX: v" SMALL_VERSION "\n");
	Output("Embedded Ruby: v" RUBY_VERSION "\n");

	if( ok != 0 )
	{
		usleep(10000);
		return 1;
	}

	if( help_mode )
	{
		Output("Usage: %s [option(s)]\n", basename(argv[0]));
		Output("--test		enable test mode\n");
		Output("--help		display help\n");
		Output("--hidden	start hidden (windows)\n");
		Output("--config xxx	set dryon.cfg file\n");
		Output("--servers xxx	set servers.txt file\n");
		Output("--userfile xxx	set userfile.txt file\n");
		usleep(10000);
		return 0;
}

	cfg.setFile( configfile_path.c_str() );

	/* some checks on required folders/files */
	if( makeSanityChecks() == 0 )
	{
		bool err= false;
		cfg.readFile();

		#define CHECK_CFG_VAR(X)	if( !cfg.isDefined(#X) ){ Error("variable '" #X "' undefined\n"); err= true; }

		// check if required config directives are set
		CHECK_CFG_VAR(	botname	 );
		CHECK_CFG_VAR(	alt_nick );
		CHECK_CFG_VAR(	realname );
		CHECK_CFG_VAR(	useauth  );
		CHECK_CFG_VAR(	script_01);


		// if one or more vars are undefined then exit now
		// exit also if the compilation of scripts fails
		if( !err &&  (PMgr.loadScriptsFromCfg(cfg) == 0) )
		{
			//// compile/test the scripts /////
			if( test_mode )
			{
				Output("** TEST MODE **\n");
				PMgr.callEvent("event_onTest");
			}
			///////////
			else
			{
				string last_srv= "", srv= "";

				cfg.readFile();
				userfile.readUserFile( userfile_path.c_str() );
				servers.loadFromFile( serverlist_path.c_str() );

				while(1)
				{
					int ret;

					if( cfg.isDefined("debugmode") )
						bot.setIntOption(OPTION_INT_DEBUGMODE, 	cfg.readIntKey("debugmode"));
					else
						bot.setIntOption(OPTION_INT_DEBUGMODE, 	0);

					bot.setIntOption(OPTION_INT_USEAUTH, 	cfg.readBoolKey("useauth")?1:0);
					bot.setStrOption(OPTION_STR_REALNAME, 	cfg.readStringKey("realname"));
					bot.setStrOption(OPTION_STR_ALTNICK,	cfg.readStringKey("alt_nick"));

					srv= servers.getNextServer();
					// if the server is the same than the last one we tried, wait 2min
					// before trying to reconnect to be nice with the server :)
					if( srv == "" )
					{
						Error("No servers in servers.txt, exiting...\n");
						break;
					}
					else if( srv == last_srv )
					{
						Debug("Waiting 2min before reconnect attempt...\n");
						sleep(120);
					}



					// if exceptions occur then they came from the STL since
					// i don't use them
					try
					{
						ret= bot.mainLoop(srv, cfg.readStringKey("botname"));
					}
					catch( exception &e )
					{
						Error("exception: %s\n", e.what());
					}

					last_srv= srv;

					if( ret!=0 )
					{
						Debug("Exiting...\n");
						break;
					}

					PMgr.callEvent("event_onBotDisconnected");
				}
			}
		}
		else
		{
			Output("Errors during loading/compilation\n");
			ret= 1;
		}
	}
	else
	{
		ret= 1;
	}


#if defined(WIN32)
}
#endif

	// i had problems when the bot exited immediatly from an error
	// the thread didn't had time to initialiase before exiting and this resulted
	// in a seg fault, with a small delay it works fine :)
	usleep(10000);

#if defined(WIN32)

WSACleanup();
Output("%sBot exited successfully, you can now close the window.%s\n", COLOR_GREEN, COLOR_RESET);
//	console.requestEnd();
//	console.waitEnd();

#endif

	return ret;
}