Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This is a non-tested version so report any bugs you find please
- -- This need my GUI API
- function login(password)
- print("Please enter your password :")
- local pass = io.read("*")
- shell.run("clear")
- if pass == password then
- print("Access granted")
- return "true"
- else
- print("Wrong password")
- os.sleep(2)
- return
- end
- end
- x, y = term.getSize()
- midX = x/2
- midY = y/2
- local function protocol(msg)
- local fPrint, sPrint = string.find(msg, "print$%a+$")
- local fNameFile, sNameFile = string.find(msg, "Nfile$%a+$")
- local fDataFile, sDataFile = string.find(msg, "Dfile$%a+$")
- local fCode, sCode = string.find(msg, "code$%a+$")
- local fSendId, sSendId = string.find(msg, "sendId$%d+$")
- local fSendData, sSendData = string.find(msg, "sendData$%a+$")
- 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 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
- end
- local function nReceive()
- local idSender, msg, distance = rednet.receive()
- protocol(msg)
- return idSender, msg, distance
- end
- function createConfig()
- if fs.exists("/clientFolder/client.config") then
- return "nothing"
- else
- print("Thanks you for download this rednet protocol")
- print("You are going to configure this program, follow this step-by-step configuration")
- os.sleep(3)
- shell.run("clear")
- print("First enter the side of your modem")
- local modemSide = io.read()
- shell.run("clear")
- print("Type your server ip")
- local ipServ = io.read()
- shell.run("clear")
- print("Enter your password :")
- local passToSave = read()
- term.clear()
- print("You have finished the configuration of your program")
- fs.makeDir("clientFolder")
- fs.makeDir("usermade")
- local conf = io.open("/clientFolder/client.config", "w")
- local data = {side = modemSide, id = ipServ, pass = passToSave}
- local dataS = textutils.serialize(data)
- local writeIsFail = conf:write(dataS)
- conf:close()
- if writeIsFail then
- print("Config succesfully writed")
- else
- print("Cannot write in the config file")
- end
- end
- end
- function loadConfig()
- gui.clear()
- gui.drawHeader("Loading...")
- if fs.exists("/clientFolder/client.config") then
- local conf = fs.open("/clientFolder/client.config", "r")
- local dataS = conf.readAll()
- conf.close()
- local data = textutils.unserialize(dataS)
- local id = data.id
- local side = data.side
- local pass = data.pass
- rednet.open(side)
- gui.drawHeader("Loaded !")
- return id, pass
- else
- print("Cannot find the config file, please restart this program")
- os.sleep(3)
- return
- end
- end
- local function drawMain()
- term.setColorBackground(colors.lightBlue)
- gui.setHeader("Network console", colors.blue)
- gui.setButton(10, 5, "Send", colors.blue, colors.lightBlue)
- gui.setButton(18, 5, "Receive", colors.blue, colors.lightBlue)
- gui.setButton(40, 17, "Exit", colors.blue, colors.lightBlue)
- gui.render(true)
- selectMain()
- end
- local function selectMain()
- local mainSel = gui.getSelection()
- if mainSel == 1 then
- drawSend()
- elseif mainSel == 2 then
- msgReceive()
- elseif mainSel == 3 then
- gui.clear()
- return
- else
- error("selectMain : wrong selection")
- end
- end
- local function drawSend()
- gui.setHeader("Network console", colors.blue)
- gui.setButton(3, 5, "Send command", colors.blue, colors.lightBlue)
- gui.setButton(3, 9, "Send file", colors.blue, colors.lightBlue)
- gui.setButton(3, 13, "Cut the communication", colors.blue, colors.lightBlue)
- gui.setButton(40, 17, "Exit", colors.blue, colors.lightBlue)
- gui.setButton(20, 27, "Cancel", colors.blue, colors.lightBlue)
- gui.render(true)
- selectSend()
- end
- local function selectSend()
- local sendSel = gui.getSelection()
- if sendSel == 1 then
- sendCommand()
- elseif sendSel == 2 then
- drawFile()
- elseif sendSel == 3 then
- sendExit()
- elseif sendSel == 4 then
- gui.clear()
- return
- elseif sendSel == 5 then
- drawMain()
- else
- error("selectSend : wrong selection")
- end
- end
- local function msgReceive()
- local idSender, msg = nReceive()
- local msgT = textutils.unserialize(msg)
- local idAdressee = msgT[1]
- local type = msgT[2]
- local body = msgT[3]
- if type == "#/program#" then
- local idSender, arg = rednet.receive()
- shell.run(body, arg)
- elseif type == "#/file#" then
- local idSender, toDo = rednet.receive()
- if toDo == "%save%" then
- local idSender, nameOfFile = rednet.receive()
- local file = fs.open("/usermade/"..nameOfFile, "w")
- file.write(body)
- file.close()
- rednet.send(tonumber(idSender), "<saved>")
- elseif toDo == "%run%" then
- loadstring(body)
- rednet.send(tonumber(idSender), "<executed>")
- else
- rednet.send(tonumber(idSender), "<error:fileinfo:wrongcommand>")
- end
- elseif type == "#/command#" then
- local idSender, command = rednet.receive()
- if command == "#/exit#" then
- return
- else
- rednet.send(tonumber(idSender), "<error:commandinfo:wrongcommand>")
- end
- else
- rednet.send(tonumber(idSender), "<error:typerequest:wrongtype>")
- end
- end
- term.clear()
- shell.run("id")
- local exit = 0
- local logged = "false"
- createConfig()
- local id, password = loadConfig()
- local id = tonumber(id)
- while logged == "false" do
- local logged = login(password)
- end
- message()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement