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