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