From 74117f069f2339bace7c5b97bb79c6cc9a57bbc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Mon, 23 Dec 2013 15:19:20 +0100 Subject: Sluift: Add utility functions New functions: - IDN functions - UUID generator Change-Id: I051dcad32c21107d90b4491b240814f8cf56925d 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 #include #include +#include #include #include #include #include #include #include +#include #include #include @@ -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); -- cgit v0.10.2-6-g49f6