· 4 years ago · Apr 01, 2021, 08:04 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>")
9    print(programName .. " uget <name> </filename>")
10end
11
12local tArgs = { ... }
13if #tArgs < 1 then
14    printUsage()
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 function login(devkey)
78    user=""
79    pass=""
80    io.stdout:write("Enter username: ")
81    user = read()
82    io.stdout:write("Enter password: ")
83    pass = read()
84    --read user details through keyboard
85    file=fs.open("userkey.txt","w")
86    page, err=http.post(
87    "https://pastebin.com/api/api_login.php",
88    "api_dev_key="..devkey.."&"..
89    "api_user_name="..user.."&"..
90    "api_user_password="..pass)
91    --#queries site for key using devkey, name and pass
92    --print(page)
93    if err then
94        printError(err)
95    end
96    file.write(page.readAll())
97    file.close()
98end
99local function list(devkey,searterm,flg)
100    fUser=fs.open("userkey.txt","r")
101    ukey = fUser.readLine()
102    fUser.close()
103    page, err=http.post(
104    "https://pastebin.com/api/api_post.php",
105    "api_dev_key="..devkey.."&"..
106    "api_user_key="..ukey.."&"..
107    "api_option=list&"..
108    "api_results_limit=1000")
109    --print(page)
110    if err then
111        printError(err)
112    end
113    fPage = fs.open("pbList.tmp","w")
114    fPage.write(page.readAll())
115    fPage.close()
116    if flg==0 then
117        fPage = fs.open("pbList.txt","w")
118    else
119        fPage = fs.open("pbUGet.tmp","w")
120    end
121    sauce="pbList.tmp"
122    --if #tArgs == 2 then
123    if searterm ~= "" then
124        fFilter=fs.open("pbFilter.tmp","w")
125        --searterm = tArgs[2]
126        tmpaste = ""
127        for line in io.lines(sauce) do
128            if string.find(line,"<paste_key>") then
129                tmpaste = line
130            end
131            if string.find(line,"<paste_title>") and string.find(line,searterm) then
132                fFilter.writeLine(tmpaste)
133                fFilter.writeLine(line)
134            end
135        end
136        fFilter.close()
137        sauce="pbFilter.tmp"
138    end
139    for line in io.lines(sauce) do
140        if string.find(line,"<paste_key>") then
141            line=string.gsub(line,"<paste_key>","")
142            line=string.gsub(line,"</paste_key>","")
143            if flg==0 then
144                io.stdout:write(line.." ")
145            end
146            fPage.write(line.." ")
147        end
148        if string.find(line,"<paste_title>") then
149            line=string.gsub(line,"<paste_title>","")
150            line=string.gsub(line,"</paste_title>","")
151            if flg==0 then
152                printError(line)
153            end
154            fPage.writeLine(line)
155        end
156    end
157    --if #tArgs == 2 then
158    if searterm~="" then
159        fs.delete("pbFilter.tmp")
160    end
161    fs.delete("pbList.tmp")
162    fPage.close()
163end
164key="eUXuCLWHr7T5d9FQmnqj5hMEQ0hyQCoN"
165local sCommand = tArgs[1]
166if sCommand == "put" then
167    -- Upload a file to pastebin.com
168    -- Determine file to upload
169    local sFile = tArgs[2]
170    local sPath = shell.resolve(sFile)
171    if not fs.exists(sPath) or fs.isDir(sPath) then
172        print("No such file")
173        return
174    end
175
176    -- Read in the file
177    local sName = fs.getName(sPath)
178    local file = fs.open(sPath, "r")
179    local sText = file.readAll()
180    local ukey = ""
181    file.close()
182    --Read user key
183    if fs.exists("userkey.txt") then
184        file = fs.open("userkey.txt","r")
185        ukey=file.readLine()
186        --print(ukey)
187        file.close()
188    end
189    -- POST the contents to pastebin
190    write("Connecting to pastebin.com... ")
191    local response = http.post(
192        "https://pastebin.com/api/api_post.php",
193        "api_option=paste&" ..
194        "api_dev_key=" .. key .. "&" ..
195        "api_user_key="..ukey.."&"..
196        "api_paste_format=lua&" ..
197        "api_paste_name=" .. textutils.urlEncode(sName) .. "&" ..
198        "api_paste_code=" .. textutils.urlEncode(sText)
199    )
200
201    if response then
202        print("Success.")
203
204        local sResponse = response.readAll()
205        response.close()
206
207        local sCode = string.match(sResponse, "[^/]+$")
208        print("Uploaded as " .. sResponse)
209        print("Run \"pastebin get " .. sCode .. "\" to download anywhere")
210
211    else
212        print("Failed.")
213    end
214
215elseif sCommand == "get" then
216    -- Download a file from pastebin.com
217    if #tArgs < 3 then
218        printUsage()
219        return
220    end
221
222    -- Determine file to download
223    local sCode = tArgs[2]
224    local sFile = tArgs[3]
225    local sPath = shell.resolve(sFile)
226    if fs.exists(sPath) then
227        print("File already exists")
228        return
229    end
230
231    -- GET the contents from pastebin
232    local res = get(sCode)
233    if res then
234        local file = fs.open(sPath, "w")
235        file.write(res)
236        file.close()
237
238        print("Downloaded as " .. sFile)
239    end
240elseif sCommand == "run" then
241    local sCode = tArgs[2]
242
243    local res = get(sCode)
244    if res then
245        local func, err = load(res, sCode, "t", _ENV)
246        if not func then
247            printError(err)
248            return
249        end
250        local success, msg = pcall(func, select(3, ...))
251        if not success then
252            printError(msg)
253        end
254    end
255elseif sCommand == "login" then
256    login(key)
257elseif sCommand == "list" then
258    if #tArgs == 2 then
259        list(key,tArgs[2])
260    elseif #tArgs == 1 then
261        list(key,"",0)
262    end
263elseif sCommand == "uget" then
264    if #tArgs < 2 then
265        printUsage()
266        return
267    end
268    list(key,tArgs[2])
269    i=0
270    for line in io.lines("pbUGet.tmp") do
271        i=i+1
272    end
273    if i==0 then
274        printError("result not found")
275    elseif i>1 then
276        printError("not specific enough")
277    else
278        prg= arg[0] or fs.getName(shell.getRunningProgram())
279        code, name = ""
280        for line in io.lines("pbUGet.tmp") do
281            code, name = string.match(line,"(%w+)(.+)")
282        end
283        if #tArgs <3 then
284        shell.run(prg.." get "..code.." "..name)
285        else
286        shell.run(prg.." get "..code.." "..tArgs[3])
287        end
288    end
289else
290    printUsage()
291    return
292end