Advertisement
Derek1017

ftp

Jan 27th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. if #args < 2 then
  4. error("Usage: ftp <side> <id>")
  5. end
  6.  
  7. local side = args[1]
  8. local id = tonumber(args[2])
  9. local username
  10. local password = ""
  11. local dir = ""
  12.  
  13. local function split(s, del)
  14. local ret = {}
  15. local del = del or " "
  16. local pattern = string.format("([^%s]+)", del)
  17. s:gsub(pattern, function(c) ret[#ret+1] = c end)
  18. return ret
  19. end
  20.  
  21. local function receive()
  22. while true do
  23. local src,mes,dst = rednet.receive(1)
  24. if tonumber(src) == id then
  25. return mes
  26. elseif src == nil or mes == nil then
  27. return ""
  28. end
  29. end
  30. end
  31.  
  32. local function getCode(s)
  33. local splt = split(s, " ")
  34. return tonumber(splt[1])
  35. end
  36.  
  37. local function connect()
  38. while true do
  39. print("Connecting to " .. id .. ", please enter username:")
  40. username = read()
  41. rednet.send(id, username)
  42. local code = getCode(receive())
  43. if code == 230 then -- Login successful
  44. print("Login successful.")
  45. return
  46. elseif code == 331 then -- Password required
  47. print("Password required for " .. username .. ":")
  48. password = read()
  49. rednet.send(id, password)
  50. code = getCode(receive())
  51. if code == 230 then
  52. print("Login successful.")
  53. return
  54. else
  55. print("Wrong password.")
  56. end
  57. elseif code == 500 then
  58. print("Already logged in.")
  59. return
  60. else
  61. print("Wrong username.")
  62. end
  63. end
  64. end
  65.  
  66. --Init
  67. rednet.open(side)
  68.  
  69. term.clear()
  70. term.setCursorPos(1,1)
  71.  
  72. connect()
  73.  
  74. -- Mainloop
  75.  
  76. while true do
  77. local command = read()
  78. local cmdargs = split(command, " ")
  79. if #cmdargs ~= 0 and cmdargs[1] ~= "" then
  80. local cmd = cmdargs[1]:lower()
  81. if cmd == "lcd" then
  82. if #cmdargs < 2 then
  83. print("Not enough parameters.")
  84. else
  85. dir = fs.combine(dir, cmdargs[2])
  86. end
  87. elseif cmd == "ls" then
  88. rednet.send(id, command)
  89. local ls = split(receive(), " ")
  90. local rec = receive()
  91. local code = getCode(rec)
  92. if code == 200 then
  93. textutils.pagedTabulate(ls)
  94. else
  95. print(rec)
  96. end
  97. elseif cmd == "lls" then
  98. local directory
  99. if #cmdargs < 2 then
  100. directory = dir
  101. else
  102. directory = fs.combine(dir, cmdargs[2])
  103. end
  104. if not fs.exists(directory) then
  105. print("File or directory not found.")
  106. else
  107. local list = fs.list(directory)
  108. textutils.pagedTabulate(list)
  109. end
  110. elseif cmd == "get" then
  111. if #cmdargs < 2 then
  112. print("Not enough parameters.")
  113. else
  114. local locfile
  115. if #cmdargs >= 3 then
  116. locfile = fs.combine(dir, cmdargs[3])
  117. else
  118. locfile = fs.combine(dir, fs.getName(cmdargs[2]))
  119. end
  120.  
  121. rednet.send(id, "GET " .. cmdargs[2])
  122. local content = receive()
  123. local rec = receive()
  124. local code = getCode(rec)
  125.  
  126. if code == 200 then
  127. local file = io.open(locfile, "w")
  128. if file then
  129. file:write(content)
  130. file:close()
  131. print("File " .. fs.getName(locfile) .. " saved successfully.")
  132. else
  133. print("Could not open file for writing.")
  134. end
  135. else
  136. print(rec)
  137. end
  138. end
  139. elseif cmd == "put" then
  140. if #cmdargs < 1 then
  141. print("Not enough parameters.")
  142. else
  143. local remfile
  144. rednet.send(id, "pwd")
  145. local pwd = receive()
  146. if getCode(receive()) == 200 then
  147. if #cmdargs >= 3 then
  148. remfile = fs.combine(pwd, cmdargs[3])
  149. else
  150. remfile = fs.combine(pwd, fs.getName(cmdargs[2]))
  151. end
  152.  
  153. rednet.send(id, "PUT " .. remfile)
  154. local rec = receive()
  155. if getCode(rec) == 150 then
  156. local file = io.open(fs.combine(dir, cmdargs[2]), "r")
  157. if file then
  158. local content = file:read("*a")
  159. file:close()
  160. rednet.send(id, content)
  161.  
  162. local rec = receive()
  163. if getCode(rec) == 200 then
  164. print("File " .. fs.getName(remfile) .. " uploaded successfully.")
  165. else
  166. print(rec)
  167. end
  168. else
  169. print("Could not open file for reading.")
  170. print("Debug: " .. fs.combine(dir, cmdargs[2]))
  171. end
  172. else
  173. print(rec)
  174. end
  175. end
  176. end
  177. elseif cmd == "help" then
  178. print("Commands:")
  179. textutils.pagedTabulate({"bye", "quit", "cd", "rm", "get", "help", "lcd", "ls", "lls", "mkdir", "put", "pwd"})
  180. elseif cmd == "pwd" then
  181. rednet.send(id, command)
  182. local pwd = receive()
  183. local rec = receive()
  184. local code = getCode(rec)
  185. if code == 200 then
  186. print(pwd)
  187. else
  188. print(rec)
  189. end
  190. else
  191. rednet.send(id, command)
  192. local rec = receive()
  193. print(rec)
  194. if getCode(rec) == 221 then
  195. break
  196. end
  197. end
  198. end
  199. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement