diff options
author | Remko Tronçon <git@el-tramo.be> | 2013-12-23 14:19:20 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2013-12-27 22:52:31 (GMT) |
commit | 74117f069f2339bace7c5b97bb79c6cc9a57bbc7 (patch) | |
tree | e2ccb18d395ebcd014de7a9188a6658cb0c0f06f /Sluift | |
parent | 8068f4a7a49f723809778e9194166046a3e5cb38 (diff) | |
download | swift-74117f069f2339bace7c5b97bb79c6cc9a57bbc7.zip swift-74117f069f2339bace7c5b97bb79c6cc9a57bbc7.tar.bz2 |
Sluift: Add utility functions
New functions:
- IDN functions
- UUID generator
Change-Id: I051dcad32c21107d90b4491b240814f8cf56925d
Diffstat (limited to 'Sluift')
-rw-r--r-- | Sluift/sluift.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index e6096a0..95e7101 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -21,12 +21,14 @@ #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 <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> @@ -80,6 +82,11 @@ SLUIFT_LUA_FUNCTION(Sluift, sleep) { return 0; } +SLUIFT_LUA_FUNCTION(Sluift, new_uuid) { + lua_pushstring(L, IDGenerator().generateID().c_str()); + return 1; +} + static int sluift_index(lua_State* L) { try { std::string key(Lua::checkString(L, 2)); @@ -214,6 +221,45 @@ SLUIFT_LUA_FUNCTION(Base64, decode) { } /******************************************************************************* + * IDN Functions + ******************************************************************************/ + +SLUIFT_LUA_FUNCTION(IDN, encode) { + IDNConverter* converter = Sluift::globals.networkFactories.getIDNConverter(); + lua_pushstring(L, converter->getIDNAEncoded(Lua::checkString(L, 1)).c_str()); + return 1; +} + +SLUIFT_LUA_FUNCTION(IDN, stringprep) { + IDNConverter* converter = Sluift::globals.networkFactories.getIDNConverter(); + IDNConverter::StringPrepProfile profile; + std::string profileString = Lua::checkString(L, 2); + if (profileString == "nameprep") { + profile = IDNConverter::NamePrep; + } + else if (profileString == "xmpp_nodeprep") { + profile = IDNConverter::XMPPNodePrep; + } + else if (profileString == "xmpp_resourceprep") { + profile = IDNConverter::XMPPResourcePrep; + } + else if (profileString == "saslprep") { + profile = IDNConverter::SASLPrep; + } + else { + throw Lua::Exception("Invalid profile"); + } + try { + lua_pushstring(L, converter->getStringPrepared(Lua::checkString(L, 1), profile).c_str()); + } + catch (const std::exception&) { + throw Lua::Exception("Error"); + } + return 1; +} + + +/******************************************************************************* * Module registration ******************************************************************************/ @@ -239,6 +285,8 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { 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"); // Register convenience functions lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.bootIndex); |