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

[Difficult][Solved]Repeat Decryption

Started by CCGrimHaxor, 11 July 2014 - 01:33 PM
CCGrimHaxor #1
Posted 11 July 2014 - 03:33 PM
Hi guys today it is a difficult thing I mostly have it done:
So I have created custom main computer and I am forcing connection to that computer from the repeat program.
So the pc sends a message to the repeat program then the repeat program sends that message to the main PC in wich it decrypts the message and sends it to the owner and logs the decrypted message. The problem I am having is that the message that is sent from the repeat program is actually a table and I was wondering how do I get the actual message from that table here is an example on how I want to do it:

Code:

local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
local msg = decrypt(message.message()) -- IDK what it is here
print(msg)

Output:
Spoilerthis is a message

So that's how I wanna do it. I need the message from the table before the senderDistance.

How do I know it is a table? This is inside the repeat program:

if type( tMessage ) == "table" and tMessage.nMessageID and tMessage.nRecipient then

Solution:
SpoilerWhat I did was I checked how rednet handles messages and I came across this:

local tMessage = {
		nMessageID = nMessageID,
		nRecipient = nRecipient,
		message = message,
		sProtocol = sProtocol,
	}
The table is then sent to the id specified here:

peripheral.call( sModem, "transmit", nRecipient, nReplyChannel, tMessage );
So when we type the message it is saved in a table called tMessage as message so that table gets sent over rednet to the computer.
But we want to capture it so we use this code:

modem.open(os.getComputerID())
local event, modemSide, senderChannel, replyChannel, msg = os.pullEvent("modem_message")
That will receive the message table and put it inside the msg variable. With that done we still have some work to do.
Now you may be wondering why just not do:
local message = msg.message
well the reason for that is the message is a table not a string so what we need to do is the following:

local message = "" -- This will create a simple string where the message will be put
if type( msg.message ) == "table" then -- We have to make sure that the message is a table!
for i, v in pairs(msg.message) do -- we go through the message table
  message = message.." "..v -- we write the line from the table to message
end -- And we are done
else -- If it is not a table
	   message = msg.message -- Just put it inside the message
end
What that does is it checks if the msg.message is a table and if it is then we loop through the table and put the message tougether but if it is not then just put the message as it is.

Final Code:

local event, modemSide, senderChannel, replyChannel, msg = os.pullEvent("modem_message")
local message = ""
if type( msg.message ) == "table" then
for i, v in pairs(msg.message) do
  message = message.." "..v
end
else
		message = msg.message
end

Hope it fixes the problem for you :D/> I tried my best to explain it

Thanks.
Edited on 12 July 2014 - 12:55 PM
Cranium #2
Posted 11 July 2014 - 04:15 PM
Instead of requesting this topic be locked, could you post your resolution? It'd help others who are having the same problem as you, so that they can search the forums.
CCGrimHaxor #3
Posted 12 July 2014 - 02:56 PM
Instead of requesting this topic be locked, could you post your resolution? It'd help others who are having the same problem as you, so that they can search the forums.
I have added a solution and how everything works.
Can you please lock the topic.
Thanks
Lyqyd #4
Posted 12 July 2014 - 02:57 PM
We don't generally lock Ask a Pro topics.