· 5 years ago · Nov 19, 2020, 03:48 PM
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
21local function get(paste)
22 write( "Connecting to pastebin.com... " )
23 local response = http.get(
24 "https://pastebin.com/raw/"..textutils.urlEncode( paste )
25 )
26
27 if response then
28 print( "Success." )
29
30 local sResponse = response.readAll()
31 response.close()
32 print(sResponse)
33 return sResponse
34 else
35 printError( "Failed." )
36 end
37end
38
39local sCommand = tArgs[1]
40if sCommand == "put" then
41 -- Upload a file to pastebin.com
42 -- Determine file to upload
43 local sFile = tArgs[2]
44 local sPath = shell.resolve( sFile )
45 if not fs.exists( sPath ) or fs.isDir( sPath ) then
46 print( "No such file" )
47 return
48 end
49
50 -- Read in the file
51 local sName = fs.getName( sPath )
52 local file = fs.open( sPath, "r" )
53 local sText = file.readAll()
54 file.close()
55
56 -- POST the contents to pastebin
57 write( "Connecting to pastebin.com... " )
58 local key = "0ec2eb25b6166c0c27a394ae118ad829"
59 local response = http.post(
60 "https://pastebin.com/api/api_post.php",
61 "api_option=paste&"..
62 "api_dev_key="..key.."&"..
63 "api_paste_format=lua&"..
64 "api_paste_name="..textutils.urlEncode(sName).."&"..
65 "api_paste_code="..textutils.urlEncode(sText)
66 )
67
68 if response then
69 print( "Success." )
70
71 local sResponse = response.readAll()
72 response.close()
73
74 local sCode = string.match( sResponse, "[^/]+$" )
75 print( "Uploaded as "..sResponse )
76 print( "Run \"pastebin get "..sCode.."\" to download anywhere" )
77
78 else
79 print( "Failed." )
80 end
81
82elseif sCommand == "get" then
83 -- Download a file from pastebin.com
84 if #tArgs < 3 then
85 printUsage()
86 return
87 end
88
89 -- Determine file to download
90 local sCode = tArgs[2]
91 local sFile = tArgs[3]
92 local sPath = shell.resolve( sFile )
93 if fs.exists( sPath ) then
94 print( "File already exists" )
95 return
96 end
97
98 -- GET the contents from pastebin
99 local res = get(sCode)
100 if res then
101 local file = fs.open( sPath, "w" )
102 file.write( res )
103 file.close()
104
105 print( "Downloaded as "..sFile )
106 end
107elseif sCommand == "run" then
108 local sCode = tArgs[2]
109
110 local res = get(sCode)
111 if res then
112 local func, err = load(res, sCode, "t", _ENV)
113 if not func then
114 printError( err )
115 return
116 end
117 local success, msg = pcall(func, table.unpack(tArgs, 3))
118 if not success then
119 printError( msg )
120 end
121 end
122else
123 printUsage()
124 return
125end