Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. os.loadAPI("api/devices")
  2.  
  3. -- GLOBAL VARIABLES --
  4. local ver="1.1"
  5. local id=os.getComputerID()
  6. local wifi
  7. local dest
  8. local sfile
  9. local sender
  10. local dcat
  11. local dfile
  12. local dpath
  13. -- /GLOBAL VARIABLES --
  14.  
  15. function Main()
  16. Clear()
  17. write("Initializing")
  18. textutils.slowWrite(".....\n\n")
  19.  
  20. wifi=devices.find("modem")
  21. if wifi~="none" then
  22. print("Wireless device found!")
  23. write("Opening connection")
  24. textutils.slowWrite(".....\n\n")
  25. rednet.open(wifi)
  26. print("Done!")
  27. sleep(1)
  28. Menu()
  29. else
  30. print("Wireless device not found!\nConnect a device and try again!")
  31. return 0
  32. end
  33. end
  34.  
  35. function Menu()
  36. Clear()
  37. print("Your ID: "..id.."\n")
  38. print("\t1. Send file")
  39. print("\t2. Receive file")
  40. print("\t0. Exit")
  41.  
  42. local event,p1=os.pullEvent("key")
  43. if p1==2 then
  44. SendMenu()
  45. elseif p1==3 then
  46. DownMenu()
  47. elseif p1==11 then
  48. term.clear()
  49. term.setCursorPos(1,1)
  50. print("Thanks for using rFTP!")
  51. rednet.close(wifi)
  52. return 0
  53. else
  54. Menu()
  55. end
  56. end
  57.  
  58. function SendMenu()
  59. Clear()
  60. print("Enter destination ID (not label):")
  61. dest=tonumber(read())
  62. print("File to send: ")
  63. sfile=read()
  64. write("\nChecking file")
  65. textutils.slowWrite(".....")
  66. if fs.exists(sfile)==true then
  67. write(" File OK!\n")
  68. else
  69. print("\nFile not found! Any key to continue...")
  70. os.pullEvent("key")
  71. Menu()
  72. end
  73. print("Estabilishing connection")
  74. rednet.send(dest,"@rftpstart")
  75. rednet.send(dest,sfile)
  76. print("Waiting for device #"..dest.." to respond...\n[Press SPACE to cancel]")
  77. Waiting()
  78. end
  79.  
  80. function Waiting()
  81. while true do
  82. local event,p1,p2=os.pullEvent()
  83.  
  84. if (event=="rednet_message") and (p2=="@rftpagree") then
  85. print("File accepted by #"..dest.."\n")
  86. sleep(1)
  87. Transfer()
  88. break
  89. elseif (event=="rednet_message") and (p2=="@rftpdeny") then
  90. print("#"..dest.." denied connection! Any key to continue...")
  91. os.pullEvent("key")
  92. Menu()
  93. break
  94. elseif (event=="key") and (p1==57) then
  95. Menu()
  96. break
  97. end
  98. end
  99. end
  100.  
  101. function Transfer()
  102. local plik=fs.open(sfile,"r")
  103. local tresc=plik.readAll()
  104. rednet.send(dest,tresc)
  105. sleep(0.3)
  106. rednet.send(dest,"@rftpend")
  107. plik.close()
  108. print("Transfer complete! Any key to continue...")
  109. os.pullEvent("key")
  110. Menu()
  111. end
  112.  
  113. function DownMenu()
  114. Clear()
  115. print("Your ID: "..id.."\n")
  116. print("Waiting for an oncoming file...\n[Press SPACE to cancel]")
  117. local event,p1,p2
  118. repeat
  119. event,p1,p2=os.pullEvent()
  120. until ((event=="rednet_message") and (p2=="@rftpstart")) or ((event=="key") and (p1==57))
  121. if (event=="rednet_message") and (p2=="@rftpstart") then
  122. sender=p1
  123. event,p1,p2=os.pullEvent("rednet_message")
  124. print("Device "..p1.." want to send you a file: "..p2..". Accept? [y/N]")
  125. local event,pp1=os.pullEvent("key")
  126. if (pp1==20) or (pp1==21) then
  127. Download()
  128. else
  129. rednet.send(p1,"@rftpdeny")
  130. Menu()
  131. end
  132. elseif (event=="key") and (p1==57) then
  133. Menu()
  134. end
  135. end
  136.  
  137. function Download()
  138. Clear()
  139.  
  140. print("Select the folder: [default: dl]")
  141. dcat=read()
  142. if dcat=="" then
  143. dcat="dl"
  144. end
  145. fs.makeDir(dcat)
  146.  
  147. print("Select filename:")
  148. dfile=read()
  149.  
  150. dpath=fs.combine(dcat,dfile)
  151.  
  152. if fs.exists(dpath)==false then
  153. SaveFile()
  154. else
  155. print("File already exists! Try again? [y/N]")
  156. local event,p1=os.pullEvent("key")
  157. if (p1==20) or (p1==21) then
  158. Download()
  159. else
  160. rednet.send(p1,"@rftpdeny")
  161. Menu()
  162. end
  163. end
  164. end
  165.  
  166. function SaveFile()
  167. local plik=fs.open(dpath,"w")
  168. rednet.send(p1,"@rftpagree")
  169. local event,p1,p2
  170. repeat
  171. event,p1,p2=os.pullEvent("rednet_message")
  172. until p1==sender
  173. if p2~="@rftpend" then
  174. plik.write(p2)
  175. end
  176. os.startTimer(10)
  177. while true do
  178. local event,p1,p2=os.pullEvent()
  179. if event=="rednet_message" and p1==sender and p2=="@rftpend" then
  180. write("\nTransfer completed! ")
  181. break
  182. elseif event=="timer" then
  183. write("\nTimed out! Check the file. ")
  184. break
  185. end
  186. end
  187. plik.close()
  188. print("Any key to continue...")
  189. os.pullEvent("key")
  190. Menu()
  191. end
  192.  
  193. function Clear()
  194. term.clear()
  195. term.setCursorPos(1,1)
  196. print("rFTP v"..ver.."\nby Jahmaican\n~~~~~~~~~~~~\n")
  197. end
  198.  
  199. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement