Advertisement
AquaJD

GUIEv2_Startup

Feb 6th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.79 KB | None | 0 0
  1. -- Colors
  2. tColors = {
  3.   ["main"] = { bg = colors.white, fg = colors.black },
  4.   ["taskbar"] = { bg = colors.blue, fg = colors.white },
  5.   ["button"] = { fg = colors.white, bar = colors.gray, drag = colors.yellow, close = colors.red  },
  6.   ["error"] = { bg = colors.red, fg = colors.white }
  7. }
  8.  
  9. -- Buttons/Boxes
  10. tButtons = {
  11.   ["button1"] = { x = 4, y = 6, w = 9, h = 3, fg = "Login", bDrag = false, bClose = false, bBar = false, bg = colors.blue },
  12.   ["button2"] = { x = 4, y = 10, w = 12, h = 3, fg = "Register", bDrag = false, bClose = false, bBar = false, bg = colors.blue },
  13.   ["button3"] = { x = 4, y = 14, w = 12, h = 3, fg = "Shutdown", bDrag = false, bClose = false, bBar = false, bg = colors.blue },
  14.   ["sideBar1"] = { x = 19, y = 3, w = 30, h = 16, fg = "Login", bDrag = false, bClose = true, bBar = true, bg = colors.blue },
  15.   ["sideBar2"] = { x = 19, y = 3, w = 30, h = 16, fg = "Register", bDrag = false, bClose = true, bBar = true, bg = colors.blue },
  16.   ["inputUser"] = { x = 21, y = 5, w = 26, h = 4, fg = "Username", bDrag = false, bClose = false, bBar = false, bg = colors.lightGray },
  17.   ["inputPass"] = { x = 21, y = 10, w = 26, h = 4, fg = "Password", bDrag = false, bClose = false, bBar = false, bg = colors.lightGray },
  18.   ["submitLogin"] = { x = 29, y = 15, w = 10, h = 3, fg = "Log in", bDrag = false, bClose = false, bBar = false, bg = colors.green },
  19.   ["submitRegister"] = { x = 28, y = 15, w = 12, h = 3, fg = "Register", bDrag = false, bClose = false, bBar = false, bg = colors.green },
  20.   ["msgSuccess"] = { x = 27, y = 15, w = 14, h = 3, fg = "Successful", bDrag = false, bClose = false, bBar = false, bg = colors.lime },
  21.   ["msgUserTaken"] = { x = 25, y = 15, w = 16, h = 3, fg = "Username taken", bDrag = false, bClose = false, bBar = false, bg = colors.red },
  22.   ["msgDenied"] = { x = 29, y = 15, w = 10, h = 3, fg = "Denied", bDrag = false, bClose = false, bBar = false, bg = colors.red }
  23. }
  24.  
  25. -- Boolean Logic
  26. bLogin = false
  27. bRegister = false
  28. bRunning = true
  29.  
  30. -- SHA-256 stuff
  31. local MOD = 2^32
  32. local MODM = MOD-1
  33.  
  34. -- User Input (for Login/Register)
  35. l_userInput = ""
  36. l_passInput = ""
  37. l_passInputN = ""
  38. r_userInput = ""
  39. r_passInput = ""
  40. r_passInputN = ""
  41.  
  42. -- More SHA-256
  43. local function memoize(f)
  44.   local mt = {}
  45.   local t = setmetatable({}, mt)
  46.   function mt:__index(k)
  47.     local v = f(k)
  48.     t[k] = v
  49.     return v
  50.   end
  51.   return t
  52. end
  53.  
  54. local function make_bitop_uncached(t, m)
  55.   local function bitop(a, b)
  56.     local res,p = 0,1
  57.     while a ~= 0 and b ~= 0 do
  58.       local am, bm = a % m, b % m
  59.       res = res + t[am][bm] * p
  60.       a = (a - am) / m
  61.       b = (b - bm) / m
  62.       p = p*m
  63.     end
  64.     res = res + (a + b) * p
  65.     return res
  66.   end
  67.   return bitop
  68. end
  69.  
  70. local function make_bitop(t)
  71.   local op1 = make_bitop_uncached(t,2^1)
  72.   local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  73.   return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  74. end
  75.  
  76. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  77.  
  78. local function bxor(a, b, c, ...)
  79.   local z = nil
  80.   if b then
  81.     a = a % MOD
  82.     b = b % MOD
  83.     z = bxor1(a, b)
  84.     if c then z = bxor(z, c, ...) end
  85.     return z
  86.   elseif a then return a % MOD
  87.   else return 0 end
  88. end
  89.  
  90. local function band(a, b, c, ...)
  91.   local z
  92.   if b then
  93.     a = a % MOD
  94.     b = b % MOD
  95.     z = ((a + b) - bxor1(a,b)) / 2
  96.     if c then z = bit32_band(z, c, ...) end
  97.     return z
  98.   elseif a then return a % MOD
  99.   else return MODM end
  100. end
  101.  
  102. local function bnot(x) return (-1 - x) % MOD end
  103.  
  104. local function rshift1(a, disp)
  105.   if disp < 0 then return lshift(a,-disp) end
  106.   return math.floor(a % 2 ^ 32 / 2 ^ disp)
  107. end
  108.  
  109. local function rshift(x, disp)
  110.   if disp > 31 or disp < -31 then return 0 end
  111.   return rshift1(x % MOD, disp)
  112. end
  113.  
  114. local function lshift(a, disp)
  115.   if disp < 0 then return rshift(a,-disp) end
  116.   return (a * 2 ^ disp) % 2 ^ 32
  117. end
  118.  
  119. local function rrotate(x, disp)
  120.     x = x % MOD
  121.     disp = disp % 32
  122.     local low = band(x, 2 ^ disp - 1)
  123.     return rshift(x, disp) + lshift(low, 32 - disp)
  124. end
  125.  
  126. local k = {
  127.   0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  128.   0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  129.   0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  130.   0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  131.   0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  132.   0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  133.   0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  134.   0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  135.   0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  136.   0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  137.   0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  138.   0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  139.   0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  140.   0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  141.   0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  142.   0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  143. }
  144.  
  145. local function str2hexa(s)
  146.   return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  147. end
  148.  
  149. local function num2s(l, n)
  150.   local s = ""
  151.   for i = 1, n do
  152.     local rem = l % 256
  153.     s = string.char(rem) .. s
  154.     l = (l - rem) / 256
  155.   end
  156.   return s
  157. end
  158.  
  159. local function s232num(s, i)
  160.   local n = 0
  161.   for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  162.   return n
  163. end
  164.  
  165. local function preproc(msg, len)
  166.   local extra = 64 - ((len + 9) % 64)
  167.   len = num2s(8 * len, 8)
  168.   msg = msg .. "\128" .. string.rep("\0", extra) .. len
  169.   assert(#msg % 64 == 0)
  170.   return msg
  171. end
  172.  
  173. local function initH256(H)
  174.   H[1] = 0x6a09e667
  175.   H[2] = 0xbb67ae85
  176.   H[3] = 0x3c6ef372
  177.   H[4] = 0xa54ff53a
  178.   H[5] = 0x510e527f
  179.   H[6] = 0x9b05688c
  180.   H[7] = 0x1f83d9ab
  181.   H[8] = 0x5be0cd19
  182.   return H
  183. end
  184.  
  185. local function digestblock(msg, i, H)
  186.   local w = {}
  187.   for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  188.   for j = 17, 64 do
  189.     local v = w[j - 15]
  190.     local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  191.     v = w[j - 2]
  192.     w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  193.   end
  194.  
  195.   local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  196.   for i = 1, 64 do
  197.     local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  198.     local maj = bxor(band(a, b), band(a, c), band(b, c))
  199.     local t2 = s0 + maj
  200.     local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  201.     local ch = bxor (band(e, f), band(bnot(e), g))
  202.     local t1 = h + s1 + ch + k[i] + w[i]
  203.     h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  204.   end
  205.  
  206.   H[1] = band(H[1] + a)
  207.   H[2] = band(H[2] + b)
  208.   H[3] = band(H[3] + c)
  209.   H[4] = band(H[4] + d)
  210.   H[5] = band(H[5] + e)
  211.   H[6] = band(H[6] + f)
  212.   H[7] = band(H[7] + g)
  213.   H[8] = band(H[8] + h)
  214. end
  215.  
  216. local function sha256(msg)
  217.   msg = preproc(msg, #msg)
  218.   local H = initH256({})
  219.   for i = 1, #msg, 64 do digestblock(msg, i, H) end
  220.   return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  221.     num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  222. end
  223.  
  224. -- Draw a Box
  225. function drawBox(tButton,extra)
  226.   term.setBackgroundColor(tButton.bg)
  227.   term.setTextColor(tColors["button"].fg)
  228.   term.setCursorPos(tButton.x,tButton.y)
  229.   for i=1,tButton.h do
  230.     for i=1,tButton.w do
  231.       write(" ")
  232.     end
  233.     term.setCursorPos(tButton.x,tButton.y+i)
  234.   end
  235.   if tButton.bBar then
  236.     term.setCursorPos(tButton.x,tButton.y)
  237.     term.setBackgroundColor(tColors["button"].bar)
  238.     for i=1,tButton.w do
  239.       write(" ")
  240.     end
  241.     term.setCursorPos(tButton.x+1,tButton.y)
  242.     term.setTextColor(tColors["button"].fg)
  243.     print(tButton.fg)
  244.     if tButton.bDrag then
  245.       term.setCursorPos(tButton.x,tButton.y)
  246.       term.setTextColor(tColors["button"].drag)
  247.       print(string.char(7))
  248.     end
  249.     if tButton.bClose then
  250.       term.setCursorPos(tButton.x+tButton.w-1,tButton.y)
  251.       term.setTextColor(tColors["button"].close)
  252.       print(string.char(7))
  253.     end
  254.   else
  255.     if tButton.h == 3 or tButton.h == 5 or tButton.h == 7 or tButton.h == 9 then
  256.       term.setCursorPos(tButton.x+(tButton.w/2)-(#tButton.fg/2),tButton.y+(tButton.h/2))
  257.       print(tButton.fg)
  258.     else
  259.       term.setCursorPos(tButton.x+(tButton.w/2)-(#tButton.fg/2),tButton.y+(tButton.h/2)-1)
  260.       print(tButton.fg)
  261.     end
  262.   end
  263.   term.setCursorPos(tButton.x+(tButton.w/2)-(#extra/2),tButton.y+(tButton.h/2)+1)
  264.   term.setTextColor(colors.gray)
  265.   print(extra)
  266. end
  267.  
  268. -- Draw main GUI (draws all other GUIs)
  269. function drawBackground()
  270.   term.setBackgroundColor(tColors["main"].bg)
  271.   term.clear()
  272.   term.setCursorPos(1,1)
  273.   term.setBackgroundColor(tColors["taskbar"].bg)
  274.   term.clearLine()
  275.   term.setCursorPos(1,1)
  276.   term.setTextColor(tColors["taskbar"].fg)
  277.   print("GUI Example V2")
  278.   drawBox(tButtons["button1"],"")
  279.   drawBox(tButtons["button2"],"")
  280.   drawBox(tButtons["button3"],"")
  281. end
  282.  
  283. -- Draw Login Screen
  284. function drawLoginScreen()
  285.   bRegister = false
  286.   bLogin = true
  287.   drawBox(tButtons["sideBar1"],"")
  288.   drawBox(tButtons["inputUser"],l_userInput)
  289.   drawBox(tButtons["inputPass"],l_passInputN)
  290.   drawBox(tButtons["submitLogin"],"")
  291. end
  292.  
  293. -- Draw Register Screen
  294. function drawRegisterScreen()
  295.   bLogin = false
  296.   bRegister = true
  297.   drawBox(tButtons["sideBar2"],"")
  298.   drawBox(tButtons["inputUser"],r_userInput)
  299.   drawBox(tButtons["inputPass"],r_passInputN)
  300.   drawBox(tButtons["submitRegister"],"")
  301. end
  302.  
  303. -- Check Login details
  304. function checkLogin()
  305.   local bSuccess = false
  306.   local user = sha256(l_userInput)
  307.   local pass = sha256(l_passInput)
  308.   if fs.exists("/guie2/users/"..user) then
  309.     local userFile = fs.open("/guie2/users/"..user,"r")
  310.     local userFile_pass = userFile.readLine()
  311.     if userFile_pass == pass then
  312.       bSuccess = true
  313.     end
  314.     userFile.close()
  315.   end
  316.   return bSuccess
  317. end
  318.  
  319. -- Check Register details
  320. function checkRegister()
  321.   local bSuccess = false
  322.   local user = sha256(r_userInput)
  323.   local pass = sha256(r_passInput)
  324.   if not fs.exists("/guie2/users/"..user) then
  325.     local userFile = fs.open("/guie2/users/"..user,"w")
  326.     userFile.writeLine(pass)
  327.     userFile.close()
  328.     bSuccess = true
  329.   end
  330.   return bSuccess
  331. end
  332.  
  333. -- Success message
  334. function drawLoginSuccess()
  335.   drawBox(tButtons["msgSuccess"],"")
  336.   sleep(1)
  337. end
  338.  
  339. -- Failure message
  340. function drawLoginFailure()
  341.   drawBox(tButtons["msgDenied"],"")
  342.   sleep(1)
  343. end
  344.  
  345. -- Success message
  346. function drawRegisterSuccess()
  347.   drawBox(tButtons["msgSuccess"],"")
  348.   sleep(1)
  349. end
  350.  
  351. -- Failure message
  352. function drawRegisterFailure()
  353.   drawBox(tButtons["msgUserTaken"],"")
  354.   sleep(1)
  355. end
  356.  
  357. -- Troubleshooting for the start
  358. if not fs.exists("/guie2") then
  359.   fs.makeDir("/guie2")
  360.   fs.makeDir("/guie2/.users/")
  361.   local adminFile = fs.open("/guie2/.users/.admin","w")
  362.   adminFile.writeLine("admin")
  363.   adminFile.close()
  364. end
  365.  
  366. -- Main Loop
  367. drawBackground()
  368. while bRunning do
  369.   local event, button, X, Y = os.pullEvent()
  370.   if event == "mouse_click" then
  371.     if button == 1 then
  372.       if X >= tButtons["button1"].x and X <= (tButtons["button1"].x + tButtons["button1"].w - 1) and Y >= tButtons["button1"].y and Y <= (tButtons["button1"].y + tButtons["button1"].h - 1) then
  373.         drawBackground()
  374.         drawLoginScreen()
  375.       elseif X >= tButtons["button2"].x and X <= (tButtons["button2"].x + tButtons["button2"].w - 1) and Y >= tButtons["button2"].y and Y <= (tButtons["button2"].y + tButtons["button2"].h - 1) then
  376.         drawBackground()
  377.         drawRegisterScreen()
  378.       elseif X >= tButtons["button3"].x and X <= (tButtons["button3"].x + tButtons["button3"].w - 1) and Y >= tButtons["button3"].y and Y <= (tButtons["button3"].y + tButtons["button3"].h - 1) then
  379.         os.shutdown()
  380.       elseif bLogin == true and X == (tButtons["sideBar1"].x + tButtons["sideBar1"].w - 1) and Y == tButtons["sideBar1"].y then
  381.         bLogin = false
  382.         drawBackground()
  383.       elseif bRegister == true and X == (tButtons["sideBar2"].x + tButtons["sideBar2"].w - 1) and Y == tButtons["sideBar2"].y then
  384.         bRegister = false
  385.         drawBackground()
  386.       elseif bLogin == true and X >= tButtons["inputUser"].x and X <= (tButtons["inputUser"].x + tButtons["inputUser"].w - 1) and Y >= tButtons["inputUser"].y and Y <= (tButtons["inputUser"].y + tButtons["inputUser"].h - 1) then
  387.         term.setTextColor(colors.black)
  388.         term.setBackgroundColor(colors.gray)
  389.         term.setCursorPos(tButtons["inputUser"].x+1,tButtons["inputUser"].y+tButtons["inputUser"].h-2)
  390.         print("                        ")
  391.         term.setCursorPos(tButtons["inputUser"].x+1,tButtons["inputUser"].y+tButtons["inputUser"].h-2)
  392.         l_userInput = read()
  393.         drawBackground()
  394.         drawLoginScreen()
  395.       elseif bLogin  == true and X >= tButtons["inputPass"].x and X <= (tButtons["inputPass"].x + tButtons["inputPass"].w - 1) and Y >= tButtons["inputPass"].y and Y <= (tButtons["inputPass"].y + tButtons["inputPass"].h - 1) then
  396.         l_passInputN = ""
  397.         term.setTextColor(colors.black)
  398.         term.setBackgroundColor(colors.gray)
  399.         term.setCursorPos(tButtons["inputPass"].x+1,tButtons["inputPass"].y+tButtons["inputPass"].h-2)
  400.         print("                        ")
  401.         term.setCursorPos(tButtons["inputPass"].x+1,tButtons["inputPass"].y+tButtons["inputPass"].h-2)
  402.         l_passInput = read("*")
  403.         for i=1,#l_passInput do
  404.           l_passInputN = l_passInputN .. "*"
  405.         end
  406.         drawBackground()
  407.         drawLoginScreen()
  408.       elseif bRegister == true and X >= tButtons["inputPass"].x and X <= (tButtons["inputPass"].x + tButtons["inputPass"].w - 1) and Y >= tButtons["inputPass"].y and Y <= (tButtons["inputPass"].y + tButtons["inputPass"].h - 1) then
  409.         r_passInputN = ""
  410.         term.setTextColor(colors.black)
  411.         term.setBackgroundColor(colors.gray)
  412.         term.setCursorPos(tButtons["inputPass"].x+1,tButtons["inputPass"].y+tButtons["inputPass"].h-2)
  413.         print("                        ")
  414.         term.setCursorPos(tButtons["inputPass"].x+1,tButtons["inputPass"].y+tButtons["inputPass"].h-2)
  415.         r_passInput = read("*")
  416.         for i=1,#r_passInput do
  417.           r_passInputN = r_passInputN .. "*"
  418.         end
  419.         drawBackground()
  420.         drawRegisterScreen()
  421.       elseif bRegister == true and X >= tButtons["inputUser"].x and X <= (tButtons["inputUser"].x + tButtons["inputUser"].w - 1) and Y >= tButtons["inputUser"].y and Y <= (tButtons["inputUser"].y + tButtons["inputUser"].h - 1) then
  422.         term.setTextColor(colors.black)
  423.         term.setBackgroundColor(colors.gray)
  424.         term.setCursorPos(tButtons["inputUser"].x+1,tButtons["inputUser"].y+tButtons["inputUser"].h-2)
  425.         print("                        ")
  426.         term.setCursorPos(tButtons["inputUser"].x+1,tButtons["inputUser"].y+tButtons["inputUser"].h-2)
  427.         r_userInput = read()
  428.         drawBackground()
  429.         drawRegisterScreen()
  430.       elseif bLogin == true and X >= tButtons["submitLogin"].x and X <= (tButtons["submitLogin"].x + tButtons["submitLogin"].w - 1) and Y >= tButtons["submitLogin"].y and Y <= (tButtons["submitLogin"].y + tButtons["submitLogin"].h - 1) then
  431.         local bReturned = checkLogin()
  432.         if bReturned == true then
  433.           drawLoginSuccess()
  434.           bRunning = false
  435.           shell.run("guie_dt")
  436.         else
  437.           drawLoginFailure()
  438.           drawBackground()
  439.           drawLoginScreen()
  440.         end
  441.       elseif bRegister == true and X >= tButtons["submitRegister"].x and X <= (tButtons["submitRegister"].x + tButtons["submitRegister"].w - 1) and Y >= tButtons["submitRegister"].y and Y <= (tButtons["submitRegister"].y + tButtons["submitRegister"].h - 1) then
  442.         local bReturned = checkRegister()
  443.         if bReturned == true then
  444.           drawRegisterSuccess()
  445.           drawBackground()
  446.         else
  447.           drawRegisterFailure()
  448.           drawBackground()
  449.           drawRegisterScreen()
  450.         end
  451.       end
  452.     end
  453.   end
  454. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement