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