Advertisement
Bjornir90

Client.lua

Nov 13th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. -- This is a non-tested version so report any bugs you find please
  2.  
  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.  
  18. function createConfig()
  19.  if fs.exists("/clientFolder/client.config") then
  20.   return "nothing"
  21.  else
  22.  print("Thanks you for download this rednet protocol")
  23.  print("You are going to configure this program, follow this step-by-step configuration")
  24.  os.sleep(3)
  25.  shell.run("clear")
  26.  print("First enter the side of your modem")
  27.  local modemSide = io.read()
  28.  shell.run("clear")
  29.  print("Type your server ip")
  30.  local ipServ = io.read()
  31.  shell.run("clear")
  32.  print("You have finished the configuration of your program")
  33.  fs.makeDir("clientFolder")
  34.  fs.makeDir("usermade")
  35.  local conf = io.open("/clientFolder/client.config", "w")
  36.  local data = {side = modemSide, id = ipServ}
  37.  local dataS = textutils.serialize(data)
  38.  local writeIsFail = conf:write(dataS)
  39.  conf:close()
  40.  if writeIsFail then
  41.   print("Config succesfully writed")
  42.  else
  43.   print("Cannot write in the config file")
  44.  end
  45. end
  46. end
  47.  
  48. function loadConfig()
  49.  shell.run("clear")
  50.  if fs.exists("/clientFolder/client.config") then
  51.   local conf = fs.open("/clientFolder/client.config", "r")
  52.   local dataS = conf.readAll()
  53.   local data = textutils.unserialize(dataS)
  54.   os.sleep(2)
  55.   local id = data.id
  56.   local side = data.side
  57.   rednet.open(side)
  58.   return id
  59.  else
  60.   print("Cannot find the config file, please restart this program")
  61.   os.sleep(3)
  62.   return
  63.  end
  64. end
  65.  
  66. function message()
  67. shell.run("clear")
  68. shell.run("id")
  69. print("Do you want to send (s) or receive (r) message ?")
  70. input = io.read()
  71. shell.run("clear")
  72.  
  73. if input == "s" then
  74.  print("Type the id of the addressee :")
  75.  idToSend = io.read()
  76.  shell.run("clear")
  77.  rednet.send(id, idToSend)
  78.  
  79.   print("What do you want to do :")
  80.   print("Send a file (f) or a program (p)?")
  81.   print("Cut the communication with the other computer (exit)?")
  82.   print("Exit this program (/quit)?")
  83.   type = io.read()
  84.   rednet.send(id, type)
  85.   shell.run("clear")
  86.  
  87.  if type == "f" then
  88.   print("Do you want to execute (e) or save the file (s) ?")
  89.   toDo = io.read()
  90.   rednet.send(id, toDo)
  91.   print("What is the name of the file ?")
  92.   fileOpen = io.read()
  93.   local file = fs.open(fileOpen, "r")
  94.   local sFile = file.readAll()
  95.   rednet.send(id, sFile)
  96.   file.close()
  97.   if toDo == "s" then
  98.   print("What is the name on the other computer ?")
  99.   nameFile = io.read()
  100.   rednet.send(id, nameFile)
  101.   end
  102.   print("File sent")
  103.  
  104.  elseif type == "p" then
  105.   print("What is the name of the program ?")
  106.   programSend = io.read()
  107.   rednet.send(id, programSend)
  108.   print("Type the argument of the program (0 if there is not argument):")
  109.   argument = io.read()
  110.   rednet.send(id, argument)
  111.   print("Program sent")
  112.  
  113.  elseif type == "exit" then
  114.   print("Communication cut")
  115.   rednet.send(id, type)
  116.  
  117.  elseif type == "/quit" then
  118.   shell.run("clear")
  119.   rednet.close("right")
  120.   return
  121.  end
  122.  
  123. elseif input == "r" then
  124.  shell.run("id")
  125.  textutils.slowPrint("Listening for request...")
  126.  while exit == 0  do
  127.   id, msg = rednet.receive()
  128.  
  129.   if msg == "p" then
  130.    id, msg1 = rednet.receive()
  131.    id, msg2 = rednet.receive()
  132.    shell.run(msg1, msg2)
  133.  
  134.   elseif msg == "f" then
  135.    id, msgDo = rednet.receive()
  136.    id, msg1 = rednet.receive()
  137.    if msgDo == "e" then
  138.    loadstring(msg1) ()
  139.    elseif msgDo == "s" then
  140.    msgName = rednet.receive()
  141.    newFile = io.open("/usermade/"..msgName, "w")
  142.    newFile:write(msg1)
  143.    newFile:close()
  144.    end
  145.   elseif msg == "exit" then
  146.    shell.run("clear")
  147.    rednet.close("right")
  148.    return
  149.    rednet.send(id, 1)
  150.  
  151.   end
  152.  end
  153. end
  154. end
  155.  
  156. shell.run("clear")
  157. shell.run("id")
  158. exit = 0
  159. createConfig()
  160. id = tonumber(loadConfig())
  161. message()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement