Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- pastebin get w8RHW808 pcan
- std pb w8RHW808 pcan
- std ld pcan <name>
- --]]
- local username
- local password
- local doSave = false --it IS unencrypted, so defaults to no.
- --Uses AgentE382's Pastebin API implementation. You made this much easier, I must say!
- local currentpath = fs.getDir(shell.getRunningProgram())
- local savepath = fs.combine(currentpath,".userdata")
- local apipath = fs.combine(currentpath,"pb")
- if not fs.exists(apipath) then
- print("Pastebin API not found! Downloading...")
- local program = http.get("http://pastebin.com/raw/5SJ0d1mV")
- if not program then
- print("FAIL!")
- error("Could not connect to pastebin...")
- else
- program = program.readAll()
- end
- local file = fs.open(apipath,"w")
- file.writeLine(program)
- file.close()
- print("Twas a success!")
- end
- os.loadAPI(apipath)
- less = function(...) --My entire 'less' program, modified for this application.
- local tArg = {...}
- local programName = fs.getName(shell.getRunningProgram())
- local oldbgcolor, oldtxtcolor = term.getBackgroundColor(), term.getTextColor()
- local renderTable = function(tab, start, ending, rightscroll, txtcolor, bgcolor)
- term.setCursorPos(1,2)
- local buffer = ""
- local scr_x, scr_y = term.getSize()
- term.setTextColor(txtcolor)
- term.setBackgroundColor(bgcolor)
- term.clear()
- local bezelSize = #tostring(#tab)
- for a = start,ending-1 do
- if tab[a] then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.lightGray)
- local bezel = tostring(a)
- local bezel = bezel..string.rep(" ",bezelSize-#bezel)
- write(bezel)
- term.setTextColor(txtcolor)
- term.setBackgroundColor(bgcolor)
- write( string.sub(buffer..tab[a],rightscroll+1,rightscroll+scr_x-bezelSize) )
- if a ~= ending-1 then
- write("\n")
- end
- else
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- end
- end
- end
- local tableFind = function(tab, str)
- for a = 1, #tab do
- if tab[a] == str then
- return true, a
- end
- end
- return false
- end
- local tableStringFind = function(tab, str)
- local output = {}
- for a = 1, #tab do
- local finder = {string.find(tab[a],str)}
- if finder ~= {} then
- table.insert(finder,a)
- table.insert(output,finder)
- end
- end
- return output
- end
- local function displayCredits()
- local credits = {
- "LESS unix command",
- "programmed by",
- "EldidiStroyrr",
- "",
- "Coding started at",
- "January 1st, 2016",
- "",
- "For normal CC1.7x computers, you're",
- "good to go!",
- }
- term.setBackgroundColor(colors.gray)
- if term.isColor() then term.setTextColor(colors.yellow) else term.setTextColor(colors.white) end
- term.clear()
- local scr_x, scr_y = term.getSize()
- for a = 1, #credits+12 do
- local msg = credits[a/2]
- if a % 2 == 0 and msg then
- term.setCursorPos((scr_x/2)-(#msg/2),scr_y)
- write(string.upper(msg))
- end
- sleep(0.4)
- term.scroll(1)
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- return "butts"
- end
- local getTableFromFile = function(filename)
- if not filename then
- return false
- end
- if not fs.exists(filename) then
- return false
- end
- local file = fs.open(filename,"r")
- local line = ""
- local output = {}
- while line do
- line = file.readLine()
- table.insert(output,line)
- end
- return output
- end
- local switches = {}
- for a = 1, #tArg do
- if string.sub(tArg[a],1,1) == "-" then
- table.insert(switches,string.sub(tArg[a],2,2))
- end
- end
- local lessfile
- local tArgFileNo = 1
- local dumpFile = nil
- if tableFind(tArg,"-d") or tableFind(tArg,"-D") then
- local _, dumpPos
- if tableFind(tArg,"-d") then
- _, dumpPos = tableFind(tArg,"-d")
- dumpPos = dumpPos + 1
- else
- _, dumpPos = tableFind(tArg,"-D")
- dumpPos = dumpPos + 1
- end
- dumpFile = tArg[dumpPos]
- if fs.isReadOnly(dumpFile) then
- error("That path is read only.")
- end
- tArgFileNo = tArgFileNo + 2
- if tableFind(tArg,"-a") then
- lessfile = tArg[tArgFileNo]
- else
- lessfile = fs.combine(fs.getDir(shell.getRunningProgram()),tArg[tArgFileNo])
- end
- end
- if tableFind(tArg,"-u") then
- local _, urlPos = tableFind(tArg,"-u")
- urlPos = urlPos + 1
- local url = tArg[urlPos]
- tArgFileNo = tArgFileNo + 2
- write("Connecting...")
- local internetFile = http.get(url)
- if not internetFile then
- print("FAILED!")
- return false
- end
- internetFile = internetFile.readAll()
- if #internetFile > fs.getFreeSpace("/") then --1 byte for every character, plus one
- error("You don't have enough space!")
- end
- local file = fs.open(".temp","w")
- file.write(internetFile)
- file.close()
- lessfile = ".temp"
- else
- lessfile = tArg[tArgFileNo]
- end
- if dumpFile then
- local file
- if tableFind(tArg,"-d") then
- file = fs.open(dumpFile,"w")
- print("Dumped to '"..dumpFile.."'")
- else
- file = fs.open(dumpFile,"a")
- print("Appended to '"..dumpFile.."'")
- end
- local file2 = fs.open(lessfile,"r")
- file.write(file2.readAll())
- file.close()
- file2.close()
- return true
- end
- local lessfilecontents = getTableFromFile(lessfile)
- local longestStringLen = 0
- for a = 1, #lessfilecontents do
- if #lessfilecontents[a] > longestStringLen then
- longestStringLen = #lessfilecontents[a]
- end
- end
- local scr_x, scr_y = term.getSize()
- local scrollX, scrollY = 0, 0
- local event, var1, var2, var3
- local function draw()
- term.setCursorPos(1,1)
- renderTable(lessfilecontents,scrollY+1,scrollY+scr_y,scrollX,colors.white,colors.gray)
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clearLine()
- if not isGuest then
- write("(E)dit,(R)emove,")
- end
- write("(D)ownload")
- end
- draw()
- local allInput = function()
- local alternateScroll = false
- local scr_x, scr_y = term.getSize()
- local lineHistory = {}
- while true do
- repeat
- event, var1, var2, var3 = os.pullEvent()
- until event == "key" or event == "char" or event == "mouse_scroll" or event == "key_up"
- oldScrollX, oldScrollY = scrollX, scrollY
- if event == "key" or event == "char" then
- if var1 == 38 then --'l' to go to line
- term.setCursorPos(1,2)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("Goto line: ")
- sleep(0)
- local lineno = tonumber(read(nil,lineHistory))
- if lineno then
- term.setCursorPos(1,1)
- if lineno < 1 then
- write("Huh? Line "..tostring(lineno).."?")
- sleep(1.2)
- elseif lineno > #lessfilecontents then
- write("Too high a number! (lines="..#lessfilecontents..")")
- sleep(1.2)
- else
- table.insert(lineHistory)
- scrollY = lineno-1 or 1
- end
- end
- draw()
- end
- if var1 == 200 or var1 == 21 then --up arrow or 'y'
- if scrollY > 0 then
- scrollY = scrollY - 1
- end
- elseif var1 == 208 then --down arrow
- if scrollY < #lessfilecontents-1 then
- scrollY = scrollY + 1
- end
- elseif var1 == 21 then --'u'
- if scrollY-math.floor(scr_y/2) > 0 then
- scrollY = scrollY - math.floor(scr_y/2)
- end
- elseif var1 == 203 then --left arrow
- if scrollX > 0 then
- scrollX = scrollX - 1
- end
- elseif var1 == 205 then --right arrow
- if scrollX < longestStringLen then
- scrollX = scrollX + 1
- end
- end
- if var1 == 45 or var1 == 16 then --'X' or 'Q' to quit
- sleep(0)
- break
- end
- if var1 == 29 then --left ctrl
- alternateScroll = true
- end
- if var1 == 199 or var1 == "g" then --home
- if alternateScroll then
- scrollX = 0
- else
- scrollY = 0
- end
- end
- if var1 == 207 or var1 == "G" then --end
- if alternateScroll then
- scrollX = longestStringLen
- else
- scrollY = #lessfilecontents-scr_y+1
- end
- end
- if var1 == 201 or var1 == "b" then --page up
- if not alternateScroll then
- scrollY = scrollY - scr_y
- if scrollY < 0 then scrollY = 0 end
- else
- scrollX = scrollX - scr_x
- if scrollX < 0 then scrollX = 0 end
- end
- end
- if var1 == 209 or var1 == "f" then --page down
- if not alternateScroll then
- scrollY = scrollY + scr_y
- if scrollY > #lessfilecontents-1 then scrollY = #lessfilecontents-1 end
- else
- scrollX = scrollX + scr_x
- if scrollX > longestStringLen then scrollX = longestStringLen end
- end
- end
- if var1 == "e" and not isGuest then --edit current paste
- term.setCursorPos(1,1)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- write("Not working as of yet!")
- sleep(0.8)
- --[[
- shell.run("edit",lessfile)
- local file = fs.open(lessfile,"r")
- local contents = file.readAll()
- file.close()
- local status = pb.create(contents,userkey,selectedFileName,nil,0,"N")
- --]]
- draw()
- elseif var1 == "d" then --download to file
- term.setCursorPos(1,2)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("DL to: /")
- sleep(0)
- local dlpath = fs.combine("",read())
- if fs.exists(fs.getDir(dlpath)) and not fs.exists(dlpath) then
- fs.copy(lessfile, dlpath)
- term.setCursorPos(1,2)
- term.clearLine()
- write("Done!")
- sleep(0.5)
- else
- term.setCursorPos(1,2)
- term.clearLine()
- write("Bad path!")
- sleep(0.5)
- end
- draw()
- end
- if var1 == "r" then --remove file
- term.setCursorPos(1,2)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("Y-you sure?? [Y,N]")
- repeat
- event, key = os.pullEvent("char")
- key = string.lower(key)
- until string.find("yn",key)
- if key == "y" then
- pb.delete(userkey,selectedFileCode)
- return
- else
- term.setCursorPos(1,2)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("Phew!")
- sleep(0.5)
- draw()
- end
- end
- elseif event == "mouse_scroll" then
- if not alternateScroll then
- if var1 == -1 then
- if scrollY > 0 then
- scrollY = scrollY - 1
- end
- elseif var1 == 1 then
- if scrollY < #lessfilecontents-1 then
- scrollY = scrollY + 1
- end
- end
- else
- if var1 == -1 then
- if scrollX > 0 then
- scrollX = scrollX - 1
- end
- elseif var1 == 1 then
- if scrollX < longestStringLen then
- scrollX = scrollX + 1
- end
- end
- end
- elseif event == "key_up" then
- if var1 == 29 then --left ctrl
- alternateScroll = false
- end
- end
- if scrollX ~= oldScrollX or scrollY ~= oldScrollY then --Yay! Flicker reduction!
- draw()
- end
- end
- end
- return allInput()
- end
- local fadeIn = function()
- term.setBackgroundColor(colors.black)
- term.clear()
- sleep(0)
- term.setBackgroundColor(colors.gray)
- term.clear()
- sleep(0)
- term.setBackgroundColor(colors.lightGray)
- term.clear()
- sleep(0)
- term.setBackgroundColor(colors.white)
- term.clear()
- term.setTextColor(colors.black)
- end
- local fadeOut = function()
- term.setBackgroundColor(colors.white)
- term.clear()
- sleep(0)
- term.setBackgroundColor(colors.lightGray)
- term.clear()
- sleep(0)
- term.setBackgroundColor(colors.gray)
- term.clear()
- sleep(0)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.white)
- end
- local cwrite = function(txt)
- local posX, posY = term.getCursorPos()
- local scr_x, scr_y = term.getSize()
- term.setCursorPos(math.ceil(scr_x/2)-(#txt/2)+1,posY)
- write(txt)
- end
- local cprint = function(txt)
- cwrite(txt)
- write("\n")
- end
- local isGuest = false
- userkey = nil
- local status
- local loginPrompt = function()
- if username and password then
- status, userkey = pcall(pb.login,username,password)
- if status then
- userkey = tostring(userkey)
- end
- updatePasteList(not status)
- return status
- else
- isGuest = false
- fadeIn()
- term.setCursorPos(1,2)
- cprint("CC Pastebin Client")
- cprint("[BETA]")
- term.setCursorPos(1,6)
- repeat
- cprint("Enter username:")
- cprint("(do blank for guest)")
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- sleep(0)
- username = read()
- if username == "" then
- isGuest = true
- else
- term.setBackgroundColor(colors.white)
- cprint("Enter password:")
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- sleep(0)
- password = read("*")
- term.setBackgroundColor(colors.white)
- status, userkey = pcall(pb.login,username,password)
- if status then
- userkey = tostring(userkey)
- end
- if userkey == "Bad API request, invalid login" then
- cprint("Invalid credentials!")
- else
- isGuest = false
- if doSave then
- local file = fs.open(savepath,"w")
- file.write(username.."\n"..password)
- file.close()
- end
- end
- end
- until (userkey ~= "Bad API request, invalid login" and status) or isGuest == true
- updatePasteList(isGuest)
- end
- end
- local renderThingie = function(tbl,txtcolor,bgcolor)
- local oldbg,oldtxt = term.getBackgroundColor(), term.getTextColor()
- for a = 1, #tbl do
- term.setTextColor(txtcolor)
- term.setBackgroundColor(bgcolor)
- term.clearLine()
- print(tbl[a])
- end
- term.setTextColor(oldtxt)
- term.setBackgroundColor(oldbg)
- end
- local selectedFileName
- local displayData = function(menuNumber)
- term.clear()
- term.setCursorPos(1,1)
- print("Viewing...")
- selectedFileName = menuCodes[menuNumber][2]
- selectedFileCode = menuCodes[menuNumber][1]
- less("-u", "http://pastebin.com/raw/"..menuCodes[menuNumber][1])
- selectedFileName = nil
- selectedFileCode = nil
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- pageNo = 1
- updatePasteList(isGuest)
- end
- local trend = false --show trendy pastes or your own
- local pageNo = 1
- local pastes
- updatePasteList = function(trendy)
- if trendy then
- pastes = pb.trending()
- else
- pastes = pb.list(userkey,1024)
- end
- local scr_x, scr_y = term.getSize()
- term.setCursorPos(1,1)
- menuPages = {}
- buffer = {}
- rawData = {}
- for a = 1, #pastes do
- table.insert(rawData,{string.sub(pastes[a].title,1,scr_x),pastes[a].format_long,pastes[a].size.." bytes",pastes[a].hits.." hits"})
- end
- for a = 1, #rawData do
- table.insert(buffer,rawData[a])
- if a % math.floor((scr_y/5)-0.1) == 0 then
- table.insert(menuPages,buffer)
- buffer = {}
- end
- end
- if #buffer > 0 then
- table.insert(menuPages,buffer)
- buffer = {}
- end
- return pastes
- end
- local displayPastes = function()
- updatePasteList(isGuest)
- while true do
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- menuCodes = {}
- cprint(tostring(pageNo).."/"..#menuPages)
- for a = 1, #menuPages[pageNo] do
- local pgob = pastes[(#menuPages[pageNo]*(pageNo-1))+a]
- table.insert(menuCodes,{pgob.key,pgob.title})
- renderThingie({"["..a.."]",table.unpack(menuPages[pageNo][a])},colors.black,colors.lightGray)
- if a ~= #menuPages[pageNo] then write("\n") end
- end
- local scr_x, scr_y = term.getSize()
- term.setCursorPos(1,scr_y)
- if isGuest then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- end
- local msg = "(T)rend,(R)fr,(N)ew,(F)ind"
- term.clearLine()
- cwrite(msg)
- local event, key
- repeat
- event, key, x, y = os.pullEvent()
- until event == "key" or event == "char"
- if event == "key" then
- key = keys.getName(key)
- if key == "t" and userkey then
- isGuest = not isGuest
- pageNo = 1
- updatePasteList(isGuest)
- end
- if key == "f" then
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("Enter paste ID: ")
- sleep(0)
- local pbid = read()
- term.setCursorPos(1,1)
- term.clearLine()
- less("-u","http://pastebin.com/raw/"..pbid)
- end
- if key == "n" then
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("NEW: ")
- sleep(0)
- local newname = read()
- if newname ~= "" then
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("Private? [Y,N]")
- repeat
- event, key = os.pullEvent("char")
- key = string.lower(key)
- until string.find("yn",key)
- local isPrivate = 0
- if key == "y" then
- isPrivate = 1
- else
- isPrivate = 0
- end
- if fs.exists(".temp") then fs.delete(".temp") end
- shell.run("edit","/.temp")
- local file = fs.open("/.temp","r")
- local contents = file.readAll()
- file.close()
- pb.create(contents,userkey,newname,nil,isPrivate,"N")
- updatePasteList(isGuest)
- end
- end
- if key == "r" or key == "f5" then --refresh pastes
- pageNo = 1
- updatePasteList(isGuest)
- end
- if key == "left" then
- if pageNo > 1 then
- pageNo = pageNo - 1
- else
- pageNo = #menuPages
- end
- end
- if key == "right" then
- if pageNo < #menuPages then
- pageNo = pageNo + 1
- else
- pageNo = 1
- end
- end
- if key == "q" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- sleep(0)
- return
- end
- elseif event == "char" then
- if tonumber(key) then
- if #menuPages[pageNo] >= tonumber(key) then
- displayData(tonumber(key))
- end
- end
- end
- end
- end
- if (fs.exists(savepath)) and (not fs.isDir(savepath)) then
- local file = fs.open(savepath,"r")
- username = file.readLine()
- password = file.readLine()
- file.close()
- status, userkey = pcall(pb.login,u,p)
- if not status then
- loginPrompt()
- else
- updatePasteList(true)
- end
- else
- loginPrompt()
- end
- displayPastes()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement