Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function printUsage()
- if term.isColor then
- term.setTextColor(colors.orange)
- print("Usages: ")
- term.setTextColor(colors.red)
- print("pastebin put <filename>")
- print("pastebin get <code> <filename>")
- print("pastebin run <code> <arguments>")
- term.setTextColor(colors.white)
- else
- print("Usages: ")
- print("pastebin put <filename>")
- print("pastebin get <code> <filename>")
- print("pastebin run <code> <arguments>")
- end
- end
- local tArgs = {...}
- if #tArgs < 2 then
- printUsage()
- return
- end
- if not http then
- printError("Pastebin requires http API")
- printError("Set http_enable to true in ComputerCraft.cfg")
- return
- end
- local function get(paste)
- write("Connecting to pastebin.com")
- local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode(paste))
- if response then
- if term.isColor then
- term.setTextColor(colors.green)
- print("Success.")
- term.setTextColor(colors.white)
- else
- print("Success.")
- end
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- printError("Failed.")
- end
- end
- local sCommand = tArgs[1]
- if sCommand == "put" then
- -- Upload a file to pastebin.com (through relay)
- -- Determine file to upload
- local sFile = tArgs[2]
- local sPath = shell.resolve(sFile)
- if not fs.exists(sPath) then
- if term.isColor() then
- term.setTextColor(colors.red)
- print("No such file.")
- term.setTextColor(colors.white)
- else
- print("No such file.")
- end
- return
- elseif fs.isDir(sPath) then
- if term.isColor() then
- term.setTextColor(colors.red)
- print("Cannot upload a directory.")
- term.setTextColor(colors.white)
- else
- print("Cannot upload a directory.")
- end
- return
- end
- --Read in the file
- local sName = fs.getName(sPath)
- local file = fs.open(sPath,"r")
- local sText = file.readAll()
- file.close()
- -- POST the contents to pastebin (through relay)
- local response = http.post(
- "http://104.131.75.188/node/paste.php",
- "name="..sName.."&"..
- "code="..sText
- )
- if response then
- if term.isColor then
- term.setTextColor(colors.green)
- print("Success.")
- term.setTextColor(colors.white)
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match(sResponse,"[^/]+$")
- print("Uploaded as "..sResponse)
- print("Run \"paste get "..sCode.."\" to download anywhere")
- else
- print("Success.")
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match(sResponse,"[^/]+$")
- print("Uploaded as "..sResponse)
- print("Run \"paste get "..sCode.."\" to download anywhere")
- end
- else
- if term.isColor then
- term.setTextColor(colors.red)
- print("Failed.")
- term.setTextColor(colors.white)
- else
- print("Failed.")
- end
- return
- end
- elseif sCommand == "get" then
- -- Download a file from pastebin.com
- if #tArgs < 3 then
- printUsage()
- return
- end
- -- Determine file to download
- local sCode = tArgs[2]
- local sFile = tArgs[3]
- local sPath = shell.resolve(sFile)
- if fs.exists(sPath) then
- if term.isColor then
- term.setTextColor(colors.red)
- print("File already exists.")
- term.setTextColor(colors.white)
- return
- else
- print("File already exists.")
- return
- end
- end
- local res = get(sCode)
- if res then
- local file = fs.open(sPath,"w")
- file.write(res)
- file.close()
- print("Downloaded as "..sFile)
- end
- elseif sCommand == "run" then
- local sCode = tArgs[2]
- local res = get(sCode)
- if res then
- local func, err = load(res,sCode,"t", _ENV)
- if not func then
- printError(err)
- return
- end
- local success,msg = pcall(func, table.unpack(tArgs,3))
- if not success then
- printError(msg)
- end
- end
- else
- printUsage()
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement