LUA - Calling function from other module without exporting table - lua

I try to setup a special behavior with Jitsi, but have not that much LUA knowlege.
A Jitsi/Prosody module "mod_muc_lobby_rooms.lua" is implementing some function like handle_create_lobby(event);. handle_create_lobby is calling other sub-function from inside.
https://github.com/jitsi/jitsi-meet/blob/master/resources/prosody-plugins/mod_muc_lobby_rooms.lua
But the module itself is not a library module, so no table is exported and another code can use "require". So my understanding from LUA yet.
For a own module, I just want use this functions from the other side, without reimplement or copy/paste it.
Is there any solution, how I can "source" the function into my module?
If possible, I want let "mod_muc_lobby_room.lua" unchanged, if some updates from Jitsi are coming.
Thanks in advance.
A lua beginner, Uwe

You can fire an event because it listen for it.
prosody.events.fire_event("create-lobby-room", event)
Or you can use the module function like this:
local muc_lobby_rooms = module:depends("muc_lobby_rooms");
muc_lobby_rooms.handle_create_lobby(event);

You can do it like that:
file=io.open("mod_muc_lobby_room.lua")
io.input(file)
load(io.read("*a"))()
io.close(file)
And the code located in mod_muc_lobby_room.lua will be executed.

Related

Hacking Lua - Inject new functions into built Lua

I am trying to hack a game (not for cheating though) by introducing new built-in methods and functions in order to communicate with the game using sockets. Here is a small "pseudo code" example of what I want to accomplish:
Inside the Lua code I am calling my_hack() and pass the current game state:
GameState = {}
-- Game state object to be passed on
function GameState:new()
-- Data
end
local gameState = GameState:new()
-- Collect game state data and pass it to 'my_hack' ..
my_hack(gameState)
and inside my_hack the object is getting sent away:
int my_hack(lua_State * l)
{
void* gameState= lua_topointer(l, 1);
// Send the game state:
socket->send_data(gameState);
return 0;
}
Now, the big question is how to introduce my_hack() to the game?
I assume, that all built in functions must be kept in some sort of lookup table. Since all Lua code is getting interpreted, functions like import etc. will have to be statically available, right? If that is correct, then it should be "enough" to find out where this code is residing in order to smuggle my code into the game that would allow me to call my_hack() in a Lua script.
There should be two options: The first is that the Lua built is embedded inside the executable and is completely static and the second is that all Lua code gets loaded dynamically from a DLL.
This question goes out to anybody who has a slightest clue about where and how I should keep looking for the built in functions. I've tried a few things with Cheat Engine but I wasn't too successful. I was able to cheat a bit ^^ but that's not what I'm looking out for.
Sorry for not providing a full answer, but if you can provide a custom Lua VM and change the standard libraries, you should be able to to change the luaL_openlibs method in the Lua source to provide a table with my_hack() inside of it.
Since the Lua interpreter is usually statically compiled into the host executable, modifying the interpreter in some way will probably not be possible.
I think your best bet is to find some piece of Lua code which gets called by the host, and from that file use dofile to run your own code.

retrieving the module object on Lua

I have a C program that uses Lua to run some scripts. I need to open the Lua libraries via C code like luaopen_socket_core(myLuaState), for some reasons I can't load the modules from the Lua code, like socket = require "luasocket".
Once understood the idea of this program now I need to load a library called struct, so I added the struct.c to my project, and when I tried to use its functions like struct.unpack the runtimer complains that there is no global variable called struct. Of course it was loaded with luaopen_struct(myLuaState) instead of struct = require "struct" which is forbidden for me.
Any suggestion about an way of having this struct variable available?
Take a look at luaL_requiref in the auxiliary library, which mimics require called from Lua.
You probably called the open-function directly and forgot to set those variables manually, that function would do it all for you.

Modular code structure in Lua

I've been working with Love2d recently to build a Conway's Game of Life implementation.
I really like the framework, but I haven't been able to figure out how to modularize my code, which I feel is crucial to solid code structure.
What I'm wanting to do is be able to import a file that has different functions in it and be able to access that through my main lua file. I have been able to write scripts and run entire files, but not specific functions.
Is there a way to do this in Lua? If so, how?
Thanks!
You can use the require function in LÖVE. It works similarly to how it works in Lua.
-- lib.lua
local lib = {} -- table to store the functions
function lib.inc(x)
return x + 1
end
return lib
And here is how you require it in another file (for example, main.lua) and use it:
local lib = require('lib')
function love.load()
print(lib.inc(1)) -- prints '2' in the terminal
end
Lua supports modules. Here is a tutorial on using them http://lua-users.org/wiki/ModulesTutorial

I just want to call some specific function in my Lua script. How to do that?

I just want to call some specific function in my Lua script.
A simple script:
msg("hello")
function showamsgbox()
msg("123")
end
I just want to let my C app call showamsgbox() only but not to run msg("hello") beacuse it will show a msgbox when i load this script! So how to do that to keep this situation away?
PS:it is just example.sometimes i want to let users make thier own plugins in my program.but I do not want them write something outside the functions(i want to use functions to decide what to do.for example function OnLoad() means it will be run when i load it ).If there is something outside functions i cannot control them!
You can't. The script defines two variables when run: a and geta. Recall that function geta()...end is the same as geta=function()...end.
The a = 9 will be called when the script is initially evaluated in a lua_State.
If you reuse that lua_State instance, you can retrieve the function and invoke it without re-initializing a.
It seems that you want to sandbox scripts. Just give them a suitable, separate environment before running them. It may be an empty one or it may contain references to the functions you want them to use. They can write at will in their environment and it will not affect yours. Then just get the value of OnLoad or whatever user function you want to call and call it.

In Erlang how can I compile a module from within a module?

I have tried :
c(module_name).
: but this only works from the shell, and gives an error when I try to run it from within a module.
If you want exactly that behaviour, c:c(module_name) will call the same function called by the shell. I would hesitate to put code that calls user_default (c) functions in production code, so you might want to look at the source for the function and replicate it in your own code so you don't get bitten by a behaviour change in a future erlang release.
You might want to have a look to the compile module and to the compile:file/2 function in specific.

Resources