Dragon53535

Monitor Messageboard

Nov 14th, 2014
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.19 KB | None | 0 0
  1. local oldPull = os.pullEvent
  2. os.pullEvent = os.pullEventRaw
  3. local function numberString(number)
  4.   local s = ""
  5.   repeat
  6.     local remainder = number % 2
  7.     s = remainder..s
  8.     number = (number-remainder)/2
  9.   until number==0
  10.   return s
  11. end
  12. function fromBinary(str)
  13.   if #str % 8 ~= 0 then
  14.     error("Malformed Binary Sequence",2)
  15.   end
  16.   local result = ""
  17.   for i = 1, #str, 8 do
  18.    result = result..string.char(tonumber(str:sub(i,i+7),2))
  19.   end
  20.   return result
  21. end
  22. function toBinary(str)
  23.   if #str > 0 then
  24.     local result = ""
  25.     for a = 1, #str do
  26.       result = result..string.format("%08d",numberString(string.byte(str:sub(a,a))))
  27.     end
  28.     return result
  29.   else return nil
  30.   end
  31. end
  32. local MOD = 2^32
  33. local MODM = MOD-1
  34. local function memoize(f)
  35.         local mt = {}
  36.         local t = setmetatable({}, mt)
  37.         function mt:__index(k)
  38.                 local v = f(k)
  39.                 t[k] = v
  40.                 return v
  41.         end
  42.         return t
  43. end
  44. local function make_bitop_uncached(t, m)
  45.         local function bitop(a, b)
  46.                 local res,p = 0,1
  47.                 while a ~= 0 and b ~= 0 do
  48.                         local am, bm = a % m, b % m
  49.                         res = res + t[am][bm] * p
  50.                         a = (a - am) / m
  51.                         b = (b - bm) / m
  52.                         p = p*m
  53.                 end
  54.                 res = res + (a + b) * p
  55.                 return res
  56.         end
  57.         return bitop
  58. end
  59. local function make_bitop(t)
  60.         local op1 = make_bitop_uncached(t,2^1)
  61.         local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  62.         return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  63. end
  64. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  65. local function bxor(a, b, c, ...)
  66.         local z = nil
  67.         if b then
  68.                 a = a % MOD
  69.                 b = b % MOD
  70.                 z = bxor1(a, b)
  71.                 if c then z = bxor(z, c, ...) end
  72.                 return z
  73.         elseif a then return a % MOD
  74.         else return 0 end
  75. end
  76. local function band(a, b, c, ...)
  77.         local z
  78.         if b then
  79.                 a = a % MOD
  80.                 b = b % MOD
  81.                 z = ((a + b) - bxor1(a,b)) / 2
  82.                 if c then z = bit32_band(z, c, ...) end
  83.                 return z
  84.         elseif a then return a % MOD
  85.         else return MODM end
  86. end
  87. local function bnot(x) return (-1 - x) % MOD end
  88. local function rshift1(a, disp)
  89.         if disp < 0 then return lshift(a,-disp) end
  90.         return math.floor(a % 2 ^ 32 / 2 ^ disp)
  91. end
  92. local function rshift(x, disp)
  93.         if disp > 31 or disp < -31 then return 0 end
  94.         return rshift1(x % MOD, disp)
  95. end
  96. local function lshift(a, disp)
  97.         if disp < 0 then return rshift(a,-disp) end
  98.         return (a * 2 ^ disp) % 2 ^ 32
  99. end
  100. local function rrotate(x, disp)
  101.     x = x % MOD
  102.     disp = disp % 32
  103.     local low = band(x, 2 ^ disp - 1)
  104.     return rshift(x, disp) + lshift(low, 32 - disp)
  105. end
  106. local k = {
  107.         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  108.         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  109.         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  110.         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  111.         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  112.         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  113.         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  114.         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  115.         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  116.         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  117.         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  118.         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  119.         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  120.         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  121.         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  122.         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  123. }
  124. local function str2hexa(s)
  125.         return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  126. end
  127. local function num2s(l, n)
  128.         local s = ""
  129.         for i = 1, n do
  130.                 local rem = l % 256
  131.                 s = string.char(rem) .. s
  132.                 l = (l - rem) / 256
  133.         end
  134.         return s
  135. end
  136. local function s232num(s, i)
  137.         local n = 0
  138.         for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  139.         return n
  140. end
  141. local function preproc(msg, len)
  142.         local extra = 64 - ((len + 9) % 64)
  143.         len = num2s(8 * len, 8)
  144.         msg = msg .. "\128" .. string.rep("\0", extra) .. len
  145.         assert(#msg % 64 == 0)
  146.         return msg
  147. end
  148. local function initH256(H)
  149.         H[1] = 0x6a09e667
  150.         H[2] = 0xbb67ae85
  151.         H[3] = 0x3c6ef372
  152.         H[4] = 0xa54ff53a
  153.         H[5] = 0x510e527f
  154.         H[6] = 0x9b05688c
  155.         H[7] = 0x1f83d9ab
  156.         H[8] = 0x5be0cd19
  157.         return H
  158. end
  159. local function digestblock(msg, i, H)
  160.         local w = {}
  161.         for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  162.         for j = 17, 64 do
  163.                 local v = w[j - 15]
  164.                 local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  165.                 v = w[j - 2]
  166.                 w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  167.         end
  168.  
  169.         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]
  170.         for i = 1, 64 do
  171.                 local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  172.                 local maj = bxor(band(a, b), band(a, c), band(b, c))
  173.                 local t2 = s0 + maj
  174.                 local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  175.                 local ch = bxor (band(e, f), band(bnot(e), g))
  176.                 local t1 = h + s1 + ch + k[i] + w[i]
  177.                 h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  178.         end
  179.  
  180.         H[1] = band(H[1] + a)
  181.         H[2] = band(H[2] + b)
  182.         H[3] = band(H[3] + c)
  183.         H[4] = band(H[4] + d)
  184.         H[5] = band(H[5] + e)
  185.         H[6] = band(H[6] + f)
  186.         H[7] = band(H[7] + g)
  187.         H[8] = band(H[8] + h)
  188. end
  189. local function sha256(msg)
  190.         msg = preproc(msg, #msg)
  191.         local H = initH256({})
  192.         for i = 1, #msg, 64 do digestblock(msg, i, H) end
  193.         return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  194.                 num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  195. end
  196.  
  197. local mainInput = "1. Login"
  198.  
  199. local uRSInput = [[1. Admin
  200. 2. Member
  201. 3. Delete
  202. 4. Exit]]
  203.  
  204. local codeInput = [[Please enter new user code.
  205. > ]]
  206.  
  207. local codeError = [[Invalid user code.]]
  208.  
  209. local regInput = [[New user setup, please enter new username and pass.
  210. Username:
  211. Password: ]]
  212.  
  213. local newOwnerInput = [[No Users detected. Enter Owner login. You must complete Owner signup or program will not function.
  214. Username:
  215. Password: ]]
  216.  
  217. local loginInput = [[Enter Username and Password. Leave blank to leave.
  218. Username:
  219. Password: ]]
  220.  
  221. local oMInput = [[Owner Menu:
  222. 1. Edit message
  223. 2. Edit User Ranks
  224. 3. Change User Code
  225. 4. Exit Program
  226. 5. Logout]]
  227.  
  228. local aMInput = [[Admin Menu:
  229. 1. Edit message
  230. 2. Edit User Ranks
  231. 3. Change User Code
  232. 4. Logout]]
  233.  
  234. local nMInput = [[Member Menu:
  235. 1. Edit message
  236. 2. Logout]]
  237.  
  238. local cCInput = [[Enter New User Access Code
  239. > ]]
  240.  
  241. local function findMon()
  242.   for a,v in pairs(rs.getSides()) do
  243.     if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
  244.       return peripheral.wrap(v)
  245.     end
  246.   end
  247.   error("No monitor connected",0)
  248. end
  249.  
  250. local mon = findMon()
  251. mon.setTextScale(1.5)
  252.  
  253. local function clear()
  254.     term.clear()
  255.     term.setCursorPos(1,1)
  256. end
  257.  
  258. local function userRankSwap(str)
  259.   clear()
  260.   local filePath = fs.combine("Users/",str)
  261.   local file = fs.open(filePath,"r")
  262.   local fileData = textutils.unserialize(file.readAll())
  263.   file.close()
  264.   if fileData[2] == "Owner" then
  265.     print("Cannot edit Owner permissions.")
  266.     sleep(2)
  267.     return
  268.   end
  269.   print("Choose rank of "..str.. ". Currently rank: "..fileData[2])
  270.   print(uRSInput)
  271.   while true do
  272.     local event, param1 = os.pullEvent("char")
  273.     if param1 == "1" then
  274.       local file = fs.open(filePath,"w")
  275.       fileData[2] = "Administrator"
  276.       file.writeLine(textutils.serialize(fileData))
  277.       file.close()
  278.       return
  279.     elseif param1 == "2" then
  280.       local file = fs.open(filePath,"w")
  281.       fileData[2] = "Member"
  282.       file.writeLine(textutils.serialize(fileData))
  283.       file.close()
  284.       return
  285.     elseif param1 == "3" then
  286.       local file = fs.open(filePath,"w")
  287.       fileData[2] = "Removed"
  288.       file.writeLine(textutils.serialize(fileData))
  289.       file.close()
  290.       return
  291.     elseif param1 == "4" then
  292.       return
  293.     end
  294.   end
  295. end
  296.  
  297. local function currentUsers()
  298.   local list = fs.list("Users/")
  299.   local totalList = {}
  300.   for a, p in ipairs(list) do
  301.     table.insert(totalList,a,p)
  302.   end
  303.   local o = 0
  304.   local x = 0
  305.   local y = 7
  306.   local z = 1
  307.   local currentList = {}
  308.   repeat
  309.     currentList = {}
  310.     clear()
  311.     y=y*z
  312.     local a = 0
  313.     x = y - 7
  314.     repeat
  315.       a=a+1
  316.       x=x+1
  317.       if totalList[x] == nil then
  318.         break
  319.       else
  320.         table.insert(currentList,totalList[x])
  321.         print(a..". "..totalList[x])
  322.       end
  323.     until a == y
  324.     print("8. Previous")
  325.     print("9. Next")
  326.     print("0. Exit")
  327.     while true do
  328.       local event, para = os.pullEvent("char")
  329.       local chrPara = tonumber(para)
  330.       if chrPara then
  331.         if (chrPara > 0 and chrPara < 8) then
  332.           if currentList[chrPara] ~= nil then
  333.             userRankSwap(currentList[chrPara])
  334.             return
  335.           elseif currentList[chrPara] == nil then
  336.             print("File doesn't exist")
  337.             sleep(.75)
  338.             break
  339.           end
  340.         elseif chrPara == 8 then
  341.           if o == 0 then
  342.             print("Cannot go back")
  343.             sleep(0.5)
  344.             break
  345.           elseif o ~= 0 then
  346.             o = o-1
  347.             x=7*8
  348.             break
  349.           end
  350.         elseif chrPara == 9  then
  351.           if totalList[x] == nil then
  352.             print("Cannot go forward")
  353.             sleep(0.5)
  354.             break
  355.           elseif totalList[x] ~= nil then
  356.             o = o + 1
  357.             break
  358.           end
  359.         elseif chrPara == 0 then
  360.           return
  361.         end
  362.       end
  363.     end
  364.   until nil
  365. end
  366.  
  367. local function changeMessage()
  368.   shell.run("edit message")
  369. end
  370.  
  371. local function changeCode()
  372.   clear()
  373.   write(cCInput)
  374.   local code = read()
  375.   if code ~= "" then
  376.     local file = fs.open("userCode","w")
  377.     file.writeLine(code)
  378.     file.close()
  379.     print("Code Saved")
  380.     sleep(1)
  381.   end
  382. end
  383.  
  384. local function ownerMenu()
  385.   clear()
  386.   print(oMInput)
  387.   local done = false
  388.   repeat
  389.     local event,param1 = os.pullEvent ("char")
  390.     if param1 == "1" then
  391.       changeMessage()
  392.       done = true
  393.     elseif param1 == "2" then
  394.       currentUsers()
  395.         done = true
  396.     elseif param1 == "3" then
  397.       changeCode()
  398.         done = true
  399.     elseif param1 == "4" then
  400.         os.pullEvent = oldPull
  401.         clear()
  402.       error()
  403.     elseif param1 == "5" then
  404.         done = true
  405.     end
  406.   until done
  407. end
  408.  
  409. local function adminMenu()
  410.   clear()
  411.   print(aMInput)
  412.   local done = false
  413.   repeat
  414.     local event,param1 = os.pullEvent ("char")
  415.     if param1 == "1" then
  416.       changeMessage()
  417.       done = true
  418.     elseif param1 == "2" then
  419.       currentUsers()
  420.         done = true
  421.     elseif param1 == "3" then
  422.       changeCode()
  423.         done = true
  424.     elseif param1 == "4" then
  425.         done = true
  426.     end
  427.   until done
  428. end
  429.  
  430. local function memberMenu()
  431.   clear()
  432.   print(nMInput)
  433.   local done = false
  434.   repeat
  435.     local event,param1 = os.pullEvent ("char")
  436.     if param1 == "1" then
  437.       changeMessage()
  438.       done = true
  439.     elseif param1 == "2" then
  440.       done = true
  441.     end
  442.   until done
  443. end
  444.  
  445. local function login()
  446.   local leave = ""
  447.   clear()
  448.   print(loginInput)
  449.   term.setCursorPos(11,2)
  450.   local user = string.lower(read())
  451.   term.setCursorPos(11,3)
  452.   local pass = read("*")
  453.   local filePath = fs.combine("Users/",user)
  454.   if user == leave and pass == leave then
  455.  
  456.   elseif user == leave or pass == leave then
  457.     print("Username or Password cannot be left blank, leave both blank to leave.")
  458.       sleep(3)
  459.       login()
  460.   elseif fs.exists(filePath) then
  461.     local file = fs.open(filePath, "r")
  462.     local fileData = textutils.unserialize(file.readAll())
  463.     file.close()
  464.       local hashData = sha256(toBinary(pass))
  465.     if hashData == fileData[1] then
  466.       if fileData[2] == "Owner" then
  467.         ownerMenu()
  468.       elseif fileData[2] == "Administrator" then
  469.         adminMenu()
  470.       elseif fileData[2] == "Member" then
  471.         memberMenu()
  472.       elseif fileData[2] == "Removed" then
  473.         print("Your account was removed")
  474.         sleep(4)
  475.         fs.delete(filePath)
  476.       end
  477.     else
  478.       print("Username and/or Password are incorrect.")
  479.       sleep(2)
  480.       login()
  481.     end
  482.   else
  483.     print("Username and/or Password are incorrect")
  484.     sleep(2)
  485.     login()
  486.   end
  487. end
  488.  
  489. local function register()
  490.   local leave = ""
  491.   clear()
  492.   print(regInput)
  493.   term.setCursorPos(11,2)
  494.   local newuser = string.lower(read())
  495.   term.setCursorPos(11,3)
  496.   local newpass = read("*")
  497.   if newuser == leave and newpass == leave then
  498.  
  499.   elseif newuser == leave or newpass == leave then
  500.     print("Username and/or password cannot be blank")
  501.     sleep(2.5)
  502.     register()
  503.   elseif fs.exists("Users/1"..newuser) == true then
  504.     print("Username already exists, please try again.")
  505.     sleep(2.5)
  506.     register()
  507.   else
  508.     newpass = sha256(toBinary(newpass))
  509.     local file = fs.open(fs.combine("Users/",newuser),"w")
  510.     file.writeLine(textutils.serialize({newpass,"Member"}))
  511.     file.close()
  512.     fs.delete("userCode")
  513.     print("User registered.")
  514.     sleep(1)
  515.   end
  516. end
  517.  
  518. local function newOwner()
  519.   local leave = ""
  520.   fs.makeDir("Users/")
  521.   clear()
  522.   print(newOwnerInput)
  523.   term.setCursorPos(11,3)
  524.   local ownerUser = string.lower(read())
  525.   term.setCursorPos(11,4)
  526.   local ownerPass = read("*")
  527.   if ownerUser == leave and ownerPass == leave then
  528.     print("Username and/or password cannot be blank")
  529.     sleep(2.5)
  530.     newOwner()
  531.   elseif ownerUser == leave or ownerPass == leave then
  532.     print("Username and/or password cannot be blank")
  533.     sleep(2.5)
  534.     newOwner()
  535.   else
  536.     ownerPass = sha256(toBinary(ownerPass))
  537.     local file = fs.open(fs.combine("Users/",ownerUser),"w")
  538.     file.writeLine(textutils.serialize({ownerPass,"Owner"}))
  539.     file.close()
  540.     print("Registered new Owner")
  541.     sleep(4)
  542.   end
  543. end
  544.  
  545. local function checkCode()
  546.   if fs.exists("userCode") then
  547.     clear()
  548.     local file = fs.open("userCode","r")
  549.     local code = file.readLine()
  550.     file.close()
  551.     write(codeInput)
  552.     local input = read()
  553.     if input == code then
  554.       register()
  555.     else
  556.       write(codeError)
  557.       sleep(1)
  558.     end
  559.   end
  560. end
  561.  
  562. local function main()
  563.   if fs.exists("Users/") then
  564.     clear()
  565.     print(mainInput)
  566.     local done = false
  567.     while not done do
  568.       local event, param1 = os.pullEvent ("char")
  569.       if param1 == "1" then
  570.         login()
  571.         done = true
  572.       elseif param1 == "." then
  573.         checkCode()
  574.         done = true
  575.       end
  576.     end
  577.   elseif not fs.exists("Users/") then
  578.     newOwner()
  579.     done = true
  580.   end
  581. end
  582.  
  583. local function monWrite(arg1)
  584.   local x,y = mon.getSize()
  585.   local z = arg1 or ""
  586.   local getx,gety = mon.getCursorPos()
  587.   local d = false
  588.   while #z > x do
  589.     getx,gety = mon.getCursorPos()
  590.     mon.write(z:sub(1,x))
  591.     mon.setCursorPos(1,gety+1)
  592.     z = z:sub(x+1,#z)
  593.     d = true
  594.   end
  595.   mon.write(z)
  596.   mon.setCursorPos(1,d and gety + 2 or gety + 1)
  597. end
  598.  
  599. local function loadMessage()
  600.   mon.clear()
  601.   mon.setCursorPos(1,1)
  602.   if fs.exists("message") then
  603.     local file = fs.open("message","r")
  604.     local fileData = {}
  605.     local line = file.readLine()
  606.     while line ~= nil do
  607.       table.insert(fileData,line)
  608.       line = file.readLine()
  609.     end  
  610.     file.close()
  611.     for a = 1, #fileData do
  612.       monWrite(fileData[a])
  613.     end
  614.   end
  615. end
  616.  
  617. while true do
  618.   loadMessage()
  619.   main()
  620. end
Advertisement
Add Comment
Please, Sign In to add comment