/*************************************************************************** * Copyright (C) 2004 by Janos Horvath * * bourne@freemail.hu * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include #endif #include "callbacks.h" #include "cluainterpreter.h" #include using namespace std; namespace nScripts { cLuaInterpreter::cLuaInterpreter(string scriptname) : mScriptName(scriptname) { mL = lua_open(); } cLuaInterpreter::~cLuaInterpreter() { char * args[] = { NULL }; if(mL) { CallFunction("UnLoad", args); lua_close(mL); } } bool cLuaInterpreter::Init() { #ifdef HAVE_LUA_5_1 luaL_openlibs(mL); #else luaopen_base(mL); luaopen_table(mL); luaopen_io(mL); luaopen_string(mL); luaopen_math(mL); luaopen_debug(mL); luaopen_loadlib(mL); #endif lua_newtable(mL); RegisterFunction("SendDataToUser", &_SendDataToUser); RegisterFunction("SendDataToAll", &_SendDataToAll); RegisterFunction("SendPMToAll", &_SendPMToAll); RegisterFunction("CloseConnection", &_CloseConnection); RegisterFunction("GetMyINFO", &_GetMyINFO); RegisterFunction("GetUserClass", &_GetUserClass); RegisterFunction("GetUserHost", &_GetUserHost); RegisterFunction("GetUserIP", &_GetUserIP); RegisterFunction("Ban", &_Ban); RegisterFunction("KickUser", &_KickUser); RegisterFunction("ParseCommand", &_ParseCommand); RegisterFunction("SetConfig", &_SetConfig); RegisterFunction("GetConfig", &_GetConfig); RegisterFunction("AddRobot", &_AddRobot); RegisterFunction("DelRobot", &_DelRobot); RegisterFunction("SQLQuery", &_SQLQuery); RegisterFunction("SQLFetch", &_SQLFetch); RegisterFunction("SQLFree", &_SQLFree); RegisterFunction("GetUsersCount", &_GetUsersCount); RegisterFunction("GetTotalShareSize", &_GetTotalShareSize); RegisterFunction("GetNickList", &_GetNickList); lua_setglobal(mL, "VH"); #ifdef HAVE_LUA_5_1 int status = luaL_dofile(mL, (char *)mScriptName.c_str()); #else int status = lua_dofile(mL, (char *)mScriptName.c_str()); #endif if (status) return false; //Call Main first if exists char * args[] = { NULL }; CallFunction("Main", args); return true; } void cLuaInterpreter::RegisterFunction(const char *fncname, int (*fncptr)(lua_State *)) { lua_pushstring(mL, fncname); lua_pushcfunction(mL, fncptr); lua_rawset(mL, -3); } bool cLuaInterpreter::CallFunction(char * func, char * args[]) { lua_settop(mL, 0); int base = lua_gettop(mL); lua_pushliteral(mL, "_TRACEBACK"); lua_rawget(mL, LUA_GLOBALSINDEX); lua_insert(mL, base); lua_getglobal(mL, func); if(lua_isnil(mL, -1)) { // function not exists lua_pop(mL, -1); // remove nil value lua_remove(mL, base); // remove _TRACEBACK } else { int i=0; while(args[i] != NULL) { lua_pushstring(mL, args[i]); i++; } int result = lua_pcall(mL, i, 1, base); if(result) { const char *msg = lua_tostring(mL, -1); if(msg == NULL) msg = "(unknown LUA error)"; cout << "LUA error: " << msg << endl; lua_pop(mL, 1); return true; } int val = (int)lua_tonumber(mL, -1); lua_pop(mL, 1); lua_remove(mL, base); // remove _TRACEBACK if(!(bool)val) return false; } return true; } };