cadergator10

MineOS database

Jun 18th, 2021 (edited)
207
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local GUI = require("GUI")
  2. local system = require("System")
  3. local cryptKey = {1, 2, 3, 4, 5}
  4. local departments = {"SD","ScD","MD","E&T","O5"}
  5. local modemPort = 199
  6.  
  7. local adminCard = "admincard"
  8.  
  9. local showUUIDWarn = true
  10.  
  11.  
  12. local component = require("component")
  13. local gpu = component.gpu
  14. local event = require("event")
  15. local ser = require("serialization")
  16. local uuid = require("uuid")
  17. local fs = require("Filesystem")
  18. local writer
  19.  
  20. ----------
  21.  
  22. local workspace, window, menu
  23. local cardStatusLabel, userList, userNameText, userLevelLabel, LevelUpButton, LevelDownButton, createAdminCardButton
  24. local cardBlockedYesButton, userNewButton, userDeleteButton, userChangeUUIDButton, MTFYesButton, listPageLabel, listUpButton, listDownButton
  25. local GOIYesButton, SecYesButton, userArmoryLabel, ArmoryUpButton, ArmoryDownButton, userUUIDLabel
  26. local userDepLabel, DepUpButton, DepDownButton, IntYesButton, StaffYesButton
  27.  
  28. ----------
  29.  
  30. local prgName = "Security database"
  31. local version = "v0.8"
  32.  
  33. local modem
  34.  
  35. local pageMult = 10
  36. local listPageNumber = 0
  37. local previousPage = 0
  38.  
  39. -----------
  40.  
  41. if component.isAvailable("os_cardwriter") then
  42.     writer = component.os_cardwriter
  43. else
  44.     GUI.alert("This requires an Open Security card writer to run")
  45.     return
  46. end
  47. if component.isAvailable("modem") then
  48.     modem = component.modem
  49. else
  50.     GUI.alert("This requires a modem to run")
  51.     return
  52. end
  53.  
  54. -----------
  55.  
  56. local function convert( chars, dist, inv )
  57.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  58. end
  59.  
  60.  
  61. local function crypt(str,k,inv)
  62.   local enc= "";
  63.   for i=1,#str do
  64.     if(#str-k[5] >= i or not inv)then
  65.       for inc=0,3 do
  66.     if(i%4 == inc)then
  67.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  68.       break;
  69.     end
  70.       end
  71.     end
  72.   end
  73.   if(not inv)then
  74.     for i=1,k[5] do
  75.       enc = enc .. string.char(math.random(32,126));
  76.     end
  77.   end
  78.   return enc;
  79. end
  80.  
  81. --// exportstring( string )
  82. --// returns a "Lua" portable version of the string
  83. local function exportstring( s )
  84.     s = string.format( "%q",s )
  85.     -- to replace
  86.     s = string.gsub( s,"\\\n","\\n" )
  87.     s = string.gsub( s,"\r","\\r" )
  88.     s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  89.     return s
  90. end
  91. --// The Save Function
  92. function saveTable(  tbl,filename )
  93.     local tableFile = fs.open(filename, "w")
  94.     tableFile:write(ser.serialize(tbl))
  95.     tableFile:close()
  96. end
  97.  
  98. --// The Load Function
  99. function loadTable( sfile )
  100.     local tableFile = fs.open(sfile, "r")
  101.     return ser.unserialize(tableFile:readAll())
  102. end
  103.  
  104.  
  105.  
  106. local function convert( chars, dist, inv )
  107.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  108. end
  109.  
  110.  
  111. local function crypt(str,k,inv)
  112.   local enc= "";
  113.   for i=1,#str do
  114.     if(#str-k[5] >= i or not inv)then
  115.       for inc=0,3 do
  116.     if(i%4 == inc)then
  117.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  118.       break;
  119.     end
  120.       end
  121.     end
  122.   end
  123.   if(not inv)then
  124.     for i=1,k[5] do
  125.       enc = enc .. string.char(math.random(32,126));
  126.     end
  127.   end
  128.   return enc;
  129. end
  130.  
  131. ----------Callbacks
  132. function updateServer()
  133.   local data = ser.serialize(userTable)
  134.   local crypted = crypt(data, cryptKey)
  135.   if modem.isOpen(modemPort) == false then
  136.     modem.open(modemPort)
  137.   end
  138.   modem.broadcast(modemPort, "updateuser", crypted)
  139. end
  140.  
  141. function updateList()
  142.   selectedId = userList.selectedItem
  143.   userList:remove()
  144.   userList = window:addChild(GUI.list(4, 4, 58, 34, 3, 0, 0xE1E1E1, 0x4B4B4B, 0xD2D2D2, 0x4B4B4B, 0x3366CC, 0xFFFFFF, false))
  145.   local temp = pageMult * listPageNumber
  146.   for i = temp + 1, temp + pageMult, 1 do
  147.   if (userTable[i] == nil) then
  148.  
  149.   else
  150.     userList:addItem(userTable[i].name).onTouch = userListCallback
  151.   end
  152.   end
  153.  
  154.   saveTable(userTable, "userlist.txt")
  155.   if (previousPage == listPageNumber) then
  156.   userList.selectedItem = selectedId
  157.   else
  158.   previousPage = listPageNumber
  159.   end
  160.   updateServer()
  161. end
  162.  
  163. function eventCallback(ev, id)
  164.   if ev == "cardInsert" then
  165.     cardStatusLabel.text = "   Card present"
  166.   elseif ev == "cardRemove" then
  167.     cardStatusLabel.text = "     No card   "
  168.   end
  169. end
  170.  
  171. function userListCallback()
  172.   selectedId = pageMult * listPageNumber + userList.selectedItem
  173.   userNameText.text = userTable[selectedId].name
  174.   userUUIDLabel.text = "UUID      : " .. userTable[selectedId].uuid
  175.   userLevelLabel.text = tostring(userTable[selectedId].level)
  176.   userArmoryLabel.text = tostring(userTable[selectedId].armory)
  177.   userDepLabel.text = departments[userTable[selectedId].department]
  178.   if userTable[selectedId].blocked == true then
  179.     cardBlockedYesButton.pressed = true
  180.   else
  181.     cardBlockedYesButton.pressed = false
  182.   end
  183.   cardBlockedYesButton.disabled = false
  184.   if userTable[selectedId].mtf == true then
  185.     MTFYesButton.pressed = true
  186.   else
  187.     MTFYesButton.pressed = false
  188.   end
  189.   MTFYesButton.disabled = false
  190.   if userTable[selectedId].goi == true then
  191.     GOIYesButton.pressed = true
  192.   else
  193.     GOIYesButton.pressed = false
  194.   end
  195.   GOIYesButton.disabled = false
  196.   if userTable[selectedId].sec == true then
  197.     SecYesButton.pressed = true
  198.   else
  199.     SecYesButton.pressed = false
  200.   end
  201.   SecYesButton.disabled = false
  202.   if userTable[selectedId].int == true then
  203.     IntYesButton.pressed = true
  204.   else
  205.     IntYesButton.pressed = false
  206.   end
  207.   IntYesButton.disabled = false
  208.   if userTable[selectedId].staff == true then
  209.     StaffYesButton.pressed = true
  210.   else
  211.     StaffYesButton.pressed = false
  212.   end
  213.   StaffYesButton.disabled = false
  214.  
  215.   listPageLabel.text = tostring(listPageNumber + 1)
  216.  
  217.   LevelUpButton.disabled = false
  218.   LevelDownButton.disabled = false
  219.   ArmoryUpButton.disabled = false
  220.   ArmoryDownButton.disabled = false
  221.   DepUpButton.disabled = false
  222.   DepDownButton.disabled = false
  223.   userNameText.disabled = false
  224. end
  225.  
  226. function mtfUserCallback()
  227.   local selected = pageMult * listPageNumber + userList.selectedItem
  228.   userTable[selected].mtf = MTFYesButton.pressed
  229.   updateList()
  230.   userListCallback()
  231. end
  232.  
  233. function goiUserCallback()
  234.   local selected = pageMult * listPageNumber + userList.selectedItem
  235.   userTable[selected].goi = GOIYesButton.pressed
  236.   updateList()
  237.   userListCallback()
  238. end
  239.  
  240. function secUserCallback()
  241.   local selected = pageMult * listPageNumber + userList.selectedItem
  242.   userTable[selected].sec = SecYesButton.pressed
  243.   updateList()
  244.   userListCallback()
  245. end
  246.  
  247. function intUserCallback()
  248.   local selected = pageMult * listPageNumber + userList.selectedItem
  249.   userTable[selected].int = IntYesButton.pressed
  250.   updateList()
  251.   userListCallback()
  252. end
  253.  
  254. function staffUserCallback()
  255.   local selected = pageMult * listPageNumber + userList.selectedItem
  256.   userTable[selected].staff = StaffYesButton.pressed
  257.   updateList()
  258.   userListCallback()
  259. end
  260.  
  261. function blockUserCallback()
  262.   local selected = pageMult * listPageNumber + userList.selectedItem
  263.   userTable[selected].blocked = cardBlockedYesButton.pressed
  264.   updateList()
  265.   userListCallback()
  266. end
  267.  
  268. function newUserCallback()
  269.   local tmpTable = {["name"] = "new", ["blocked"] = false, ["level"] = 1, ["date"] = os.date(), ["armory"] = 0, ["mtf"] = false, ["sec"] = false, ["int"] = false, ["staff"] = false, ["goi"] = false, ["department"] = 1, ["uuid"] = uuid.next()}
  270.   table.insert(userTable, tmpTable)
  271.   updateList()
  272. end
  273.  
  274. function deleteUserCallback()
  275.   local selected = pageMult * listPageNumber + userList.selectedItem
  276.   userTable[selected] = nil
  277.   updateList()
  278.   userNameText.text = ""
  279.   userLevelLabel.text = "#"
  280.   userArmoryLabel.text = "#"
  281.   userDepLabel.text = "NAN"
  282.   LevelUpButton.disabled = true
  283.   LevelDownButton.disabled = true
  284.   ArmoryUpButton.disabled = true
  285.   ArmoryDownButton.disabled = true
  286.   DepUpButton.disabled = true
  287.   DepDownButton.disabled = true
  288.   userNameText.disabled = true
  289.   MTFYesButton.disabled = true
  290.   GOIYesButton.disabled = true
  291.   SecYesButton.disabled = true
  292.   IntYesButton.disabled = true
  293.   StaffYesButton.disabled = true
  294. end
  295.  
  296. function changeUUID()
  297.     if showUUIDWarn == true then
  298.         showUUIDWarn = false
  299.         GUI.alert("This will reset this user's uuid, rendering all cards linked to it useless. Use this if a card gets stolen or in another emergency. Use at own risk.")
  300.     else
  301.         local selected = pageMult * listPageNumber + userList.selectedItem
  302.         userTable[selected].uuid = uuid.next()
  303.         updateList()
  304.         userListCallback()
  305.     end
  306. end
  307.  
  308. function writeCardCallback()
  309.   local selected = pageMult * listPageNumber + userList.selectedItem
  310.   local data =  userTable[selected].date .. " " .. userTable[selected].name .. " " .. userTable[selected].uuid
  311.   local crypted = crypt(data, cryptKey)
  312.   writer.write(crypted, userTable[selected].name .. "'s security pass", false, 0)
  313. end
  314.  
  315. function writeAdminCardCallback()
  316.   local data =  adminCard
  317.   local crypted = crypt(data, cryptKey)
  318.   writer.write(crypted, "ADMIN DIAGNOSTIC CARD", false, 14)
  319. end
  320.  
  321. function levelUpCallback()
  322.   local selected = pageMult * listPageNumber + userList.selectedItem
  323.   if userTable[selected].level < 101 then
  324.     userTable[selected].level = userTable[selected].level + 1
  325.   end
  326.   updateList()
  327.   userListCallback()
  328. end
  329.  
  330. function levelDownCallback()
  331.   local selected = pageMult * listPageNumber + userList.selectedItem
  332.   if userTable[selected].level > 1 then
  333.     userTable[selected].level = userTable[selected].level - 1
  334.   end
  335.   updateList()
  336.   userListCallback()
  337. end
  338.  
  339. function armorUpCallback()
  340.   local selected = pageMult * listPageNumber + userList.selectedItem
  341.   if userTable[selected].armory < 4 then
  342.     userTable[selected].armory = userTable[selected].armory + 1
  343.   end
  344.   updateList()
  345.   userListCallback()
  346. end
  347.  
  348. function armorDownCallback()
  349.   local selected = pageMult * listPageNumber + userList.selectedItem
  350.   if userTable[selected].armory > 0 then
  351.     userTable[selected].armory = userTable[selected].armory - 1
  352.   end
  353.   updateList()
  354.   userListCallback()
  355. end
  356.  
  357. function depUpCallback()
  358.   local selected = pageMult * listPageNumber + userList.selectedItem
  359.   if userTable[selected].department < 5 then
  360.     userTable[selected].department = userTable[selected].department + 1
  361.   end
  362.   updateList()
  363.   userListCallback()
  364. end
  365.  
  366. function depDownCallback()
  367.   local selected = pageMult * listPageNumber + userList.selectedItem
  368.   if userTable[selected].department > 1 then
  369.     userTable[selected].department = userTable[selected].department - 1
  370.   end
  371.   updateList()
  372.   userListCallback()
  373. end
  374.  
  375. function pageUpCallback()
  376.   if listPageNumber < #userTable/pageMult - 1 then
  377.     listPageNumber = listPageNumber + 1
  378.   end
  379.   updateList()
  380.   userListCallback()
  381. end
  382.  
  383. function pageDownCallback()
  384.   if listPageNumber > 0 then
  385.     listPageNumber = listPageNumber - 1
  386.   end
  387.   updateList()
  388.   userListCallback()
  389. end
  390.  
  391. function inputCallback()
  392.   local selected = pageMult * listPageNumber + userList.selectedItem
  393.   userTable[selected].name = userNameText.text
  394.   updateList()
  395.   userListCallback()
  396. end
  397.  
  398. ----------GUI SETUP
  399. GUI.alert("NOTICE!!! I updated how the server saves users, so you will have to delete the userlist before opening the application! It is in the root folder. If you already did this or this is the first time opening this, you are alright.")
  400. workspace, window, menu = system.addWindow(GUI.filledWindow(2,2,150,45,0xE1E1E1))
  401.  
  402. local layout = window:addChild(GUI.layout(1, 1, window.width, window.height, 1, 1))
  403.  
  404. local contextMenu = menu:addContextMenuItem("File")
  405. contextMenu:addItem("Close").onTouch = function()
  406. window:remove()
  407. end
  408.  
  409. window:addChild(GUI.panel(3,3,60,36,0x6B6E74))
  410. userList = window:addChild(GUI.list(4, 4, 58, 34, 3, 0, 0xE1E1E1, 0x4B4B4B, 0xD2D2D2, 0x4B4B4B, 0x3366CC, 0xFFFFFF, false))
  411. userList:addItem("HELLO")
  412. listPageNumber = 0
  413. userTable = loadTable("userlist.txt")
  414. if userTable == nil then
  415.   userTable = {}
  416. end
  417. updateList()
  418.  
  419. --user infos
  420. window:addChild(GUI.label(64,12,3,3,0x165FF2,"User name : "))
  421. userUUIDLabel = window:addChild(GUI.label(64,14,3,3,0x165FF2,"UUID      : user not selected"))
  422. window:addChild(GUI.label(64,16,3,3,0x165FF2,"Level     : "))
  423. window:addChild(GUI.label(64,18,3,3,0x165FF2,"MTF       : "))
  424. window:addChild(GUI.label(64,20,3,3,0x165FF2,"GOI       : "))
  425. window:addChild(GUI.label(64,22,3,3,0x165FF2,"Security  : "))
  426. window:addChild(GUI.label(64,24,3,3,0x165FF2,"Intercom  : "))
  427. window:addChild(GUI.label(64,26,3,3,0x165FF2,"STAFF     : "))
  428. window:addChild(GUI.label(64,28,3,3,0x165FF2,"ArmorLevel: "))
  429. window:addChild(GUI.label(64,30,3,3,0x165FF2,"Department: "))
  430. window:addChild(GUI.label(64,32,3,3,0x165FF2,"Blocked   : "))
  431.  
  432. listPageLabel = window:addChild(GUI.label(4,38,3,3,0x165FF2,tostring(listPageNumber + 1)))
  433. listUpButton = window:addChild(GUI.button(8,38,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "+"))
  434. listUpButton.onTouch = pageUpCallback
  435. listDownButton = window:addChild(GUI.button(12,38,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "-"))
  436. listDownButton.onTouch = pageDownCallback
  437.  
  438. userNameText = window:addChild(GUI.input(88,12,16,1, 0xEEEEEE, 0x555555, 0x999999, 0xFFFFFF, 0x2D2D2D, "", "input name"))
  439. userNameText.onInputFinished = inputCallback
  440. userLevelLabel = window:addChild(GUI.label(88,16,3,3,0x165FF2,"#"))
  441. LevelUpButton = window:addChild(GUI.button(92,16,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "+"))
  442. LevelUpButton.onTouch = levelUpCallback
  443. LevelDownButton = window:addChild(GUI.button(96,16,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "-"))
  444. LevelDownButton.onTouch = levelDownCallback
  445. MTFYesButton = window:addChild(GUI.button(88,18,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
  446. MTFYesButton.switchMode = true
  447. MTFYesButton.onTouch = mtfUserCallback
  448. GOIYesButton = window:addChild(GUI.button(88,20,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
  449. GOIYesButton.switchMode = true
  450. GOIYesButton.onTouch = goiUserCallback
  451. SecYesButton = window:addChild(GUI.button(88,22,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
  452. SecYesButton.switchMode = true
  453. SecYesButton.onTouch = secUserCallback
  454. IntYesButton = window:addChild(GUI.button(88,24,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
  455. IntYesButton.switchMode = true
  456. IntYesButton.onTouch = intUserCallback
  457. StaffYesButton = window:addChild(GUI.button(88,26,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
  458. StaffYesButton.switchMode = true
  459. StaffYesButton.onTouch = staffUserCallback
  460. userArmoryLabel= window:addChild(GUI.label(88,28,3,3,0x165FF2,"#"))
  461. ArmoryUpButton = window:addChild(GUI.button(92,28,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "+"))
  462. ArmoryUpButton.onTouch = armorUpCallback
  463. ArmoryDownButton= window:addChild(GUI.button(96,28,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "-"))
  464. ArmoryDownButton.onTouch = armorDownCallback
  465. userDepLabel = window:addChild(GUI.label(88,30,3,3,0x165FF2,"NAN"))
  466. DepUpButton = window:addChild(GUI.button(92,30,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "+"))
  467. DepUpButton.onTouch = depUpCallback
  468. DepDownButton = window:addChild(GUI.button(96,30,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "-"))
  469. DepDownButton.onTouch = depDownCallback
  470. cardBlockedYesButton = window:addChild(GUI.button(88,32,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
  471. cardBlockedYesButton.switchMode = true
  472. cardBlockedYesButton.onTouch = blockUserCallback
  473.  
  474. LevelUpButton.disabled = true
  475. LevelDownButton.disabled = true
  476. ArmoryUpButton.disabled = true
  477. ArmoryDownButton.disabled = true
  478. DepUpButton.disabled = true
  479. DepDownButton.disabled = true
  480. userNameText.disabled = true
  481. MTFYesButton.disabled = true
  482. GOIYesButton.disabled = true
  483. SecYesButton.disabled = true
  484. IntYesButton.disabled = true
  485. StaffYesButton.disabled = true
  486. cardBlockedYesButton.disabled = true
  487.  
  488. --Line and user buttons
  489.  
  490. window:addChild(GUI.panel(64,36,86,1,0x6B6E74))
  491. userNewButton = window:addChild(GUI.button(4,42,16,1,0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "new"))
  492. userNewButton.onTouch = newUserCallback
  493. userDeleteButton = window:addChild(GUI.button(18,42,16,1,0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "delete"))
  494. userDeleteButton.onTouch = deleteUserCallback
  495. userChangeUUIDButton = window:addChild(GUI.button(32,42,16,1,0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "reset uuid"))
  496. userChangeUUIDButton.onTouch = changeUUID
  497. createAdminCardButton = window:addChild(GUI.button(46,42,16,1,0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "admin card"))
  498. createAdminCardButton.onTouch = writeAdminCardCallback
  499.  
  500. --CardWriter frame
  501.  
  502. window:addChild(GUI.panel(114, 2, 38, 6, 0x6B6E74))
  503. cardStatusLabel = window:addChild(GUI.label(116, 4, 3,3,0x165FF2,"     No card   "))
  504.  
  505. --write card button
  506. cardWriteButton = window:addChild(GUI.button(128,42,16,1,0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "write"))
  507. cardWriteButton.onTouch = writeCardCallback
  508.  
  509. event.addHandler(eventCallback)
  510.  
  511. workspace:draw()
  512. workspace:start()
  513.  
RAW Paste Data