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; }
void DryonBot::onPrivMsg(const string &sender, const string &dest, const string &msg) { vector<string> msg_parts; Tokenize(msg, msg_parts, " "); user_Info *usr; GET_USR(usr, sender); const string &command= msg_parts[0].substr(1); // load user flags from userfile.txt checkUserFlags(usr, dest); if( command=="reload" ) { /* if user has owner flag then reload all config files */ if( usr->hasFlag(USRLVL_OWNER) ) { // force update of user flags for(userDataTypeIter it= userData.begin(); it!= userData.end(); it++) (*it).second.flags_set= false; servers.loadFromFile("servers.txt"); userfile.saveFile(); userfile.readUserFile(); cfg.readFile(); PMgr.loadScriptsFromCfg(cfg); } return; } // user list else if( command=="userlist" ) { if( usr->hasAccount() && usr->hasFlag(USRLVL_OWNER) ) { map<string, UsersData> &ulist= userfile.getAccessList(); notice(sender, "User list: %d entries", ulist.size()); notice(sender, "-------- Start (Show accounts and global flags) --------"); for(map<string, UsersData>::iterator it= ulist.begin(); it!=ulist.end(); it++) { string what= it->first; if( (what[0]=='a') && (what[1]=='/') ) { what= '#' + what.substr(2); } notice(sender, "%15s %35s %5s", it->second.name.c_str(), what.c_str(), it->second.flags.c_str()); } notice(sender, "-------- End --------"); } return; } else if( command == "showflags" ) { if( usr->hasAccount() && usr->hasFlag(USRLVL_MASTER) ) { map<string, UsersData> &ulist= userfile.getAccessList(); string &chan= msg_parts[1]; notice(sender, "User list for %s", chan.c_str()); for(map<string, UsersData>::iterator it= ulist.begin(); it!=ulist.end(); it++) { string what= it->first; UsersData &tmp= it->second; for(map<string,string>::iterator it2= tmp.channel_flags.begin(); it2 != tmp.channel_flags.end(); it2++) { if( it2->first == chan ) notice(sender, "%15s %5s", it->second.name.c_str(), it2->second.c_str()); } } notice(sender, "-------- End --------"); } } // return a list of loaded plugins else if( command=="scripts" ) { uint count= PMgr.getPluginCount(); for(int i= count-1; i>= 0; i--) { notice(sender, "%2d ) %s\n", i, PMgr.getPlugin(i)->getPath()); } } else if( command=="quit" ) { if( usr->hasFlag(USRLVL_OWNER) ) { quit("exit requested"); } return; } else if( command=="help" ) { if( usr->hasAccount() ) { notice(sender, "Core commands:"); if( usr->hasFlag(USRLVL_OWNER) ) { notice(sender, ":reload - reload all plugins and config file"); notice(sender, ":userlist - display user list"); } notice(sender, ":help - display this :<"); notice(sender, ":scripts - display list of currently loaded scripts"); notice(sender, "---"); PMgr.sendCommandsList(sender, dest, usr); //return; } } else if( command=="nfo" ) { if( msg_parts.size() == 1 ) { Output("Nick: '%s'\n", bot_data.nick.c_str()); Output("Host: '%s'\n", bot_data.full_host.c_str()); Output("Auth: '%s'\n", bot_data.auth.c_str()); } else if( msg_parts.size() >= 2 ) { user_Info *u= getUserData(msg_parts[1]); if( u!=NULL ) { Output("Nick: '%s'\n", u->nick.c_str()); Output("Host: '%s'\n", u->full_host.c_str()); Output("Auth: '%s'\n", u->auth.c_str()); } else { notice(sender, "Nick unknown: %s", msg_parts[1].c_str()); } } } else { chan_Info *chan; if( isChannelName(dest) ) { GET_CHAN(chan, dest); PMgr.callEvent("event_onPrivMsg", "ucv", usr, chan, &msg_parts); PMgr.callCommand(usr, (dummy_Info*)chan, msg_parts); } else { PMgr.callEvent("event_onPrivMsg", "uuv", usr, &bot_data, &msg_parts); PMgr.callCommand(usr, (dummy_Info*)&bot_data, msg_parts); } } }