1lann

Better Pastebin

Nov 30th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3.  
  4. local dnt = false
  5. local data = {}
  6. local databaseLoc = "/.pastebin-database"
  7. local function loadDatabase()
  8.     if not(fs.exists(databaseLoc)) or fs.isDir(databaseLoc) then
  9.         fs.delete(databaseLoc)
  10.         local f = io.open(databaseLoc,"w")
  11.         f:write(textutils.serialize({false,{}}))
  12.         f:close()
  13.     end
  14.  
  15.     local f = io.open(databaseLoc,"r")
  16.     local database = textutils.unserialize(f:read("*a"))
  17.     f:close()
  18.  
  19.     if database then
  20.         if type(database[1]) == "boolean" then
  21.             dnt = database[1]
  22.             data = database[2]
  23.         else
  24.             print("Database corrupted, resetting")
  25.             fs.delete(databaseLoc)
  26.             local f = io.open(databaseLoc,"w")
  27.             f:write(textutils.serialize({false,{}}))
  28.             f:close()
  29.         end
  30.     else
  31.         print("Database corrupted, resetting")
  32.         fs.delete(databaseLoc)
  33.         local f = io.open(databaseLoc,"w")
  34.         f:write(textutils.serialize({false,{}}))
  35.         f:close()
  36.     end
  37. end
  38.  
  39. local function betterGet(url)
  40.     http.request(url)
  41.     while true do
  42.         local e, rUrl, rmsg = os.pullEvent()
  43.         if (e == "http_success") and (rUrl == url) then
  44.             if rmsg then
  45.                 local data = rmsg.readAll()
  46.                 rmsg.close()
  47.                 if data then
  48.                     return "success", data
  49.                 else
  50.                     sleep(1)
  51.                     http.request(url)
  52.                 end
  53.             else
  54.                 sleep(1)
  55.                 http.request(url)
  56.             end
  57.         elseif (e == "http_failure") and (rUrl == url) then
  58.             return "failure"
  59.         end
  60.     end
  61. end
  62.  
  63. local function track(file, url)
  64.     if not dnt then
  65.         data[shell.resolve(file)] = url
  66.         local f = io.open(databaseLoc,"w")
  67.         f:write(textutils.serialize({false,data}))
  68.         f:close()
  69.     end
  70. end
  71.  
  72. local function retrievePaste(paste, file)
  73.     loadDatabase()
  74.  
  75.     local pasteCode = nil
  76.     if paste:find("i=") then
  77.         pasteCode = paste:match("i=(%w+)")
  78.     elseif paste:find("pastebin%.com/") then
  79.         pasteCode = paste:match("pastebin%.com/(%w+)")
  80.     else
  81.         pasteCode = paste
  82.     end
  83.  
  84.     term.write("Retrieving: " .. pasteCode .. "...")
  85.     local resp, data = betterGet("http://pastebin.com/raw.php?i=" .. textutils.urlEncode(pasteCode))
  86.     local _, y = term.getCursorPos()
  87.     term.setCursorPos(1, y)
  88.     term.clearLine()
  89.     if resp == "success" then
  90.         if fs.exists(file) then
  91.             term.write("File exists, overwrite? [Y/n] ")
  92.             local input = read()
  93.             term.setCursorPos(1, y)
  94.             term.clearLine()
  95.             if input:lower():find("n") then
  96.                 print("Cancelled!")
  97.                 return false
  98.             end
  99.         end
  100.  
  101.         local f = io.open(file, "w")
  102.         f:write(data)
  103.         f:close()
  104.         track(file, "http://pastebin.com/raw.php?i=" .. textutils.urlEncode(pasteCode))
  105.         print("Paste saved!")
  106.     else
  107.         print("Failed to retrive paste! [1]")
  108.     end
  109. end
  110.  
  111. local function printUsage()
  112.     print("Usage:")
  113.     print("pastebin (get) <id> <filename>")
  114.     print("pastebin wget <url> <filename>")
  115.     print("pastebin <filename>")
  116.     print("pastebin dnt (Toggles tracking)")
  117. end
  118.  
  119. if tArgs[1] == "dnt" then
  120.     loadDatabase()
  121.     if dnt then
  122.         local f = io.open(databaseLoc,"w")
  123.         dnt = false
  124.         f:write(textutils.serialize({dnt,data}))
  125.         f:close()
  126.         print("Tracking turned on")
  127.     else
  128.         local f = io.open(databaseLoc,"w")
  129.         dnt = true
  130.         f:write(textutils.serialize({dnt,data}))
  131.         f:close()
  132.         print("Tracking turned off")
  133.     end
  134. elseif (tArgs[1] == "wget") then
  135.     if tArgs[2] and tArgs[3] then
  136.         loadDatabase()
  137.         local resp,data = betterGet(tArgs[2])
  138.         if resp == "success" then
  139.             if fs.exists(tArgs[3]) then
  140.                 write("File exists, overwrite [Y/n]? ")
  141.                 local input = read()
  142.                 if input:lower():find("n") then
  143.                     print("Cancelled!")
  144.                     return false
  145.                 else
  146.                     local f = io.open(tArgs[3],"w")
  147.                     f:write(data)
  148.                     f:close()
  149.                     track(tArgs[3],tArgs[2])
  150.                     print("File saved!")
  151.                     return true
  152.                 end
  153.             else
  154.                 local f = io.open(tArgs[3],"w")
  155.                 f:write(data)
  156.                 f:close()
  157.                 track(tArgs[3],tArgs[2])
  158.                 print("File saved!")
  159.                 return true
  160.             end
  161.         else
  162.             print("Failed to retrive file! [2]")
  163.             return false
  164.         end
  165.     else
  166.         printUsage()
  167.     end
  168. elseif tArgs[1] == "get" and tArgs[2] and tArgs[3] then
  169.     retrievePaste(tArgs[2], tArgs[3])
  170. elseif tArgs[1] and tArgs[2] then
  171.     retrievePaste(tArgs[1],tArgs[2])
  172. elseif tArgs[1] then
  173.     loadDatabase()
  174.     if data[shell.resolve(tArgs[1])] then
  175.         print("Retrieving:\n"..data[shell.resolve(tArgs[1])])
  176.         resp,data = betterGet(data[shell.resolve(tArgs[1])])
  177.         if resp == "success" then
  178.             local f = io.open(tArgs[1],"w")
  179.             f:write(data)
  180.             f:close()
  181.             print("Paste saved!")
  182.             return true
  183.         else
  184.             print("Failed to retrieve paste! [3]")
  185.         end
  186.     else
  187.         print("Could not find origin URL in database!")
  188.         printUsage()
  189.     end
  190. else
  191.     printUsage()
  192. end
Advertisement
Add Comment
Please, Sign In to add comment