Advertisement
Bjornir90

Clientest.lua

Nov 15th, 2012
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.00 KB | None | 0 0
  1. -- This is a non-tested version so report any bugs you find please
  2. -- This need my GUI API
  3. function login(password)
  4.  print("Please enter your password :")
  5.  local pass = io.read("*")
  6.  shell.run("clear")
  7.  if pass == password then
  8.   print("Access granted")
  9.   return "true"
  10.  else
  11.   print("Wrong password")
  12.   os.sleep(2)
  13.   return
  14.  end
  15. end
  16.  
  17. x, y = term.getSize()
  18. midX = x/2
  19. midY = y/2
  20.  
  21. local function protocol(msg)
  22.  local fPrint, sPrint = string.find(msg, "print$%a+$")
  23.  local fNameFile, sNameFile = string.find(msg, "Nfile$%a+$")
  24.  local fDataFile, sDataFile = string.find(msg, "Dfile$%a+$")
  25.  local fCode, sCode = string.find(msg, "code$%a+$")
  26.  local fSendId, sSendId = string.find(msg, "sendId$%d+$")
  27.  local fSendData, sSendData = string.find(msg, "sendData$%a+$")
  28.  
  29.  if fPrint then
  30.   shPrint = "true"
  31.  end
  32.  if fNameFile and fDataFile then
  33.   shFile = "true"
  34.  end
  35.  if fCode then
  36.   shCode = "true"
  37.  end
  38.  if fSendId and fSendData  then
  39.   shSend = "true"
  40.  end
  41.  
  42.  if shPrint then
  43.   dataToPrint = string.sub(msg, fPrint+6, sPrint-1)
  44.   term.clear()
  45.   term.setCursorPos(midX-#dataToPrint/2, midY)
  46.   print(dataToPrint)
  47.  end
  48.  if shCode then
  49.   dataToLoad = string.sub(msg, fCode+5, sCode-1)
  50.   loadstring(dataToLoad)
  51.  end
  52.  if shSend then
  53.   dataToSend = string.sub(msg, fSendData+9, sSendData-1)
  54.   idToSend = tonumber(string.sub(msg, fSendId+7, sSendId-1))
  55.   rednet.send(idToSend, dataToSend)
  56.  end
  57.  if shFile then
  58.   dataTWF = string.sub(msg, fDataFile+6, sDataFile-1)
  59.   nameFile = string.sub(msg, fNameFile+6, sNameFile-1)
  60.   fileTW = fs.open(nameFile, "w")
  61.   fileTW.write(dataTWF)
  62.   fileTW.close()
  63.  end
  64. end
  65.  
  66. local function nReceive()
  67.  local idSender, msg, distance = rednet.receive()
  68.  protocol(msg)
  69.  return idSender, msg, distance
  70. end
  71.  
  72. function createConfig()
  73.  if fs.exists("/clientFolder/client.config") then
  74.   return "nothing"
  75.  else
  76.  print("Thanks you for download this rednet protocol")
  77.  print("You are going to configure this program, follow this step-by-step configuration")
  78.  os.sleep(3)
  79.  shell.run("clear")
  80.  print("First enter the side of your modem")
  81.  local modemSide = io.read()
  82.  shell.run("clear")
  83.  print("Type your server ip")
  84.  local ipServ = io.read()
  85.  shell.run("clear")
  86.  print("Enter your password :")
  87.  local passToSave = read()
  88.  term.clear()
  89.  print("You have finished the configuration of your program")
  90.  fs.makeDir("clientFolder")
  91.  fs.makeDir("usermade")
  92.  local conf = io.open("/clientFolder/client.config", "w")
  93.  local data = {side = modemSide, id = ipServ, pass = passToSave}
  94.  local dataS = textutils.serialize(data)
  95.  local writeIsFail = conf:write(dataS)
  96.  conf:close()
  97.  if writeIsFail then
  98.   print("Config succesfully writed")
  99.  else
  100.   print("Cannot write in the config file")
  101.  end
  102. end
  103. end
  104.  
  105. function loadConfig()
  106.  gui.clear()
  107.  gui.drawHeader("Loading...")
  108.  if fs.exists("/clientFolder/client.config") then
  109.   local conf = fs.open("/clientFolder/client.config", "r")
  110.   local dataS = conf.readAll()
  111.   conf.close()
  112.   local data = textutils.unserialize(dataS)
  113.   local id = data.id
  114.   local side = data.side
  115.   local pass = data.pass
  116.   rednet.open(side)
  117.   gui.drawHeader("Loaded !")
  118.   return id, pass
  119.  else
  120.   print("Cannot find the config file, please restart this program")
  121.   os.sleep(3)
  122.   return
  123.  end
  124. end
  125.  
  126. local function drawMain()
  127.  term.setColorBackground(colors.lightBlue)
  128.  gui.setHeader("Network console", colors.blue)
  129.  gui.setButton(10, 5, "Send", colors.blue, colors.lightBlue)
  130.  gui.setButton(18, 5, "Receive", colors.blue, colors.lightBlue)
  131.  gui.setButton(40, 17, "Exit", colors.blue, colors.lightBlue)
  132.  gui.render(true)
  133.  selectMain()
  134. end
  135.  
  136. local function selectMain()
  137.  local mainSel = gui.getSelection()
  138.  if mainSel == 1 then
  139.   drawSend()
  140.  elseif mainSel == 2 then
  141.   msgReceive()
  142.  elseif mainSel == 3 then
  143.   gui.clear()
  144.   return
  145.  else
  146.   error("selectMain : wrong selection")
  147.  end
  148. end
  149.  
  150. local function drawSend()
  151.  gui.setHeader("Network console", colors.blue)
  152.  gui.setButton(3, 5, "Send command", colors.blue, colors.lightBlue)
  153.  gui.setButton(3, 9, "Send file", colors.blue, colors.lightBlue)
  154.  gui.setButton(3, 13, "Cut the communication", colors.blue, colors.lightBlue)
  155.  gui.setButton(40, 17, "Exit", colors.blue, colors.lightBlue)
  156.  gui.setButton(20, 27, "Cancel", colors.blue, colors.lightBlue)
  157.  gui.render(true)
  158.  selectSend()
  159. end
  160.  
  161. local function selectSend()
  162.  local sendSel = gui.getSelection()
  163.  if sendSel == 1 then
  164.   sendCommand()
  165.  elseif sendSel == 2 then
  166.   drawFile()
  167.  elseif sendSel == 3 then
  168.   sendExit()
  169.  elseif sendSel == 4 then
  170.   gui.clear()
  171.   return
  172.  elseif sendSel == 5 then
  173.   drawMain()
  174.  else
  175.   error("selectSend : wrong selection")
  176.  end
  177. end
  178.  
  179. local function msgReceive()
  180. local idSender, msg = nReceive()
  181. local msgT = textutils.unserialize(msg)
  182. local idAdressee = msgT[1]
  183. local type = msgT[2]
  184. local body = msgT[3]
  185.  if type == "#/program#" then
  186.   local idSender, arg = rednet.receive()
  187.   shell.run(body, arg)
  188.  elseif type == "#/file#" then
  189.   local idSender, toDo = rednet.receive()
  190.   if toDo == "%save%" then
  191.    local idSender, nameOfFile = rednet.receive()
  192.    local file = fs.open("/usermade/"..nameOfFile, "w")
  193.    file.write(body)
  194.    file.close()
  195.    rednet.send(tonumber(idSender), "<saved>")
  196.   elseif toDo == "%run%" then
  197.    loadstring(body)
  198.    rednet.send(tonumber(idSender), "<executed>")
  199.   else
  200.    rednet.send(tonumber(idSender), "<error:fileinfo:wrongcommand>")
  201.   end
  202.  elseif type == "#/command#" then
  203.   local idSender, command = rednet.receive()
  204.   if command == "#/exit#" then
  205.    return
  206.   else
  207.    rednet.send(tonumber(idSender), "<error:commandinfo:wrongcommand>")
  208.   end
  209.  else
  210.   rednet.send(tonumber(idSender), "<error:typerequest:wrongtype>")
  211.  end
  212. end
  213.  
  214.  
  215.  
  216. term.clear()
  217. shell.run("id")
  218. local exit = 0
  219. local logged = "false"
  220. createConfig()
  221. local id, password = loadConfig()
  222. local id = tonumber(id)
  223. while logged == "false" do
  224. local logged = login(password)
  225. end
  226. message()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement