Advertisement
ozozx235

PB.lua

Sep 17th, 2023 (edited)
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.44 KB | None | 0 0
  1. local function printUsage()
  2.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  3.     print("Usages:")
  4.     print(programName .. " logout")
  5.     print(programName .. " put <filename>")
  6.     print(programName .. " get <code> <filename>")
  7.     print(programName .. " run <code> <arguments>")
  8.     print(programName .. " login")
  9.     print(programName .. " list </search term>")
  10.     print(programName .. " uget <name> </filename>")
  11. end
  12.  
  13. local tArgs = { ... }
  14. if #tArgs < 1 then
  15.     printUsage()
  16.     return
  17. end
  18.  
  19. if not http then
  20.     printError("Pastebin requires the http API")
  21.     printError("Set http.enabled to true in CC: Tweaked's config")
  22.     return
  23. end
  24.  
  25. --- Attempts to guess the pastebin ID from the given code or URL
  26. local function extractId(paste)
  27.     local patterns = {
  28.         "^([%a%d]+)$",
  29.         "^https?://pastebin.com/([%a%d]+)$",
  30.         "^pastebin.com/([%a%d]+)$",
  31.         "^https?://pastebin.com/raw/([%a%d]+)$",
  32.         "^pastebin.com/raw/([%a%d]+)$",
  33.     }
  34.  
  35.     for i = 1, #patterns do
  36.         local code = paste:match(patterns[i])
  37.         if code then return code end
  38.     end
  39.  
  40.     return nil
  41. end
  42.  
  43. local function get(url)
  44.     local paste = extractId(url)
  45.     if not paste then
  46.         io.stderr:write("Invalid pastebin code.\n")
  47.         io.write("The code is the ID at the end of the pastebin.com URL.\n")
  48.         return
  49.     end
  50.  
  51.     write("Connecting to pastebin.com... ")
  52.     -- Add a cache buster so that spam protection is re-checked
  53.     local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
  54.     local response, err = http.get(
  55.         "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
  56.     )
  57.  
  58.     if response then
  59.         -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
  60.         local headers = response.getResponseHeaders()
  61.         if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
  62.             io.stderr:write("Failed.\n")
  63.             print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
  64.             return
  65.         end
  66.  
  67.         print("Success.")
  68.  
  69.         local sResponse = response.readAll()
  70.         response.close()
  71.         return sResponse
  72.     else
  73.         io.stderr:write("Failed.\n")
  74.         print(err)
  75.     end
  76. end
  77.  
  78. local function logout()
  79.     fs.delete(".uKey.auth")
  80. end
  81.  
  82. local function login(devkey)
  83.     user=""
  84.     pass=""
  85.     io.stdout:write("Enter username: ")
  86.     user = read()
  87.     --term.setCursorBlink(false)
  88.     io.stdout:write("Enter password: ")
  89.     pass = read("")
  90.     --term.setCursorBlink(true)
  91.     --read user details through keyboard
  92.     file=fs.open(".uKey.auth","w")
  93.     page, err=http.post(
  94.     "https://pastebin.com/api/api_login.php",
  95.     "api_dev_key="..devkey.."&"..
  96.     "api_user_name="..user.."&"..
  97.     "api_user_password="..pass)
  98.     --#queries site for key using devkey, name and pass
  99.     --print(page)
  100.     if err then
  101.         printError(err)
  102.     end
  103.     file.write(page.readAll())
  104.     file.close()
  105. end
  106. local function list(devkey,searterm,flg)
  107.     fUser=fs.open(".uKey.auth","r")
  108.     ukey = fUser.readLine()
  109.     fUser.close()
  110.     page, err=http.post(
  111.     "https://pastebin.com/api/api_post.php",
  112.     "api_dev_key="..devkey.."&"..
  113.     "api_user_key="..ukey.."&"..
  114.     "api_option=list&"..
  115.     "api_results_limit=1000")
  116.     --print(page)
  117.     if err then
  118.         printError(err)
  119.     end
  120.     fPage = fs.open("pbList.tmp","w")
  121.     fPage.write(page.readAll())
  122.     fPage.close()
  123.     if flg==0 then
  124.         fPage = fs.open("pbList.txt","w")
  125.     else
  126.         fPage = fs.open("pbUGet.tmp","w")
  127.     end
  128.     sauce="pbList.tmp"
  129.     --if #tArgs == 2 then
  130.     if searterm ~= "" then
  131.         fFilter=fs.open("pbFilter.tmp","w")
  132.         --searterm = tArgs[2]
  133.         tmpaste = ""
  134.         for line in io.lines(sauce) do
  135.             if string.find(line,"<paste_key>") then
  136.                 tmpaste = line
  137.             end
  138.             if string.find(line,"<paste_title>") and string.find(line,searterm) then
  139.                 fFilter.writeLine(tmpaste)
  140.                 fFilter.writeLine(line)
  141.             end
  142.         end
  143.         fFilter.close()
  144.         sauce="pbFilter.tmp"
  145.     end
  146.     for line in io.lines(sauce) do
  147.         if string.find(line,"<paste_key>") then
  148.             line=string.gsub(line,"<paste_key>","")
  149.             line=string.gsub(line,"</paste_key>","")
  150.             if flg==0 then
  151.                 io.stdout:write(line.." ")
  152.             end
  153.             fPage.write(line.." ")
  154.         end
  155.         if string.find(line,"<paste_title>") then
  156.             line=string.gsub(line,"<paste_title>","")
  157.             line=string.gsub(line,"</paste_title>","")
  158.             if flg==0 then
  159.                 printError(line)
  160.             end
  161.             fPage.writeLine(line)
  162.         end
  163.     end
  164.     --if #tArgs == 2 then
  165.     if searterm~="" then
  166.         fs.delete("pbFilter.tmp")
  167.     end
  168.     fs.delete("pbList.tmp")
  169.     fPage.close()
  170. end
  171. key="eUXuCLWHr7T5d9FQmnqj5hMEQ0hyQCoN"
  172. local sCommand = tArgs[1]
  173. if sCommand == "logout" then
  174.     logout()
  175. elseif sCommand == "put" then
  176.     -- Upload a file to pastebin.com
  177.     -- Determine file to upload
  178.     local sFile = tArgs[2]
  179.     local sPath = shell.resolve(sFile)
  180.     if not fs.exists(sPath) or fs.isDir(sPath) then
  181.         print("No such file")
  182.         return
  183.     end
  184.  
  185.     -- Read in the file
  186.     local sName = fs.getName(sPath)
  187.     local file = fs.open(sPath, "r")
  188.     local sText = file.readAll()
  189.     local ukey = ""
  190.     file.close()
  191.     --Read user key
  192.     if fs.exists(".UKey.auth") then
  193.         file = fs.open(".UKey.auth","r")
  194.         ukey=file.readLine()
  195.         --print(ukey)
  196.         file.close()
  197.     end
  198.     -- POST the contents to pastebin
  199.     write("Connecting to pastebin.com... ")
  200.     local response = http.post(
  201.         "https://pastebin.com/api/api_post.php",
  202.         "api_option=paste&" ..
  203.         "api_dev_key=" .. key .. "&" ..
  204.         "api_user_key="..ukey.."&"..
  205.         "api_paste_format=lua&" ..
  206.         "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
  207.         "api_paste_code=" .. textutils.urlEncode(sText)
  208.     )
  209.  
  210.     if response then
  211.         print("Success.")
  212.  
  213.         local sResponse = response.readAll()
  214.         response.close()
  215.  
  216.         local sCode = string.match(sResponse, "[^/]+$")
  217.         print("Uploaded as " .. sResponse)
  218.         print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
  219.  
  220.     else
  221.         print("Failed.")
  222.     end
  223.  
  224. elseif sCommand == "get" then
  225.     -- Download a file from pastebin.com
  226.     if #tArgs < 3 then
  227.         printUsage()
  228.         return
  229.     end
  230.  
  231.     -- Determine file to download
  232.     local sCode = tArgs[2]
  233.     local sFile = tArgs[3]
  234.     local sPath = shell.resolve(sFile)
  235.     if fs.exists(sPath) then
  236.         print("File already exists")
  237.         return
  238.     end
  239.  
  240.     -- GET the contents from pastebin
  241.     local res = get(sCode)
  242.     if res then
  243.         local file = fs.open(sPath, "w")
  244.         file.write(res)
  245.         file.close()
  246.  
  247.         print("Downloaded as " .. sFile)
  248.     end
  249. elseif sCommand == "run" then
  250.     local sCode = tArgs[2]
  251.  
  252.     local res = get(sCode)
  253.     if res then
  254.         local func, err = load(res, sCode, "t", _ENV)
  255.         if not func then
  256.             printError(err)
  257.             return
  258.         end
  259.         local success, msg = pcall(func, select(3, ...))
  260.         if not success then
  261.             printError(msg)
  262.         end
  263.     end
  264. elseif sCommand == "login" then
  265.     login(key)
  266. elseif sCommand == "list" then
  267.     if #tArgs == 2 then
  268.         list(key,tArgs[2])
  269.     elseif #tArgs == 1 then
  270.         list(key,"",0)
  271.     end
  272. elseif sCommand == "uget" then
  273.     if #tArgs < 2 then
  274.         printUsage()
  275.         return
  276.     end
  277.     list(key,tArgs[2])
  278.     i=0
  279.     for line in io.lines("pbUGet.tmp") do
  280.         i=i+1
  281.     end
  282.     if i==0 then
  283.         printError("result not found")
  284.     elseif i>1 then
  285.         printError("not specific enough")
  286.     else
  287.         prg= arg[0] or fs.getName(shell.getRunningProgram())
  288.         code, name = ""
  289.         for line in io.lines("pbUGet.tmp") do
  290.             code, name = string.match(line,"(%w+)(.+)")
  291.         end
  292.         if #tArgs <3 then
  293.         shell.run(prg.." get "..code.." "..name)
  294.         else
  295.         shell.run(prg.." get "..code.." "..tArgs[3])
  296.         end
  297.     end
  298. else
  299.     printUsage()
  300.     return
  301. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement