Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local dnt = false
- local data = {}
- local databaseLoc = "/.pastebin-database"
- local function loadDatabase()
- if not(fs.exists(databaseLoc)) or fs.isDir(databaseLoc) then
- fs.delete(databaseLoc)
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,{}}))
- f:close()
- end
- local f = io.open(databaseLoc,"r")
- local database = textutils.unserialize(f:read("*a"))
- f:close()
- if database then
- if type(database[1]) == "boolean" then
- dnt = database[1]
- data = database[2]
- else
- print("Database corrupted, resetting")
- fs.delete(databaseLoc)
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,{}}))
- f:close()
- end
- else
- print("Database corrupted, resetting")
- fs.delete(databaseLoc)
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,{}}))
- f:close()
- end
- end
- local function betterGet(url)
- http.request(url)
- while true do
- local e, rUrl, rmsg = os.pullEvent()
- if (e == "http_success") and (rUrl == url) then
- if rmsg then
- local data = rmsg.readAll()
- rmsg.close()
- if data then
- return "success", data
- else
- sleep(1)
- http.request(url)
- end
- else
- sleep(1)
- http.request(url)
- end
- elseif (e == "http_failure") and (rUrl == url) then
- return "failure"
- end
- end
- end
- local function track(file, url)
- if not dnt then
- data[shell.resolve(file)] = url
- local f = io.open(databaseLoc,"w")
- f:write(textutils.serialize({false,data}))
- f:close()
- end
- end
- local function retrievePaste(paste, file)
- loadDatabase()
- local pasteCode = nil
- if paste:find("i=") then
- pasteCode = paste:match("i=(%w+)")
- elseif paste:find("pastebin%.com/") then
- pasteCode = paste:match("pastebin%.com/(%w+)")
- else
- pasteCode = paste
- end
- term.write("Retrieving: " .. pasteCode .. "...")
- local resp, data = betterGet("http://pastebin.com/raw.php?i=" .. textutils.urlEncode(pasteCode))
- local _, y = term.getCursorPos()
- term.setCursorPos(1, y)
- term.clearLine()
- if resp == "success" then
- if fs.exists(file) then
- term.write("File exists, overwrite? [Y/n] ")
- local input = read()
- term.setCursorPos(1, y)
- term.clearLine()
- if input:lower():find("n") then
- print("Cancelled!")
- return false
- end
- end
- local f = io.open(file, "w")
- f:write(data)
- f:close()
- track(file, "http://pastebin.com/raw.php?i=" .. textutils.urlEncode(pasteCode))
- print("Paste saved!")
- else
- print("Failed to retrive paste! [1]")
- end
- end
- local function printUsage()
- print("Usage:")
- print("pastebin (get) <id> <filename>")
- print("pastebin wget <url> <filename>")
- print("pastebin <filename>")
- print("pastebin dnt (Toggles tracking)")
- end
- if tArgs[1] == "dnt" then
- loadDatabase()
- if dnt then
- local f = io.open(databaseLoc,"w")
- dnt = false
- f:write(textutils.serialize({dnt,data}))
- f:close()
- print("Tracking turned on")
- else
- local f = io.open(databaseLoc,"w")
- dnt = true
- f:write(textutils.serialize({dnt,data}))
- f:close()
- print("Tracking turned off")
- end
- elseif (tArgs[1] == "wget") then
- if tArgs[2] and tArgs[3] then
- loadDatabase()
- local resp,data = betterGet(tArgs[2])
- if resp == "success" then
- if fs.exists(tArgs[3]) then
- write("File exists, overwrite [Y/n]? ")
- local input = read()
- if input:lower():find("n") then
- print("Cancelled!")
- return false
- else
- local f = io.open(tArgs[3],"w")
- f:write(data)
- f:close()
- track(tArgs[3],tArgs[2])
- print("File saved!")
- return true
- end
- else
- local f = io.open(tArgs[3],"w")
- f:write(data)
- f:close()
- track(tArgs[3],tArgs[2])
- print("File saved!")
- return true
- end
- else
- print("Failed to retrive file! [2]")
- return false
- end
- else
- printUsage()
- end
- elseif tArgs[1] == "get" and tArgs[2] and tArgs[3] then
- retrievePaste(tArgs[2], tArgs[3])
- elseif tArgs[1] and tArgs[2] then
- retrievePaste(tArgs[1],tArgs[2])
- elseif tArgs[1] then
- loadDatabase()
- if data[shell.resolve(tArgs[1])] then
- print("Retrieving:\n"..data[shell.resolve(tArgs[1])])
- resp,data = betterGet(data[shell.resolve(tArgs[1])])
- if resp == "success" then
- local f = io.open(tArgs[1],"w")
- f:write(data)
- f:close()
- print("Paste saved!")
- return true
- else
- print("Failed to retrieve paste! [3]")
- end
- else
- print("Could not find origin URL in database!")
- printUsage()
- end
- else
- printUsage()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement