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