View difference between Paste ID: FVRn1VSf and gtrKRUSQ
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 = "http://pastebin.com/raw.php?i=" .. pasteId
24+
  local url = "http://pastebin.com/raw.php?i=" .. 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-
function encode(code)
46+
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-
function run(pasteId, ...)
58+
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-
function put(path)
71+
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+
  config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
81-
  local file, reason = io.open(path, "r")
81+
  local file, reason = io.open(path, "r")
82-
82+
83-
  if not file then
83+
  if not file then
84-
    io.stderr:write("Failed opening file for reading: " .. reason)
84+
    io.stderr:write("Failed opening file for reading: " .. reason)
85-
    return
85+
    return
86-
  end
86+
  end
87-
87+
88-
  local data = file:read("*a")
88+
  local data = file:read("*a")
89-
  file:close()
89+
  file:close()
90-
90+
91-
  io.write("Uploading to pastebin.com... ")
91+
  io.write("Uploading to pastebin.com... ")
92-
  local result, response = pcall(internet.request,
92+
  local result, response = pcall(internet.request,
93-
        "http://pastebin.com/api/api_post.php", 
93+
        "http://pastebin.com/api/api_post.php", 
94-
        "api_option=paste&" ..
94+
        "api_option=paste&" ..
95-
        "api_dev_key=" .. config.key .. "&" ..
95+
        "api_dev_key=" .. config.key .. "&" ..
96-
        "api_paste_format=lua&" ..
96+
        "api_paste_format=lua&" ..
97-
        "api_paste_expire_date=N&" ..
97+
        "api_paste_expire_date=N&" ..
98-
        "api_paste_name=" .. encode(fs.name(path)) .. "&" ..
98+
        "api_paste_name=" .. encode(fs.name(path)) .. "&" ..
99-
        "api_paste_code=" .. encode(data))
99+
        "api_paste_code=" .. encode(data))
100-
100+
101-
  if result then
101+
  if result then
102-
    local info = ""
102+
    local info = ""
103-
    for chunk in response do
103+
    for chunk in response do
104-
      info = info .. chunk
104+
      info = info .. chunk
105-
    end
105+
    end
106-
    if string.match(info, "^Bad API request, ") then
106+
    if string.match(info, "^Bad API request, ") then
107-
      io.write("failed.\n")
107+
      io.write("failed.\n")
108-
      io.write(info)
108+
      io.write(info)
109-
    else
109+
    else
110-
      io.write("success.\n")
110+
      io.write("success.\n")
111-
      local pasteId = string.match(info, "[^/]+$")
111+
      local pasteId = string.match(info, "[^/]+$")
112-
      io.write("Uploaded as " .. info .. "\n")
112+
      io.write("Uploaded as " .. info .. "\n")
113-
      io.write('Run "pastebin get ' .. pasteId .. '" to download anywhere.')
113+
      io.write('Run "pastebin get ' .. pasteId .. '" to download anywhere.')
114-
    end
114+
    end
115-
  else
115+
  else
116-
    io.write("failed.\n")
116+
    io.write("failed.\n")
117-
    io.stderr:write(response)
117+
    io.stderr:write(response)
118-
  end
118+
  end
119-
end
119+
end
120-
120+
121-
local command = args[1]
121+
local command = args[1]
122-
if command == "put" then
122+
if command == "put" then
123-
  if #args == 2 then
123+
  if #args == 2 then
124-
    put(shell.resolve(args[2]))
124+
    put(shell.resolve(args[2]))
125-
    return
125+
    return
126-
  end
126+
  end
127-
elseif command == "get" then
127+
elseif command == "get" then
128-
  if #args == 3 then
128+
  if #args == 3 then
129-
    local path = shell.resolve(args[3])
129+
    local path = shell.resolve(args[3])
130-
    if fs.exists(path) then
130+
    if fs.exists(path) then
131-
      if not options.f or not os.remove(path) then
131+
      if not options.f or not os.remove(path) then
132-
        io.stderr:write("file already exists")
132+
        io.stderr:write("file already exists")
133-
        return
133+
        return
134-
      end
134+
      end
135-
    end
135+
    end
136-
    get(args[2], path)
136+
    get(args[2], path)
137-
    return
137+
    return
138-
  end
138+
  end
139-
elseif command == "run" then
139+
elseif command == "run" then
140-
  if #args >= 2 then
140+
  if #args >= 2 then
141-
    run(args[2], table.unpack(args, 3))
141+
    run(args[2], table.unpack(args, 3))
142-
    return
142+
    return
143-
  end
143+
  end
144-
end
144+
end
145-
145+
146-
-- If we come here there was some invalid input.
146+
-- If we come here there was some invalid input.
147-
io.write("Usages:\n")
147+
io.write("Usages:\n")
148-
io.write("pastebin put [-f] <file>\n")
148+
io.write("pastebin put [-f] <file>\n")
149-
io.write("pastebin get [-f] <id> <file>\n")
149+
io.write("pastebin get [-f] <id> <file>\n")
150-
io.write("pastebin run [-f] <id> [<arguments...>]\n")
150+
io.write("pastebin run [-f] <id> [<arguments...>]\n")
151-
io.write(" -f: Force overwriting existing files.\n")
151+
io.write(" -f: Force overwriting existing files.\n")
152-
io.write(" -k: keep line endings as-is (will convert\n")
152+
io.write(" -k: keep line endings as-is (will convert\n")
153
io.write("     Windows line endings to Unix otherwise).")