Advertisement
2222Jonathan

CCTLuaUpdater

Dec 6th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. local SELF_CODE = "9dv8uUg7"
  2. local SELF_NAME = "Update.lua"
  3. local VERSION = "1.1"
  4. local tmpFile = "tmp.update"
  5.  
  6. -- Check if two strings are different or not.
  7. function diff(s1, s2)
  8. local l1 = #s1
  9. local l2 = #s2
  10. if l1 ~= l2 then return true end
  11.  
  12. for i=1,l1 do
  13. if s1[i] ~= s2[i] then
  14. return true
  15. end
  16. end
  17. return false
  18. end
  19.  
  20. function printUsage()
  21. textutils.pagedPrint("Version:"..VERSION)
  22. textutils.pagedPrint("Usage: Update self")
  23. textutils.pagedPrint(" Update the program.")
  24. textutils.pagedPrint(" (The program need to be named Update.lua!)")
  25.  
  26. textutils.pagedPrint("Usage: Update run")
  27. textutils.pagedPrint(" Update all marked files.")
  28.  
  29. textutils.pagedPrint("Usage: Update remove <path>")
  30. textutils.pagedPrint(" Unmark the specified file.")
  31.  
  32. textutils.pagedPrint("Usage: Update add <paste> <path>")
  33. textutils.pagedPrint(" Mark the specified file.")
  34.  
  35. textutils.pagedPrint("Usage: Update move <path> <new path>")
  36. textutils.pagedPrint(" Move the specified file.")
  37.  
  38. textutils.pagedPrint("Usage: Update <paste> <path>")
  39. textutils.pagedPrint(" Update a specified file, fetch data from pastebin.")
  40. error()
  41. end
  42.  
  43. local tArgs = { ... }
  44. if #tArgs < 1 or #tArgs > 3 then
  45. printUsage()
  46. end
  47.  
  48. --- Attempts to guess the pastebin ID from the given code or URL
  49. local function extractId(paste)
  50. local patterns = {
  51. "^([%a%d]+)$",
  52. "^https?://pastebin.com/([%a%d]+)$",
  53. "^https?://pastebin.com/edit/([%a%d]+)$",
  54. "^pastebin.com/([%a%d]+)$",
  55. "^pastebin.com/edit/([%a%d]+)$",
  56. "^https?://pastebin.com/raw/([%a%d]+)$",
  57. "^pastebin.com/raw/([%a%d]+)$",
  58. }
  59.  
  60. for i = 1, #patterns do
  61. local code = paste:match( patterns[i] )
  62. if code then return code end
  63. end
  64.  
  65. return nil
  66. end
  67.  
  68. function isPaste(url)
  69. if not url then return false end
  70. local paste = extractId( url )
  71. if not paste then
  72. io.stderr:write( "Invalid pastebin code.\n" )
  73. io.write( "The code is the ID at the end of the pastebin.com URL.\n" )
  74. return false
  75. end
  76. local cacheBuster = ("%x"):format(math.random(0, 2^30))
  77. local response, err = http.get(
  78. "https://pastebin.com/raw/"..textutils.urlEncode( paste ).."?cb="..cacheBuster
  79. )
  80. if response then
  81. -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
  82. local headers = response.getResponseHeaders()
  83. if not headers["Content-Type"] or not headers["Content-Type"]:find( "^text/plain" ) then
  84. io.stderr:write( "Failed.\n" )
  85. textutils.pagedPrint( "Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode( paste ) )
  86. return false
  87. end
  88. return true
  89. else
  90. return false
  91. end
  92. end
  93.  
  94. function checkPath(path, shouldAskToReplace, shouldCreate)
  95. local hasPath = fs.exists(path)
  96. if not hasPath then
  97. -- Create directories if they does not exist.
  98. local hasDirs = string.find(path, "/")
  99. if hasDirs ~= nil then
  100. if not shouldCreate then
  101. textutils.pagedPrint("Error: File was not found!")
  102. error()
  103. end
  104. local dirs = string.match(path, "^(.+/)")
  105. fs.makeDir(dirs)
  106. print("Created directory:" .. dirs)
  107. end
  108.  
  109. -- Check if the file exists
  110. hasPath = fs.exists(path)
  111. if not hasPath then
  112. if not shouldCreate then
  113. print("Error: File was not found!")
  114. error()
  115. end
  116. -- Create the file
  117. f = fs.open(path, "w")
  118. f.close()
  119. print("Created file:" .. path)
  120. end
  121. else
  122. if not shouldAskToReplace then
  123. return
  124. end
  125.  
  126. print("File already exists, do you want to replace it? [Y/N]")
  127. local input = read()
  128. if input == "Y" or input == "y" then
  129. return
  130. else
  131. print("Abort command!")
  132. error()
  133. end
  134. end
  135. end
  136.  
  137. function checkPastePath(paste, path, shouldAskToReplace, shouldCreate)
  138. local hasPaste = isPaste(paste)
  139. if not hasPaste then
  140. textutils.pagedPrint("That was not a valid paste!")
  141. printUsage()
  142. end
  143. checkPath(path, shouldAskToReplace, shouldCreate)
  144. end
  145.  
  146. function runPaste(paste, path)
  147. checkPastePath(paste, path, false, true)
  148. print("----" .. paste .. path .. "----")
  149.  
  150. local f = fs.open(path, "r")
  151. local s1 = f.readAll()
  152. f.close()
  153.  
  154. fs.delete(path)
  155. shell.run("pastebin", "get", paste, path)
  156.  
  157. f = fs.open(path, "r")
  158. local s2 = f.readAll()
  159. f.close()
  160.  
  161. if diff(s1, s2) then
  162. print(path, "was updated!")
  163. else
  164. print(path, "was not changed since last update!")
  165. end
  166. end
  167.  
  168. -- Run command
  169. if tArgs[1] == "self" then
  170. shell.run(SELF_NAME, SELF_CODE, SELF_NAME)
  171. return
  172. end
  173.  
  174. -- Run command
  175. if tArgs[1] == "run" then
  176. local file = fs.open(tmpFile, fs.exists(tmpFile) and "r")
  177. if not file then
  178. textutils.pagedPrint("Error: No entries as been added!")
  179. printUsage()
  180. end
  181. local list = {}
  182. local line = nil
  183. repeat
  184. line = file.readLine()
  185. local ps = line
  186. line = file.readLine()
  187. local pt = line
  188. -- Add entry
  189. table.insert(list, #list+1, {paste=ps, path=pt})
  190. until not line
  191. file.close()
  192.  
  193. for i=1, #list-1 do
  194. local entry = list[i]
  195. runPaste(entry.paste, entry.path)
  196. end
  197. return
  198. end
  199.  
  200. -- Add command
  201. if tArgs[1] == "add" then
  202. local paste = tArgs[2]
  203. local path = tArgs[3]
  204. checkPastePath(paste, path, true, true)
  205. paste = extractId(paste)
  206. local file = fs.open(tmpFile, fs.exists(tmpFile) and "a" or "w")
  207. file.writeLine(paste)
  208. file.writeLine(path)
  209. file.close()
  210. return
  211. end
  212.  
  213. -- Remove command
  214. if tArgs[1] == "remove" then
  215. local path = tArgs[2]
  216. checkPath(path, false, false)
  217. local file = fs.open(tmpFile, fs.exists(tmpFile) and "r")
  218. if not file then error() end
  219.  
  220. -- Open the file and fetch all but the given entry.
  221. local list = {}
  222. local line = nil
  223. repeat
  224. line = file.readLine()
  225. -- If eof, break
  226. if line == nil then break end
  227.  
  228. line2 = line
  229. line = file.readLine()
  230. -- If eof, break
  231. if line == nil then break end
  232. -- If this is not the entry we are searching for, add it to the list, else ignore it.
  233. if line ~= path then
  234. -- Add current paste
  235. table.insert(list, #list+1, line2)
  236. -- Add corresponding path
  237. table.insert(list, #list+1, line)
  238. end
  239. until not line
  240. file.close()
  241.  
  242. -- Create the file again from scratch and add the entries.
  243. local file = fs.open(tmpFile, fs.exists(tmpFile) and "w")
  244. for i=1, #list do
  245. file.write(list[i])
  246. file.write("\n")
  247. end
  248. file.close()
  249. return
  250. end
  251.  
  252. -- move command
  253. if tArgs[1] == "move" then
  254. local from = tArgs[2]
  255. local to = tArgs[3]
  256.  
  257. local file = fs.open(tmpFile, fs.exists(tmpFile) and "r")
  258. if not file then error() end
  259. local code = nil
  260. local line = nil
  261. repeat
  262. line = file.readLine()
  263. -- If eof, break
  264. if line == nil then break end
  265.  
  266. paste = line
  267. line = file.readLine()
  268. -- If eof, break
  269. if line == nil then break end
  270. -- If this is not the entry we are searching for, set its paste code, else ignore it.
  271. if line ~= from then
  272. -- Set paste code.
  273. code = paste
  274. break
  275. end
  276. until not line
  277. file.close()
  278.  
  279. if code == nil then
  280. print("Error:", from, "could not be found!")
  281. return
  282. end
  283.  
  284. shell.run(SELF_NAME, "remove", from)
  285. shell.run(SELF_NAME, "add", code, to)
  286. fs.delete(from)
  287. return
  288. end
  289.  
  290. -- version command
  291. if tArgs[1] == "version" or tArgs[1] == "v" then
  292. print("Version:", VERSION)
  293. return
  294. end
  295.  
  296. if #tArgs ~= 2 then
  297. printUsage()
  298. end
  299.  
  300. -- Normal command
  301. local paste = tArgs[1]
  302. local path = tArgs[2]
  303. runPaste(paste, path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement