Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local GUI = require("GUI")
- local system = require("System")
- local cryptKey = {1, 2, 3, 4, 5}
- local departments = {"SD","ScD","MD","E&T","O5"}
- local modemPort = 199
- local component = require("component")
- local gpu = component.gpu
- local event = require("event")
- local ser = require("serialization")
- local fs = require("Filesystem")
- writer = component.os_cardwriter
- ----------
- local cardStatusLabel, userList, userNameText, userLevelLabel, LevelUpButton, LevelDownButton
- local cardBlockedYesButton, cardBlockedNoButton, userNewButton, userDeleteButton, MTFYesButton, MTFNoButton
- local GOIYesButton, GOINoButton, SecYesButton, SecNoButton, userArmoryLabel, ArmoryUpButton, ArmoryDownButton
- local userDepLabel, DepUpButton, DepDownButton, IntYesButton, IntNoButton, StaffYesButton, StaffNoButton
- ----------
- local prgName = "Security database"
- local version = "v0.6"
- local modem = component.modem
- -----------
- local function convert( chars, dist, inv )
- return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
- end
- local function crypt(str,k,inv)
- local enc= "";
- for i=1,#str do
- if(#str-k[5] >= i or not inv)then
- for inc=0,3 do
- if(i%4 == inc)then
- enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
- break;
- end
- end
- end
- end
- if(not inv)then
- for i=1,k[5] do
- enc = enc .. string.char(math.random(32,126));
- end
- end
- return enc;
- end
- --// exportstring( string )
- --// returns a "Lua" portable version of the string
- local function exportstring( s )
- s = string.format( "%q",s )
- -- to replace
- s = string.gsub( s,"\\\n","\\n" )
- s = string.gsub( s,"\r","\\r" )
- s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
- return s
- end
- --// The Save Function
- function saveTable( tbl,filename )
- local charS,charE = " ","\n"
- local file,err
- -- create a pseudo file that writes to a string and return the string
- if not filename then
- file = { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
- charS,charE = "",""
- -- write table to tmpfile
- elseif filename == true or filename == 1 then
- charS,charE,file = "","",io.tmpfile()
- -- write table to file
- -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
- else
- file,err = fs.open(filename, "w")
- --file,err = io.open( filename, "w" )
- if err then return _,err end
- end
- -- initiate variables for save procedure
- local tables,lookup = { tbl },{ [tbl] = 1 }
- file:write( "return {"..charE )
- for idx,t in ipairs( tables ) do
- if filename and filename ~= true and filename ~= 1 then
- file:write( "-- Table: {"..idx.."}"..charE )
- end
- file:write( "{"..charE )
- local thandled = {}
- for i,v in ipairs( t ) do
- thandled[i] = true
- -- escape functions and userdata
- if type( v ) ~= "userdata" then
- -- only handle value
- if type( v ) == "table" then
- if not lookup[v] then
- table.insert( tables, v )
- lookup[v] = #tables
- end
- file:write( charS.."{"..lookup[v].."},"..charE )
- elseif type( v ) == "function" then
- file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
- else
- local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
- file:write( charS..value..","..charE )
- end
- end
- end
- for i,v in pairs( t ) do
- -- escape functions and userdata
- if (not thandled[i]) and type( v ) ~= "userdata" then
- -- handle index
- if type( i ) == "table" then
- if not lookup[i] then
- table.insert( tables,i )
- lookup[i] = #tables
- end
- file:write( charS.."[{"..lookup[i].."}]=" )
- else
- local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
- file:write( charS..index.."=" )
- end
- -- handle value
- if type( v ) == "table" then
- if not lookup[v] then
- table.insert( tables,v )
- lookup[v] = #tables
- end
- file:write( "{"..lookup[v].."},"..charE )
- elseif type( v ) == "function" then
- file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
- else
- local value = ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
- file:write( value..","..charE )
- end
- end
- end
- file:write( "},"..charE )
- end
- file:write( "}" )
- -- Return Values
- -- return stringtable from string
- if not filename then
- -- set marker for stringtable
- return file.str.."--|"
- -- return stringttable from file
- elseif filename == true or filename == 1 then
- file:seek ( "set" )
- -- no need to close file, it gets closed and removed automatically
- -- set marker for stringtable
- return file:read( "*a" ).."--|"
- -- close file and return 1
- else
- file:close()
- return 1
- end
- end
- --// The Load Function
- function loadTable( sfile )
- local tables, err, _
- -- catch marker for stringtable
- if string.sub( sfile,-3,-1 ) == "--|" then
- tables,err = loadstring( sfile )
- else
- tables,err = loadfile( sfile )
- end
- if err then return _,err
- end
- tables = tables()
- for idx = 1,#tables do
- local tolinkv,tolinki = {},{}
- for i,v in pairs( tables[idx] ) do
- if type( v ) == "table" and tables[v[1]] then
- table.insert( tolinkv,{ i,tables[v[1]] } )
- end
- if type( i ) == "table" and tables[i[1]] then
- table.insert( tolinki,{ i,tables[i[1]] } )
- end
- end
- -- link values, first due to possible changes of indices
- for _,v in ipairs( tolinkv ) do
- tables[idx][v[1]] = v[2]
- end
- -- link indices
- for _,v in ipairs( tolinki ) do
- tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
- end
- end
- return tables[1]
- end
- local function convert( chars, dist, inv )
- return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
- end
- local function crypt(str,k,inv)
- local enc= "";
- for i=1,#str do
- if(#str-k[5] >= i or not inv)then
- for inc=0,3 do
- if(i%4 == inc)then
- enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
- break;
- end
- end
- end
- end
- if(not inv)then
- for i=1,k[5] do
- enc = enc .. string.char(math.random(32,126));
- end
- end
- return enc;
- end
- ----------Callbacks
- function updateServer()
- local data = ser.serialize(userTable)
- local crypted = crypt(data, cryptKey)
- if modem.isOpen(modemPort) == false then
- modem.open(modemPort)
- end
- modem.broadcast(modemPort, "updateuser", crypted)
- end
- function updateList()
- userList:remove()
- for key,value in pairs(userTable) do
- userList.addItem(value.name)
- end
- saveTable(userTable, "userlist.txt")
- updateServer()
- end
- function eventCallback(ev, id)
- if ev == "cardInsert" then
- cardStatusLabel.text = " Card present"
- elseif ev == "cardRemove" then
- cardStatusLabel.text = " No card "
- end
- end
- function writeCardCallback()
- local selected = userList.selectedItem
- local data = userTable[selected].date .. " " .. userTable[selected].name .. " " .. tostring(userTable[selected].level) .. " " .. tostring(userTable[selected].blocked)
- local crypted = crypt(data, cryptKey)
- writer.write(crypted, userTable[selected].name .. "'s security pass", false, 8)
- end
- function userListCallback()
- selectedId = userList.selectedItem
- userNameText.text = userTable[selectedId].name
- userLevelLabel.text = toString(userTable[selectedId].level)
- userArmoryLabel.text = toString(userTable[selectedId].armory)
- userDepLabel.text = departments[userTable[selectedId].department]
- if userTable[selectedID].blocked == true then
- cardBlockedYesButton.pressed = true
- else
- cardBlockedYesButton.pressed = false
- end
- if userTable[selectedID].mtf == true then
- MTFYesButton.pressed = true
- else
- MTFYesButton.pressed = false
- end
- if userTable[selectedID].goi == true then
- GOIYesButton.pressed = true
- else
- GOIYesButton.pressed = false
- end
- if userTable[selectedID].sec == true then
- SecYesButton.pressed = true
- else
- SecYesButton.pressed = false
- end
- if userTable[selectedID].int == true then
- IntYesButton.pressed = true
- else
- IntYesButton.pressed = false
- end
- if userTable[selectedID].staff == true then
- StaffYesButton.pressed = true
- else
- StaffYesButton.pressed = false
- end
- end
- function inputCallback()
- local selected = userList.selectedItem
- userTable[selected].name = userNameText.text
- updateList()
- userListCallback()
- end
- ----------GUI SETUP
- local workspace, window, menu = system.addWindow(GUI.filledWindow(2,2,150,45,0xE1E1E1))
- local layout = window:addChild(GUI.layout(1, 1, window.width, window.height, 1, 1))
- local contextMenu = menu:addContextMenuItem("File")
- contextMenu:addItem("Close").onTouch = function()
- window:remove()
- end
- window:addChild(GUI.panel(3,3,60,36,0x6B6E74))
- userList = window:addChild(GUI.list(4, 4, 58, 34, 3, 0, 0xE1E1E1, 0x4B4B4B, 0xD2D2D2, 0x4B4B4B, 0x3366CC, 0xFFFFFF, false))
- userTable = loadTable("userlist.txt")
- if userTable == nil then
- userTable = {}
- end
- updateList()
- --user infos
- window:addChild(GUI.label(64,12,3,3,0x165FF2,"User name : "))
- window:addChild(GUI.label(64,14,3,3,0x165FF2,"Level : "))
- window:addChild(GUI.label(64,16,3,3,0x165FF2,"MTF : "))
- window:addChild(GUI.label(64,18,3,3,0x165FF2,"GOI : "))
- window:addChild(GUI.label(64,20,3,3,0x165FF2,"Security : "))
- window:addChild(GUI.label(64,22,3,3,0x165FF2,"Intercom : "))
- window:addChild(GUI.label(64,24,3,3,0x165FF2,"STAFF : "))
- window:addChild(GUI.label(64,26,3,3,0x165FF2,"ArmorLevel: "))
- window:addChild(GUI.label(64,28,3,3,0x165FF2,"Department: "))
- window:addChild(GUI.label(64,30,3,3,0x165FF2,"Blocked : "))
- userNameText = window:addChild(GUI.input(88,12,16,1, 0xEEEEEE, 0x555555, 0x999999, 0xFFFFFF, 0x2D2D2D, "", "input name"))
- --userNameText.onInputFinished = inputCallback()
- userLevelLabel = window:addChild(GUI.label(88,14,3,3,0x165FF2,"#"))
- LevelUpButton = window:addChild(GUI.button(92,14,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "+"))
- --LevelUpButton.onTouch =
- LevelDownButton = window:addChild(GUI.button(96,14,3,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "-"))
- --LevelDownButton.onTouch =
- MTFYesButton = window:addChild(GUI.button(88,16,16,1, 0xFFFFFF, 0x555555, 0x880000, 0xFFFFFF, "toggle"))
- MTFYesButton.switchMode = true
- --MTFYesButton.onTouch =
- --CardWriter frame
- window:addChild(GUI.panel(114, 2, 38, 6, 0x6B6E74))
- cardStatusLabel = window:addChild(GUI.label(116, 4, 3,3,0x165FF2," No card "))
- event.addHandler(eventCallback)
- workspace:draw()
- workspace:start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement