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