1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 06:33:23 +00:00

Allow running pastebin with the URL

For instance, `pastebin run https://pastebin.com/LYAxmSby` will now
extract the code and download appropriately. Also add an error message
when we received something which is not a valid pastebin code.

See #134.
This commit is contained in:
SquidDev 2019-03-10 11:11:49 +00:00
parent 35ce0974cd
commit 47721bf76b
2 changed files with 30 additions and 5 deletions

View File

@ -18,7 +18,32 @@ if not http then
return
end
local function get(paste)
--- 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))
@ -31,7 +56,7 @@ local function get(paste)
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 ) )
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

View File

@ -1,17 +1,17 @@
describe("The http library", function()
describe("http.checkURL", function()
it("Accepts well formed domains", function()
it("accepts well formed domains", function()
expect({ http.checkURL("https://google.com")}):same({ true })
end)
it("Rejects malformed URLs", function()
it("rejects malformed URLs", function()
expect({ http.checkURL("google.com")}):same({ false, "Must specify http or https" })
expect({ http.checkURL("https:google.com")}):same({ false, "URL malformed" })
expect({ http.checkURL("https:/google.com")}):same({ false, "URL malformed" })
expect({ http.checkURL("wss://google.com")}):same({ false, "Invalid protocol 'wss'" })
end)
it("Rejects local domains", function()
it("rejects local domains", function()
expect({ http.checkURL("http://localhost")}):same({ false, "Domain not permitted" })
expect({ http.checkURL("http://127.0.0.1")}):same({ false, "Domain not permitted" })
end)