coaster3000

pastebin

May 19th, 2013
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. local function printUsage()
  2.     print( "Usages:" )
  3.     print( "pastebin put <filename>" )
  4.     print( "pastebin put <filename> <username>")
  5.     print( "pastebin get <code> <filename>" )
  6. end
  7.  
  8. local tArgs = { ... }
  9. if #tArgs < 2 then
  10.     printUsage()
  11.     return
  12. end
  13.  
  14. if not http then
  15.     print( "Pastebin requires http API" )
  16.     print( "Set enableAPI_http to 1 in mod_ComputerCraft.cfg" )
  17.     return
  18. end
  19.  
  20. local sCommand = tArgs[1]
  21. if sCommand == "put" then
  22.     -- Upload a file to pastebin.com
  23.     -- Determine file to upload
  24.     local sFile = tArgs[2]
  25.     local sPath = shell.resolve( sFile )
  26.     if not fs.exists( sPath ) or fs.isDir( sPath ) then
  27.         print( "No such file" )
  28.         return
  29.     end
  30.    
  31.     -- Read in the file
  32.     local sName = fs.getName( sPath )
  33.     local file = fs.open( sPath, "r" )
  34.     local sText = file.readAll()
  35.     file.close()
  36.    
  37.     -- POST the contents to pastebin
  38.     write( "Connecting to pastebin.com... " )
  39.     local key = "0ec2eb25b6166c0c27a394ae118ad829"
  40.     local userKey = "none"
  41.     if #tArgs > 2 then
  42.         print("login specified..")
  43.         local username = tArgs[3]
  44.         write("Please input password:")
  45.         local pass = read("*")
  46.         userKey = http.post("http://pastebin.com/api/api_login.php", "api_dev_key="..key.."&"..
  47.             "api_user_name="..username.."&"..
  48.             "api_user_password="..pass)
  49.         userKey = userKey.readAll()
  50.         if string.find(userKey, "Bad API request") ~= nil then
  51.             print("Failed to paste...")
  52.             print(userKey)
  53.             return
  54.         end
  55.        
  56.     end
  57.    
  58.     local response = nil
  59.  
  60.     local d = "api_option=paste&"..
  61.         "api_dev_key="..key.."&"..
  62.         "api_paste_format=lua&"..
  63.         "api_paste_name="..textutils.urlEncode(sName).."&"..
  64.         "api_paste_code="..textutils.urlEncode(sText)
  65.    
  66.     if (userKey ~= "none") and (userKey ~= nil) then
  67.         response = http.post("http://pastebin.com/api/api_post.php",
  68.             "api_option=paste&"..
  69.             "api_dev_key="..key.."&"..
  70.             "api_user_key="..userKey.."&"..
  71.             "api_paste_format=lua&"..
  72.             "api_paste_name="..textutils.urlEncode(sName).."&"..
  73.             "api_paste_code="..textutils.urlEncode(sText))
  74.     else
  75.         response = http.post(
  76.             "http://pastebin.com/api/api_post.php",
  77.             "api_option=paste&"..
  78.             "api_dev_key="..key.."&"..
  79.             "api_paste_format=lua&"..
  80.             "api_paste_name="..textutils.urlEncode(sName).."&"..
  81.             "api_paste_code="..textutils.urlEncode(sText)
  82.         )
  83.     end
  84.  
  85.     if response then
  86.        
  87.        
  88.         local sResponse = response.readAll()
  89.         response.close()
  90.         if string.find(sResponse, "Bad API request") ~= nil then
  91.             print("Failed to paste..")
  92.             print(sResponse)
  93.             print("")
  94.             print(d)
  95.             return
  96.         end
  97.                
  98.         local sCode = string.match( sResponse, "[^/]+$" )
  99.         print( "Uploaded as "..sResponse )
  100.         print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
  101.  
  102.     else
  103.         print( "Failed." )
  104.     end
  105.    
  106. elseif sCommand == "get" then
  107.     -- Download a file from pastebin.com
  108.     if #tArgs < 3 then
  109.         printUsage()
  110.         return
  111.     end
  112.  
  113.     -- Determine file to download
  114.     local sCode = tArgs[2]
  115.     local sFile = tArgs[3]
  116.     local sPath = shell.resolve( sFile )
  117.     if fs.exists( sPath ) then
  118.         print( "File already exists" )
  119.         return
  120.     end
  121.    
  122.     -- GET the contents from pastebin
  123.     write( "Connecting to pastebin.com... " )
  124.     local response = http.get(
  125.         "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  126.         )
  127.        
  128.     if response then
  129.         print( "Success." )
  130.        
  131.         local sResponse = response.readAll()
  132.         response.close()
  133.        
  134.         local file = fs.open( sPath, "w" )
  135.         file.write( sResponse )
  136.         file.close()
  137.        
  138.         print( "Downloaded as "..sFile )
  139.        
  140.     else
  141.         print( "Failed." )
  142.     end
  143.  
  144. else
  145.     printUsage()
  146.     return
  147. end
Advertisement
Add Comment
Please, Sign In to add comment