Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- gFile by Reinified (IGN Fifti)
- -- Version 2.0
- -- Public edit
- -- Request information gathering
- term.clear()
- term.setCursorPos(1,1)
- print("---- [GFILE] ----")
- print("Enter server rednet ID: ")
- write("> ")
- nServerKey = tonumber(read())
- term.clear()
- term.setCursorPos(1,1)
- print("--- [LOGIN] ---")
- 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.")
- print("Enter your username: ")
- write("> ")
- uName = tostring(read())
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the action you wish to perform (u = upload, d = download)")
- write("> ")
- local action = tostring(read())
- if action ~= "u" and action ~= "d" then
- print("You must give a valid action (u or d)")
- return false
- end
- term.clear()
- term.setCursorPos(1,1)
- if action == "d" then
- print("--- [REQUEST] --- ")
- print("Enter requested file:")
- write("> ")
- file = tostring(read())
- print("Enter downloaded file name: (What you want the file to be called on your pc)")
- write("> ")
- nFile = tostring(read())
- if fs.exists(nFile) then
- print("A file by that name already exists on your pc.")
- return false
- end
- else -- else of action handling if statement (means action was upload if this runs)
- print("--- [UPLOAD] ---")
- print("Enter the file path of the file you wish to upload (Example: images/birthday")
- write("> ")
- file = tostring(read())
- print("Enter what you want the file to be called on the server:")
- write("> ")
- sFileName = tostring(read())
- if not fs.exists(file) then
- print("That file doesnt exist on your pc.")
- return false
- end
- end -- end of action handling if statement
- print("Enter server key: ")
- write("> ")
- nDecCode = read()
- local request = {}
- request.action = action
- if action == "d" then request.file = file end
- if action == "u" then
- f = fs.open(file, "r")
- request.fileContent = f.readAll() -- this errors if you try to send a folder
- request.fileName = sFileName
- f.close()
- end
- request.name = uName
- request.code = nDecCode
- -- Sending request
- rednet.open(peripheral.find("modem"))
- rednet.send(nServerKey, request, "gfp") -- All gFile requests are done on the "gfp" protocol
- id, resp = rednet.receive(10)
- if resp == nil then
- print("No server by that name was found.")
- return false
- end
- -- Communicating response
- if string.sub(resp,1,3) == "gf_" then
- if resp == "gf_1" then -- success
- print("Upload completed successfully.")
- return true
- end -- errors here
- if resp == "gf_2" then -- not found
- print("The server could not find that file.")
- return false
- end
- if resp == "gf_3_0" then -- false username
- print("Your username is not valid on this server.")
- return false
- end
- if resp == "gf_3_1" then -- false key
- print("Your key is incorrect.")
- return false
- end
- if resp == "gf_10" then -- other error.
- print("A general error occured. Please contact the server owner to find out what is wrong.")
- return false
- end
- if resp == "gf_4" then -- request is missing parts or has invalid action etc
- print("Your request was incomplete. This usually happens when you modify the code of the server")
- return false
- end
- if resp == "gf_5" then -- tried to make a folder
- print("you cant upload files straight into a folder.")
- return false
- end
- if resp == "gf_6" then -- file starts with gf_
- print("The file you're uploading can't start with 'gf_'.")
- return false
- end
- if resp == "gf_7" then -- file is not for public
- print("That file is private.")
- return false
- end
- if resp == "gf_8" then -- asked for directory
- print("You cant ask for that file because it is a directory.")
- return false
- end
- if resp == "gf_9" then -- uploading, file exists
- print("That file already exists.")
- return false
- end
- else
- f = fs.open(nFile, "w")
- f.write(resp)
- f.close()
- print("Downloaded successfully!")
- end
Add Comment
Please, Sign In to add comment