Advertisement
Rayzin

Rayzin's FTP

Apr 8th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1.     local component = require("component")
  2. local fs = require("filesystem")
  3. local term = require("term")
  4. local md = component.modem
  5. local event = require("event")
  6.  
  7. --0 for send   1 for receive
  8. ftpMode = 0
  9.  
  10. discoveryAddr = "DIS"
  11. ftpAddr = "FTP"
  12. ftpPort = 0
  13.  
  14.  
  15. term.clear()
  16.  
  17.  
  18. --file send function
  19. function sendFile(filename,dest,port)
  20. local numPackets = 1
  21. local filepath = ("/home/ftpDir/" .. tostring(filename))
  22. file = io.open(filepath,"r")
  23. local filesize = fs.size(filepath)
  24.  
  25. md.send(dest,port,"File Incoming")
  26. os.sleep(0.5)
  27. md.send(dest,port,filename)
  28.  
  29. print("Sending file")
  30. local contents = file : read(filesize)
  31. md.send(dest,port,contents)
  32.  
  33. file : close()
  34. print("File sent!")
  35. end
  36.  
  37. --file receive function
  38. function receiveFile()
  39. local numPackets = 1
  40. local _,_,from,port,_,message = event.pull("modem_message")
  41. local filename = message
  42. print("Received filename: "..filename)
  43. local filePath = ("/home/ftpDir/" .. filename)
  44.  
  45. local fileContents = ""
  46. print("File Receiving: 0/"..numPackets)
  47. local _,_,from,port,_,message = event.pull("modem_message")
  48. fileContents = (fileContents .. tostring(message))
  49. print("Received 1/"..numPackets)
  50.  
  51. file = io.open(filePath,"w")
  52. print("Writing contents")
  53. file : write(fileContents)
  54. file : close()
  55. end
  56.  
  57.  
  58. --Open discovery port
  59. print("OPEN DISCOVERY")
  60. md.open(1)
  61.  
  62. --get DISCOVERY ADDRESS
  63. print("GET DISCOVERY")
  64. md.broadcast(1,"DISCOVERY")
  65. repeat
  66. local _,_,from,port,_,message = event.pull("modem_message")
  67. message = tostring(message)
  68. from = tostring(from)
  69. discoveryAddr = from
  70. until(message == "DISCOVERY_RECEIVE")
  71. print("GOT DISCOVERY: " .. discoveryAddr)
  72.  
  73.  
  74. --Get FTP info from DISCOVERY
  75. print("GET FTP INFO")
  76. md.send(discoveryAddr, 1, "FTP_INFO")
  77. local _,_,from,port,_,message = event.pull("modem_message")
  78. print("Receiving ".. tostring(message) .. " packets.")
  79. local _,_,from,port,_,message = event.pull("modem_message")
  80. ftpPort = tonumber(message)
  81. if ftpPort == nil then
  82. print("No FTP Port registered!")
  83. else
  84. print("GOT FTP PORT: "..ftpPort)
  85. end
  86.  
  87. local _,_,from,port,_,message = event.pull("modem_message")
  88. ftpAddr = tostring(message)
  89. if ftpPort == nil then
  90. print("No FTP Address registered!")
  91. else
  92. print("GOT FTP ADDRESS: " .. ftpAddr)
  93. end
  94.  
  95. md.send(discoveryAddr,1,"FTP_LOG")
  96.  
  97. --Open FTP Port and Close DISCOVERY
  98. md.close(1)
  99. md.open(ftpPort)
  100.  
  101. --check for /home/ftp/
  102. dExists = fs.isDirectory("/home/ftpDir/")
  103. if dExists == true then
  104. print("/home/ftp exists!")
  105. end
  106. if dExists == false then
  107. print("Creating directory at /home/ftpDir/ ...")
  108. fs.makeDirectory("/home/ftpDir/")
  109. print("Done!")
  110. end
  111.  
  112.  
  113. --Begin main part
  114.  
  115. term.clear()
  116. print("INIT COMPLETE!")
  117. term.write("Send or Receive >")
  118. term.setCursor(18,2)
  119. term.setCursorBlink(true)
  120. mode = tostring(io.read())
  121. term.setCursorBlink(false)
  122. if (mode == "send") or (mode == "Send") or (mode == "s") then
  123. ftpMode = 0
  124.  
  125. elseif (mode == "receive") or (mode == "Receive") or (mode == "r") then
  126. ftpMode = 1
  127.  
  128. else
  129. print("Illegal input!")
  130. os.sleep(1)
  131. os.exit()
  132. end
  133.  
  134. if ftpMode == 0 then
  135. print("Possible files:")
  136. for file in fs.list("/home/ftpDir/") do
  137. print(file)
  138. if file == nil then break end
  139. end
  140.  
  141. print("")
  142. print("Select file:")
  143. fileSelect = io.read()
  144.  
  145. md.broadcast(ftpPort,"SEND_REQUEST")
  146. local _,_,from,port,_,message = event.pull("modem_message")
  147.  
  148. if message == "Accept" then
  149. print("Request Accept at "..from)
  150. sendFile(fileSelect,from,ftpPort,1)
  151. end
  152.  
  153. end
  154.  
  155. if ftpMode == 1 then
  156. print("Receive files:")
  157. local _,_,from,port,_,message = event.pull("modem_message")
  158. message = tostring(message)
  159. if message == "SEND_REQUEST" then
  160. print("Send Request incoming")
  161. print("Press Y to accept")
  162. local wasYPressed = io.read()
  163. if (wasYPressed == "y") or (wasYPressed == "Y") then
  164. md.send(from,port,"Accept")
  165. local _,_,from,port,_,message = event.pull("modem_message")
  166. message = tostring(message)
  167. if message == "File Incoming" then receiveFile() end
  168. print("Done!")
  169. end
  170. end
  171. end
  172. md.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement