This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Dave-ee Jones's profile picture

load() multiple functions in a string?

Started by Dave-ee Jones, 22 September 2017 - 02:48 AM
Dave-ee Jones #1
Posted 22 September 2017 - 04:48 AM
Hoi,

So, I'm trying to load multiple functions in a string, passing them to load(), but whenever I pass them the load() doesn't do ANYTHING. I don't know why..
The string looks like this:

local testString = [[os.queueEvent('char','t')
os.queueEvent('char','e')
os.queueEvent('char','s')
os.queueEvent('char','t')]]

I've tried the other way of doing it..

local testString = "os.queueEvent('char','t'); os.queueEvent('char','e'); os.queueEvent('char','s'); os.queueEvent('char','t')"

Just for clarification, the function is being sent over rednet to another computer which listens with this bit of code:

while true do
  local event,id,msg = os.pullEvent()
  if event == "rednet_message" then
	load(msg)()
  end
end

It does work if I just do one function in the string (e.g. 'os.queueEvent('char','t')'), but whenever I add more it doesn't like it.
I know I could pass a function over rednet and call it over the other side but I want to get this working, because it should be and since it's not I needa fix it!
Edited on 22 September 2017 - 02:49 AM
Lupus590 #2
Posted 22 September 2017 - 11:42 AM
I know I could pass a function over rednet

Currently you can't do this.

Edit: It might be worth checking the source of the load function in bios.
Edited on 22 September 2017 - 10:04 AM
supernicejohn #3
Posted 22 September 2017 - 03:44 PM
If you could send one function via a string, maybe wrap all functions in one anonymous function?
Not tested :P/>
SquidDev #4
Posted 22 September 2017 - 06:08 PM
The code you've provided above should function perfectly fine. For instance:

load([[os.queueEvent("char", "x") os.queueEvent("char", "y")]])()
adds "xy" to the prompt. I suspect something elsewhere in your code is pulling events.
Dave-ee Jones #5
Posted 25 September 2017 - 01:39 AM
I know I could pass a function over rednet
Currently you can't do this.

You can..? Unless you mean you can't send functions over rednet but you can using the modem API?

--# 0's code
local function test()
  print("test")
end
rednet.send(1,test)
--# 1's code
local id,msg = os.pullEvent("rednet_message")
msg()

If you can't do it like that you could just send a table with the function in the table, with other items (like what some remote shell programs do). Or you could just do what I'm trying to do and load a string.

The code you've provided above should function perfectly fine. For instance:

load([[os.queueEvent("char", "x") os.queueEvent("char", "y")]])()
adds "xy" to the prompt. I suspect something elsewhere in your code is pulling events.

Yeah, you're probably right. I'll keep playing with it.
SquidDev #6
Posted 25 September 2017 - 01:44 AM
You can..? Unless you mean you can't send functions over rednet but you can using the modem API?
rednet uses the modems behind the scenes, so for this one can consider them equivalent. It wouldn't make a difference if you put it in a table - it just gets converted to nil. You can send a string to be executed, but that's opening yourself to all sorts of exploits - you'd want some form of security before going down that route.
Dave-ee Jones #7
Posted 25 September 2017 - 01:54 AM
You can..? Unless you mean you can't send functions over rednet but you can using the modem API?
rednet uses the modems behind the scenes, so for this one can consider them equivalent. It wouldn't make a difference if you put it in a table - it just gets converted to nil. You can send a string to be executed, but that's opening yourself to all sorts of exploits - you'd want some form of security before going down that route.

K. I assumed that rednet did some annoying magic that made all sent functions nil, but modems sent packets as they were without rednet 'filtering'. But never mind. So the only real way of executing functions across rednet is to have the client have a function that uses the rednet message's contents as arguments OR execute a string sent over rednet. Annoying :/ I wonder why mine isn't working then..

In terms of security, I was thinking about doing something like a rolling code system. Have the client and host know a passcode to decrypt/encrypt the code while it's sent over their modems. The host decides the new code everytime, while the client receives it and decrypts the code, but sends the actual code (unencrypted) when it wants to use it. Makes it useless to outsiders reading the messages because the host replies with an encrypted code that was just generated by it, allowing the client to have a code for next time.

Samy Kamkar can't hack this one, haha! (Unless of course he hacks one of the computers and steals the passcode..).
Edited on 25 September 2017 - 12:55 AM
Bomb Bloke #8
Posted 25 September 2017 - 07:13 AM
I wonder why mine isn't working then..

I suspect something elsewhere in your code is pulling events.
Dave-ee Jones #9
Posted 25 September 2017 - 07:29 AM
I suspect something elsewhere in your code is pulling events

Yeah, you're probably right. I'll keep playing with it.

2 can play at that game. :P/>
Lupus590 #10
Posted 25 September 2017 - 10:01 AM
This may be useful for security: http://www.computercraft.info/forums2/index.php?/topic/27228-blink-communication-api-with-sender-verification/