Advertisement
LoicxdBmx

Client FTP

Aug 20th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. function download()
  2. print("Download a file")
  3. write("Enter the filename: ")
  4. file = read()
  5. write("Save as? ")
  6. name = read()
  7. rednet.send(server, pass .. "download|" .. file)
  8. o, rfile = rednet.receive(2)
  9. if o == nil then error("Server timed out!")
  10. elseif rfile == "error" then
  11. print("File could not be found!")
  12. sleep(3)
  13. main()
  14. else
  15. f = io.open("/" .. name, "w")
  16. f:write(rfile)
  17. print "File downloaded!"
  18. f:close()
  19. sleep(3)
  20. main()
  21. end
  22. end
  23.  
  24. function upload()
  25. print("Upload a file")
  26. write("File to upload: ")
  27. file = read()
  28. write("Upload as? ")
  29. name = read()
  30. if fs.exists("/" .. file) then
  31. f = io.open("/" .. file, "r")
  32. content = f:read("*a")
  33. f:close()
  34. rendet.send(server, pass .. "upload|" .. name .. "|" .. content)
  35. o, a = rednet.receive(2)
  36. if a == "ok" then
  37. print("File uploaded!")
  38. sleep(3)
  39. main()
  40. else error("Server timed out!")
  41. end
  42. else
  43. print("File could not be found!")
  44. sleep(3)
  45. main()
  46. end
  47. end
  48.  
  49. function rename()
  50. print("Rename a file")
  51. write("Current name: ")
  52. old = read()
  53. write("New name: ")
  54. new = read()
  55. rednet.send(server, pass .. "rename|" .. old .. "|" .. new)
  56. o, a = rednet.receive(2)
  57. if a == "ok" then
  58. print ("File renamed!")
  59. sleep(3)
  60. main()
  61. elseif a == "error" then
  62. print("File does not exist!")
  63. sleep(3)
  64. main()
  65. else
  66. error("Server timed out!")
  67. end
  68. end
  69.  
  70. function delete()
  71. print ("Delete a file")
  72. write ("Delete what? ")
  73. name = read()
  74. rednet.send(server, pass .. "delete|" .. name)
  75. o, m = rednet.receive(2)
  76. if o == nil then
  77. error("Server timed out!")
  78. elseif m == "ok" then
  79. print("File deleted!")
  80. sleep(3)
  81. main()
  82. else
  83. print("File does not exist!")
  84. sleep(3)
  85. main()
  86. end
  87. end
  88.  
  89.  
  90. function main()
  91. term.clear()
  92. term.setCursorPos(1,1)
  93. rednet.send(server, pass ..  "list|list")
  94. o, a = rednet.receive(2)
  95. r = {}
  96. if o == nil then error("Could not connect to server!") else
  97. while a ~= "*eof" do
  98. table.insert(r, a)
  99. o, a = rednet.receive()
  100. end
  101. print("Connection sucessful!")
  102. print("Server files:")
  103. textutils.tabulate(r)
  104. print("1. Download")
  105. print("2. Upload")
  106. print("3. Rename")
  107. print("4. Delete")
  108. print("5. Refresh")
  109. print("6, Quit")
  110. option = read()
  111. if option == "1" then download()
  112. elseif option == "2" then upload()
  113. elseif option == "3" then rename()
  114. elseif option == "4" then delete()
  115. elseif option == "5" then main()
  116. elseif option == "6" then
  117. end
  118. end
  119. end
  120.  
  121. term.clear()
  122. term.setCursorPos(1,1)
  123. write ("Connect to which server: ")
  124. sserver = read()
  125. server = (sserver + 0)
  126. write ("Enter the server's password: ")
  127. pass = read("*")
  128. rednet.open("right")
  129.  
  130. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement