/*************************************************************************** * 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 #define ERR_PARAM "wrong parameter(s)" #define ERR_CALL "call error" extern "C" { #include #include #include } #include "cpilua.h" #include "callbacks.h" #include #include #include #include #include using namespace std; using namespace nDirectConnect; cServerDC * GetCurrentVerlihub() { return (cServerDC *)cServerDC::sCurrentServer; } int _SendDataToUser(lua_State *L) { string data, nick; if(lua_gettop(L) == 3) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } data = (char *)lua_tostring(L, 2); if(!lua_isstring(L, 3)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 3); if(!SendDataToUser((char *)data.c_str(), (char *)nick.c_str())) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _SendDataToAll(lua_State *L) { string data; int min_class, max_class; if(lua_gettop(L) == 4) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } data = (char *)lua_tostring(L, 2); if(!lua_isnumber(L, 3)) { luaerror(L, ERR_PARAM); return 2; } min_class = (int)lua_tonumber(L, 3); if(!lua_isnumber(L, 4)) { luaerror(L, ERR_PARAM); return 2; } max_class = (int)lua_tonumber(L, 4); if(!SendDataToAll((char*)data.c_str(), min_class, max_class)) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _SendPMToAll(lua_State *L) { string data, from; int min_class, max_class; if(lua_gettop(L) == 5) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } data = (char *)lua_tostring(L, 2); if(!lua_isstring(L, 3)) { luaerror(L, ERR_PARAM); return 2; } from = (char *)lua_tostring(L, 3); if(!lua_isnumber(L, 4)) { luaerror(L, ERR_PARAM); return 2; } min_class = (int)lua_tonumber(L, 4); if(!lua_isnumber(L, 5)) { luaerror(L, ERR_PARAM); return 2; } max_class = (int)lua_tonumber(L, 5); if(!SendPMToAll((char*)data.c_str(), (char *)from.c_str(), min_class, max_class)) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _CloseConnection(lua_State *L) { string nick; if(lua_gettop(L) == 2) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); if(!CloseConnection((char*)nick.c_str())) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _GetMyINFO(lua_State *L) { string nick; char *myinfo; if(lua_gettop(L) == 2) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); myinfo = GetMyINFO((char*)nick.c_str()); lua_pushboolean(L, 0); lua_pushstring(L, myinfo); return 2; } else { luaerror(L, ERR_PARAM); return 2; } } int _GetNickList(lua_State *L) { char *nicklist; if(lua_gettop(L) == 1) { #if HAVE_GETNICKLIST nicklist = GetNickList(); #else nicklist = "not available"; #endif lua_pushboolean(L, 0); lua_pushstring(L, nicklist); return 2; } else { luaerror(L, ERR_PARAM); return 2; } } int _GetUserClass(lua_State *L) { string nick; int uclass; if(lua_gettop(L) == 2) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); uclass = GetUserClass((char*)nick.c_str()); lua_pushboolean(L, 1); lua_pushnumber(L, uclass); return 2; } else { luaerror(L, ERR_PARAM); return 2; } } int _GetUserHost(lua_State *L) { string nick, host; if(lua_gettop(L) == 2) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); host = GetUserHost((char*)nick.c_str()); lua_pushboolean(L, 1); lua_pushstring(L, (char *)host.c_str()); return 2; } else { luaerror(L, ERR_PARAM); return 2; } } int _GetUserIP(lua_State *L) { string nick, ip; if(lua_gettop(L) == 2) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); ip = GetUserIP((char*)nick.c_str()); lua_pushboolean(L, 1); lua_pushstring(L, (char *)ip.c_str()); return 2; } else { luaerror(L, ERR_PARAM); return 2; } } int _Ban(lua_State *L) { // NOTE: this one is not implemented yet! string nick; unsigned howlong; int bantype; if(lua_gettop(L) == 4) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); if(!lua_isnumber(L, 3)) { luaerror(L, ERR_PARAM); return 2; } howlong = (unsigned)lua_tonumber(L, 3); if(!lua_isnumber(L, 4)) { luaerror(L, ERR_PARAM); return 2; } bantype = (int)lua_tonumber(L, 4); if(!Ban((char *)nick.c_str(), howlong, bantype)) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _KickUser(lua_State *L) { string nick, op, data; if(lua_gettop(L) == 4) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } op = (char *)lua_tostring(L, 2); if(!lua_isstring(L, 3)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 3); if(!lua_isstring(L, 4)) { luaerror(L, ERR_PARAM); return 2; } data = (char *)lua_tostring(L, 4); if(!KickUser((char *)op.c_str(), (char *)nick.c_str(), (char *)data.c_str())) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _ParseCommand(lua_State *L) { // NOTE: this one is not implemented yet! string data; if(lua_gettop(L) == 2) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } data = (char *)lua_tostring(L,2); if(!ParseCommand((char *)data.c_str())) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _SetConfig(lua_State *L) { string config_name, var, val; if(lua_gettop(L) == 4) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } config_name = (char *)lua_tostring(L, 2); if(!lua_isstring(L, 3)) { luaerror(L, ERR_PARAM); return 2; } var = (char *)lua_tostring(L, 3); if(!lua_isstring(L, 4)) { luaerror(L, ERR_PARAM); return 2; } val = (char *)lua_tostring(L, 4); if(!SetConfig((char *)config_name.c_str(), (char *)var.c_str(), (char *)val.c_str())) { luaerror(L, ERR_CALL); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _GetConfig(lua_State *L) { char *val = new char[64]; string config_name, var; int size; if(lua_gettop(L) == 3) { if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } config_name = (char *)lua_tostring(L, 2); if(!lua_isstring(L, 3)) { luaerror(L, ERR_PARAM); return 2; } var = (char *)lua_tostring(L, 3); size = GetConfig((char *)config_name.c_str(), (char *)var.c_str(), val, 64); if(size < 0) { luaerror(L, "error in script_api's GetConfig"); return 2; } if(size >= 64) { delete [] val; val = new char[size+1]; GetConfig((char *)config_name.c_str(), (char *)var.c_str(), val, size); } lua_pushboolean(L, 1); lua_pushstring(L, val); delete [] val; val = 0; return 2; } else { luaerror(L, ERR_PARAM); return 2; } } int _AddRobot(lua_State *L) { string nick, desc, speed, email, share; int uclass; if(lua_gettop(L) == 7) { cServerDC *server = GetCurrentVerlihub(); if(server == NULL) { luaerror(L, "could not get current server"); return 2; } cpiLua *pi = (cpiLua *)server->mPluginManager.GetPlugin(LUA_PI_IDENTIFIER); if(pi == NULL) { luaerror(L, "could not get current LUA plugin"); return 2; } if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); if(!lua_isnumber(L, 3)) { luaerror(L, ERR_PARAM); return 2; } uclass = (int)lua_tonumber(L, 3); if(!lua_isstring(L, 4)) { luaerror(L, ERR_PARAM); return 2; } desc = (char *)lua_tostring(L, 4); if(!lua_isstring(L, 5)) { luaerror(L, ERR_PARAM); return 2; } speed = (char *)lua_tostring(L, 5); if(!lua_isstring(L, 6)) { luaerror(L, ERR_PARAM); return 2; } email = (char *)lua_tostring(L, 6); if(!lua_isstring(L, 7)) { luaerror(L, ERR_PARAM); return 2; } share = (char *)lua_tostring(L, 7); cPluginRobot *robot = pi->NewRobot(nick, uclass); if(robot != NULL) { server->mP.Create_MyINFO(robot->mMyINFO, robot->mNick, desc, speed, email, share); robot->mMyINFO_basic = robot->mMyINFO; string omsg = "$Hello "; omsg+= robot->mNick; server->mHelloUsers.SendToAll(omsg, server->mC.delayed_myinfo, true); omsg = server->mP.GetMyInfo(robot, eUC_NORMUSER); server->mUserList.SendToAll(omsg, true, true); if(uclass >= 3) server->mUserList.SendToAll(server->mOpList.GetNickList(), true); } else { luaerror(L, "could not create robot, possibly exists with this nick"); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _DelRobot(lua_State *L) { string nick; if(lua_gettop(L) == 2) { cServerDC *server = GetCurrentVerlihub(); if(server == NULL) { luaerror(L, "could not get current server"); return 2; } cpiLua *pi = (cpiLua *)server->mPluginManager.GetPlugin(LUA_PI_IDENTIFIER); if(pi == NULL) { luaerror(L, "could not get current LUA plugin"); return 2; } if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } nick = (char *)lua_tostring(L, 2); cPluginRobot *robot = (cPluginRobot *)server->mUserList.GetUserByNick(nick); if(robot != NULL) { pi->DelRobot(robot); } else { luaerror(L, "could not delete robot, possibly doesn't exists with this nick"); return 2; } } else { luaerror(L, ERR_PARAM); return 2; } lua_pushboolean(L, 1); return 1; } int _SQLQuery(lua_State *L) { if(lua_gettop(L) == 2) { cServerDC *server = GetCurrentVerlihub(); if(server == NULL) { luaerror(L, "could not get current server"); return 2; } cpiLua *pi = (cpiLua *)server->mPluginManager.GetPlugin(LUA_PI_IDENTIFIER); if(pi == NULL) { luaerror(L, "could not get current LUA plugin"); return 2; } if(!lua_isstring(L, 2)) { luaerror(L, ERR_PARAM); return 2; } pi->mQuery->Clear(); pi->mQuery->OStream() << (char *)lua_tostring(L, 2); pi->mQuery->Query(); int i = pi->mQuery->StoreResult(); lua_pushboolean(L, 1); if(i > 0) lua_pushnumber(L, i); else lua_pushnumber(L, 0); return 2; } else { luaerror(L, ERR_PARAM); return 2; } } int _SQLFetch(lua_State *L) { if(lua_gettop(L) == 2) { cServerDC *server = GetCurrentVerlihub(); if(server == NULL) { luaerror(L, "could not get current server"); return 2; } cpiLua *pi = (cpiLua *)server->mPluginManager.GetPlugin(LUA_PI_IDENTIFIER); if(pi == NULL) { luaerror(L, "could not get current LUA plugin"); return 2; } if(!lua_isnumber(L, 2)) { luaerror(L, ERR_PARAM); return 2; } int r = (int)lua_tonumber(L, 2); if(!pi->mQuery->GetResult()) { luaerror(L, "could not seek data"); return 2; } pi->mQuery->DataSeek(r); MYSQL_ROW row; if(!(row = pi->mQuery->Row())) { luaerror(L, "could not fetch row"); return 2; } lua_pushboolean(L, 1); int j = 0; while(j < pi->mQuery->Cols()) { lua_pushstring(L, (char *)row[j]); j++; } return j + 1; } else { luaerror(L, ERR_PARAM); return 2; } } int _SQLFree(lua_State *L) { cServerDC *server = GetCurrentVerlihub(); if(server == NULL) { luaerror(L, "could not get current server"); return 2; } cpiLua *pi = (cpiLua *)server->mPluginManager.GetPlugin(LUA_PI_IDENTIFIER); if(pi == NULL) { luaerror(L, "could not get current LUA plugin"); return 2; } pi->mQuery->Clear(); lua_pushboolean(L, 1); return 1; } int _GetUsersCount(lua_State *L) { lua_pushboolean(L, 1); lua_pushnumber(L, GetUsersCount()); return 2; } int _GetTotalShareSize(lua_State *L) { lua_pushboolean(L, 1); lua_pushnumber(L, GetTotalShareSize()); return 2; } void luaerror(lua_State *L, const char * errstr) { lua_pushboolean(L, 0); lua_pushstring(L, errstr); }