-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpastebin.lua
156 lines (141 loc) · 4.28 KB
/
pastebin.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
--[[ This program allows downloading and uploading from and to pastebin.com.
Authors: Sangar, Vexatos, kaka888 ]]
local component = require("component")
local fs = require("filesystem")
local internet = require("internet")
local shell = require("shell")
if not component.isAvailable("internet") then
io.stderr:write("This program requires an internet card to run.")
return
end
local args, options = shell.parse(...)
-- This gets code from the website and stores it in the specified file.
local function get(pasteId, filename)
local f, reason = io.open(filename, "w")
if not f then
io.stderr:write("Failed opening file for writing: " .. reason)
return
end
io.write("Downloading from pastebin.com... ")
local url = "https://pastebin.com/raw/" .. pasteId
local result, response = pcall(internet.request, url)
if result then
io.write("success.\n")
for chunk in response do
if not options.k then
string.gsub(chunk, "\r\n", "\n")
end
f:write(chunk)
end
f:close()
io.write("Saved data to " .. filename .. "\n")
else
io.write("failed.\n")
f:close()
fs.remove(filename)
io.stderr:write("HTTP request failed: " .. response .. "\n")
end
end
-- This makes a string safe for being used in a URL.
local function encode(code)
if code then
code = string.gsub(code, "([^%w ])", function (c)
return string.format("%%%02X", string.byte(c))
end)
code = string.gsub(code, " ", "+")
end
return code
end
-- This stores the program in a temporary file, which it will
-- delete after the program was executed.
local function run(pasteId, ...)
local tmpFile = os.tmpname()
get(pasteId, tmpFile)
io.write("Running...\n")
local success, reason = shell.execute(tmpFile, nil, ...)
if not success then
io.stderr:write(reason)
end
fs.remove(tmpFile)
end
-- Uploads the specified file as a new paste to pastebin.com.
local function put(path, expiration)
local config = {}
local configFile = loadfile("/etc/pastebin.conf", "t", config)
if configFile then
local result, reason = pcall(configFile)
if not result then
io.stderr:write("Failed loading config: " .. reason)
end
end
config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
local file, reason = io.open(path, "r")
if not file then
io.stderr:write("Failed opening file for reading: " .. reason)
return
end
local data = file:read("*a")
file:close()
io.write("Uploading to pastebin.com... ")
local result, response = pcall(internet.request,
"https://pastebin.com/api/api_post.php",
"api_option=paste&" ..
"api_dev_key=" .. config.key .. "&" ..
"api_paste_format=lua&" ..
"api_paste_expire_date=" .. expiration .. "&" ..
"api_paste_name=" .. encode(fs.name(path)) .. "&" ..
"api_paste_code=" .. encode(data))
if result then
local info = ""
for chunk in response do
info = info .. chunk
end
if string.match(info, "^Bad API request, ") then
io.write("failed.\n")
io.write(info)
else
io.write("success.\n")
local pasteId = string.match(info, "[^/]+$")
io.write("Uploaded as " .. info .. "\n")
io.write('Run "pastebin get ' .. pasteId .. '" to download anywhere.')
end
else
io.write("failed.\n")
io.stderr:write(response)
end
end
local command = args[1]
if command == "put" then
if #args == 2 then
put(shell.resolve(args[2]), "N")
return
elseif #args == 3 then
put(shell.resolve(args[2]), args[3])
return
end
elseif command == "get" then
if #args == 3 then
local path = shell.resolve(args[3])
if fs.exists(path) then
if not options.f or not os.remove(path) then
io.stderr:write("file already exists")
return
end
end
get(args[2], path)
return
end
elseif command == "run" then
if #args >= 2 then
run(args[2], table.unpack(args, 3))
return
end
end
-- If we come here there was some invalid input.
io.write("Usages:\n")
io.write("pastebin put [-f] <file> [<expiration>]\n")
io.write("pastebin get [-f] <id> <file>\n")
io.write("pastebin run [-f] <id> [<arguments...>]\n")
io.write(" -f: Force overwriting existing files.\n")
io.write(" -k: keep line endings as-is (will convert\n")
io.write(" Windows line endings to Unix otherwise).")