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