Advertisement
guitarplayer616

Pastebin.com - CC Edition

Jun 25th, 2015
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.71 KB | None | 0 0
  1. local button = {}
  2. local menus = {"signIn","home"}
  3. local menu = "signIn"
  4. local native = term.current()
  5. local corY,corY2 = 1,1
  6. local usercode,username,password,pastebinWin,signInWin,submitWin
  7. local eventsToggle = true
  8. fs.open("log","w")
  9.  
  10. local w,h = term.getSize()
  11. local c,r,l = 'c','r','l'
  12. local deftxt, defback = colors.white,colors.lightGray
  13.  
  14. --Console(temporary)
  15. function logCorTab(e)
  16.     if eventsToggle then
  17.         for i = 1,4 do
  18.             term.setBackgroundColor(defback)
  19.             term.setCursorPos(w-15,i)
  20.             term.write(string.rep(" ",16))
  21.         end
  22.         corY = 1
  23.         for i = 1,#e do
  24.             draw(e[i],r,corY)
  25.             corY = corY + 1
  26.         end
  27.     end
  28. end
  29.  
  30. function logCor(words)
  31.     if eventsToggle then
  32.         draw(words,r,corY)
  33.         corY2 = corY2+1
  34.     end
  35. end
  36.  
  37. function log(words)
  38.     words = tostring(words)
  39.     local h = fs.open("log","a")
  40.     h.writeLine(words)
  41.     h.close()
  42. end
  43.  
  44. --WindowClasses (todo = betterscrollin, movable/draggable/resizable/scrollbarable windows)
  45. local windows = {}
  46.  
  47. function windows.writeLn(self,words,txtcol,backcol,x,y)
  48.     local w,h = self.getSize()
  49.     txtcol = txtcol or deftxt
  50.     backcol = backcol or defback
  51.     if x and y then
  52.         self.setCursorPos(x,y)
  53.     end
  54.     self.setBackgroundColor(backcol)
  55.     self.setTextColor(txtcol)
  56.     self.write(words)
  57.     local x,y = self.getCursorPos()
  58.     self.setCursorPos(x-#words,y+1)
  59. end
  60.  
  61. function windows.clearWin(self,backcol)
  62.     self.setBackgroundColor(backcol)
  63.     self.clear()
  64. end
  65.  
  66. function windows.toggle(self)
  67.     if self.visible == nil then
  68.         self.visible = true
  69.     end
  70.     if self.visible == false then
  71.         self.setVisible(true)
  72.         self.visible = true
  73.     elseif self.visible == true then
  74.         self.setVisible(false)
  75.         self.visible = false
  76.         updateMenus()
  77.     end
  78. end
  79.        
  80.  
  81. --uWordsAPI/WebtoolsAPI
  82.  
  83. function getColors()
  84.   for i,v in pairs(colors) do
  85.     if tonumber(v) then
  86.       loadstring(i.."="..v)()
  87.     end
  88.   end
  89.   grey = gray
  90.   lightGrey = lightGray
  91. end
  92.  
  93. function basics(e)
  94.     if e[1] == "key" then
  95.         if e[2] == keys.q then
  96.             error()
  97.         elseif e[2] == keys.leftAlt then
  98.             shell.run("pastebin run NybMLEHL")
  99.         elseif e[2] == keys.tab then
  100.             if eventsToggle == true then
  101.                 eventsToggle = false
  102.                 updateMenus()
  103.             else
  104.                 eventsToggle = true
  105.             end
  106.             logCorTab(e)
  107.         elseif e[2] == keys.delete then
  108.             menu = "home"
  109.             updateMenus()
  110.         end
  111.     end
  112. end
  113.  
  114. function hitbox(e,x,y,x2,y2,num,func,...)
  115.         --takes events (e) and compares to box(x,y,x2,y2) to see if box had been clicked, if so run function
  116.         if e[1] == "mouse_click" and e[2] == num and e[3] >= x and e[3] <= x2 and e[4] <= y and e[4] >=y2 then
  117.                 local returns = {func(...)}
  118.         if returns then return returns end
  119.         end
  120. end
  121.  
  122. function clearScreen(backcol)
  123.     term.setBackgroundColor(backcol)
  124.     term.clear()
  125.     term.setCursorPos(1,1)
  126. end
  127.  
  128. function draw(words,x,y,txtcol,backcol)
  129.     --put at top of file/program: local c,r,l = 'c','r','l'; local deftxt,defback
  130.     local w,h = term.getSize()
  131.     local offset = 0
  132.         local writeLn
  133.     words = tostring(words)
  134.         words = words or " "
  135.         if not (x and y) then
  136.                 x,y = term.getCursorPos()
  137.                 writeLn = true
  138.     elseif not x then
  139.         x,_ = term.getCursorPos()
  140.                 writeLn = true
  141.         elseif not y then
  142.         _,y = term.getCursorPos()
  143.         writeLn = true
  144.     end
  145.         txtcol = txtcol or deftxt
  146.         backcol = backcol or defback
  147.         if not tonumber(x) then
  148.                 x = x:gsub(" ", "")
  149.                 if x:sub(1, 1) == "l" then
  150.                         if x:find("+") then
  151.                                 offset = tonumber(x:sub(x:find("+") + 1))
  152.                                 term.setCursorPos(1 + offset, y)
  153.                         else
  154.                                 term.setCursorPos(1, y)
  155.                         end
  156.                 elseif x:sub(1, 1) == "c" then
  157.                         if x:find("+") then
  158.                                 offset = tonumber(x:sub(x:find("+") + 1))
  159.                                 term.setCursorPos(w/2 - #words/2 + 1 + offset, y)
  160.                         elseif x:find("-") then
  161.                                 offset = tonumber(x:sub(x:find("-") + 1))
  162.                                 term.setCursorPos(w/2 - #words/2 + 1 - offset, y)
  163.                         else
  164.                                 term.setCursorPos(w/2 - #words/2 + 1, y)
  165.                         end
  166.                 elseif x:sub(1, 1) == "r" then
  167.                         if x:find("-") then
  168.                                 offset = tonumber(x:sub(x:find("-") + 1))
  169.                                 term.setCursorPos(w - #words+ 1 - offset, y)
  170.                         else
  171.                                 term.setCursorPos(w - #words+ 1, y)
  172.                         end
  173.                 end
  174.         else
  175.                 term.setCursorPos(x, y)
  176.         end
  177.         term.setTextColor(txtcol)
  178.         term.setBackgroundColor(backcol)
  179.         if writeLn then
  180.                 write(words..'\n')
  181.         else
  182.                 write(words)
  183.         end
  184. end
  185.  
  186. --ButtonMethods
  187.  
  188. function button.signIn(x,y,txtcol,backcol,bool)
  189.     local userdetails
  190.     term.setCursorPos(x,y)
  191.     term.setTextColor(txtcol)
  192.     term.setBackgroundColor(backcol)
  193.     term.write("___________          ")
  194.     term.setCursorPos(x,y)
  195.     if not bool then
  196.         userdetails = read()
  197.     else
  198.         userdetails = read("*")
  199.     end
  200.     if userdetails then return userdetails end
  201. end
  202.  
  203. function button.authenticate(user,pass)
  204.     if user and pass then  
  205.         log("received authent")
  206.         log("Authenticated: "..user)
  207.         log("Authenticated: "..pass)
  208.         draw(string.rep(" ",70),1,h-1)
  209.         draw(string.rep(" ",70),1,h)
  210.         draw("Connecting to Pastebin...",1,h,yellow)
  211.         local userkey = http.post(
  212.             "http://pastebin.com/api/api_login.php",
  213.             "api_option=login&"..
  214.             "api_dev_key=996a06d80a378f151e589ac86c83e865&"..
  215.             "api_user_name="..textutils.urlEncode(username).."&"..
  216.             "api_user_password="..textutils.urlEncode(password)
  217.         )
  218.    
  219.         local postdata = userkey.readAll()
  220.         term.setTextColor(colors.red)
  221.         log(postdata)
  222.         userkey.close()
  223.         if postdata:sub(1,3) == 'Bad' then
  224.             usercode = nil
  225.             log("Authentication Request Failed")
  226.             draw(string.rep(" ",70),1,h-1)
  227.             draw(string.rep(" ",70),1,h)
  228.             draw("The Username or Password is incorrect,\nPlease try again...",1,h-1,red)
  229.             username,password = nil,nil
  230.         else
  231.             log("Authentication Successful")
  232.             usercode = postdata
  233.             menu = "home"
  234.             updateMenus()
  235.         end
  236.     else
  237.         draw(string.rep(" ",70),1,h)
  238.         draw(string.rep(" ",70),1,h-1)
  239.         draw("Please fill all required fields with your Username and Password...",1,h-1,red)
  240.     end
  241. end
  242.  
  243. function button.pastebin()
  244.     log("PastebinPopup request received")
  245.     pastebinWin:toggle()
  246. end
  247.  
  248. function button.login()
  249.     log("Login Request Received")
  250. end
  251. --==0-0==--
  252.  
  253. getColors()
  254. username,password = nil,nil
  255.  
  256.   pastebinWin = window.create(native,1,2,16,5)
  257.   signInWin = window.create(native,w-9,2,w,4)
  258.   --submitWin =
  259.  
  260. for i,v in pairs(windows) do
  261.     pastebinWin[i] = v
  262.     signInWin[i] = v
  263. end
  264.  
  265. pastebinWin:clearWin(lightBlue)
  266. pastebinWin:writeLn(" -New Paste",gray,lightBlue)
  267. pastebinWin:writeLn(" -Upload file",gray,lightBlue)
  268. pastebinWin:writeLn(" -Download file",gray,lightBlue)
  269. pastebinWin:writeLn(" -Run Pastrami",gray,lightBlue)
  270.  
  271.  
  272.  
  273. function updateMenus()
  274.     if menu == "signIn" then
  275.         clearScreen(lightGray)
  276.         local signInBackCol,guestBackCol = white,lightBlue
  277.         draw("--------------",c,5,lightBlue)
  278.         draw("PastebinCC",c,6,white,defback)
  279.         draw("--------------",c,7,lightBlue)
  280.         draw("Username: ___________",c,9,grey)
  281.         draw("Password: ___________",c,11,grey)
  282.         draw(" Sign In ",'r-13',13,black,signInBackCol)
  283.         draw(" Sign in as Guest ",1,1,white,guestBackCol)
  284.     elseif menu == "home" then
  285.         clearScreen(lightGray)
  286.         paintutils.drawLine(1,1,w,1,lightBlue)
  287.         paintutils.drawLine(1,2,w,2,blue)
  288.         draw("Pastebin.com",l,1,white,lightBlue)   
  289.         paintutils.drawBox(2,4,w-1,h-1,white)
  290.         draw("[new paste]",3,4,gray,white)
  291.         --draw("[+] create new paste [^] trending pastes",2,3,white,blue)
  292.         --draw("sign in|my pastebin|my profile|logout",r,2,white,lightBlue)
  293.         draw(" sign in ",r,1,white,lightBlue)
  294.         draw(" Submit ",'r-2',h,gray,lightBlue)
  295.     end
  296. end
  297.  
  298. updateMenus()
  299.  
  300. while menu == "signIn" do
  301.     local e = {os.pullEvent()}
  302.     logCorTab(e)
  303.     local user = hitbox(e,w/2,9,w/2+10,9,1,button.signIn,w/2+1,9,black,defback)
  304.     if user then username = user[1]; log("Username: "..username); end
  305.     pass = hitbox(e,w/2,11,w/2+10,11,1,button.signIn,w/2+1,11,black,defback,'secret')
  306.     if pass then password = pass[1]; log("Password: "..password); end
  307.     hitbox(e,29,13,38,13,1,button.authenticate,username,password)
  308.     basics(e)
  309. end
  310.  
  311. while menu == "home" do
  312.     local e = {os.pullEvent()}
  313.     logCorTab(e)
  314.     basics(e)
  315.     hitbox(e,1,1,#"Pastebin.com",1,1,button.pastebin)
  316.     hitbox(e,w-#" sign in ",1,w,1,1,button.login)
  317. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement