diff options
Diffstat (limited to 'Swiftob/LuaCommands.cpp')
-rw-r--r-- | Swiftob/LuaCommands.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/Swiftob/LuaCommands.cpp b/Swiftob/LuaCommands.cpp index 1192452..18535f3 100644 --- a/Swiftob/LuaCommands.cpp +++ b/Swiftob/LuaCommands.cpp @@ -1,32 +1,32 @@ /* - * Copyright (c) 2011 Isode Limited. + * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiftob/LuaCommands.h> #include <boost/bind.hpp> #include <vector> #include <algorithm> #include <iostream> #include <Swiften/Base/foreach.h> #include <Swiften/Client/Client.h> #include <Swiften/Network/TimerFactory.h> #include <boost/filesystem/operations.hpp> #include <Swiften/Base/Path.h> #include <Swiftob/Commands.h> #include <Swiften/Base/BoostFilesystemVersion.h> #define LUA_COMMANDS "__Lua_Commands" #define STORAGE "__Storage" static const luaL_Reg defaultLibraries[] = { {"", luaopen_base}, {LUA_LOADLIBNAME, luaopen_package}, {LUA_TABLIBNAME, luaopen_table}, {LUA_IOLIBNAME, luaopen_io}, {LUA_OSLIBNAME, luaopen_os}, @@ -353,107 +353,107 @@ static int l_store_setting(lua_State *L) { } static int l_get_setting(lua_State *L) { return LuaCommands::commandsFromLua(L)->get_setting(L); } int LuaCommands::store_setting(lua_State *L) { if (!lua_isstring(L, 2) || !lua_isstring(L, 1)) { return luaL_error(L, "both setting and key must be strings"); } std::string value(lua_tostring(L, 2)); std::string key(lua_tostring(L, 1)); lua_pop(L, 2); storageFromLua(L)->saveSetting(key, value); return 0; } int LuaCommands::get_setting(lua_State *L) { if (!lua_isstring(L, 1)) { return luaL_error(L, "key must be a string"); } std::string key(lua_tostring(L, 1)); lua_pop(L, 1); lua_pushstring(L, storageFromLua(L)->getSetting(key).c_str()); return 1; } void LuaCommands::handleLuaListener(int callbackIndex, lua_State* L, Swift::Message::ref message) { lua_rawgeti(L, LUA_REGISTRYINDEX, callbackIndex); - lua_pushstring(L, message->getBody().c_str()); + lua_pushstring(L, message->getBody().get_value_or("").c_str()); lua_pushstring(L, message->getFrom().toBare().toString().c_str()); lua_pushstring(L, message->getFrom().getResource().c_str()); messageOntoStack(message, L); int result = lua_pcall(L, 4, 0, 0); if (result != 0) { std::string error(lua_tostring(L, -1)); lua_pop(L, 1); error = "Listener failed: " + error; std::cout << error << std::endl; } } void LuaCommands::handleLuaCommand(int callbackIndex, lua_State* L, const std::string& command, const std::string& params, Swift::Message::ref message) { lua_rawgeti(L, LUA_REGISTRYINDEX, callbackIndex); lua_pushstring(L, command.c_str()); lua_pushstring(L, params.c_str()); messageOntoStack(message, L); int result = lua_pcall(L, 3, 0, 0); if (result != 0) { std::string error(lua_tostring(L, -1)); lua_pop(L, 1); error = "Command '" + command + "' failed: " + error; std::cout << error << std::endl; commands_->replyTo(message, error, false); } } void LuaCommands::messageOntoStack(Swift::Message::ref message, lua_State* L) { lua_createtable(L, 0, 4); std::string typeString; switch (message->getType()) { case Message::Chat : typeString = "chat";break; case Message::Groupchat : typeString = "groupchat";break; case Message::Normal : typeString = "normal";break; case Message::Error : typeString = "error";break; case Message::Headline : typeString = "headline";break; } lua_pushstring(L, typeString.c_str()); lua_setfield(L, -2, "type"); lua_pushstring(L, message->getFrom().toString().c_str()); lua_setfield(L, -2, "from"); lua_pushstring(L, message->getFrom().toBare().toString().c_str()); lua_setfield(L, -2, "frombare"); lua_pushstring(L, message->getTo().toString().c_str()); lua_setfield(L, -2, "to"); - lua_pushstring(L, message->getBody().c_str()); + lua_pushstring(L, message->getBody().get_value_or("").c_str()); lua_setfield(L, -2, "body"); } void LuaCommands::loadScript(boost::filesystem::path filePath) { std::cout << "Trying to load file from " << filePath << std::endl; lua_State* lua = luaL_newstate(); initialize(lua); lua_pushlightuserdata(lua, this); lua_setfield(lua, LUA_REGISTRYINDEX, LUA_COMMANDS); #if BOOST_FILESYSTEM_VERSION == 2 // TODO: Delete this when boost 1.44 becomes a minimum requirement, and we no longer need v2 std::string filename = filePath.filename(); #else std::string filename = filePath.filename().string(); #endif filename += ".storage"; boost::filesystem::path storagePath(boost::filesystem::path(path_) / stringToPath(filename)); Storage* storage = new Storage(storagePath); lua_pushlightuserdata(lua, storage); lua_setfield(lua, LUA_REGISTRYINDEX, STORAGE); lua_register(lua, "swiftob_register_command", &l_register_command); lua_register(lua, "swiftob_register_listener", &l_register_listener); lua_register(lua, "swiftob_reply_to", &l_reply_to); lua_register(lua, "swiftob_get_software_version", &l_get_software_version); lua_register(lua, "swiftob_muc_input_to_jid", &l_muc_input_to_jid); lua_register(lua, "swiftob_store_setting", &l_store_setting); lua_register(lua, "swiftob_get_setting", &l_get_setting); lua_register(lua, "swiftob_muc_kick", &l_muc_kick); int fileLoaded = luaL_dofile(lua, filePath.string().c_str()); if (fileLoaded == 0 ) { std::cout << "Loaded" << std::endl; |