AliquotMesozoic

PasteManager

Jan 6th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1. local loginURL = 'http://pastebin.com/api/api_login.php'
  2. local pasteURL = 'http://pastebin.com/api/api_post.php'
  3. local getURL = 'http://pastebin.com/raw.php'
  4. local saveFile = fs.path(shell.running()) .. "pastebin.cfg"
  5. pastebin = {
  6.     --These three are required
  7.     api_dev_key = 'ec318e63f2bde4100bec7a66d5e1b950',
  8.     api_option = 'paste',
  9.     api_paste_code = '', --This will contain our paste
  10.  
  11.     --These are optional, but it's better if they're set
  12.     api_user_key = '', --This is retrieved by using the login function
  13.     api_paste_name = nil,
  14.     api_paste_format = 'Lua', --Syntax highlighting
  15.     api_paste_private = 0, --0: Global, 1: Unlisted, 2: Private
  16.     api_paste_expire_date = 'N', --'N' = Never
  17. }
  18.  
  19. local function makeRequest(url, postData)
  20.     local response = http.request(url, postData)
  21.     local result = ''
  22.     if response then
  23.         for line in response do
  24.             result = result .. line
  25.         end
  26.     end
  27.     return result
  28. end
  29.  
  30. local function saveData()
  31.     local file = fs.open(saveFile, "w")
  32.     if not file then
  33.         print("Unable to save data! You will not be able to login.")
  34.     end
  35.     file:write(text.serialize(pastebin))
  36.     file:close()
  37. end
  38.  
  39. local function readData()
  40.     if fs.exists(saveFile) then
  41.         local file = fs.open(saveFile, "r")
  42.         local content = file:read(fs.size(saveFile))
  43.         file:close()
  44.         pastebin = text.unserialize(content)
  45.     end
  46. end
  47.  
  48. local function login(username, password)
  49.     loginData = {api_dev_key=pastebin.api_dev_key, api_user_name=username, api_user_password=password}
  50.     local response = makeRequest(loginURL, loginData)
  51.     if not response or response:find("Bad API request") then
  52.         return response
  53.     end
  54.     pastebin.api_user_key = response
  55.     saveData()
  56.     return true
  57. end
  58.  
  59. local options = {
  60.     login = function(...)
  61.         local arg = {...}
  62.         if not arg[3] then
  63.             print("Missing required arguments: username and password")
  64.             print("Usage: pastebin login <username> <password>")
  65.             return false
  66.         end
  67.         if not login(arg[2], arg[3]) then
  68.             print("Unable to login!")
  69.         else print("Logged in successfully: User key set to -> " .. pastebin.api_user_key) end
  70.     end;
  71.     logout = function(...)
  72.         local arg = {...}
  73.         pastebin.api_user_key = ''
  74.         saveData()
  75.         print("Successfully logged out.")
  76.     end;
  77.     get = function(...)
  78.         local arg = {...}
  79.         if not arg[3] then
  80.             print("Missing required arguments: paste_key and file_name")
  81.             print("Usage: pastebin get <paste key> <intended file name>")
  82.             return false
  83.         end
  84.         local response = makeRequest(getURL.."?i="..arg[2])
  85.         if response then
  86.             local file = fs.open(arg[3], "w")
  87.             file:write(response)
  88.             file:close()
  89.             print("Paste successfully downloaded as " .. arg[3])
  90.             return
  91.         end
  92.         print("Oops! Something went wrong...")
  93.     end;
  94.     paste = function(...)
  95.         local arg = {...}
  96.         if not arg[2] then
  97.             print("Missing required arguments: file_name")
  98.             print("Usage: pastebin paste <file name> [paste name] [syntax format]")
  99.             return
  100.         elseif not fs.exists(arg[2]) then
  101.             print("Invalid file name: Please specify a valid file to upload.")
  102.             print("Usage: pastebin paste <file name> [paste name] [syntax format]")
  103.         end
  104.         local file = fs.open(arg[2],"r")
  105.         local content = file:read(fs.size(arg[2]))
  106.         file:close()
  107.         readData()
  108.         pastebin.api_paste_code = content
  109.         pastebin.api_paste_name = arg[3]
  110.         pastebin.api_paste_format = arg[4] or pastebin.api_paste_format
  111.         print(makeRequest(pasteURL, pastebin))
  112.     end;
  113. }
  114.  
  115. local tArgs = {...}
  116. if not tArgs[1] or not options[tArgs[1]] then
  117.     print("Usage:")
  118.     print("pastebin login <username> <password>")
  119.     print("pastebin logout")
  120.     print("pastebin get <paste key> <intended file name>")
  121.     print("pastebin paste <file name> [paste name] [syntax format]")
  122. else
  123.     options[tArgs[1]](table.unpack(tArgs))
  124. end
Advertisement
Add Comment
Please, Sign In to add comment