Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Modified Pastebin script for ComputerCraft
- --by wok86
- --Allows pasting to pastebin account
- --Requires either preset info or input
- --information when pasting.
- local key = "dc7eb75661914ea2f2944041f68041df"
- --local pbuser = ""
- --local pbpass = ""
- --local pbpriv =
- local function printUsage()
- print( "Usages:" )
- print( "pastebin put <filename>" )
- print( "pastebin get <code> <filename>" )
- print( "pastebin run <code> <arguments>" )
- end
- local tArgs = { ... }
- if #tArgs < 2 then
- printUsage()
- return
- end
- if not http then
- printError( "Pastebin requires http API" )
- printError( "Set http_enable to true in ComputerCraft.cfg" )
- return
- end
- local function get(paste)
- write( "Connecting to pastebin.com... " )
- local response = http.get(
- "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
- )
- if response then
- print( "Success." )
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- printError( "Failed." )
- end
- end
- local sCommand = tArgs[1]
- if sCommand == "put" then
- -- Upload a file to pastebin.com
- -- 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
- -- Read in the file
- local sName = fs.getName( sPath )
- local file = fs.open( sPath, "r" )
- local sText = file.readAll()
- file.close()
- -- POST the contents to pastebin
- if pbuser == nil then
- write("PB Username: ")
- pbuser = read()
- end
- if pbpass == nil then
- write("PB Password: ")
- pbpass = read("*")
- end
- if pbpriv == nil then
- print("Paste Privacy")
- print("0 = Public")
- print("1 = Unlisted")
- print("2 = Private")
- write("Privacy option: ")
- pbpriv = read()
- end
- write( "Connecting to pastebin.com... " )
- local uResponse = http.post(
- "http://pastebin.com/api/api_login.php",
- "api_dev_key="..key.."&"..
- "api_user_name="..pbuser.."&"..
- "api_user_password="..pbpass
- )
- if uResponse then
- local ukResponse = uResponse.readAll()
- uResponse.close()
- uKey = ukResponse
- --print("User API key: "..uKey)
- else
- print("Failed to get uKey")
- end
- local response = http.post(
- "http://pastebin.com/api/api_post.php",
- "api_option=paste&"..
- "api_user_key="..uKey.."&"..
- "api_dev_key="..key.."&"..
- "api_paste_format=lua&"..
- "api_paste_private="..pbpriv.."&"..
- "api_paste_name="..textutils.urlEncode(sName).."&"..
- "api_paste_code="..textutils.urlEncode(sText)
- )
- if response then
- print( "Success." )
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match( sResponse, "[^/]+$" )
- print( "Uploaded as "..sResponse )
- print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
- else
- print( "Failed." )
- end
- elseif sCommand == "get" then
- -- Download a file from pastebin.com
- if #tArgs < 3 then
- printUsage()
- return
- end
- -- Determine file to download
- local sCode = tArgs[2]
- local sFile = tArgs[3]
- local sPath = shell.resolve( sFile )
- if fs.exists( sPath ) then
- print( "File already exists" )
- return
- end
- -- GET the contents from pastebin
- local res = get(sCode)
- if res then
- local file = fs.open( sPath, "w" )
- file.write( res )
- file.close()
- print( "Downloaded as "..sFile )
- end
- elseif sCommand == "run" then
- local sCode = tArgs[2]
- local res = get(sCode)
- if res then
- local func, err = loadstring(res)
- if not func then
- printError( err )
- return
- end
- setfenv(func, getfenv())
- local success, msg = pcall(func, unpack(tArgs, 3))
- if not success then
- printError( msg )
- end
- end
- else
- printUsage()
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement