Advertisement
Guest User

Main.lua

a guest
Sep 1st, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.25 KB | None | 0 0
  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.  
  8. local component = require("component")
  9. local gpu = component.gpu
  10. local event = require("event")
  11. local ser = require("serialization")
  12. local fs = require("Filesystem")
  13. writer = component.os_cardwriter
  14.  
  15. ----------
  16.  
  17. local cardStatusLabel, userList, userNameText, userLevelLabel, LevelUpButton, LevelDownButton
  18. local cardBlockedYesButton, cardBlockedNoButton, userNewButton, userDeleteButton, MTFYesButton, MTFNoButton
  19. local GOIYesButton, GOINoButton, SecYesButton, SecNoButton, userArmoryLabel, ArmoryUpButton, ArmoryDownButton
  20. local userDepLabel, DepUpButton, DepDownButton, IntYesButton, IntNoButton, StaffYesButton, StaffNoButton
  21.  
  22. ----------
  23.  
  24. local prgName = "Security database"
  25. local version = "v0.6"
  26.  
  27. local modem = component.modem
  28.  
  29. -----------
  30.  
  31. local function convert( chars, dist, inv )
  32.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  33. end
  34.  
  35.  
  36. local function crypt(str,k,inv)
  37.   local enc= "";
  38.   for i=1,#str do
  39.     if(#str-k[5] >= i or not inv)then
  40.       for inc=0,3 do
  41.     if(i%4 == inc)then
  42.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  43.       break;
  44.     end
  45.       end
  46.     end
  47.   end
  48.   if(not inv)then
  49.     for i=1,k[5] do
  50.       enc = enc .. string.char(math.random(32,126));
  51.     end
  52.   end
  53.   return enc;
  54. end
  55.  
  56. --// exportstring( string )
  57. --// returns a "Lua" portable version of the string
  58. local function exportstring( s )
  59.     s = string.format( "%q",s )
  60.     -- to replace
  61.     s = string.gsub( s,"\\\n","\\n" )
  62.     s = string.gsub( s,"\r","\\r" )
  63.     s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  64.     return s
  65. end
  66. --// The Save Function
  67. function saveTable(  tbl,filename )
  68.     local charS,charE = "   ","\n"
  69.     local file,err
  70.     -- create a pseudo file that writes to a string and return the string
  71.     if not filename then
  72.         file =  { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
  73.         charS,charE = "",""
  74.     -- write table to tmpfile
  75.     elseif filename == true or filename == 1 then
  76.         charS,charE,file = "","",io.tmpfile()
  77.     -- write table to file
  78.     -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
  79.     else
  80.         file,err = fs.open(filename, "w")
  81.         --file,err = io.open( filename, "w" )
  82.         if err then return _,err end
  83.     end
  84.     -- initiate variables for save procedure
  85.     local tables,lookup = { tbl },{ [tbl] = 1 }
  86.     file:write( "return {"..charE )
  87.     for idx,t in ipairs( tables ) do
  88.         if filename and filename ~= true and filename ~= 1 then
  89.             file:write( "-- Table: {"..idx.."}"..charE )
  90.         end
  91.         file:write( "{"..charE )
  92.         local thandled = {}
  93.         for i,v in ipairs( t ) do
  94.             thandled[i] = true
  95.             -- escape functions and userdata
  96.             if type( v ) ~= "userdata" then
  97.                 -- only handle value
  98.                 if type( v ) == "table" then
  99.                     if not lookup[v] then
  100.                         table.insert( tables, v )
  101.                         lookup[v] = #tables
  102.                     end
  103.                     file:write( charS.."{"..lookup[v].."},"..charE )
  104.                 elseif type( v ) == "function" then
  105.                     file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
  106.                 else
  107.                     local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  108.                     file:write(  charS..value..","..charE )
  109.                 end
  110.             end
  111.         end
  112.         for i,v in pairs( t ) do
  113.             -- escape functions and userdata
  114.             if (not thandled[i]) and type( v ) ~= "userdata" then
  115.                 -- handle index
  116.                 if type( i ) == "table" then
  117.                     if not lookup[i] then
  118.                         table.insert( tables,i )
  119.                         lookup[i] = #tables
  120.                     end
  121.                     file:write( charS.."[{"..lookup[i].."}]=" )
  122.                 else
  123.                     local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
  124.                     file:write( charS..index.."=" )
  125.                 end
  126.                 -- handle value
  127.                 if type( v ) == "table" then
  128.                     if not lookup[v] then
  129.                         table.insert( tables,v )
  130.                         lookup[v] = #tables
  131.                     end
  132.                     file:write( "{"..lookup[v].."},"..charE )
  133.                 elseif type( v ) == "function" then
  134.                     file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
  135.                 else
  136.                     local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  137.                     file:write( value..","..charE )
  138.                 end
  139.             end
  140.         end
  141.         file:write( "},"..charE )
  142.     end
  143.     file:write( "}" )
  144.     -- Return Values
  145.     -- return stringtable from string
  146.     if not filename then
  147.         -- set marker for stringtable
  148.         return file.str.."--|"
  149.     -- return stringttable from file
  150.     elseif filename == true or filename == 1 then
  151.         file:seek ( "set" )
  152.         -- no need to close file, it gets closed and removed automatically
  153.         -- set marker for stringtable
  154.         return file:read( "*a" ).."--|"
  155.     -- close file and return 1
  156.     else
  157.         file:close()
  158.         return 1
  159.     end
  160. end
  161.  
  162. --// The Load Function
  163. function loadTable( sfile )
  164.     local tables, err, _
  165.     -- catch marker for stringtable
  166.     if string.sub( sfile,-3,-1 ) == "--|" then
  167.         tables,err = loadstring( sfile )
  168.     else
  169.         tables,err = loadfile( sfile )
  170.     end
  171.     if err then return _,err
  172.     end
  173.     tables = tables()
  174.     for idx = 1,#tables do
  175.         local tolinkv,tolinki = {},{}
  176.         for i,v in pairs( tables[idx] ) do
  177.             if type( v ) == "table" and tables[v[1]] then
  178.                 table.insert( tolinkv,{ i,tables[v[1]] } )
  179.             end
  180.             if type( i ) == "table" and tables[i[1]] then
  181.                 table.insert( tolinki,{ i,tables[i[1]] } )
  182.             end
  183.         end
  184.         -- link values, first due to possible changes of indices
  185.         for _,v in ipairs( tolinkv ) do
  186.             tables[idx][v[1]] = v[2]
  187.         end
  188.         -- link indices
  189.         for _,v in ipairs( tolinki ) do
  190.             tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
  191.         end
  192.     end
  193.     return tables[1]
  194. end
  195.  
  196.  
  197.  
  198. local function convert( chars, dist, inv )
  199.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  200. end
  201.  
  202.  
  203. local function crypt(str,k,inv)
  204.   local enc= "";
  205.   for i=1,#str do
  206.     if(#str-k[5] >= i or not inv)then
  207.       for inc=0,3 do
  208.     if(i%4 == inc)then
  209.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  210.       break;
  211.     end
  212.       end
  213.     end
  214.   end
  215.   if(not inv)then
  216.     for i=1,k[5] do
  217.       enc = enc .. string.char(math.random(32,126));
  218.     end
  219.   end
  220.   return enc;
  221. end
  222.  
  223. ----------Callbacks
  224. function updateServer()
  225.   local data = ser.serialize(userTable)
  226.   local crypted = crypt(data, cryptKey)
  227.   if modem.isOpen(modemPort) == false then
  228.     modem.open(modemPort)
  229.   end
  230.   modem.broadcast(modemPort, "updateuser", crypted)
  231. end
  232.  
  233. function updateList()
  234.   userList:remove()
  235.   for key,value in pairs(userTable) do
  236.     userList.addItem(value.name)
  237.   end
  238.   saveTable(userTable, "userlist.txt")
  239.   updateServer()
  240. end
  241.  
  242. function eventCallback(ev, id)
  243.   if ev == "cardInsert" then
  244.     cardStatusLabel.text = "   Card present"
  245.   elseif ev == "cardRemove" then
  246.     cardStatusLabel.text = "     No card   "
  247.   end
  248. end
  249.  
  250. function writeCardCallback()
  251.   local selected = userList.selectedItem
  252.   local data =  userTable[selected].date .. " " .. userTable[selected].name .. " " .. tostring(userTable[selected].level) .. " " .. tostring(userTable[selected].blocked)
  253.   local crypted = crypt(data, cryptKey)
  254.   writer.write(crypted, userTable[selected].name .. "'s security pass", false, 8)
  255. end
  256.  
  257. function userListCallback()
  258.   selectedId = userList.selectedItem
  259.   userNameText.text = userTable[selectedId].name
  260.   userLevelLabel.text = toString(userTable[selectedId].level)
  261.   userArmoryLabel.text = toString(userTable[selectedId].armory)
  262.   userDepLabel.text = departments[userTable[selectedId].department]
  263.   if userTable[selectedID].blocked == true then
  264.     cardBlockedYesButton.pressed = true
  265.   else
  266.     cardBlockedYesButton.pressed = false
  267.   end
  268.   if userTable[selectedID].mtf == true then
  269.     MTFYesButton.pressed = true
  270.   else
  271.     MTFYesButton.pressed = false
  272.   end
  273.   if userTable[selectedID].goi == true then
  274.     GOIYesButton.pressed = true
  275.   else
  276.     GOIYesButton.pressed = false
  277.   end
  278.   if userTable[selectedID].sec == true then
  279.     SecYesButton.pressed = true
  280.   else
  281.     SecYesButton.pressed = false
  282.   end
  283.   if userTable[selectedID].int == true then
  284.     IntYesButton.pressed = true
  285.   else
  286.     IntYesButton.pressed = false
  287.   end
  288.   if userTable[selectedID].staff == true then
  289.     StaffYesButton.pressed = true
  290.   else
  291.     StaffYesButton.pressed = false
  292.   end
  293. end
  294.  
  295. function inputCallback()
  296.   local selected = userList.selectedItem
  297.   userTable[selected].name = userNameText.text
  298.   updateList()
  299.   userListCallback()
  300. end
  301.  
  302. ----------GUI SETUP
  303.  
  304. local workspace, window, menu = system.addWindow(GUI.filledWindow(2,2,150,45,0xE1E1E1))
  305.  
  306. local layout = window:addChild(GUI.layout(1, 1, window.width, window.height, 1, 1))
  307.  
  308. local contextMenu = menu:addContextMenuItem("File")
  309. contextMenu:addItem("Close").onTouch = function()
  310. window:remove()
  311. end
  312.  
  313. window:addChild(GUI.panel(3,3,60,36,0x6B6E74))
  314. userList = window:addChild(GUI.list(4, 4, 58, 34, 3, 0, 0xE1E1E1, 0x4B4B4B, 0xD2D2D2, 0x4B4B4B, 0x3366CC, 0xFFFFFF, false))
  315. userTable = loadTable("userlist.txt")
  316. if userTable == nil then
  317.   userTable = {}
  318. end
  319. updateList()
  320.  
  321. --user infos
  322. window:addChild(GUI.label(64,12,3,3,0x165FF2,"User name : "))
  323. window:addChild(GUI.label(64,14,3,3,0x165FF2,"Level     : "))
  324. window:addChild(GUI.label(64,16,3,3,0x165FF2,"MTF       : "))
  325. window:addChild(GUI.label(64,18,3,3,0x165FF2,"GOI       : "))
  326. window:addChild(GUI.label(64,20,3,3,0x165FF2,"Security  : "))
  327. window:addChild(GUI.label(64,22,3,3,0x165FF2,"Intercom  : "))
  328. window:addChild(GUI.label(64,24,3,3,0x165FF2,"STAFF     : "))
  329. window:addChild(GUI.label(64,26,3,3,0x165FF2,"ArmorLevel: "))
  330. window:addChild(GUI.label(64,28,3,3,0x165FF2,"Department: "))
  331. window:addChild(GUI.label(64,30,3,3,0x165FF2,"Blocked   : "))
  332. userNameText = window:addChild(GUI.input(88,12,16,1, 0xEEEEEE, 0x555555, 0x999999, 0xFFFFFF, 0x2D2D2D, "", "input name"))
  333. --userNameText.onInputFinished = inputCallback()
  334. userLevelLabel = window:addChild(GUI.label(88,14,3,3,0x165FF2,"#"))
  335. LevelUpButton = window:addChild(GUI.button(92,14,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "+"))
  336. --LevelUpButton.onTouch =
  337. LevelDownButton = window:addChild(GUI.button(96,14,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "-"))
  338. --LevelDownButton.onTouch =
  339. MTFYesButton = window:addChild(GUI.button(88,16,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
  340. MTFYesButton.switchMode = true
  341. --MTFYesButton.onTouch =
  342.  
  343. --CardWriter frame
  344. window:addChild(GUI.panel(114, 2, 38, 6, 0x6B6E74))
  345. cardStatusLabel = window:addChild(GUI.label(116, 4, 3,3,0x165FF2,"     No card   "))
  346.  
  347. event.addHandler(eventCallback)
  348.  
  349. workspace:draw()
  350. workspace:start()
  351.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement