craniumkid22

SmartPaste Lite

Jul 4th, 2014
737
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None | 0 0
  1. --check for HTTP API
  2. if not http then
  3.     print("Please enable the HTTP API within the ComputerCraft configuration.")
  4.     return
  5. end
  6.  
  7. --variables
  8. local devKey = "3575e50cc6635311078014a550bc3f9f"
  9. local userKey = nil
  10. local logged = false
  11. local tArgs = {...}
  12. local badResponses = {
  13.     "Bad API request, IP blocked",
  14.     "Bad API request, api_paste_code was empty",
  15.     "Bad API request, maximum paste file size exceeded",
  16.     "Error, this is a private paste. If this is your private paste, please login to Pastebin first."
  17.     }
  18. local badRequests = {
  19.     "Bad API request, invalid login",
  20.     "Bad API request, account not active"
  21.     }
  22.  
  23. --check for arguments
  24. local function usage()
  25.     print("Usage:")
  26.     print(shell.getRunningProgram().." <get> <pastebin code> <save file>")
  27.     print(shell.getRunningProgram().." <put> <save file> [username]")
  28.     print("Required: < >")
  29.     print("Optional: [ ]")
  30. end
  31.  
  32. if #tArgs >= 2 and #tArgs <= 4 then
  33.     if tArgs[1] == "get" then
  34.         print("Retrieving from url: http://pastebin.com/"..tArgs[2])
  35.         print("Saving to file: "..tArgs[3])
  36.         if #tArgs[2] > 8 or #tArgs[2] < 8 then
  37.             print("Invalid Pastebin code!")
  38.         else
  39.             print("Connecting to Pastebin...")
  40.             local site = http.get("http://pastebin.com/raw.php?i="..tArgs[2])
  41.             if not site then
  42.                 print("Unable to connect to Pastebin!")
  43.             else
  44.                 print("Connected! Reading url contents...")
  45.                 local siteContent = site.readAll()
  46.                 if siteContent == badResponses[4] then
  47.                     print("\nERROR - Private paste!\nUnable to download.")
  48.                     return
  49.                 else
  50.                     if not fs.exists(tArgs[3]) then
  51.                         local file = fs.open(tArgs[3], "w")
  52.                         file.write(siteContent)
  53.                         file.close()
  54.                         print("Download complete! Saved as '"..tArgs[3].."'")
  55.                     else
  56.                         print("File already exists!")
  57.                     end
  58.                 end
  59.             end
  60.         end
  61.     elseif tArgs[1] == "put" then
  62.         if not fs.exists(tArgs[2]) then
  63.             print("No file exists!")
  64.         else
  65.             if not tArgs[3] then
  66.                 print("Reading file...")
  67.                 local file = fs.open(tArgs[2], "r")
  68.                 local postText = file.readAll()
  69.                 file.close()
  70.                 local postName = tArgs[2]
  71.  
  72.                 local response = http.post(
  73.                     "http://pastebin.com/api/api_post.php",
  74.                     "api_option=paste&"..
  75.                     "api_dev_key="..devKey.."&"..
  76.                     "api_paste_format=lua&"..
  77.                     "api_paste_name="..textutils.urlEncode(postName).."&"..
  78.                     "api_paste_code="..textutils.urlEncode(postText)
  79.                     ).readAll()
  80.                 if response then
  81.                     for i = 1, #badResponses do
  82.                         if response == badResponses[i] then
  83.                             print(badResponses[i])
  84.                             break
  85.                         else
  86.                             print("Save successful! File stored at:\n"..response)
  87.                             break
  88.                         end
  89.                     end
  90.                 else
  91.                     print("Unable to connect to Pastebin! Please try again.")
  92.                 end
  93.             else
  94.                 print("Using private authentication. Password required!")
  95.                 write("Password: ")
  96.                 local password = read("*")
  97.                 local response = http.post(
  98.                     "http://pastebin.com/api/api_login.php",
  99.                     "api_dev_key="..devKey.."&"..
  100.                     "api_user_name="..textutils.urlEncode(tArgs[3]).."&"..
  101.                     "api_user_password="..textutils.urlEncode(password)
  102.                     ).readAll()
  103.                
  104.                 if response == badRequests[1] then
  105.                     print("Invalid login!")
  106.                 elseif response == badRequests[2] then
  107.                     print("Inactive account!")
  108.                 else
  109.                     local userKey = response
  110.                     print("Reading file...")
  111.                     local file = fs.open(tArgs[2], "r")
  112.                     local postText = file.readAll()
  113.                     file.close()
  114.                     local postName = tArgs[2]
  115.                     local response = http.post(
  116.                         "http://pastebin.com/api/api_post.php",
  117.                         "api_option=paste&"..
  118.                         "api_dev_key="..devKey.."&"..
  119.                         "api_user_key="..userKey.."&"..
  120.                         "api_paste_format=lua&"..
  121.                         "api_paste_private=0&"..
  122.                         "api_paste_name="..textutils.urlEncode(postName).."&"..
  123.                         "api_paste_code="..textutils.urlEncode(postText)
  124.                         ).readAll()
  125.  
  126.                     if response then
  127.                         for i = 1, #badResponses do
  128.                             if response == badResponses[i] then
  129.                                 print(badResponses[i])
  130.                                 break
  131.                             else
  132.                                 print("Save successful! File stored at:\n"..response)
  133.                                 break
  134.                             end
  135.                         end
  136.                     else
  137.                         print("Unable to connect to Pastebin! Please try again.")
  138.                     end
  139.                 end
  140.             end
  141.         end
  142.     else
  143.         usage()
  144.     end
  145. else
  146.     usage()
  147. end
Advertisement
Add Comment
Please, Sign In to add comment