· 4 years ago · May 25, 2021, 12:28 AM
1--This program was made only to be used by other programs of mine, not to be used on its own
2local function printUsage()
3 local programName = "pastebin"
4 print("Usages:")
5 print(programName .. " put <filename>")
6 print(programName .. " get <code> <filename>")
7 print(programName .. " run <code> <arguments>")
8end
9
10local tArgs = { ... }
11if #tArgs < 2 then
12 printUsage()
13 return
14end
15
16if not http then
17 printError("Pastebin requires the http API")
18 printError("Set http.enabled to true in CC: Tweaked's config")
19 return
20end
21
22--- Attempts to guess the pastebin ID from the given code or URL
23local function extractId(paste)
24 local patterns = {
25 "^([%a%d]+)$",
26 "^https?://pastebin.com/([%a%d]+)$",
27 "^pastebin.com/([%a%d]+)$",
28 "^https?://pastebin.com/raw/([%a%d]+)$",
29 "^pastebin.com/raw/([%a%d]+)$",
30 }
31
32 for i = 1, #patterns do
33 local code = paste:match(patterns[i])
34 if code then return code end
35 end
36
37 return nil
38end
39
40local function get(url)
41 local paste = extractId(url)
42 if not paste then
43 io.stderr:write("Invalid pastebin code.\n")
44 io.write("The code is the ID at the end of the pastebin.com URL.\n")
45 return
46 end
47
48 write("Connecting to pastebin.com... ")
49 -- Add a cache buster so that spam protection is re-checked
50 local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
51 local response, err = http.get(
52 "https://pastebin.com/raw/" .. textutils.urlEncode(paste) .. "?cb=" .. cacheBuster
53 )
54
55 if response then
56 -- If spam protection is activated, we get redirected to /paste with Content-Type: text/html
57 local headers = response.getResponseHeaders()
58 if not headers["Content-Type"] or not headers["Content-Type"]:find("^text/plain") then
59 io.stderr:write("Failed.\n")
60 print("Pastebin blocked the download due to spam protection. Please complete the captcha in a web browser: https://pastebin.com/" .. textutils.urlEncode(paste))
61 return
62 end
63
64 print("Success.")
65
66 local sResponse = response.readAll()
67 response.close()
68 return sResponse
69 else
70 io.stderr:write("Failed.\n")
71 print(err)
72 end
73end
74
75local sCommand = tArgs[1]
76if sCommand == "put" then
77 -- Upload a file to pastebin.com
78 -- Determine file to upload
79 local sFile = tArgs[2]
80 local sPath = shell.resolve(sFile)
81 if not fs.exists(sPath) or fs.isDir(sPath) then
82 print("No such file")
83 return
84 end
85
86 -- Read in the file
87 local sName = fs.getName(sPath)
88 local file = fs.open(sPath, "r")
89 local sText = file.readAll()
90 file.close()
91
92 -- POST the contents to pastebin
93 write("Connecting to pastebin.com... ")
94 local key = "0ec2eb25b6166c0c27a394ae118ad829"
95 local response = http.post(
96 "https://pastebin.com/api/api_post.php",
97 "api_option=paste&" ..
98 "api_dev_key=" .. key .. "&" ..
99 "api_paste_format=lua&" ..
100 "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
101 "api_paste_code=" .. textutils.urlEncode(sText)
102 )
103
104 if response then
105 print("Success.")
106
107 local sResponse = response.readAll()
108 response.close()
109
110 local sCode = string.match(sResponse, "[^/]+$")
111 print("Uploaded as " .. sResponse)
112 print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
113
114 else
115 print("Failed.")
116 end
117
118elseif sCommand == "get" then
119 -- Download a file from pastebin.com
120 if #tArgs < 3 then
121 printUsage()
122 return
123 end
124
125 -- Determine file to download
126 local sCode = tArgs[2]
127 local sFile = tArgs[3]
128 local sPath = sFile
129 if fs.exists(sPath) then
130 print("File already exists")
131 return
132 end
133
134 -- GET the contents from pastebin
135 local res = get(sCode)
136 if res then
137 local file = fs.open(sPath, "w")
138 file.write(res)
139 file.close()
140
141 print("Downloaded as " .. sFile)
142 end
143elseif sCommand == "run" then
144 local sCode = tArgs[2]
145
146 local res = get(sCode)
147 if res then
148 local func, err = load(res, sCode, "t", _ENV)
149 if not func then
150 printError(err)
151 return
152 end
153 local success, msg = pcall(func, select(3, ...))
154 if not success then
155 printError(msg)
156 end
157 end
158else
159 printUsage()
160 return
161end