2222Jonathan

Untitled

Dec 6th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. function printUsage()
  4. print(" Usage: Update run")
  5. print(" Usage: Update remove <path>")
  6. print(" Usage: Update add <paste> <path>")
  7. print(" Usage: Update <paste> <path>")
  8. error()
  9. end
  10.  
  11. if #tArgs < 1 or #tArgs > 3 then
  12. printUsage()
  13. end
  14.  
  15. --- Attempts to guess the pastebin ID from the given code or URL
  16. local function extractId(paste)
  17. local patterns = {
  18. "^([%a%d]+)$",
  19. "^https?://pastebin.com/([%a%d]+)$",
  20. "^https?://pastebin.com/edit/([%a%d]+)$",
  21. "^pastebin.com/([%a%d]+)$",
  22. "^pastebin.com/edit/([%a%d]+)$",
  23. "^https?://pastebin.com/raw/([%a%d]+)$",
  24. "^pastebin.com/raw/([%a%d]+)$",
  25. }
  26.  
  27. for i = 1, #patterns do
  28. local code = paste:match( patterns[i] )
  29. if code then return code end
  30. end
  31.  
  32. return nil
  33. end
  34.  
  35. function isPaste(url)
  36. if not url then return false end
  37. local paste = extractId( url )
  38. if not paste then
  39. io.stderr:write( "Invalid pastebin code.\n" )
  40. io.write( "The code is the ID at the end of the pastebin.com URL.\n" )
  41. return false
  42. end
  43. local cacheBuster = ("%x"):format(math.random(0, 2^30))
  44. local response, err = http.get(
  45. "https://pastebin.com/raw/"..textutils.urlEncode( paste ).."?cb="..cacheBuster
  46. )
  47. if response then
  48. -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
  49. local headers = response.getResponseHeaders()
  50. if not headers["Content-Type"] or not headers["Content-Type"]:find( "^text/plain" ) then
  51. io.stderr:write( "Failed.\n" )
  52. print( "Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode( paste ) )
  53. return false
  54. end
  55. return true
  56. else
  57. return false
  58. end
  59. end
  60.  
  61. function checkPath(path, shouldAskToReplace, shouldCreate)
  62. local hasPath = fs.exists(path)
  63. if not hasPath then
  64. -- Create directories if they does not exist.
  65. local hasDirs = string.find(path, "/")
  66. if hasDirs ~= nil then
  67. if not shouldCreate then
  68. print("Error: File was not found!")
  69. error()
  70. end
  71. local dirs = string.match(path, "^(.+/)")
  72. fs.makeDir(dirs)
  73. print("Created directory:", dirs)
  74. end
  75.  
  76. -- Check if the file exists
  77. hasPath = fs.exists(path)
  78. if not hasPath then
  79. if not shouldCreate then
  80. print("Error: File was not found!")
  81. error()
  82. end
  83. -- Create the file
  84. f = fs.open(path, "w")
  85. f.close()
  86. print("Created file:", path)
  87. end
  88. else
  89. if not shouldAskToReplace then
  90. return
  91. end
  92.  
  93. print("File already exists, do you want to replace it? [Y/N]")
  94. local input = read()
  95. if input == "Y" or input == "y" then
  96. return
  97. else
  98. print("Abort command!")
  99. error()
  100. end
  101. end
  102. end
  103.  
  104. function checkPastePath(paste, path, shouldAskToReplace, shouldCreate)
  105. local hasPaste = isPaste(paste)
  106. if not hasPaste then
  107. print("That was not a valid paste!")
  108. printUsage()
  109. end
  110. checkPath(path, shouldAskToReplace, shouldCreate)
  111. end
  112.  
  113. function runPaste(paste, path)
  114. checkPastePath(paste, path, false, true)
  115. print("----", paste, path, "----")
  116. fs.delete(path)
  117. shell.run("pastebin", "get", paste, path)
  118. end
  119.  
  120. local tmpFile = "tmpUPDATE.lua"
  121. -- Run command
  122. if tArgs[1] == "run" then
  123. local file = fs.open(tmpFile, fs.exists(tmpFile) and "r")
  124. if not file then
  125. print("Error: No entries as been added!")
  126. printUsage()
  127. end
  128. local list = {}
  129. local line = nil
  130. repeat
  131. line = file.readLine()
  132. local ps = line
  133. line = file.readLine()
  134. local pt = line
  135. -- Add entry
  136. table.insert(list, #list+1, {paste=ps, path=pt})
  137. until not line
  138. file.close()
  139.  
  140. for i=1, #list-1 do
  141. local entry = list[i]
  142. runPaste(entry.paste, entry.path)
  143. end
  144. return
  145. end
  146.  
  147. -- Add command
  148. if tArgs[1] == "add" then
  149. local paste = tArgs[2]
  150. local path = tArgs[3]
  151. checkPastePath(paste, path, true, true)
  152. paste = extractId(paste)
  153. local file = fs.open(tmpFile, fs.exists(tmpFile) and "a" or "w")
  154. file.writeLine(paste)
  155. file.writeLine(path)
  156. file.close()
  157. return
  158. end
  159.  
  160. -- Remove command
  161. if tArgs[1] == "remove" then
  162. local path = tArgs[2]
  163. checkPath(path, false, false)
  164. local file = fs.open(tmpFile, fs.exists(tmpFile) and "r")
  165. if not file then error() end
  166.  
  167. -- Open the file and fetch all but the given entry.
  168. local list = {}
  169. local line = nil
  170. repeat
  171. line = file.readLine()
  172. -- If eof, break
  173. if line == nil then break end
  174.  
  175. line2 = line
  176. line = file.readLine()
  177. -- If eof, break
  178. if line == nil then break end
  179. -- If this is not the entry we are searching for, add it to the list, else ignore it.
  180. if line ~= path then
  181. -- Add current paste
  182. table.insert(list, #list+1, line2)
  183. -- Add corresponding path
  184. table.insert(list, #list+1, line)
  185. end
  186. until not line
  187. file.close()
  188.  
  189. -- Create the file again from scratch and add the entries.
  190. local file = fs.open(tmpFile, fs.exists(tmpFile) and "w")
  191. for i=1, #list do
  192. file.write(list[i])
  193. file.write("\n")
  194. end
  195. file.close()
  196. return
  197. end
  198.  
  199. if #tArgs ~= 2 then
  200. printUsage()
  201. end
  202.  
  203. -- Normal command
  204. local paste = tArgs[1]
  205. local path = tArgs[2]
  206. runPaste(paste, path)
Add Comment
Please, Sign In to add comment