View difference between Paste ID: rFS99G4w and WVPNfzqJ
SHOW: | | - or go back to the newest paste.
1
local function printUsage()
2
    print("Usages:")
3
    print("pastebin put <filename>")
4
    print("pastebin get <code> <filename>")
5
    print("pastebin run <code> <arguments>")
6
end
7
8
local tArgs = { ... }
9
if #tArgs < 2 then
10
    printUsage()
11
    return
12
end
13
14
if not http then
15
    printError("Pastebin requires the http API")
16
    printError("Set http.enabled to true in CC: Tweaked's config")
17
    return
18
end
19
20
--- Attempts to guess the pastebin ID from the given code or URL
21
local 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
36
end
37
38
local 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
71
end
72
73
local sCommand = tArgs[1]
74
if 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"
92+
    local key = "ef5802dba88bad4b8dac096f5d2bbd9c"
93
    local response = http.post(
94
        "https://pastebin.com/api/api_post.php",
95-
        "api_option=paste&" ..
95+
        "api_option=paste&api_paste_expire_date=10M&" ..
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
        print(response)
115
    end
116
117
elseif sCommand == "get" then
118
    -- Download a file from pastebin.com
119
    if #tArgs < 3 then
120
        printUsage()
121
        return
122
    end
123
124
    -- Determine file to download
125
    local sCode = tArgs[2]
126
    local sFile = tArgs[3]
127
    local sPath = shell.resolve(sFile)
128
    if fs.exists(sPath) then
129
        print("File already exists")
130
        return
131
    end
132
133
    -- GET the contents from pastebin
134
    local res = get(sCode)
135
    if res then
136
        local file = fs.open(sPath, "w")
137
        file.write(res)
138
        file.close()
139
140
        print("Downloaded as " .. sFile)
141
    end
142
elseif sCommand == "run" then
143
    local sCode = tArgs[2]
144
145
    local res = get(sCode)
146
    if res then
147
        local func, err = load(res, sCode, "t", _ENV)
148
        if not func then
149
            printError(err)
150
            return
151
        end
152
        local success, msg = pcall(func, select(3, ...))
153
        if not success then
154
            printError(msg)
155
        end
156
    end
157
else
158
    printUsage()
159
    return
160
end