Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function cprint(...)
- local s = "&1"
- for k, v in ipairs(arg) do
- s = s .. v
- end
- s = s .. "&0"
- local fields = {}
- local lastcolor, lastpos = "0", 0
- for pos, clr in s:gmatch"()&(%x)" do
- table.insert(fields, {s:sub(lastpos + 2, pos - 1), lastcolor})
- lastcolor, lastpos = clr , pos
- end
- for i = 2, #fields do
- if term.isColor() then
- term.setTextColor(2 ^ (tonumber(fields[i][2], 16)))
- end
- io.write(fields[i][1])
- end
- end
- local key = "0ec2eb25b6166c0c27a394ae118ad829"
- local username = "noUsername"
- local password = "noPassword"
- if fs.exists(".userpastebin.developerapikey") then
- local h = fs.open(".userpastebin.developerapikey","r")
- key = h.readAll()
- h.close()
- else
- local h = fs.open(".userpastebin.developerapikey","w")
- h.write(key)
- h.close()
- end
- if fs.exists(".userpastebin.username") then
- local h = fs.open(".userpastebin.username", "r")
- username = h.readAll()
- h.close()
- end
- if fs.exists(".userpastebin.password") then
- local h = fs.open(".userpastebin.password", "r")
- password = h.readAll()
- h.close()
- end
- local function printUsage()
- cprint( "&1Usages&0:\n" )
- cprint( "&1userpastebin &3put &1<&5filename&1>\n" )
- cprint( "&1userpastebin &3get &1<&5code&1> <&5filename&1>\n" )
- cprint( "&1userpastebin &3run &1<&5code&1> <&5arguments&1>\n" )
- cprint( "&1userpastebin &3login &1<&5username&1>\n" )
- cprint( "&1userpastebin &3login logout &1\n" )
- cprint( "&1userpastebin &3setkey &1<&5key&1>\n" )
- cprint( "&1userpastebin &3setkey default\n\n" )
- cprint( "&1The Current Key Is&0: &d"..key.."\n\n" )
- cprint( "&1userpastebin &3key help\n" )
- 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)
- cprint( "&1Connecting to pastebin.com... " )
- local response = http.get(
- "http://pastebin.com/raw.php?i="..textutils.urlEncode( paste )
- )
- if response then
- cprint( "&1Success.\n" )
- local sResponse = response.readAll()
- response.close()
- return sResponse
- else
- printError( "Failed." )
- end
- end
- local sCommand = tArgs[1]
- if sCommand == "put" then
- local sFile = tArgs[2]
- local sPath = shell.resolve( sFile )
- if not fs.exists( sPath ) or fs.isDir( sPath ) then
- cprint( "&1No such file\n" )
- return
- end
- local sName = fs.getName( sPath )
- local file = fs.open( sPath, "r" )
- local sText = file.readAll()
- file.close()
- cprint( "&1Connecting to pastebin.com... \n" )
- local skey = "0ec2eb25b6166c0c27a394ae118ad829"
- if key ~= skey then
- if username == "noUsername" then
- cprint("&5Please Enter Your Username: ")
- username = read()
- cprint("\n&5Password: ")
- password = read("*")
- cprint("&1\n")
- else
- cprint("&1Getting User Key...\n")
- local skeyr = http.post(
- "http://pastebin.com/api/api_login.php",
- "api_dev_key="..textutils.urlEncode(key)..
- "&api_user_name="..textutils.urlEncode(username)..
- "&api_user_password="..textutils.urlEncode(password)
- )
- skey = skeyr.readAll()
- cprint("&1Received User Key...\n")
- cprint("&1Username: &5"..username.."&1\n")
- end
- end
- local response = http.post(
- "http://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).."&"..
- "api_user_key="..textutils.urlEncode(skey)
- )
- if response then
- cprint( "&1Success.\n" )
- local sResponse = response.readAll()
- response.close()
- local sCode = string.match( sResponse, "[^/]+$" )
- cprint( "&1Uploaded as "..sResponse.."\n" )
- cprint( "&1Run \"userpastebin get "..sCode.."\" to download anywhere\n" )
- else
- cprint( "&1Failed.\n" )
- end
- elseif sCommand == "get" then
- if #tArgs < 3 then
- printUsage()
- return
- end
- local sCode = tArgs[2]
- local sFile = tArgs[3]
- local sPath = shell.resolve( sFile )
- if fs.exists( sPath ) then
- cprint( "&1File already exists. If you want to replace the existing file, type 'YES'. Otherwise, type 'NO'.\n" )
- local replaceb = read()
- if replaceb == "NO" then
- return
- elseif replaceb == "YES" then
- cprint( "&1Okay, replacing file...\n" )
- else
- return
- end
- end
- local res = get(sCode)
- if res then
- local file = fs.open( sPath, "w" )
- file.write( res )
- file.close()
- cprint( "&1Downloaded as "..sFile.."\n" )
- end
- elseif sCommand == "run" then
- local sCode = tArgs[2]
- local res = get(sCode)
- if res then
- local func, err = loadstring(res)
- if not func then
- printError( err )
- return
- end
- setfenv(func, getfenv())
- local success, msg = pcall(func, unpack(tArgs, 3))
- if not success then
- printError( msg )
- end
- end
- elseif sCommand == "setkey" then
- local sCode = tArgs[2]
- if sCode == "default" then
- sCode = "0ec2eb25b6166c0c27a394ae118ad829"
- end
- h = fs.open(".userpastebin.developerapikey","w")
- h.write(sCode)
- h.close()
- h = fs.open(".userpastebin.developerapikey","r")
- if h.readAll() == sCode then
- cprint("&1Key Updated Successfully&0.&1 New Key&0:\n&d"..sCode.."\n")
- else
- cprint("&eFailed&0!")
- end
- h.close()
- elseif sCommand == "key" then
- local sCode = tArgs[2]
- if sCode == "help" then
- cprint("\n&1Using an API key will let you upload right to your account&0.&1 Go to &3pastebin.com/api &1after logging in to find your key&0.\n")
- end
- elseif sCommand == "login" then
- local sName = tArgs[2]
- if sName == "logout" then
- if username == "noUsername" then
- cprint("&1You are not logged in!\n")
- return
- else
- username = "noUsername"
- password = "noPassword"
- local h = fs.open(".userpastebin.username", "w")
- h.write("noUsername")
- h.close()
- local h = fs.open(".userpastebin.password", "w")
- h.write("noPassword")
- h.close()
- return
- end
- else
- cprint("&5Password: ")
- sPass = read("*")
- cprint("&1\n")
- local h = fs.open(".userpastebin.username", "w")
- h.write(sName)
- h.close()
- local h = fs.open(".userpastebin.password", "w")
- h.write(sPass)
- h.close()
- return
- end
- else
- printUsage()
- return
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement