Advertisement
AstolfoFate

ftpd

Jun 18th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.61 KB | None | 0 0
  1. rednet.open("back")
  2. rednet.host("ftp", "testserver")
  3.  
  4. while true do
  5.   id, msg, protocol = rednet.receive()
  6.   print("sender id : ", id, " || sender message: ", msg, " || message protocol: ", protocol)
  7.   if protocol == "sls" then
  8.     rednet.send(id, fs.list(""), "sls")
  9.  
  10.   elseif protocol == "put" then
  11.     local nid, nmsg = rednet.receive("ftp_name")
  12.     if nid == id then
  13.       local file = fs.open(nmsg, "w")
  14.       i = 1
  15.       repeat
  16.         local line = msg[i]
  17.         file.writeLine(line)
  18.         i = i + 1
  19.       until line == nil
  20.       file.close()
  21.     end
  22.  
  23.   elseif protocol == "get" then
  24.     local nid, nmsg = rednet.receive("ftp_name")
  25.     if nid == id and fs.exists(nmsg) == true then
  26.       local file = fs.open(nmsg, "r")
  27.       filedata = {}
  28.       i = 1
  29.       repeat
  30.         local line = file.readLine()
  31.         print(line)
  32.         table.insert(filedata, line)
  33.         i = i + 1
  34.       until line == nil
  35.       file.close()
  36.       rednet.send(id, filedata, "get")
  37.       print("sent: ", nmsg)
  38.     else
  39.       rednet.send(id, "file doesn't exit", "get")
  40.     end
  41.  
  42.   elseif protocol == "delete" then
  43.     local nid, nmsg = rednet.receive("ftp_name")
  44.     if nid == id then
  45.       fs.delete(nmsg)
  46.       rednet.send(id, "file deleted", "delete")
  47.     else
  48.       rednet.send(id, "file doesn't exist", "delete")
  49.     end
  50.   end
  51. end
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. --[[ old program
  62. rednet.open("back")
  63. while true do
  64.   local id, msg = rednet.receive()
  65.   local file = fs.open("save", "w")
  66.   i = 1
  67.   repeat
  68.     local line = msg[i]
  69.     file.writeLine(msg[i])
  70.     i = i + 1
  71.   until line == nil
  72.   file.close()
  73. end
  74. ]]
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement