cleaned up pastebin.lua a bit (indentation, general formatting) and fixed one or two bugs, as well as adding support for custom config and handling failed uploads ("Bad API request ...")

This commit is contained in:
Florian Nücke 2014-01-27 00:24:39 +01:00
parent a78eb10804
commit ee239fb3e2
2 changed files with 136 additions and 144 deletions

View File

@ -1,160 +1,152 @@
--This program gets code from Pastebin.com.
--It also allows you to put your code on the website
--Edited by Vexatos
--[[ This program allows downloading and uploading from and to pastebin.com.
Authors: Sangar, Vexatos ]]
local component = require("component")
local fs = require("filesystem")
local internet = require("internet")
local component = require("component")
local shell = require("shell")
local term = require("term")
local args, options = shell.parse(...)
local function printUsage()
print("Usages:")
print("pastebin put [-f] <file>")
print("pastebin get [-f] <id> <file>")
print("pastebin run [-f] <id> <arguments>")
print(" -f: Force overwriting existing files.")
print(" -k: keep line endings as-is (will convert")
print(" Windows line endings to Unix otherwise).")
end
if #args < 2 then
printUsage()
if not component.isAvailable("internet") then
print("This program requires an internet card to run.")
return
end
if not component.isAvailable("internet") then
print( "Error: Pastebin requires an Internet Card to run" )
return
end
local args, options = shell.parse(...)
--This gets code from the website and stores it in the specified file
local function get(paste, filename)
term.write( "Connecting to pastebin.com... " )
local f, reason = io.open(filename, "w")
if not f then
print("failed opening file for writing: " .. reason)
return
end
local url = "http://pastebin.com/raw.php?i=" .. paste
local result, response = pcall(internet.request, url)
if result then
print("Success.")
for chunk in response do
if not options.k then
string.gsub(chunk, "\r\n", "\n")
end
f:write(chunk)
end
f:close()
print("saved data to " .. filename)
else
f:close()
fs.remove(filename)
print("http request failed: " .. response)
end
end
--This makes a string safe for being used in a URL
function encode( code )
if code then
code = string.gsub (code, "([^%w ])",
function (c)
return string.format ("%%%02X", string.byte(c))
end)
code = string.gsub (code, " ", "+")
end
return code
end
--This stores the program in a temporary file, which it will
--delete after the program was executed
function run(paste)
local tmpFile = "/tmp/tmp_pastebin.lua"
get(paste,shell.resolve(tmpFile))
term.clear()
print("Running...")
local success, msg = shell.execute(tmpFile, _ENV, table.unpack(args, 3))
if not success then
print( msg )
end
fs.remove(tmpFile)
end
--This lets you put your own code on pastebin.com
function put(file)
--local fKey = io.open("/etc/pastebin.key","r")
--local key = fKey:read("*a")
--fKey:close()
local path = shell.resolve( file )
local sName = fs.name( path )
local key = "fd92bd40a84c127eeb6804b146793c97"
local fText, reason = io.open(path,"r")
if not fText then
print("failed opening file for reading: " .. reason)
local function get(pasteId, filename)
local f, reason = io.open(filename, "w")
if not f then
print("Failed opening file for writing: " .. reason)
return
end
local sText = fText:read("*a")
fText:close()
local result,response = pcall(internet.request,
"http://pastebin.com/api/api_post.php",
"api_option=paste&"..
"api_dev_key="..key.."&"..
"api_paste_format=lua&"..
"api_paste_expire_date=N&"..
"api_paste_name="..encode(sName).."&"..
"api_paste_code="..encode(sText)
)
if response then
print( "Success." )
local sResponse = ""
for chunk in response do
sResponse = sResponse..chunk
end
local sCode = string.match( sResponse, "[^/]+$" )
print( "Uploaded as "..sResponse )
print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
else
print( "Failed: "..response )
end
end
local sCommand = args[1]
local kArg = args[2]
if #args >=2 then
if sCommand=="put" then
if #args == 2 then
local file = kArg
put(file)
else
printUsage()
return
end
elseif sCommand == "get" then
if #args == 3 then
local sFile = shell.resolve(args[3])
if fs.exists(sFile) then
if not options.f or not os.remove(sFile) then
print("file already exists")
return
end
term.write("Downloading from pastebin.com... ")
local url = "http://pastebin.com/raw.php?i=" .. pasteId
local result, response = pcall(internet.request, url)
if result then
print("success.")
for chunk in response do
if not options.k then
string.gsub(chunk, "\r\n", "\n")
end
get(kArg,sFile)
else
printUsage()
return
f:write(chunk)
end
elseif sCommand == "run" then
run(kArg)
f:close()
print("Saved data to " .. filename)
else
f:close()
fs.remove(filename)
print("HTTP request failed: " .. response)
end
else
printUsage()
return
end
-- This makes a string safe for being used in a URL.
function encode(code)
if code then
code = string.gsub(code, "([^%w ])", function (c)
return string.format("%%%02X", string.byte(c))
end)
code = string.gsub (code, " ", "+")
end
return code
end
-- This stores the program in a temporary file, which it will
-- delete after the program was executed.
function run(pasteId, ...)
local tmpFile = os.tmpname()
get(pasteId, tmpFile)
print("Running...")
local success, reason = shell.execute(tmpFile, _ENV, ...)
if not success then
print(reason)
end
fs.remove(tmpFile)
end
-- Uploads the specified file as a new paste to pastebin.com.
function put(path)
local config = {}
local configFile = loadfile("/etc/pastebin.conf", "t", config)
if configFile then
local result, reason = pcall(configFile)
if not result then
print("Failed loading config: " .. reason)
end
end
config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
local file, reason = io.open(path, "r")
if not file then
print("Failed opening file for reading: " .. reason)
return
end
local data = file:read("*a")
file:close()
term.write("Uploading to pastebin.com... ")
local result, response = pcall(internet.request,
"http://pastebin.com/api/api_post.php",
"api_option=paste&" ..
"api_dev_key=" .. config.key .. "&" ..
"api_paste_format=lua&" ..
"api_paste_expire_date=N&" ..
"api_paste_name=" .. encode(fs.name(path)) .. "&" ..
"api_paste_code=" .. encode(data))
if result then
local info = ""
for chunk in response do
info = info .. chunk
end
if string.match(info, "^Bad API request, ") then
print("failed.")
print(info)
else
print("success.")
local pasteId = string.match(info, "[^/]+$")
print("Uploaded as " .. info)
print('Run "pastebin get ' .. pasteId .. '" to download anywhere.')
end
else
print("failed: " .. response)
end
end
local command = args[1]
if command == "put" then
if #args == 2 then
put(shell.resolve(args[2]))
return
end
elseif command == "get" then
if #args == 3 then
local path = shell.resolve(args[3])
if fs.exists(path) then
if not options.f or not os.remove(path) then
print("file already exists")
return
end
end
get(args[2], path)
return
end
elseif command == "run" then
if #args >= 2 then
run(args[2], table.unpack(args, 3))
return
end
end
-- If we come here there was some invalid input.
print("Usages:")
print("pastebin put [-f] <file>")
print("pastebin get [-f] <id> <file>")
print("pastebin run [-f] <id> [<arguments...>]")
print(" -f: Force overwriting existing files.")
print(" -k: keep line endings as-is (will convert")
print(" Windows line endings to Unix otherwise).")

View File

@ -50,7 +50,7 @@ end
function os.tmpname()
if fs.exists("tmp") then
for i = 1, 10 do
local name = "tmp/" .. math.random(1, 0x7FFFFFFF)
local name = "/tmp/" .. math.random(1, 0x7FFFFFFF)
if not fs.exists(name) then
return name
end