summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2014-07-11 18:33:44 (GMT)
committerSwift Review <review@swift.im>2014-07-14 14:39:03 (GMT)
commit00375bb2ac48dae174889ed9cea8dc8de55e1efd (patch)
treeb4995c82217dd9ac0fbd9d9b9f371f8b39c6d933
parent4386fa0e6fa8c361d51ec085aefa2d15a61d399a (diff)
downloadswift-contrib-00375bb2ac48dae174889ed9cea8dc8de55e1efd.zip
swift-contrib-00375bb2ac48dae174889ed9cea8dc8de55e1efd.tar.bz2
Sluift: Add 'fs' module
The 'fs' module provides filesystem functions. Currently has function to list directory contents and test whether a path is a file. Test-Information: Tested in external script. Change-Id: I14ba614b0b3bd52f5d9e87a40dc6477d99604d88
-rw-r--r--Sluift/sluift.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp
index 2fd1e50..5e837c1 100644
--- a/Sluift/sluift.cpp
+++ b/Sluift/sluift.cpp
@@ -1,49 +1,50 @@
/*
* 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 <boost/filesystem.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[];
@@ -219,70 +220,101 @@ SLUIFT_LUA_FUNCTION_WITH_HELP(
/*******************************************************************************
* Crypto functions
******************************************************************************/
SLUIFT_LUA_FUNCTION_WITH_HELP(
Crypto, new_certificate,
"Creates a new X.509 certificate from DER data.\n",
"der the DER-encoded certificate data",
"") {
ByteArray certData(Lua::checkByteArray(L, 1));
Certificate::ref cert(Sluift::globals.tlsFactories.getCertificateFactory()->createCertificateFromDER(certData));
lua_createtable(L, 0, 0);
lua_pushstring(L, cert->getSubjectName().c_str());
lua_setfield(L, -2, "subject_name");
lua_pushstring(L, Certificate::getSHA1Fingerprint(cert, Sluift::globals.networkFactories.getCryptoProvider()).c_str());
lua_setfield(L, -2, "sha1_fingerprint");
Lua::pushStringArray(L, cert->getCommonNames());
lua_setfield(L, -2, "common_names");
Lua::pushStringArray(L, cert->getSRVNames());
lua_setfield(L, -2, "srv_names");
Lua::pushStringArray(L, cert->getDNSNames());
lua_setfield(L, -2, "dns_names");
Lua::pushStringArray(L, cert->getXMPPAddresses());
lua_setfield(L, -2, "xmpp_addresses");
Lua::registerTableToString(L, -1);
return 1;
}
+/*******************************************************************************
+ * Filesystem Functions
+ ******************************************************************************/
+
+SLUIFT_LUA_FUNCTION(FS, list) {
+ boost::filesystem::path dir(std::string(Lua::checkString(L, 1)));
+ if (!boost::filesystem::exists(dir) || !boost::filesystem::is_directory(dir)) {
+ lua_pushnil(L);
+ lua_pushstring(L, "Argument is not an existing directory");
+ return 2;
+ }
+
+ boost::filesystem::directory_iterator i(dir);
+ std::vector<boost::filesystem::path> items(
+ i, boost::filesystem::directory_iterator());
+
+ lua_createtable(L, boost::numeric_cast<int>(items.size()), 0);
+ for (size_t i = 0; i < items.size(); ++i) {
+ lua_pushstring(L, items[i].string().c_str());
+ lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
+ }
+ Lua::registerTableToString(L, -1);
+ return 1;
+}
+
+SLUIFT_LUA_FUNCTION(FS, is_file) {
+ boost::filesystem::path file(std::string(Lua::checkString(L, 1)));
+ lua_pushboolean(L, boost::filesystem::is_regular_file(file));
+ return 1;
+}
+
/*******************************************************************************
* JID Functions
******************************************************************************/
SLUIFT_LUA_FUNCTION(JID, to_bare) {
JID jid(std::string(Lua::checkString(L, 1)));
lua_pushstring(L, jid.toBare().toString().c_str());
return 1;
}
SLUIFT_LUA_FUNCTION(JID, node) {
JID jid(std::string(Lua::checkString(L, 1)));
lua_pushstring(L, jid.getNode().c_str());
return 1;
}
SLUIFT_LUA_FUNCTION(JID, domain) {
JID jid(std::string(Lua::checkString(L, 1)));
lua_pushstring(L, jid.getDomain().c_str());
return 1;
}
SLUIFT_LUA_FUNCTION(JID, resource) {
JID jid(std::string(Lua::checkString(L, 1)));
lua_pushstring(L, jid.getResource().c_str());
return 1;
}
SLUIFT_LUA_FUNCTION(JID, escape_node) {
lua_pushstring(L, JID::getEscapedNode(Lua::checkString(L, 1)).c_str());
return 1;
}
/*******************************************************************************
@@ -378,70 +410,72 @@ SLUIFT_LUA_FUNCTION(iTunes, get_current_track) {
/*******************************************************************************
* Module registration
******************************************************************************/
static const luaL_Reg sluift_functions[] = { {NULL, NULL} };
SLUIFT_API int luaopen_sluift(lua_State* L) {
// Initialize & store the module table
luaL_register(L, lua_tostring(L, 1), sluift_functions);
lua_pushinteger(L, -1);
lua_setfield(L, -2, "timeout");
lua_pushboolean(L, 0);
lua_setfield(L, -2, "debug");
lua_pushvalue(L, -1);
Sluift::globals.moduleLibIndex = luaL_ref(L, LUA_REGISTRYINDEX);
// Load core lib code
if (luaL_loadbuffer(L, core_lua, core_lua_size, "core.lua") != 0) {
lua_error(L);
}
lua_pushvalue(L, -2);
lua_call(L, 1, 1);
Sluift::globals.coreLibIndex = luaL_ref(L, LUA_REGISTRYINDEX);
// 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");
+ Lua::FunctionRegistry::getInstance().createFunctionTable(L, "FS");
+ lua_setfield(L, -2, "fs");
#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);