Xyzzy

upastebin

Mar 9th, 2015
520
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Modified Pastebin script for ComputerCraft
  2. --by wok86
  3. --Allows pasting to pastebin account
  4. --Requires either preset info or input
  5. --information when pasting.
  6.  
  7. local key = "dc7eb75661914ea2f2944041f68041df"
  8. --local pbuser = ""
  9. --local pbpass = ""
  10. --local pbpriv =
  11.  
  12. local function printUsage()
  13.     print( "Usages:" )
  14.     print( "pastebin put <filename>" )
  15.     print( "pastebin get <code> <filename>" )
  16.     print( "pastebin run <code> <arguments>" )
  17. end
  18.  
  19. local tArgs = { ... }
  20. if #tArgs < 2 then
  21.     printUsage()
  22.     return
  23. end
  24.  
  25. if not http then
  26.     printError( "Pastebin requires http API" )
  27.     printError( "Set http_enable to true in ComputerCraft.cfg" )
  28.     return
  29. end
  30.  
  31. local function get(paste)
  32.     write( "Connecting to pastebin.com... " )
  33.     local response = http.get(
  34.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
  35.     )
  36.        
  37.     if response then
  38.         print( "Success." )
  39.        
  40.         local sResponse = response.readAll()
  41.         response.close()
  42.         return sResponse
  43.     else
  44.         printError( "Failed." )
  45.     end
  46. end
  47.  
  48. local sCommand = tArgs[1]
  49. if sCommand == "put" then
  50.     -- Upload a file to pastebin.com
  51.     -- Determine file to upload
  52.     local sFile = tArgs[2]
  53.     local sPath = shell.resolve( sFile )
  54.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  55.         print( "No such file" )
  56.         return
  57.     end
  58.    
  59.     -- Read in the file
  60.     local sName = fs.getName( sPath )
  61.     local file = fs.open( sPath, "r" )
  62.     local sText = file.readAll()
  63.     file.close()
  64.    
  65.    
  66.     -- POST the contents to pastebin
  67.     if pbuser == nil then    
  68.       write("PB Username: ")
  69.       pbuser = read()
  70.     end
  71.    
  72.     if pbpass == nil then
  73.       write("PB Password: ")
  74.       pbpass = read("*")
  75.     end
  76.    
  77.     if pbpriv == nil then
  78.       print("Paste Privacy")
  79.       print("0 = Public")
  80.       print("1 = Unlisted")
  81.       print("2 = Private")
  82.       write("Privacy option: ")
  83.       pbpriv = read()
  84.     end
  85.    
  86.     write( "Connecting to pastebin.com... " )
  87.     local uResponse = http.post(
  88.         "http://pastebin.com/api/api_login.php",
  89.         "api_dev_key="..key.."&"..
  90.         "api_user_name="..pbuser.."&"..
  91.         "api_user_password="..pbpass
  92.     )
  93.    
  94.     if uResponse then
  95.       local ukResponse = uResponse.readAll()
  96.       uResponse.close()
  97.       uKey = ukResponse
  98.       --print("User API key: "..uKey)
  99.     else
  100.       print("Failed to get uKey")
  101.     end
  102.    
  103.     local response = http.post(
  104.         "http://pastebin.com/api/api_post.php",
  105.         "api_option=paste&"..
  106.         "api_user_key="..uKey.."&"..
  107.         "api_dev_key="..key.."&"..
  108.         "api_paste_format=lua&"..
  109.         "api_paste_private="..pbpriv.."&"..
  110.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  111.         "api_paste_code="..textutils.urlEncode(sText)
  112.     )
  113.        
  114.     if response then
  115.         print( "Success." )
  116.        
  117.         local sResponse = response.readAll()
  118.         response.close()
  119.                
  120.         local sCode = string.match( sResponse, "[^/]+$" )
  121.         print( "Uploaded as "..sResponse )
  122.         print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  123.  
  124.     else
  125.         print( "Failed." )
  126.     end
  127.    
  128. elseif sCommand == "get" then
  129.     -- Download a file from pastebin.com
  130.     if #tArgs < 3 then
  131.         printUsage()
  132.         return
  133.     end
  134.  
  135.     -- Determine file to download
  136.     local sCode = tArgs[2]
  137.     local sFile = tArgs[3]
  138.     local sPath = shell.resolve( sFile )
  139.     if fs.exists( sPath ) then
  140.         print( "File already exists" )
  141.         return
  142.     end
  143.    
  144.     -- GET the contents from pastebin
  145.     local res = get(sCode)
  146.     if res then        
  147.         local file = fs.open( sPath, "w" )
  148.         file.write( res )
  149.         file.close()
  150.        
  151.         print( "Downloaded as "..sFile )
  152.     end
  153. elseif sCommand == "run" then
  154.     local sCode = tArgs[2]
  155.  
  156.     local res = get(sCode)
  157.     if res then
  158.         local func, err = loadstring(res)
  159.         if not func then
  160.             printError( err )
  161.             return
  162.         end
  163.         setfenv(func, getfenv())
  164.         local success, msg = pcall(func, unpack(tArgs, 3))
  165.         if not success then
  166.             printError( msg )
  167.         end
  168.     end
  169. else
  170.     printUsage()
  171.     return
  172. end
RAW Paste Data