Serval_Mart-Lag

pastebin ComputerCraft (CC: T) with personal key

May 27th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.11 KB | None | 0 0
  1. local function printUsage()
  2.     print("Usages:")
  3.     print("pastebin put <filename>")
  4.     print("pastebin get <code> <filename>")
  5.     print("pastebin run <code> <arguments>")
  6.     print("pastebin connect")
  7.     print("pastebin disconnect")
  8.     print("pastebin list")
  9.     print("pastebin infos")
  10.     print("pastebin delete <code>")
  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 print_parsed_list(input)
  79.     local sCodes = string.gmatch(input, "<paste_key>([^\<]+)</paste_key>")
  80.     local sNames = string.gmatch(input, "<paste_title>([^\<]+)</paste_title>")
  81.     print("code     | name")
  82.     local code = sCodes()
  83.     local name = sNames()
  84.     while code ~= nil and name ~= nil do
  85.         print(code.." | "..name)
  86.         code = sCodes()
  87.         name = sNames()
  88.     end
  89. end
  90.  
  91. local sCommand = tArgs[1]
  92.  
  93. local dev_key = "b022e80b75dbb06591e8393a57bf1a9c"
  94.  
  95. if sCommand == "connect" then
  96.     write("Username: ")
  97.     username = read()
  98.     write("Password: ")
  99.     password = read("*")
  100.  
  101.     write("Connecting to pastebin.com... ")
  102.     local response = http.post(
  103.     "https://pastebin.com/api/api_login.php",
  104.     "api_dev_key=" .. dev_key .. "&" ..
  105.     "api_user_name=" .. textutils.urlEncode(username) .. "&" ..
  106.     "api_user_password=" .. textutils.urlEncode(password)
  107.     )
  108.     if response then
  109.         print("Success.")
  110.  
  111.         local sResponse = response.readAll()
  112.         response.close()
  113.  
  114.         print(sResponse)
  115.         settings.set("pastebin.key", sResponse)
  116.         settings.save(".settings")
  117.     else
  118.         print("failed")
  119.     end
  120.  
  121. elseif sCommand == "disconnect" then
  122.     settings.unset("pastebin.key")
  123.     settings.save(".settings")
  124.     print("Disconnected")
  125.  
  126. elseif sCommand == "list" then
  127.     local user_key = settings.get("pastebin.key")
  128.     if not user_key then
  129.         io.stderr:write("You aren't logged")
  130.         print("first use: pastebin connect")
  131.         return
  132.     end
  133.  
  134.     write("Connecting to pastebin.com... ")
  135.     local response = http.post(
  136.     "https://pastebin.com/api/api_post.php",
  137.     "api_option=list&" ..
  138.     "api_dev_key=" .. dev_key .. "&" ..
  139.     "api_user_key=" .. user_key
  140.     )
  141.  
  142.     if response then
  143.         local sResponse = response.readAll()
  144.         response.close()
  145.         if sResponse == "Bad API request, invalid api_user_key" then
  146.             print("Failed.")
  147.             print("You aren't logged")
  148.             print("first use: pastebin connect")
  149.         elseif sResponse == "No pastes found." then
  150.             print("Success.")
  151.             print("No pastes found.")
  152.         else
  153.             print("Success.")
  154.             print_parsed_list(sResponse)
  155.         end
  156.     else
  157.         print("Failed.")
  158.     end
  159.  
  160. elseif sCommand == "infos" then
  161.     local user_key = settings.get("pastebin.key")
  162.     if not user_key then
  163.         io.stderr:write("You aren't logged")
  164.         print("first use: pastebin connect")
  165.         return
  166.     end
  167.  
  168.     write("Connecting to pastebin.com... ")
  169.     local response = http.post(
  170.     "https://pastebin.com/api/api_post.php",
  171.     "api_option=userdetails&" ..
  172.     "api_dev_key=" .. dev_key .. "&" ..
  173.     "api_user_key=" .. user_key
  174.     )
  175.  
  176.     if response then
  177.         local sResponse = response.readAll()
  178.         response.close()
  179.         if sResponse == "Bad API request, invalid api_user_key" then
  180.             print("Failed.")
  181.             print("You aren't logged")
  182.             print("first use: pastebin connect")
  183.         else
  184.             print("Success.")
  185.             local sValues = string.gmatch(sResponse, "<user_([^\>]+)>([^\<]+)<")
  186.             local fields, value = sValues()
  187.             while fields ~= nil do
  188.                 print(fields..": "..value)
  189.                 fields, value = sValues()
  190.             end
  191.         end
  192.     else
  193.         print("Failed.")
  194.     end
  195.  
  196. elseif sCommand == "delete" then
  197.     if #tArgs < 2 then
  198.         printUsage()
  199.         return
  200.     end
  201.  
  202.     local user_key = settings.get("pastebin.key")
  203.     if not user_key then
  204.         io.stderr:write("You aren't logged")
  205.         print("first use: pastebin connect")
  206.         return
  207.     end
  208.  
  209.     local paste_key = extractId(tArgs[2])
  210.     if not paste_key then
  211.         io.stderr:write("Invalid pastebin code.\n")
  212.         io.write("The code is the ID at the end of the pastebin.com URL.\n")
  213.         return
  214.     end
  215.  
  216.     write("Connecting to pastebin.com... ")
  217.     local response = http.post(
  218.     "https://pastebin.com/api/api_post.php",
  219.     "api_option=delete&" ..
  220.     "api_dev_key=" .. dev_key .. "&" ..
  221.     "api_user_key=" .. user_key .. "&" ..
  222.     "api_paste_key=" ..paste_key
  223.     )
  224.  
  225.     if response then
  226.         print("Success.")
  227.  
  228.         local sResponse = response.readAll()
  229.         response.close()
  230.  
  231.         print(sResponse)
  232.     else
  233.         print("Failed.")
  234.     end
  235.  
  236. elseif sCommand == "put" then
  237.     if #tArgs < 2 then
  238.         printUsage()
  239.         return
  240.     end
  241.     -- Upload a file to pastebin.com
  242.     -- Determine file to upload
  243.     local user_key = settings.get("pastebin.key")
  244.     local sFile = tArgs[2]
  245.     local sPath = shell.resolve(sFile)
  246.     if not fs.exists(sPath) or fs.isDir(sPath) then
  247.         print("No such file")
  248.         return
  249.     end
  250.  
  251.     -- Read in the file
  252.     local sName = fs.getName(sPath)
  253.     local file = fs.open(sPath, "r")
  254.     local sText = file.readAll()
  255.     file.close()
  256.  
  257.     -- POST the contents to pastebin
  258.     write("Connecting to pastebin.com... ")
  259.     local params = "api_option=paste&" ..
  260.     "api_dev_key=" .. dev_key .. "&" ..
  261.     "api_paste_format=lua&" ..
  262.     "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
  263.     "api_paste_code=" .. textutils.urlEncode(sText)
  264.     if user_key ~= nil then
  265.         params = params .. "&" .. "api_user_key=" .. user_key
  266.         write("Connected")
  267.     end
  268.     local response = http.post(
  269.         "https://pastebin.com/api/api_post.php",
  270.         params)
  271.  
  272.     if response then
  273.         print("Success.")
  274.  
  275.         local sResponse = response.readAll()
  276.         response.close()
  277.  
  278.         local sCode = string.match(sResponse, "[^/]+$")
  279.         print("Uploaded as " .. sResponse)
  280.         print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
  281.  
  282.     else
  283.         print("Failed.")
  284.     end
  285.  
  286. elseif sCommand == "get" then
  287.     -- Download a file from pastebin.com
  288.     if #tArgs < 3 then
  289.         printUsage()
  290.         return
  291.     end
  292.  
  293.     -- Determine file to download
  294.     local sCode = tArgs[2]
  295.     local sFile = tArgs[3]
  296.     local sPath = shell.resolve(sFile)
  297.     if fs.exists(sPath) then
  298.         print("File already exists")
  299.         return
  300.     end
  301.  
  302.     -- GET the contents from pastebin
  303.     local res = get(sCode)
  304.     if res then
  305.         local file = fs.open(sPath, "w")
  306.         file.write(res)
  307.         file.close()
  308.  
  309.         print("Downloaded as " .. sFile)
  310.     end
  311. elseif sCommand == "run" then
  312.     local sCode = tArgs[2]
  313.  
  314.     local res = get(sCode)
  315.     if res then
  316.         local func, err = load(res, sCode, "t", _ENV)
  317.         if not func then
  318.             printError(err)
  319.             return
  320.         end
  321.         local success, msg = pcall(func, select(3, ...))
  322.         if not success then
  323.             printError(msg)
  324.         end
  325.     end
  326. else
  327.     printUsage()
  328.     return
  329. end
Add Comment
Please, Sign In to add comment