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

What Is Wrong With This Pattern ? Solved

Started by bjornir90, 17 July 2013 - 10:20 AM
bjornir90 #1
Posted 17 July 2013 - 12:20 PM
Hello !
So basically the problem is that this pattern detect nothing, so what is wrong ?


Thanks you :)/>/>




--This is server side please install it on a special computer with nothing else


--To see how to play go to : (url post)
--Hack it !
--Made by Bjornir90
--Based on hack the game
--If you want to use part of this code, ask me before doing anything please.



fs.makeDir("hack_it")
fs.makeDir("hack_it/files")
fs.makeDir("hack_it/files/movies")
fs.makeDir("hack_it/files/pictures")
fs.makeDir("hack_it/files/movies/child")
fs.makeDir("hack_it/files/programs")

-- Definitions
local x, y = term.getSize()
local midX = x/2
local midY = y/2
local modem = peripheral.wrap("top")
local channel = 1
modem.open(channel)
IP = {} --Simply the id of computers, not local to allow hacking
local stop = "false"
local actualChannel = 1


--Functions
local function switchChan()
local newChannel = math.random(1, 65535)
modem.transmit(actualChannel, actualChannel, "#switch#")
modem.transmit(actualChannel, actualChannel, tostring(newChannel))
print("Sent request to change channel to "..newChannel)
modem.closeAll()
modem.open(newChannel)
local actualChannel = newChannel
end

--HERE __________________________________________

local function protocol(msg)
print("Started check for BRP request")	 --Print it
local fIPsender, sIPsender = string.find(msg, "ip<%d+>")
local fPrint, sPrint = string.find(msg, "print<%a+>") --Search for thing to print
local fNameFile, sNameFile = string.find(msg, "Nfile<%a+>") --Search for name of file to save
local fDataFile, sDataFile = string.find(msg, "Dfile<%a+>") --Search for things to save into th--e file
local fCode, sCode = string.find(msg, "code<%a+>") --Search for a command
local fSendId, sSendId = string.find(msg, "sendId<%d+>") --Search for an id to forward to
local fSendData, sSendData = string.find(msg, "sendData<%a+>") --Search for the data to forward to
local fUpload, sUpload = string.find(msg, "download<%a+>") --Search for a download request
--Check for what we must do
print(fIPsender)			--Print a blank line
sleep(2)
if fIPsender then
print("Found ip of sender")	--It didn't print that
local ipSender = string.sub(msg, fIPsender+3, sIPsender-1)
for i=1, #IP do
  if ipSender == IP[i] then
   print("Request from"..ipSender)
   if fPrint then		  
	shPrint = "true"
   end
   if fNameFile and fDataFile then
	shFile = "true"
   end
   if fCode then
	shCode = "true"
   end
   if fSendId and fSendData  then
	shSend = "true"
   end
   if fUpload and sUpload then
	shUpload = "true"
   end

if shPrint then
  dataToPrint = string.sub(msg, fPrint+6, sPrint-1)
  term.clear()
  term.setCursorPos(midX-#dataToPrint/2, midY)
  print(dataToPrint)
end
if shCode then
  dataToLoad = string.sub(msg, fCode+5, sCode-1)
  loadstring(dataToLoad)
end
if shSend then
  dataToSend = string.sub(msg, fSendData+9, sSendData-1)
  idToSend = tonumber(string.sub(msg, fSendId+7, sSendId-1))
  rednet.send(idToSend, dataToSend)
end
if shFile then
  dataTWF = string.sub(msg, fDataFile+6, sDataFile-1)
  nameFile = string.sub(msg, fNameFile+6, sNameFile-1)
  fileTW = fs.open(nameFile, "w")
  fileTW.write(dataTWF)
  fileTW.close()
end
if shUpload then
  local path = string.sub(msg, fUpload+9, sUpload-1)
  local file = fs.open("Hack_it/"..path, "r")
  local toUpload = file.readAll()
  modem.transmit(actualChannel, actualChannel, toUpload)
  file.close()
end
else
  local logFile = fs.open("/Hack_it/log", "a")
  logFile.write("<detected<\nIntrusion detected, system not harmed\n")
  logFile.close()
end
end
end
end

--End



--Actual game
while stop == "false" do
local messageT = {os.pullEvent("modem_message")}
if messageT[5] == "#end#" then
  stop = "true"
end
if messageT[5] == "won" then
  modem.open(1)
  if IP[2] then
  modem.transmit(1, 1, "true")
  else
  modem.transmit(1,1,"false")
  end
end
protocol(messageT[5])
print("Received message :"..messageT[5])
switchChan()
if not fs.exists("/Hack_it/files/firewall.bat") then
  if not fs.exists("/Hack_it/files/programs/traceur.exe") then
   modem.transmit(actualChannel, actualChannel, "true")
  end
end
end

ETHANATOR360 #2
Posted 17 July 2013 - 12:41 PM
please post all the code
bjornir90 #3
Posted 17 July 2013 - 12:49 PM
Posted full code in the OP
Xenon #4
Posted 17 July 2013 - 03:58 PM
Use parentheses instead of < > would be my suggestion, although not sure about this.
Grim Reaper #5
Posted 17 July 2013 - 04:15 PM
Use parentheses instead of < > would be my suggestion, although not sure about this.

Xenon is right. The <> characters aren't special characters when working with Lua patterns (from my knowledge and testing just now :P/>).

This is the pattern that I would use to look for print statements:

local _string = "print ('Test!')"
local startPos, endPos = _string:find ("print%s-%(%p-.-%p-%)")
bjornir90 #6
Posted 18 July 2013 - 03:03 PM
I choose the <> because they are not special characters : they act as string delimitation in my protocol. Anyway thanks you for help, but I still don't understand what is wrong with that…..
Grim Reaper #7
Posted 19 July 2013 - 12:27 AM
Could you give us a sample message, then? If a print statement in one of the messages looks like

print<"Hello, what's up?">
or

print<Hello, what's up?>
then you're not searching for all of the possible characters in a parameter to the function call (including the quotes to specify a string in the first example).

However, if a print call in one of the messages you're parsing looks like

print<abcdefghijklmnopqrstuvwxyz>
or

print<ABCDEFGHIJKLMNOPQRSTUVWXYZ>
or any combination of the two, then you're pattern should work just fine… :P/>
bjornir90 #8
Posted 19 July 2013 - 03:51 AM
Could you give us a sample message, then? If a print statement in one of the messages looks like

print<"Hello, what's up?">
or

print<Hello, what's up?>
then you're not searching for all of the possible characters in a parameter to the function call (including the quotes to specify a string in the first example).

However, if a print call in one of the messages you're parsing looks like

print<abcdefghijklmnopqrstuvwxyz>
or

print<ABCDEFGHIJKLMNOPQRSTUVWXYZ>
or any combination of the two, then you're pattern should work just fine… :P/>
The message are like the second message but I found the probleme : I was not searching for optional space and punctuation….. Thanks you a lot ! Now I need to figure out how I will detect that in every order possible ( like "hello, I'm me" or "There is no punctuation"…
Grim Reaper #9
Posted 19 July 2013 - 05:03 AM
You could do something like this:


local startPos, endPos = string.find (_string, "print%s-<.->")
bjornir90 #10
Posted 19 July 2013 - 06:30 AM
Ok Thanks you ! I will try this :)/>