· 4 years ago · Apr 24, 2021, 10:44 AM
1local arg = { ... }
2if #arg < 2 then
3 -- Check to see if there are saved login details
4 if fs.exists("credentials") then
5 local h = fs.open("credentials", "r")
6 local data = h.readAll()
7 h.close()
8 local data = textutils.unserialize(data)
9 api_user_name = data[1]
10 api_user_password = data[2]
11 else
12 term.clear()
13 term.setCursorPos(1,1)
14 error("Login credentials not specified. Usage: update <username> <password> [-s:save credentials locally]", 0)
15 end
16else
17 api_user_name = arg[1]
18 api_user_password = arg[2]
19 if fs.exists("username") ~= true then
20 local h = fs.open("username", "w")
21 h.write(api_user_name)
22 h.close()
23 end
24 if arg[3] == "-s" then
25 local h = fs.open("credentials", "w")
26 h.write(textutils.serialize({api_user_name, api_user_password}))
27 h.close()
28 end
29end
30
31-- You need to put your api_dev_key here
32local api_dev_key = "yourapikeyhere" -- e.g., "cfe49e216beea143e6acf8h5149d93f1"
33
34-- Authenticates with Pastebin.
35-- Example: local agente382 = pastebin.login("AgentE382", "thisisnotmypassword")
36--
37-- Returns user key for use with other API functions.
38function login(api_user_name, api_user_password)
39 assert(type(api_user_name) == "string" and type(api_user_password) == "string", "Both arguments are required and must be strings!")
40 return http.post(
41 "https://pastebin.com/api/api_login.php",
42 "api_dev_key="..api_dev_key.."&"..
43 "api_user_name="..textutils.urlEncode(api_user_name).."&"..
44 "api_user_password="..textutils.urlEncode(api_user_password)
45 ).readAll()
46end
47
48-- Internal function that parses the pastebin XML response.
49local function parse(response)
50 local objs = {}
51 for prefix, objstr in string.gmatch(response, "<(.-)>(.-)</%1>") do
52 local obj = {}
53 for key, value in string.gmatch(objstr, "<"..prefix.."_(.-)>(.-)</"..prefix.."_%1>") do
54 obj[key] = value
55 end
56 objs[#objs+1] = obj
57 end
58 return objs
59end
60
61-- Used to be a custom function, but I realized CC-Lua had one already.
62local url_encode = textutils.urlEncode
63
64-- Fetches a paste from Pastebin.
65-- Example: local rc4 = pastebin.get("Rxe673BJ")
66--
67-- Returns requested paste as a string or an error message.
68function get(api_paste_key)
69 assert(type(api_paste_key) == "string", "Enter a valid paste key as a string!")
70 local response = http.get("https://pastebin.com/raw.php?i="..url_encode(api_paste_key))
71 return response and response.readAll()
72end
73
74-- Lists data about all of a user's pastes. Uses user key from login(). Note that this makes it impossible to get data about random people's pastes.
75-- Example: local allpastes = pastebin.list(agente382, 1000)
76--
77-- Returns a list of data about all pastes associated with a user's account, using the table format described at the end of the document.
78function list(api_user_key, api_results_limit)
79 assert(type(api_user_key) == "string", "Enter a valid user key as a string!")
80 local urlstr = {"api_dev_key=", url_encode(api_dev_key), "&api_user_key=", url_encode(api_user_key), "&api_option=list"}
81 if api_results_limit then if type(api_results_limit) == "number" then if api_results_limit > 1000 then api_results_limit = 1000 elseif api_results_limit < 1 then api_results_limit = 1 end local len = #urlstr urlstr[len+1] = "&api_results_limit=" urlstr[len+2] = url_encode(api_results_limit) else io.stderr:write("Results limit must be a number!\n") end end
82 local response = http.post("https://pastebin.com/api/api_post.php", table.concat(urlstr)).readAll()
83 if string.find(response,"<") then
84 return parse(response)
85 else
86 return response
87 end
88end
89
90-- Table Format:
91-- Pastes:
92-- {
93-- [1] = {
94-- date = "1297953260",
95-- format_short = "javascript",
96-- key = "0b42rwhf",
97-- size = "15",
98-- hits = "15",
99-- format_long = "JavaScript",
100-- url = "https://pastebin.com/0b42rwhf",
101-- title = "javascript test",
102-- private = "0", -- 0:Public - 1:Unlisted - 2:Private
103-- expire_date = "1297956860"
104-- },
105-- [2] = {
106-- date = "1297694343",
107-- format_short = "text",
108-- key = "0C343n0d",
109-- size = "490",
110-- hits = "65",
111-- format_long = "None",
112-- url = "https://pastebin.com/0C343n0d",
113-- title = "Welcome To Pastebin V3",
114-- private = "0", -- 0:Public - 1:Unlisted - 2:Private
115-- expire_date = "0"
116-- }
117-- }
118------------------------
119
120term.clear()
121term.setCursorPos(1,1)
122print("Preparing to update programs...")
123
124user_data = login(api_user_name, api_user_password)
125local allPastes = list(user_data) --"put all pastes in a table"
126local folderName = api_user_name
127if peripheral.isPresent("bottom") and peripheral.getType("bottom") == "drive" then
128 print("Disk drive found, updating to disk.")
129 sleep(1)
130 if fs.exists("/disk/" ..folderName.. "/updates") then --"remove the old updates folder"
131 fs.delete("/disk/" ..folderName.. "/updates")
132 end
133 if fs.exists("/disk/" ..folderName) then --"remove the old folder"
134 fs.delete("/disk/" ..folderName)
135 end
136 fs.makeDir("disk/" ..folderName) --"make a new updates folder"
137 downloadDir = "disk/" ..folderName.. "/"
138else
139 print("Updating computer.")
140 sleep(1)
141 if fs.exists("/" ..folderName.. "/updates") then --"remove the old updates folder"
142 fs.delete("/" ..folderName.. "/updates")
143 end
144 fs.makeDir("/" ..folderName) --"make a new updates folder"
145 downloadDir = folderName.. "/"
146end
147
148for i,v in ipairs(allPastes) do --"for each of my pastes download it and save it (with the name it has on pastebin)"
149 code = http.get("https://pastebin.com/raw.php?i="..allPastes[i]["key"])
150 if code then
151 h = fs.open(downloadDir..allPastes[i]["title"],"w")
152 h.write(code.readAll())
153 h.close()
154 term.setCursorPos(1,4)
155 print("\""..allPastes[i]["title"].."\" successfully downloaded ")
156 else
157 print(allPastes[i]["title"] "failed to download")
158 end
159end
160
161term.setCursorPos(1,4)
162print("Successfully updated "..#allPastes.." programs.")
163if downloadDir == "disk/" ..folderName then
164 fs.delete("/" ..folderName)
165 fs.copy("disk/" ..folderName, folderName)
166 print("Successfully updated computer.")
167end
168if downloadDir == "disk/" ..folderName.. "/" then
169 print("Setting disk startup program.")
170 if fs.exists("/disk/startup") then
171 fs.delete("/disk/startup")
172 end
173 fs.copy("disk/" ..folderName.. "/refresh", "disk/startup")
174 if fs.exists("/disk/username") then
175 fs.delete("/disk/username")
176 end
177 fs.copy("username", "disk/username")
178 print("Done.")
179end