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