Skip to content

Commit

Permalink
Add optional Lua scripting capabilties
Browse files Browse the repository at this point in the history
  • Loading branch information
kannibalox committed Nov 6, 2024
1 parent ee12073 commit 286eb35
Show file tree
Hide file tree
Showing 10 changed files with 1,179 additions and 0 deletions.
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ AC_LANG_PUSH(C++)
TORRENT_WITH_XMLRPC_C
AC_LANG_POP(C++)

TORRENT_WITH_LUA

AC_DEFINE(HAVE_CONFIG_H, 1, true if config.h was included)
AC_DEFINE(USER_AGENT, [std::string(PACKAGE "/" VERSION "/") + torrent::version()], Http user agent)

Expand Down
36 changes: 36 additions & 0 deletions rtorrent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- "rtorrent" is a global variable set by the module loading call
-- Autocall
local mt = {}
function mt.__call (t, ...)
name = table.concat(rawget(t, "__namestack"), ".")
success, ret = pcall(rtorrent.call, name, ...)
if not success then error(name..": "..ret, 2) end
return ret
end
function mt.__index (t, key)
ns = rawget(t, "__namestack") or {}
table.insert(ns, key)
return setmetatable({__namestack=ns}, mt)
end
rtorrent["autocall"] = setmetatable({}, mt)

-- autocall-config
local mt = {}
function mt.__call (t, ...)
name = table.concat(rawget(t, "__namestack"), ".")
success, ret = pcall(rtorrent.call, name, "", ...)
if not success then error(name..": "..ret, 2) end
return ret
end
function mt.__index (t, key)
ns = rawget(t, "__namestack")
if ns == nil then
if _G[key] ~= nil then return _G[key] end
ns = {}
end
table.insert(ns, key)
return setmetatable({__namestack=ns}, mt)
end
rtorrent["autocall_config"] = setmetatable({}, mt)

return rtorrent
659 changes: 659 additions & 0 deletions scripts/ax_lua.m4

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions scripts/checks.m4
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,25 @@ AC_DEFUN([TORRENT_WITH_XMLRPC_C], [
])


AC_DEFUN([TORRENT_WITH_LUA], [
AC_ARG_WITH(lua,
AS_HELP_STRING([--with-lua],[enable LUA support]),
[
if test "$withval" = "no"; then
AC_MSG_RESULT(no)
else
AX_PROG_LUA
AX_LUA_LIBS
AX_LUA_HEADERS
AC_DEFINE(HAVE_LUA, 1, Use LUA.)
LIBS="$LIBS $LUA_LIB"
CXXFLAGS="$CXXFLAGS $LUA_INCLUDE"
fi
],[
AC_MSG_RESULT(ignored)
])
])

AC_DEFUN([TORRENT_WITH_INOTIFY], [
AC_LANG_PUSH(C++)
Expand Down
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ libsub_root_a_SOURCES = \
rpc/xmlrpc.h \
rpc/xmlrpc.cc \
\
rpc/lua.h \
rpc/lua.cc \
\
ui/download.cc \
ui/download.h \
ui/download_list.cc \
Expand Down
8 changes: 8 additions & 0 deletions src/command_local.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "utils/file_status_cache.h"

#include "globals.h"
#include "rpc/lua.h"
#include "control.h"
#include "command_helpers.h"

Expand Down Expand Up @@ -232,6 +233,7 @@ void
initialize_command_local() {
core::DownloadList* dList = control->core()->download_list();
core::DownloadStore* dStore = control->core()->download_store();
rpc::LuaEngine* luaEngine = control->lua_engine();
torrent::ChunkManager* chunkManager = torrent::chunk_manager();
torrent::FileManager* fileManager = torrent::file_manager();

Expand Down Expand Up @@ -315,6 +317,12 @@ initialize_command_local() {

CMD2_ANY_V ("session.save", std::bind(&core::DownloadList::session_save, dList));

#ifdef HAVE_LUA
CMD2_ANY ("lua.execute", std::bind(&rpc::execute_lua, luaEngine, std::placeholders::_1, std::placeholders::_2, 0));
CMD2_ANY ("lua.execute.str", std::bind(&rpc::execute_lua, luaEngine, std::placeholders::_1, std::placeholders::_2, rpc::LuaEngine::flag_string));
CMD2_ANY ("lua.import", std::bind(&rpc::execute_lua, luaEngine, std::placeholders::_1, std::placeholders::_2, rpc::LuaEngine::flag_autocall_upvalue));
#endif

#define CMD2_EXECUTE(key, flags) \
CMD2_ANY(key, std::bind(&rpc::ExecFile::execute_object, &rpc::execFile, std::placeholders::_2, flags));

Expand Down
3 changes: 3 additions & 0 deletions src/control.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "rpc/command_scheduler.h"
#include "rpc/parse_commands.h"
#include "rpc/scgi.h"
#include "rpc/lua.h"
#include "rpc/object_storage.h"
#include "ui/root.h"

Expand All @@ -68,6 +69,7 @@ Control::Control() :

m_commandScheduler(new rpc::CommandScheduler()),
m_objectStorage(new rpc::object_storage()),
m_luaEngine(new rpc::LuaEngine()),
m_directory_events(new torrent::directory_events()),

m_tick(0),
Expand Down Expand Up @@ -99,6 +101,7 @@ Control::~Control() {
delete m_directory_events;
delete m_commandScheduler;
delete m_objectStorage;
delete m_luaEngine;
}

void
Expand Down
3 changes: 3 additions & 0 deletions src/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace rpc {
class CommandScheduler;
class XmlRpc;
class object_storage;
class LuaEngine;
}

namespace torrent {
Expand Down Expand Up @@ -101,6 +102,7 @@ class Control {

rpc::CommandScheduler* command_scheduler() { return m_commandScheduler; }
rpc::object_storage* object_storage() { return m_objectStorage; }
rpc::LuaEngine* lua_engine() { return m_luaEngine; }

torrent::directory_events* directory_events() { return m_directory_events; }

Expand All @@ -125,6 +127,7 @@ class Control {

rpc::CommandScheduler* m_commandScheduler;
rpc::object_storage* m_objectStorage;
rpc::LuaEngine* m_luaEngine;
torrent::directory_events* m_directory_events;

uint64_t m_tick;
Expand Down
Loading

0 comments on commit 286eb35

Please sign in to comment.