Advertisement
AstolfoFate

ftp

Jun 18th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. -- File Transfers over CRFv3.1
  2.  
  3. --[[
  4. Packet structure:
  5. rednet.send(id, {1, 2, 3, 4}, "protocol")
  6. [1] : Ident string
  7. [2] : Action
  8. [3] : Target
  9. [4] : Data Parameter
  10.  
  11. Protocols:
  12. ftp_connect
  13. ftp_list
  14. ftp_get
  15. ftp_put
  16. ftp_delete
  17. ]]
  18.  
  19.  
  20. --[ Network ]--
  21. rednet.open("back")
  22. -----------------------------
  23. --[ Functions ]--
  24. -----------------------------
  25. function split(string, delimiter)
  26.   result = {}
  27.   for token in string.gmatch(string, "%a+") do
  28.     table.insert(result, token)
  29.   end
  30.   return result
  31. end
  32.  
  33. function clearScreen()
  34.   term.clear()
  35.   term.setCursorPos(1, 1)
  36. end
  37.  
  38. function printUsage()
  39.   print("Usage: ")
  40.   print("clear")
  41.   print("connect <server name>")
  42.   print("ls")
  43.   print("get <file name>")
  44.   print("put <file name>")
  45.   print("delete <file name>")
  46.   print("exit")
  47. end
  48.  
  49. function main()
  50. -----------------------------
  51.       --[ Input ]--
  52. -----------------------------
  53.   local menu = true
  54.   while menu == true do
  55.     local server_id = 0
  56.     write("ftp>")
  57.     local input = read()
  58.     local args = {}
  59.     args = split(input, " ")
  60.     local cmd = args[1]
  61.     table.remove(args, 1)
  62.     --  commands = {"clear", "connect", "ls", "get", "put", "delete", "exit"}
  63.  
  64.     -----------------------------
  65.       --[ Process/Output ]--
  66.     -----------------------------
  67.     if cmd == "clear" then
  68.       clearScreen()
  69.  
  70.     elseif cmd == "connect" then
  71.       server_name = args[1]
  72.       server_id = rednet.lookup("ftp", server_name)
  73.       print("server id : ", server_id)
  74.  
  75.     elseif cmd == "put" then
  76.       filename = args[1]
  77.       file = fs.open(filename, "r")
  78.       filedata = {}
  79.  
  80.       if file == nil then
  81.         print("File not found.")
  82.       else
  83.         repeat
  84.           line = file.readLine()
  85.           table.insert(filedata, line)
  86.         until line == nil
  87.         print("sending to: ", server_id)
  88.         rednet.send(server_id, filedata, "put")
  89.         rednet.send(server_id, filename, "ftp_name")
  90.       end
  91.  
  92.     elseif cmd == "ls" then
  93.       rednet.send(server_id, "", "ftp_list")
  94.       local senderId, message = rednet.receive(2)
  95.  
  96.       if senderId == server_id then
  97.         i = 1
  98.         repeat
  99.           line = message[i]
  100.           print(line)
  101.           i = i + 1
  102.         until line == nil
  103.       else
  104.         print("Not connected to an ftp server")
  105.       end
  106.  
  107.     elseif cmd == "get" then
  108.       filename = args[1]
  109.       rednet.send(server_id, "", "get")
  110.       rednet.send(server_id, filename, "ftp_name")
  111.       print("waiting")
  112.       local id, msg, protocol = rednet.receive("get")
  113.       print("received")
  114.       if msg == "file doesn't exist" then
  115.         print(msg)
  116.         --elseif fs.exists(filename) == true then
  117.         --    print("file already exists with that name")
  118.       elseif protocol == "get" then
  119.         local file = fs.open(filename, "w")
  120.         i = 1
  121.         repeat
  122.           local line = msg[i]
  123.           print(line)
  124.           file.writeLine(line)
  125.           i = i + 1
  126.         until line == nil
  127.         file.close()
  128.       end
  129.  
  130.     elseif cmd == "delete" then
  131.       filename = args[1]
  132.       rednet.send(server_id, "", "delete")
  133.       rednet.send(server_id, filename, "ftp_name")
  134.       local id, msg = rednet.receive("delete")
  135.       print(msg)
  136.  
  137.     elseif cmd == "exit" then
  138.       print("Goodbye.")
  139.       do return end
  140.  
  141.     elseif cmd == "help"
  142.       printUsage()
  143.     else
  144.       print("Unknown Command")
  145.       printUsage()
  146.     end -- ends if statement
  147.   end -- ends while loop
  148. end -- ends function
  149. main()
  150. --[[
  151. -----------------------------
  152. --[ Input ]--
  153. -----------------------------
  154. print("What file will you send?")
  155. local filename = read()
  156. fs.makeDir("saves")
  157. local file = fs.open("saves/"..filename, "r")
  158. local filedata = {}
  159. -----------------------------
  160. --[ Process ]--
  161. -----------------------------
  162. --sending file
  163. repeat
  164.   local line = file.readLine()
  165.   table.insert(filedata, line)
  166. until line == nil
  167. -----------------------------
  168. --[ Output ]--
  169. -----------------------------
  170. rednet.send(1, filedata, "ftp_send")
  171. ]]
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement