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