opus/sys/apps/pastebin.lua

115 lines
2.2 KiB
Lua
Raw Permalink Normal View History

2019-03-26 13:08:39 +00:00
local function printUsage()
2019-04-14 14:04:22 +00:00
print( "Usages:" )
print( "pastebin put <filename>" )
print( "pastebin get <code> <filename>" )
print( "pastebin run <code> <arguments>" )
2019-03-26 13:08:39 +00:00
end
if not http then
2019-04-14 14:04:22 +00:00
printError( "Pastebin requires http API" )
printError( "Set http_enable to true in ComputerCraft.cfg" )
return
2019-03-26 13:08:39 +00:00
end
local pastebin = require('opus.http.pastebin')
2019-04-11 12:41:23 +00:00
2019-04-12 12:33:41 +00:00
local tArgs = { ... }
2019-03-26 13:08:39 +00:00
local sCommand = tArgs[1]
2019-04-12 12:33:41 +00:00
2019-03-26 13:08:39 +00:00
if sCommand == "put" then
2019-04-14 14:04:22 +00:00
-- Upload a file to pastebin.com
2019-04-12 12:33:41 +00:00
2019-04-14 14:04:22 +00:00
if #tArgs < 2 then
printUsage()
return
end
2019-04-12 12:33:41 +00:00
2019-04-14 14:04:22 +00:00
-- Determine file to upload
local sFile = tArgs[2]
local sPath = shell.resolve( sFile )
if not fs.exists( sPath ) or fs.isDir( sPath ) then
print( "No such file" )
return
end
2019-03-26 13:08:39 +00:00
2019-04-14 14:04:22 +00:00
print( "Connecting to pastebin.com... " )
2019-04-11 12:41:23 +00:00
2019-04-14 14:04:22 +00:00
local resp, msg = pastebin.put(sPath)
2019-03-26 13:08:39 +00:00
2019-04-14 14:04:22 +00:00
if resp then
print( "Uploaded as " .. resp )
print( "Run \"pastebin get "..resp.."\" to download anywhere" )
2019-03-26 13:08:39 +00:00
2019-04-14 14:04:22 +00:00
else
printError( msg )
end
2019-03-26 13:08:39 +00:00
elseif sCommand == "get" then
2019-04-14 14:04:22 +00:00
-- Download a file from pastebin.com
2019-04-12 12:33:41 +00:00
2019-04-14 14:04:22 +00:00
if #tArgs < 3 then
printUsage()
return
end
2019-03-26 13:08:39 +00:00
2019-04-14 14:04:22 +00:00
local sCode = pastebin.parseCode(tArgs[2])
2019-04-12 12:33:41 +00:00
if not sCode then
return false, "Invalid pastebin code. The code is the ID at the end of the pastebin.com URL."
2019-04-14 14:04:22 +00:00
end
2019-04-11 12:41:23 +00:00
2019-04-14 14:04:22 +00:00
-- Determine file to download
local sFile = tArgs[3]
local sPath = shell.resolve( sFile )
if fs.exists( sPath ) then
printError( "File already exists" )
return
end
2019-03-26 13:08:39 +00:00
2019-04-14 14:04:22 +00:00
print( "Connecting to pastebin.com... " )
2019-04-12 12:33:41 +00:00
2019-04-14 14:04:22 +00:00
local resp, msg = pastebin.get(sCode, sPath)
2019-03-26 13:08:39 +00:00
2019-04-14 14:04:22 +00:00
if resp then
print( "Downloaded as " .. sPath )
else
printError( msg )
end
2019-04-11 12:41:23 +00:00
2019-03-26 13:08:39 +00:00
elseif sCommand == "run" then
2019-04-14 14:04:22 +00:00
-- Download and run a file from pastebin.com
2019-04-12 12:33:41 +00:00
2019-04-14 14:04:22 +00:00
if #tArgs < 2 then
printUsage()
return
end
2019-04-12 12:33:41 +00:00
2019-04-14 14:04:22 +00:00
local sCode = pastebin.parseCode(tArgs[2])
2019-04-12 12:33:41 +00:00
if not sCode then
return false, "Invalid pastebin code. The code is the ID at the end of the pastebin.com URL."
2019-04-14 14:04:22 +00:00
end
2019-03-26 13:08:39 +00:00
2019-04-14 14:04:22 +00:00
print( "Connecting to pastebin.com... " )
2019-04-11 12:41:23 +00:00
2019-04-12 12:33:41 +00:00
local res, msg = pastebin.download(sCode)
if not res then
2019-04-14 14:04:22 +00:00
printError( msg )
2019-04-12 12:33:41 +00:00
return res, msg
end
2019-04-14 14:04:22 +00:00
res, msg = load(res, sCode, "t", _ENV)
2019-04-12 12:33:41 +00:00
if not res then
2019-04-14 14:04:22 +00:00
printError( msg )
2019-04-12 12:33:41 +00:00
return res, msg
end
2019-04-14 14:04:22 +00:00
res, msg = pcall(res, table.unpack(tArgs, 3))
if not res then
printError( msg )
end
2019-03-26 13:08:39 +00:00
else
2019-04-11 12:41:23 +00:00
2019-04-14 14:04:22 +00:00
printUsage()
return
2019-03-26 13:08:39 +00:00
end
2019-04-12 12:33:41 +00:00