Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function printUsage()
- local programName = arg[0] or fs.getName(shell.getRunningProgram())
- print("Usages:")
- print(programName .. " logout")
- print(programName .. " put <filename>")
- print(programName .. " get <code> <filename>")
- print(programName .. " run <code> <arguments>")
- print(programName .. " login")
- print(programName .. " list </search term>")
- print(programName .. " uget <name> </filename>")
- end
- local tArgs = { ... }
- if #tArgs < 1 then
- printUsage()
- return
- end
- if not http then
- printError("Pastebin requires the http API")
- printError("Set http.enabled to true in CC: Tweaked's config")
- return
- end
- --- Attempts to guess the pastebin ID from the given code or URL
- local function extractId(paste)
- local patterns = {
- "^([%a%d]+)$",
- "^https?://pastebin.com/([%a%d]+)$",
- "^pastebin.com/([%a%d]+)$",
- "^https?://pastebin.com/raw/([%a%d]+)$",
- "^pastebin.com/raw/([%a%d]+)$",
- }
- for i = 1, #patterns do
- local code = paste:match(patterns[i])
- if code then return code end
- end
- return nil
- end
- local function get(url)
- local paste = extractId(url)
- if not paste then
- io.stderr:write("Invalid pastebin code.\n")
- io.write("The code is the ID at the end of the pastebin.com URL.\n")
- return
- end
- write("Connecting to pastebin.com... ")
- -- Add a cache buster so that spam protection is re-checked
- local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
- local response, err = http.get(
- "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
- )
- if response then
- -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
- local headers = response.getResponseHeaders()
- if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
- io.stderr:write("Failed.\n")
- print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
- return
- end
- print("Success.")
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- io.stderr:write("Failed.\n")
- print(err)
- end
- end
- local function logout()
- fs.delete(".uKey.auth")
- end
- local function login(devkey)
- user=""
- pass=""
- io.stdout:write("Enter username: ")
- user = read()
- --term.setCursorBlink(false)
- io.stdout:write("Enter password: ")
- pass = read("")
- --term.setCursorBlink(true)
- --read user details through keyboard
- file=fs.open(".uKey.auth","w")
- page, err=http.post(
- "https://pastebin.com/api/api_login.php",
- "api_dev_key="..devkey.."&"..
- "api_user_name="..user.."&"..
- "api_user_password="..pass)
- --#queries site for key using devkey, name and pass
- --print(page)
- if err then
- printError(err)
- end
- file.write(page.readAll())
- file.close()
- end
- local function list(devkey,searterm,flg)
- fUser=fs.open(".uKey.auth","r")
- ukey = fUser.readLine()
- fUser.close()
- page, err=http.post(
- "https://pastebin.com/api/api_post.php",
- "api_dev_key="..devkey.."&"..
- "api_user_key="..ukey.."&"..
- "api_option=list&"..
- "api_results_limit=1000")
- --print(page)
- if err then
- printError(err)
- end
- fPage = fs.open("pbList.tmp","w")
- fPage.write(page.readAll())
- fPage.close()
- if flg==0 then
- fPage = fs.open("pbList.txt","w")
- else
- fPage = fs.open("pbUGet.tmp","w")
- end
- sauce="pbList.tmp"
- --if #tArgs == 2 then
- if searterm ~= "" then
- fFilter=fs.open("pbFilter.tmp","w")
- --searterm = tArgs[2]
- tmpaste = ""
- for line in io.lines(sauce) do
- if string.find(line,"<paste_key>") then
- tmpaste = line
- end
- if string.find(line,"<paste_title>") and string.find(line,searterm) then
- fFilter.writeLine(tmpaste)
- fFilter.writeLine(line)
- end
- end
- fFilter.close()
- sauce="pbFilter.tmp"
- end
- for line in io.lines(sauce) do
- if string.find(line,"<paste_key>") then
- line=string.gsub(line,"<paste_key>","")
- line=string.gsub(line,"</paste_key>","")
- if flg==0 then
- io.stdout:write(line.." ")
- end
- fPage.write(line.." ")
- end
- if string.find(line,"<paste_title>") then
- line=string.gsub(line,"<paste_title>","")
- line=string.gsub(line,"</paste_title>","")
- if flg==0 then
- printError(line)
- end
- fPage.writeLine(line)
- end
- end
- --if #tArgs == 2 then
- if searterm~="" then
- fs.delete("pbFilter.tmp")
- end
- fs.delete("pbList.tmp")
- fPage.close()
- end
- key="eUXuCLWHr7T5d9FQmnqj5hMEQ0hyQCoN"
- local sCommand = tArgs[1]
- if sCommand == "logout" then
- logout()
- elseif sCommand == "put" then
- -- Upload a file to pastebin.com
- -- Determine file to upload
- local sFile = tArgs[2]
- local sPath = shell.resolve(sFile)
- if not fs.exists(sPath) or fs.isDir(sPath) then
- print("No such file")
- return
- end
- -- Read in the file
- local sName = fs.getName(sPath)
- local file = fs.open(sPath, "r")
- local sText = file.readAll()
- local ukey = ""
- file.close()
- --Read user key
- if fs.exists(".UKey.auth") then
- file = fs.open(".UKey.auth","r")
- ukey=file.readLine()
- --print(ukey)
- file.close()
- end
- -- POST the contents to pastebin
- write("Connecting to pastebin.com... ")
- local response = http.post(
- "https://pastebin.com/api/api_post.php",
- "api_option=paste&" ..
- "api_dev_key=" .. key .. "&" ..
- "api_user_key="..ukey.."&"..
- "api_paste_format=lua&" ..
- "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
- "api_paste_code=" .. textutils.urlEncode(sText)
- )
- if response then
- print("Success.")
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match(sResponse, "[^/]+$")
- print("Uploaded as " .. sResponse)
- print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
- else
- print("Failed.")
- 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
- print("File already exists")
- return
- end
- -- GET the contents from pastebin
- 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, select(3, ...))
- if not success then
- printError(msg)
- end
- end
- elseif sCommand == "login" then
- login(key)
- elseif sCommand == "list" then
- if #tArgs == 2 then
- list(key,tArgs[2])
- elseif #tArgs == 1 then
- list(key,"",0)
- end
- elseif sCommand == "uget" then
- if #tArgs < 2 then
- printUsage()
- return
- end
- list(key,tArgs[2])
- i=0
- for line in io.lines("pbUGet.tmp") do
- i=i+1
- end
- if i==0 then
- printError("result not found")
- elseif i>1 then
- printError("not specific enough")
- else
- prg= arg[0] or fs.getName(shell.getRunningProgram())
- code, name = ""
- for line in io.lines("pbUGet.tmp") do
- code, name = string.match(line,"(%w+)(.+)")
- end
- if #tArgs <3 then
- shell.run(prg.." get "..code.." "..name)
- else
- shell.run(prg.." get "..code.." "..tArgs[3])
- end
- end
- else
- printUsage()
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement