abstract_abstract

past.lua

Oct 22nd, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.67 KB | None | 0 0
  1. -- SPDX-FileCopyrightText: 2017 Daniel Ratcliffe
  2. --
  3. -- SPDX-License-Identifier: LicenseRef-CCPL
  4.  
  5. local function printUsage()
  6.     local programName = arg[0] or fs.getName(shell.getRunningProgram())
  7.     print("Usages:")
  8.     print(programName .. " put <filename>")
  9.     print(programName .. " get <code> <filename>")
  10.     print(programName .. " run <code> <arguments>")
  11. end
  12.  
  13. local tArgs = { ... }
  14. if #tArgs < 2 then
  15.     printUsage()
  16.     return
  17. end
  18.  
  19. if not http then
  20.     printError("Pastebin requires the http API, but it is not enabled")
  21.     printError("Set http.enabled to true in CC: Tweaked's server 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 sCommand = tArgs[1]
  79. if sCommand == "put" then
  80.     -- Upload a file to pastebin.com
  81.     -- Determine file to upload
  82.     local sFile = tArgs[2]
  83.     local sPath = shell.resolve(sFile)
  84.     if not fs.exists(sPath) or fs.isDir(sPath) then
  85.         print("No such file")
  86.         return
  87.     end
  88.  
  89.     -- Read in the file
  90.     local sName = fs.getName(sPath)
  91.     local file = fs.open(sPath, "r")
  92.     local sText = file.readAll()
  93.     file.close()
  94.  
  95.     -- POST the contents to pastebin
  96.     write("Connecting to pastebin.com... ")
  97.     local key = "7bce7025cbe07fd47dbbe79395ea8d73"
  98.     local response = http.post(
  99.         "https://pastebin.com/api/api_post.php",
  100.         "api_option=paste&" ..
  101.         "api_dev_key=" .. key .. "&" ..
  102.         "api_user_key=" .. "c11bb1afa0c23e71e61a169c1fa9a0f5" .. "&" ..
  103.         "api_paste_format=lua&" ..
  104.         "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
  105.         "api_paste_code=" .. textutils.urlEncode(sText)
  106.     )
  107.  
  108.     if response then
  109.         print("Success.")
  110.  
  111.         local sResponse = response.readAll()
  112.         response.close()
  113.  
  114.         local sCode = string.match(sResponse, "[^/]+$")
  115.         print("Uploaded as " .. sResponse)
  116.         print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
  117.  
  118.     else
  119.         print("Failed.")
  120.     end
  121.  
  122. elseif sCommand == "get" then
  123.     -- Download a file from pastebin.com
  124.     if #tArgs < 3 then
  125.         printUsage()
  126.         return
  127.     end
  128.  
  129.     -- Determine file to download
  130.     local sCode = tArgs[2]
  131.     local sFile = tArgs[3]
  132.     local sPath = shell.resolve(sFile)
  133.     if fs.exists(sPath) then
  134.         print("File already exists")
  135.         return
  136.     end
  137.  
  138.     -- GET the contents from pastebin
  139.     local res = get(sCode)
  140.     if res then
  141.         local file = fs.open(sPath, "w")
  142.         file.write(res)
  143.         file.close()
  144.  
  145.         print("Downloaded as " .. sFile)
  146.     end
  147. elseif sCommand == "run" then
  148.     local sCode = tArgs[2]
  149.  
  150.     local res = get(sCode)
  151.     if res then
  152.         local func, err = load(res, sCode, "t", _ENV)
  153.         if not func then
  154.             printError(err)
  155.             return
  156.         end
  157.         local success, msg = pcall(func, select(3, ...))
  158.         if not success then
  159.             printError(msg)
  160.         end
  161.     end
  162. else
  163.     printUsage()
  164.     return
  165. end
  166.  
Add Comment
Please, Sign In to add comment