Reinified

(CLIENT) gFile-2.0

Apr 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. -- gFile by Reinified (IGN Fifti)
  2. -- Version 2.0
  3. -- Public edit
  4.  
  5.  
  6. -- Request information gathering
  7. term.clear()
  8. term.setCursorPos(1,1)
  9. print("---- [GFILE] ----")
  10. print("Enter server rednet ID: ")
  11. write("> ")
  12. nServerKey = tonumber(read())
  13. term.clear()
  14. term.setCursorPos(1,1)
  15. print("--- [LOGIN] ---")
  16. print("When using gFile, all users are stored server specifically. Your username must be registered on the server you make requests to if this server is private.")
  17. print("Enter your username: ")
  18. write("> ")
  19. uName = tostring(read())
  20. term.clear()
  21. term.setCursorPos(1,1)
  22. print("Enter the action you wish to perform (u = upload, d = download)")
  23. write("> ")
  24. local action = tostring(read())
  25.  
  26. if action ~= "u" and action ~= "d" then
  27. print("You must give a valid action (u or d)")
  28. return false
  29. end
  30.  
  31. term.clear()
  32. term.setCursorPos(1,1)
  33.  
  34. if action == "d" then
  35. print("--- [REQUEST] --- ")
  36. print("Enter requested file:")
  37. write("> ")
  38. file = tostring(read())
  39. print("Enter downloaded file name: (What you want the file to be called on your pc)")
  40. write("> ")
  41. nFile = tostring(read())
  42.  
  43. if fs.exists(nFile) then
  44. print("A file by that name already exists on your pc.")
  45. return false
  46. end
  47.  
  48. else -- else of action handling if statement (means action was upload if this runs)
  49. print("--- [UPLOAD] ---")
  50. print("Enter the file path of the file you wish to upload (Example: images/birthday")
  51. write("> ")
  52. file = tostring(read())
  53. print("Enter what you want the file to be called on the server:")
  54. write("> ")
  55. sFileName = tostring(read())
  56.  
  57. if not fs.exists(file) then
  58. print("That file doesnt exist on your pc.")
  59. return false
  60. end
  61. end -- end of action handling if statement
  62.  
  63. print("Enter server key: ")
  64. write("> ")
  65. nDecCode = read()
  66.  
  67. local request = {}
  68. request.action = action
  69. if action == "d" then request.file = file end
  70. if action == "u" then
  71. f = fs.open(file, "r")
  72. request.fileContent = f.readAll() -- this errors if you try to send a folder
  73. request.fileName = sFileName
  74. f.close()
  75. end
  76. request.name = uName
  77. request.code = nDecCode
  78.  
  79.  
  80. -- Sending request
  81. rednet.open(peripheral.find("modem"))
  82. rednet.send(nServerKey, request, "gfp") -- All gFile requests are done on the "gfp" protocol
  83. id, resp = rednet.receive(10)
  84. if resp == nil then
  85. print("No server by that name was found.")
  86. return false
  87. end
  88.  
  89. -- Communicating response
  90. if string.sub(resp,1,3) == "gf_" then
  91. if resp == "gf_1" then -- success
  92. print("Upload completed successfully.")
  93. return true
  94. end -- errors here
  95. if resp == "gf_2" then -- not found
  96. print("The server could not find that file.")
  97. return false
  98. end
  99. if resp == "gf_3_0" then -- false username
  100. print("Your username is not valid on this server.")
  101. return false
  102. end
  103. if resp == "gf_3_1" then -- false key
  104. print("Your key is incorrect.")
  105. return false
  106. end
  107. if resp == "gf_10" then -- other error.
  108. print("A general error occured. Please contact the server owner to find out what is wrong.")
  109. return false
  110. end
  111. if resp == "gf_4" then -- request is missing parts or has invalid action etc
  112. print("Your request was incomplete. This usually happens when you modify the code of the server")
  113. return false
  114. end
  115. if resp == "gf_5" then -- tried to make a folder
  116. print("you cant upload files straight into a folder.")
  117. return false
  118. end
  119. if resp == "gf_6" then -- file starts with gf_
  120. print("The file you're uploading can't start with 'gf_'.")
  121. return false
  122. end
  123. if resp == "gf_7" then -- file is not for public
  124. print("That file is private.")
  125. return false
  126. end
  127. if resp == "gf_8" then -- asked for directory
  128. print("You cant ask for that file because it is a directory.")
  129. return false
  130. end
  131. if resp == "gf_9" then -- uploading, file exists
  132. print("That file already exists.")
  133. return false
  134. end
  135.  
  136. else
  137. f = fs.open(nFile, "w")
  138. f.write(resp)
  139. f.close()
  140. print("Downloaded successfully!")
  141. end
Add Comment
Please, Sign In to add comment