diff options
| author | Edwin Mons <edwin.mons@isode.com> | 2014-05-23 09:01:23 (GMT) |
|---|---|---|
| committer | Swift Review <review@swift.im> | 2014-06-22 12:35:26 (GMT) |
| commit | bd7f30aec53fc776be678577dbe4f9afec5898a6 (patch) | |
| tree | 66afad4382dc16f7405a856dd0b5abc38db51653 /Sluift/sluift.cpp | |
| parent | 1eb14b6bde145ca54ac9b981df339fb8c56d3930 (diff) | |
| download | swift-contrib-bd7f30aec53fc776be678577dbe4f9afec5898a6.zip swift-contrib-bd7f30aec53fc776be678577dbe4f9afec5898a6.tar.bz2 | |
Sluift component support
Change-Id: Ib8af01c04c866e198c04d35236dea4da464c9116
Diffstat (limited to 'Sluift/sluift.cpp')
| -rw-r--r-- | Sluift/sluift.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index b55649b..2fd1e50 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -1,53 +1,54 @@ /* * Copyright (c) 2011-2014 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include <Sluift/sluift.h> #include <lua.hpp> #include <string> #include <boost/bind.hpp> #include <boost/numeric/conversion/cast.hpp> #include <boost/assign/list_of.hpp> #include "Watchdog.h" #include <Sluift/Lua/Check.h> #include <Sluift/SluiftClient.h> +#include <Sluift/SluiftComponent.h> #include <Sluift/globals.h> #include <Sluift/Lua/Exception.h> #include <Sluift/Lua/LuaUtils.h> #include <Sluift/Lua/FunctionRegistration.h> #include <Swiften/Base/sleep.h> #include <Swiften/Base/foreach.h> #include <Swiften/Base/IDGenerator.h> #include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> #include <Swiften/Serializer/PayloadSerializer.h> #include <Swiften/TLS/Certificate.h> #include <Swiften/TLS/CertificateFactory.h> #include <Sluift/LuaElementConvertor.h> #include <Sluift/Lua/Debug.h> #include <Swiften/StringCodecs/Base64.h> #include <Swiften/StringCodecs/Hexify.h> #include <Swiften/IDN/IDNConverter.h> #include <Swiften/Crypto/CryptoProvider.h> #include <Swiften/Crypto/PlatformCryptoProvider.h> #include <Sluift/ITunesInterface.h> using namespace Swift; namespace Swift { namespace Sluift { SluiftGlobals globals; } } extern "C" const char core_lua[]; extern "C" size_t core_lua_size; static inline bool getGlobalDebug(lua_State* L) { lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.moduleLibIndex); lua_getfield(L, -1, "debug"); @@ -56,70 +57,96 @@ static inline bool getGlobalDebug(lua_State* L) { return result; } /******************************************************************************* * Module functions ******************************************************************************/ SLUIFT_LUA_FUNCTION_WITH_HELP( Sluift, new_client, "Creates a new client.\n\nReturns a @{Client} object.\n", "jid The JID to connect as\n" "passphrase The passphrase to use\n", "" ) { Lua::checkString(L, 1); JID jid(std::string(Lua::checkString(L, 1))); std::string password(Lua::checkString(L, 2)); SluiftClient** client = reinterpret_cast<SluiftClient**>(lua_newuserdata(L, sizeof(SluiftClient*))); lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); lua_getfield(L, -1, "Client"); lua_setmetatable(L, -3); lua_pop(L, 1); *client = new SluiftClient(jid, password, &Sluift::globals.networkFactories, &Sluift::globals.eventLoop); (*client)->setTraceEnabled(getGlobalDebug(L)); return 1; } SLUIFT_LUA_FUNCTION_WITH_HELP( + Sluift, new_component, + + "Creates a new component.\n\nReturns a @{Component} object.\n", + + "jid The JID to connect as\n" + "passphrase The passphrase to use\n", + + "" +) { + Lua::checkString(L, 1); + JID jid(std::string(Lua::checkString(L, 1))); + std::string password(Lua::checkString(L, 2)); + + SluiftComponent** component = reinterpret_cast<SluiftComponent**>(lua_newuserdata(L, sizeof(SluiftComponent*))); + + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); + lua_getfield(L, -1, "Component"); + lua_setmetatable(L, -3); + lua_pop(L, 1); + + *component = new SluiftComponent(jid, password, &Sluift::globals.networkFactories, &Sluift::globals.eventLoop); + (*component)->setTraceEnabled(getGlobalDebug(L)); + return 1; +} + +SLUIFT_LUA_FUNCTION_WITH_HELP( Sluift, sha1, "Compute the SHA-1 hash of given data", "data the data to hash", "" ) { static boost::shared_ptr<CryptoProvider> crypto(PlatformCryptoProvider::create()); if (!lua_isstring(L, 1)) { throw Lua::Exception("Expected string"); } size_t len; const char* data = lua_tolstring(L, 1, &len); ByteArray result = crypto->getSHA1Hash(createByteArray(data, len)); lua_pushlstring(L, reinterpret_cast<char*>(vecptr(result)), result.size()); return 1; } SLUIFT_LUA_FUNCTION_WITH_HELP( Sluift, sleep, "Sleeps for the given time.", "milliseconds the amount of milliseconds to sleep", "" ) { Sluift::globals.eventLoop.runOnce(); int timeout = Lua::checkIntNumber(L, 1); Watchdog watchdog(timeout, Sluift::globals.networkFactories.getTimerFactory()); while (!watchdog.getTimedOut()) { Swift::sleep(boost::numeric_cast<unsigned int>(std::min(100, timeout))); Sluift::globals.eventLoop.runOnce(); } return 0; } SLUIFT_LUA_FUNCTION_WITH_HELP( Sluift, new_uuid, "Generates a new UUID", "", "" @@ -376,48 +403,58 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { // Register functions Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, "Sluift"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "JID"); lua_setfield(L, -2, "jid"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "Base64"); lua_setfield(L, -2, "base64"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "IDN"); lua_setfield(L, -2, "idn"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "Crypto"); lua_setfield(L, -2, "crypto"); #ifdef HAVE_ITUNES Lua::FunctionRegistry::getInstance().createFunctionTable(L, "iTunes"); lua_setfield(L, -2, "itunes"); #endif // Register convenience functions lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> coreLibExports = boost::assign::list_of ("tprint")("disco")("help")("get_help")("copy")("with")("read_file")("create_form"); foreach (const std::string& coreLibExport, coreLibExports) { lua_getfield(L, -1, coreLibExport.c_str()); lua_setfield(L, -3, coreLibExport.c_str()); } lua_pop(L, 1); // Load client metatable lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> tables = boost::assign::list_of("Client"); foreach(const std::string& table, tables) { lua_getfield(L, -1, table.c_str()); Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, table); lua_pop(L, 1); } lua_pop(L, 1); + // Load component metatable + lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); + std::vector<std::string> comp_tables = boost::assign::list_of("Component"); + foreach(const std::string& table, comp_tables) { + lua_getfield(L, -1, table.c_str()); + Lua::FunctionRegistry::getInstance().addFunctionsToTable(L, table); + lua_pop(L, 1); + } + lua_pop(L, 1); + // Register documentation for all elements foreach (boost::shared_ptr<LuaElementConvertor> convertor, Sluift::globals.elementConvertor.getConvertors()) { boost::optional<LuaElementConvertor::Documentation> documentation = convertor->getDocumentation(); if (documentation) { Lua::registerClassHelp(L, documentation->className, documentation->description); } } // Register global documentation Lua::registerExtraHelp(L, -1, "sluift"); return 1; } |
Swift