Advertisement
CaptainSpaceCat

PastebinCC Collab Edition

Jun 25th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.02 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. self.active = true
  74.         elseif self.visible == true then
  75.                 self.setVisible(false)
  76.                 self.visible = false
  77.                 updateMenus()
  78.         end
  79. end
  80.                
  81.  
  82. --uWordsAPI/WebtoolsAPI
  83.  
  84. function getColors()
  85.   for i,v in pairs(colors) do
  86.           if tonumber(v) then
  87.           loadstring(i.."="..v)()
  88.         end
  89.   end
  90.   grey = gray
  91.   lightGrey = lightGray
  92. end
  93.  
  94. function basics(e)
  95.         if e[1] == "key" then
  96.                 if e[2] == keys.q then
  97.                         error()
  98.                 elseif e[2] == keys.leftAlt then
  99.                         shell.run("pastebin run NybMLEHL")
  100.                 elseif e[2] == keys.tab then
  101.                         if eventsToggle == true then
  102.                                 eventsToggle = false
  103.                                 updateMenus()
  104.                         else
  105.                                 eventsToggle = true
  106.                         end
  107.                         logCorTab(e)
  108.                 elseif e[2] == keys.delete then
  109.                         menu = "home"
  110.                         updateMenus()
  111.                 end
  112.         end
  113. end
  114.  
  115. function hitbox(e,x,y,x2,y2,num,func,...)
  116.         --takes events (e) and compares to box(x,y,x2,y2) to see if box had been clicked, if so run function
  117.         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
  118.                 local returns = {func(...)}
  119.                 if returns then return returns end
  120.         end
  121. end
  122.  
  123. function clearScreen(backcol)
  124.         term.setBackgroundColor(backcol)
  125.         term.clear()
  126.         term.setCursorPos(1,1)
  127. end
  128.  
  129. function draw(words,x,y,txtcol,backcol)
  130.         --put at top of file/program: local c,r,l = 'c','r','l'; local deftxt,defback
  131.         local w,h = term.getSize()
  132.         local offset = 0
  133.         local writeLn
  134.         words = tostring(words)
  135.         words = words or " "
  136.         if not (x and y) then
  137.                 x,y = term.getCursorPos()
  138.                 writeLn = true
  139.         elseif not x then
  140.                 x,_ = term.getCursorPos()
  141.                 writeLn = true
  142.         elseif not y then
  143.                 _,y = term.getCursorPos()
  144.                 writeLn = true
  145.         end
  146.         txtcol = txtcol or deftxt
  147.         backcol = backcol or defback
  148.         if not tonumber(x) then
  149.                 x = x:gsub(" ", "")
  150.                 if x:sub(1, 1) == "l" then
  151.                         if x:find("+") then
  152.                                 offset = tonumber(x:sub(x:find("+") + 1))
  153.                                 term.setCursorPos(1 + offset, y)
  154.                         else
  155.                                 term.setCursorPos(1, y)
  156.                         end
  157.                 elseif x:sub(1, 1) == "c" then
  158.                         if x:find("+") then
  159.                                 offset = tonumber(x:sub(x:find("+") + 1))
  160.                                 term.setCursorPos(w/2 - #words/2 + 1 + offset, y)
  161.                         elseif x:find("-") then
  162.                                 offset = tonumber(x:sub(x:find("-") + 1))
  163.                                 term.setCursorPos(w/2 - #words/2 + 1 - offset, y)
  164.                         else
  165.                                 term.setCursorPos(w/2 - #words/2 + 1, y)
  166.                         end
  167.                 elseif x:sub(1, 1) == "r" then
  168.                         if x:find("-") then
  169.                                 offset = tonumber(x:sub(x:find("-") + 1))
  170.                                 term.setCursorPos(w - #words+ 1 - offset, y)
  171.                         else
  172.                                 term.setCursorPos(w - #words+ 1, y)
  173.                         end
  174.                 end
  175.         else
  176.                 term.setCursorPos(x, y)
  177.         end
  178.         term.setTextColor(txtcol)
  179.         term.setBackgroundColor(backcol)
  180.         if writeLn then
  181.                 write(words..'\n')
  182.         else
  183.                 write(words)
  184.         end
  185. end
  186.  
  187. --ButtonMethods
  188.  
  189. function button.signIn(x,y,txtcol,backcol,bool)
  190.         local userdetails
  191.         term.setCursorPos(x,y)
  192.         term.setTextColor(txtcol)
  193.         term.setBackgroundColor(backcol)
  194.         term.write("___________          ")
  195.         term.setCursorPos(x,y)
  196.         if not bool then
  197.                 userdetails = read()
  198.         else
  199.                 userdetails = read("*")
  200.         end
  201.         if userdetails then return userdetails end
  202. end
  203.  
  204. function button.authenticate(user,pass)
  205.         if user and pass then        
  206.                 log("received authent")
  207.                 log("Authenticated: "..user)
  208.                 log("Authenticated: "..pass)
  209.                 draw(string.rep(" ",70),1,h-1)
  210.                 draw(string.rep(" ",70),1,h)
  211.                 draw("Connecting to Pastebin...",1,h,yellow)
  212.                 local userkey = http.post(
  213.                         "http://pastebin.com/api/api_login.php",
  214.                         "api_option=login&"..
  215.                         "api_dev_key=996a06d80a378f151e589ac86c83e865&"..
  216.                         "api_user_name="..textutils.urlEncode(user).."&"..
  217.                         "api_user_password="..textutils.urlEncode(pass)
  218.                 )
  219.    
  220.                 local postdata = userkey.readAll()
  221.                 term.setTextColor(colors.red)
  222.                 log(postdata)
  223.                 userkey.close()
  224.                 if postdata:sub(1,3) == 'Bad' then
  225.                         usercode = nil
  226.                         log("Authentication Request Failed")
  227.                         draw(string.rep(" ",70),1,h-1)
  228.                         draw(string.rep(" ",70),1,h)
  229.                         draw("The Username or Password is incorrect,\nPlease try again...",1,h-1,red)
  230.                         username,password = nil,nil
  231.                 else
  232.                         log("Authentication Successful")
  233.                         usercode = postdata
  234.                         menu = "home"
  235.                         updateMenus()
  236.                 end
  237.         else
  238.                 draw(string.rep(" ",70),1,h)
  239.                 draw(string.rep(" ",70),1,h-1)
  240.                 draw("Please fill all required fields with your Username and Password...",1,h-1,red)
  241.         end
  242. end
  243.  
  244. function button.pastebin()
  245.         log("PastebinPopup request received")
  246.         pastebinWin:toggle()
  247. end
  248.  
  249. function button.login()
  250.         log("Login Request Received")
  251.         signInWin:toggle()
  252. end
  253.  
  254. function button.newPaste()
  255. log("newPaste")
  256. end
  257. function button.upload()
  258. log("upload")
  259. end
  260. function button.download()
  261. log("download")
  262. end
  263. function run()
  264. log("run")
  265. end
  266.  
  267. function button.edit()
  268. return true
  269. end
  270.  
  271. -----------===========PASTEBIN++===========-------------
  272. local location = {
  273.     ["x"] = 1,
  274.     ["y"] = 1
  275. }
  276. local position = {
  277.     ["x"] = 1,
  278.     ["y"] = 1
  279. }
  280. local program = {
  281. [1] = " "
  282. }
  283. local currentLine, currentPos = 1, 1
  284. local pChanged, lChanged
  285. local pW, pH = w - 4, h - 6
  286. local numlength = 0
  287.  
  288. function addLetter(char, line, pos)
  289. program[line] = program[line]:sub(1, pos - 1) .. tostring(char) .. program[line]:sub(pos)
  290. end
  291.  
  292. function fixCursorPos()
  293. if position["y"] < 1 then
  294.          pChanged = true
  295.          location["y"] = location["y"] + position["y"] - 1
  296.          position["y"] = 1
  297.  elseif position["y"] > pH then
  298.          pChanged = true
  299.          location["y"] = location["y"] + position["y"] - pH
  300.          position["y"] = pH
  301. end
  302. if location["y"] < 1 then
  303.          pChanged = false
  304.          location["y"] = 1
  305. elseif location["y"] + pH > #program then
  306.          pChanged = false
  307.          location["y"] = #program - pH
  308. end
  309. if position["x"] + location["x"] - 1 > #program[1] then
  310.                 position["x"] = #program[1] - location["x"] + 2
  311. end
  312. if position["x"] < 1 then
  313.         pChanged = true
  314.         location["x"] = location["x"] + position["x"] - 1
  315.         position["x"] = 1
  316. elseif position["x"] > w - numlength then
  317.         pChanged = true
  318.         location["x"] = location["x"] + position["x"] - (w-numlength)
  319.         position["x"] = w - numlength
  320. end
  321. if location["x"] < 1 then
  322.         pChanged = false
  323.         location["x"] = 1
  324. elseif location["x"] + w - numlength > #program then
  325.         pChanged = false
  326.         location["x"] = #program - w + numlength
  327. end
  328. currentLine = location["y"] + position["y"] - 1
  329. currentPos = position["x"] + location["x"] - 1
  330. end
  331.  
  332. function showProgram()
  333.  
  334. end
  335.  
  336. function showProgramLine()
  337.  
  338. end
  339.  
  340. function pastebinEdit(events)
  341. term.setCursorBlink(true)
  342. if events[1] == "char" then
  343. addLetter(events[2], currentLine, currentPos)
  344. position["x"] = position["x"] + 1
  345. elseif events[1] == "key" then
  346. if events[2] == keys.up then
  347. position["y"] = position["y"] - 1
  348. elseif events[2] == keys.down then
  349. position["y"] = position["y"] + 1
  350. elseif events[2] == keys.left then
  351. position["x"] = position["x"] - 1
  352. elseif events[2] == keys.right then
  353. position["x"] = position["x"] + 1
  354. elseif events[2] == keys.backspace then
  355.  
  356. elseif events[2] == keys.enter then
  357.  
  358. elseif events[2] == keys.tab then
  359. addLetter("  ", currentLine, currentPos)
  360. position["x"] = position["x"] + 2
  361. end
  362. end
  363. fixCursorPos()
  364. term.setCursorPos(currentLine + 5, currentPos + 2)
  365. if pChanged then
  366. showProgram()
  367. pChanged = false
  368. lChanged = false
  369. elseif lChanged then
  370. showProgramLine()
  371. lChanged = false
  372. end
  373. end
  374.  
  375. -------------------=================0-0=================-----------------------
  376.  
  377. getColors()
  378. username,password = nil,nil
  379.  
  380.   pastebinWin = window.create(native,1,2,16,5)
  381.   signInWin = window.create(native,w-15,2,w,3)
  382.   submitWin = window.create(native,w-(#" Submit "+2),h,w-2,h)
  383. pasteCodeWin = window.create(native, 3, 5, w - 2, h - 2)
  384.  
  385. for i,v in pairs(windows) do
  386.         pastebinWin[i] = v
  387.         signInWin[i] = v
  388. end
  389.  
  390. pastebinWin:clearWin(lightBlue)
  391. pastebinWin:writeLn(" -New Paste",gray,lightBlue)
  392. pastebinWin:writeLn(" -Upload file",gray,lightBlue)
  393. pastebinWin:writeLn(" -Download file",gray,lightBlue)
  394. pastebinWin:writeLn(" -Run Postrayme",gray,lightBlue)
  395.  
  396. signInWin:clearWin(lightBlue)
  397. signInWin:writeLn("Enter Username",gray,lightBlue)
  398. signInWin:writeLn("Enter Password",gray,lightBlue)
  399.  
  400.  
  401. function updateMenus()
  402.         if menu == "signIn" then
  403.                 clearScreen(lightGray)
  404.                 local signInBackCol,guestBackCol = white,lightBlue
  405.                 draw("--------------",c,5,lightBlue)
  406.                 draw("PastebinCC",c,6,white,defback)
  407.                 draw("--------------",c,7,lightBlue)
  408.                 draw("Username: ___________",c,9,grey)
  409.                 draw("Password: ___________",c,11,grey)
  410.                 draw(" Sign In ",'r-13',13,black,signInBackCol)
  411.                 draw(" Sign in as Guest ",1,1,white,guestBackCol)
  412.         elseif menu == "home" then
  413.                 clearScreen(lightGray)
  414.                 paintutils.drawLine(1,1,w,1,lightBlue)
  415.                 paintutils.drawLine(1,2,w,2,blue)
  416.                 draw("Pastebin.com",l,1,white,lightBlue)        
  417.                 paintutils.drawBox(2,4,w-1,h-1,white)
  418.                 draw("[new paste]",3,4,gray,white)
  419.                 --draw("[+] create new paste [^] trending pastes",2,3,white,blue)
  420.                 --draw("sign in|my pastebin|my profile|logout",r,2,white,lightBlue)
  421.                 draw(" sign in ",r,1,white,lightBlue)
  422.                 draw(" Submit ",'r-2',h,gray,lightBlue)
  423.         end
  424. end
  425.  
  426. updateMenus()
  427.  
  428. while menu == "signIn" do
  429.         local e = {os.pullEvent()}
  430.         logCorTab(e)
  431.         local user = hitbox(e,w/2,9,w/2+10,9,1,button.signIn,w/2+1,9,black,defback)
  432.         if user then username = user[1]; log("Username: "..username); end
  433.         pass = hitbox(e,w/2,11,w/2+10,11,1,button.signIn,w/2+1,11,black,defback,'secret')
  434.         if pass then password = pass[1]; log("Password: "..password); end
  435.         hitbox(e,29,13,38,13,1,button.authenticate,username,password)
  436.         basics(e)
  437. end
  438.  
  439. local cEdit
  440. while menu == "home" do
  441.         local e = {os.pullEvent()}
  442.         logCorTab(e)
  443.         basics(e)
  444.         hitbox(e,1,1,#"Pastebin.com",1,1,button.pastebin)
  445.       hitbox(e,w-#" sign in ",1,w,1,1,button.login)
  446. if e[1] == "mouse_click" then
  447. hitbox(e, 2, 4, w - 1, h - 1, 1, pastebinEdit(e))
  448. end
  449. if signInWin.active then
  450. log(signInWin.active)
  451. local user, pass
  452. local ureturns = hitbox(e,36,2,51,2,1,function() term.setCursorPos(37,2);signInWin.clearLine(); read(); end)
  453. local preturns = hitbox(e,36,3,51,3,1,function() term.setCursorPos(37,3);signInWin.clearLine(); read(); end)
  454. if ureturns then user = ureturns[1] end
  455. if preturns then pass = preturns[1] end
  456. if user and pass then
  457. local userkey = http.post(
  458.     "http://pastebin.com/api/api_login.php",
  459.     "api_option=login&"..
  460.     "api_dev_key=996a06d80a378f151e589ac86c83e865&"..
  461.     "api_user_name="..textutils.urlEncode(user).."&"..
  462.     "api_user_password="..textutils.urlEncode(pass)
  463. )
  464.    
  465.     local postdata = userkey.readAll()
  466.   if postdata:sub(1,3) ~= "Bad" then
  467. signInWin:writeLn("Success",green,lightBlue)
  468. else
  469. signInWin:writeLn("Invalid",red,lightBlue)
  470.    end
  471.   end
  472.    end
  473. if pastebinWin.active then
  474. hitbox(e,1,2,15,2,button.newPaste)
  475. hitbox(e,1,3,15,3,button.upload)
  476. hitbox(e,1,4,15,4,button.download)
  477. hitbox(e,1,5,15,5,button.run)
  478. end
  479. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement