Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = { ... }
- function printUsage()
- print(" Usage: Update run")
- print(" Usage: Update remove <path>")
- print(" Usage: Update add <paste> <path>")
- print(" Usage: Update <paste> <path>")
- error()
- end
- if #tArgs < 1 or #tArgs > 3 then
- printUsage()
- end
- --- Attempts to guess the pastebin ID from the given code or URL
- local function extractId(paste)
- local patterns = {
- "^([%a%d]+)$",
- "^https?://pastebin.com/([%a%d]+)$",
- "^https?://pastebin.com/edit/([%a%d]+)$",
- "^pastebin.com/([%a%d]+)$",
- "^pastebin.com/edit/([%a%d]+)$",
- "^https?://pastebin.com/raw/([%a%d]+)$",
- "^pastebin.com/raw/([%a%d]+)$",
- }
- for i = 1, #patterns do
- local code = paste:match( patterns[i] )
- if code then return code end
- end
- return nil
- end
- function isPaste(url)
- if not url then return false end
- local paste = extractId( url )
- if not paste then
- io.stderr:write( "Invalid pastebin code.\n" )
- io.write( "The code is the ID at the end of the pastebin.com URL.\n" )
- return false
- end
- local cacheBuster = ("%x"):format(math.random(0, 2^30))
- local response, err = http.get(
- "https://pastebin.com/raw/"..textutils.urlEncode( paste ).."?cb="..cacheBuster
- )
- if response then
- -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
- local headers = response.getResponseHeaders()
- if not headers["Content-Type"] or not headers["Content-Type"]:find( "^text/plain" ) then
- io.stderr:write( "Failed.\n" )
- print( "Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode( paste ) )
- return false
- end
- return true
- else
- return false
- end
- end
- function checkPath(path, shouldAskToReplace, shouldCreate)
- local hasPath = fs.exists(path)
- if not hasPath then
- -- Create directories if they does not exist.
- local hasDirs = string.find(path, "/")
- if hasDirs ~= nil then
- if not shouldCreate then
- print("Error: File was not found!")
- error()
- end
- local dirs = string.match(path, "^(.+/)")
- fs.makeDir(dirs)
- print("Created directory:", dirs)
- end
- -- Check if the file exists
- hasPath = fs.exists(path)
- if not hasPath then
- if not shouldCreate then
- print("Error: File was not found!")
- error()
- end
- -- Create the file
- f = fs.open(path, "w")
- f.close()
- print("Created file:", path)
- end
- else
- if not shouldAskToReplace then
- return
- end
- print("File already exists, do you want to replace it? [Y/N]")
- local input = read()
- if input == "Y" or input == "y" then
- return
- else
- print("Abort command!")
- error()
- end
- end
- end
- function checkPastePath(paste, path, shouldAskToReplace, shouldCreate)
- local hasPaste = isPaste(paste)
- if not hasPaste then
- print("That was not a valid paste!")
- printUsage()
- end
- checkPath(path, shouldAskToReplace, shouldCreate)
- end
- function runPaste(paste, path)
- checkPastePath(paste, path, false, true)
- print("----", paste, path, "----")
- fs.delete(path)
- shell.run("pastebin", "get", paste, path)
- end
- local tmpFile = "tmpUPDATE.lua"
- -- Run command
- if tArgs[1] == "run" then
- local file = fs.open(tmpFile, fs.exists(tmpFile) and "r")
- if not file then
- print("Error: No entries as been added!")
- printUsage()
- end
- local list = {}
- local line = nil
- repeat
- line = file.readLine()
- local ps = line
- line = file.readLine()
- local pt = line
- -- Add entry
- table.insert(list, #list+1, {paste=ps, path=pt})
- until not line
- file.close()
- for i=1, #list-1 do
- local entry = list[i]
- runPaste(entry.paste, entry.path)
- end
- return
- end
- -- Add command
- if tArgs[1] == "add" then
- local paste = tArgs[2]
- local path = tArgs[3]
- checkPastePath(paste, path, true, true)
- paste = extractId(paste)
- local file = fs.open(tmpFile, fs.exists(tmpFile) and "a" or "w")
- file.writeLine(paste)
- file.writeLine(path)
- file.close()
- return
- end
- -- Remove command
- if tArgs[1] == "remove" then
- local path = tArgs[2]
- checkPath(path, false, false)
- local file = fs.open(tmpFile, fs.exists(tmpFile) and "r")
- if not file then error() end
- -- Open the file and fetch all but the given entry.
- local list = {}
- local line = nil
- repeat
- line = file.readLine()
- -- If eof, break
- if line == nil then break end
- line2 = line
- line = file.readLine()
- -- If eof, break
- if line == nil then break end
- -- If this is not the entry we are searching for, add it to the list, else ignore it.
- if line ~= path then
- -- Add current paste
- table.insert(list, #list+1, line2)
- -- Add corresponding path
- table.insert(list, #list+1, line)
- end
- until not line
- file.close()
- -- Create the file again from scratch and add the entries.
- local file = fs.open(tmpFile, fs.exists(tmpFile) and "w")
- for i=1, #list do
- file.write(list[i])
- file.write("\n")
- end
- file.close()
- return
- end
- if #tArgs ~= 2 then
- printUsage()
- end
- -- Normal command
- local paste = tArgs[1]
- local path = tArgs[2]
- runPaste(paste, path)
Add Comment
Please, Sign In to add comment