Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional Lua scripting capabilties #1260

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

kannibalox
Copy link
Contributor

@kannibalox kannibalox commented Jan 18, 2024

This was tested on Lua 5.4, since while there shouldn't be any version-specific features in use, the lack of native 64-bit integer support in versions before 5.3 seems like a deal breaker.

There are three RPC commands added:
lua.execute, which allows executing Lua code, with a rtorrent package that has basic support for making direct RPC calls from a file:

local ofile = io.open("/tmp/out.lua.txt", "w")
-- Purposefully inefficient as a demonstration of `rtorrent.call`
for _, result in ipairs(rtorrent.call("d.multicall2", "", "main", "d.hash=")) do
   ofile:write(rtorrent.call("d.name", result[1]).."\n")
end
-- Alternative syntactical sugar
r = rtorrent.autocall
ofile:write(r.system.hostname())
ofile:close()

That's a little verbose to be a replacement for rtorrent.rc, so there's also lua.import, which uses the same "autocall" Lua magic for a simpler syntax:

local base = "/tmp/rtorrent/"
scgi_port("0.0.0.0:9998")
session(base.."session")
directory(base.."data")
network.http.max_open.set(256)

lua.execute.str is mainly to allow events, but could it definitely use a more convenient syntax:

local function insert_method(name, func_name, arg0, arg1, arg2, arg3)
   args = {
      arg0 or "$argument.0=",
      arg1 or "$argument.1=",
      arg2 or "$argument.2=",
      arg3 or "$argument.3=",
   }
   arg_str = table.concat(args, ",")
   rtorrent.call("method.insert", "", name, "simple", "lua.execute.str=\"return "..func_name.."(...)\","..arg_str)
end
function event_handler(target, name)
   rtorrent.call("print", "", "Lua event: Inserted \""..target..": "..name.."\"")
end
insert_method("d.event_handler", "event_handler", "(d.name)")

rtorrent.call("method.set_key", "", "event.download.inserted", "print_name", "d.event_handler=")

Implements #490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant