-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathpastebin.lua
More file actions
124 lines (106 loc) · 3.02 KB
/
pastebin.lua
File metadata and controls
124 lines (106 loc) · 3.02 KB
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
local function printUsage()
print( "Usages:" )
print( "pastebin put <filename>" )
print( "pastebin get <code> <filename>" )
print( "pastebin run <code> <arguments>" )
end
local tArgs = { ... }
if #tArgs < 2 then
printUsage()
return
end
if not http then
printError( "Pastebin requires http API" )
printError( "Set http_enable to true in ComputerCraft.cfg" )
return
end
local function get(paste)
write( "Connecting to pastebin.com... " )
local response = http.get(
"https://pastebin.com/raw/"..textutils.urlEncode( paste )
)
if response then
print( "Success." )
local sResponse = response.readAll()
response.close()
return sResponse
else
print( "Failed." )
end
end
local sCommand = tArgs[1]
if sCommand == "put" then
-- Upload a file to pastebin.com
-- Determine file to upload
local sFile = tArgs[2]
local sPath = shell.resolve( sFile )
if not fs.exists( sPath ) or fs.isDir( sPath ) then
print( "No such file" )
return
end
-- Read in the file
local sName = fs.getName( sPath )
local file = fs.open( sPath, "r" )
local sText = file.readAll()
file.close()
-- POST the contents to pastebin
write( "Connecting to pastebin.com... " )
local key = "0ec2eb25b6166c0c27a394ae118ad829"
local response = http.post(
"https://pastebin.com/api/api_post.php",
"api_option=paste&"..
"api_dev_key="..key.."&"..
"api_paste_format=lua&"..
"api_paste_name="..textutils.urlEncode(sName).."&"..
"api_paste_code="..textutils.urlEncode(sText)
)
if response then
print( "Success." )
local sResponse = response.readAll()
response.close()
local sCode = string.match( sResponse, "[^/]+$" )
print( "Uploaded as "..sResponse )
print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
else
print( "Failed." )
end
elseif sCommand == "get" then
-- Download a file from pastebin.com
if #tArgs < 3 then
printUsage()
return
end
-- Determine file to download
local sCode = tArgs[2]
local sFile = tArgs[3]
local sPath = shell.resolve( sFile )
if fs.exists( sPath ) then
print( "File already exists" )
return
end
-- GET the contents from pastebin
local res = get(sCode)
if res then
local file = fs.open( sPath, "w" )
file.write( res )
file.close()
print( "Downloaded as "..sFile )
end
elseif sCommand == "run" then
local sCode = tArgs[2]
local res = get(sCode)
if res then
local func, err = load(res, sCode, "t", _ENV)
if not func then
printError( err )
return
end
local success, msg = pcall(func, select(3, ...))
if not success then
printError( msg )
end
end
else
printUsage()
return
end