Advertisement
D_Puppy

Security system user database for oc and opensecurity

Jul 22nd, 2016
4,984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.20 KB | None | 0 0
  1. local cryptKey = {1, 2, 3, 4, 5}
  2. local modemPort = 199
  3.  
  4.  
  5. local component = require("component")
  6. local gpu = component.gpu
  7. local gui = require("gui")
  8. local event = require("event")
  9. local ser = require("serialization")
  10. writer = component.os_cardwriter
  11.  
  12. local myGui, cardStatusLabel, userList, userNameText, userLevelLabel, LevelUpButton, LevelDownButton
  13. local cardBlockedYesButton, cardBlockedNoButton, userNewButton, userDeleteButton
  14.  
  15. local prgName = "Access System"
  16. local version = "v0.1a"
  17.  
  18. local modem = component.modem
  19.  
  20. local function convert( chars, dist, inv )
  21.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  22. end
  23.  
  24.  
  25. local function crypt(str,k,inv)
  26.   local enc= "";
  27.   for i=1,#str do
  28.     if(#str-k[5] >= i or not inv)then
  29.       for inc=0,3 do
  30.     if(i%4 == inc)then
  31.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  32.       break;
  33.     end
  34.       end
  35.     end
  36.   end
  37.   if(not inv)then
  38.     for i=1,k[5] do
  39.       enc = enc .. string.char(math.random(32,126));
  40.     end
  41.   end
  42.   return enc;
  43. end
  44.  
  45. --// exportstring( string )
  46. --// returns a "Lua" portable version of the string
  47. local function exportstring( s )
  48.     s = string.format( "%q",s )
  49.     -- to replace
  50.     s = string.gsub( s,"\\\n","\\n" )
  51.     s = string.gsub( s,"\r","\\r" )
  52.     s = string.gsub( s,string.char(26),"\"..string.char(26)..\"" )
  53.     return s
  54. end
  55. --// The Save Function
  56. function saveTable(  tbl,filename )
  57.     local charS,charE = "   ","\n"
  58.     local file,err
  59.     -- create a pseudo file that writes to a string and return the string
  60.     if not filename then
  61.         file =  { write = function( self,newstr ) self.str = self.str..newstr end, str = "" }
  62.         charS,charE = "",""
  63.     -- write table to tmpfile
  64.     elseif filename == true or filename == 1 then
  65.         charS,charE,file = "","",io.tmpfile()
  66.     -- write table to file
  67.     -- use io.open here rather than io.output, since in windows when clicking on a file opened with io.output will create an error
  68.     else
  69.         file,err = io.open( filename, "w" )
  70.         if err then return _,err end
  71.     end
  72.     -- initiate variables for save procedure
  73.     local tables,lookup = { tbl },{ [tbl] = 1 }
  74.     file:write( "return {"..charE )
  75.     for idx,t in ipairs( tables ) do
  76.         if filename and filename ~= true and filename ~= 1 then
  77.             file:write( "-- Table: {"..idx.."}"..charE )
  78.         end
  79.         file:write( "{"..charE )
  80.         local thandled = {}
  81.         for i,v in ipairs( t ) do
  82.             thandled[i] = true
  83.             -- escape functions and userdata
  84.             if type( v ) ~= "userdata" then
  85.                 -- only handle value
  86.                 if type( v ) == "table" then
  87.                     if not lookup[v] then
  88.                         table.insert( tables, v )
  89.                         lookup[v] = #tables
  90.                     end
  91.                     file:write( charS.."{"..lookup[v].."},"..charE )
  92.                 elseif type( v ) == "function" then
  93.                     file:write( charS.."loadstring("..exportstring(string.dump( v )).."),"..charE )
  94.                 else
  95.                     local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  96.                     file:write(  charS..value..","..charE )
  97.                 end
  98.             end
  99.         end
  100.         for i,v in pairs( t ) do
  101.             -- escape functions and userdata
  102.             if (not thandled[i]) and type( v ) ~= "userdata" then
  103.                 -- handle index
  104.                 if type( i ) == "table" then
  105.                     if not lookup[i] then
  106.                         table.insert( tables,i )
  107.                         lookup[i] = #tables
  108.                     end
  109.                     file:write( charS.."[{"..lookup[i].."}]=" )
  110.                 else
  111.                     local index = ( type( i ) == "string" and "["..exportstring( i ).."]" ) or string.format( "[%d]",i )
  112.                     file:write( charS..index.."=" )
  113.                 end
  114.                 -- handle value
  115.                 if type( v ) == "table" then
  116.                     if not lookup[v] then
  117.                         table.insert( tables,v )
  118.                         lookup[v] = #tables
  119.                     end
  120.                     file:write( "{"..lookup[v].."},"..charE )
  121.                 elseif type( v ) == "function" then
  122.                     file:write( "loadstring("..exportstring(string.dump( v )).."),"..charE )
  123.                 else
  124.                     local value =  ( type( v ) == "string" and exportstring( v ) ) or tostring( v )
  125.                     file:write( value..","..charE )
  126.                 end
  127.             end
  128.         end
  129.         file:write( "},"..charE )
  130.     end
  131.     file:write( "}" )
  132.     -- Return Values
  133.     -- return stringtable from string
  134.     if not filename then
  135.         -- set marker for stringtable
  136.         return file.str.."--|"
  137.     -- return stringttable from file
  138.     elseif filename == true or filename == 1 then
  139.         file:seek ( "set" )
  140.         -- no need to close file, it gets closed and removed automatically
  141.         -- set marker for stringtable
  142.         return file:read( "*a" ).."--|"
  143.     -- close file and return 1
  144.     else
  145.         file:close()
  146.         return 1
  147.     end
  148. end
  149.  
  150. --// The Load Function
  151. function loadTable( sfile )
  152.     local tables, err, _
  153.     -- catch marker for stringtable
  154.     if string.sub( sfile,-3,-1 ) == "--|" then
  155.         tables,err = loadstring( sfile )
  156.     else
  157.         tables,err = loadfile( sfile )
  158.     end
  159.     if err then return _,err
  160.     end
  161.     tables = tables()
  162.     for idx = 1,#tables do
  163.         local tolinkv,tolinki = {},{}
  164.         for i,v in pairs( tables[idx] ) do
  165.             if type( v ) == "table" and tables[v[1]] then
  166.                 table.insert( tolinkv,{ i,tables[v[1]] } )
  167.             end
  168.             if type( i ) == "table" and tables[i[1]] then
  169.                 table.insert( tolinki,{ i,tables[i[1]] } )
  170.             end
  171.         end
  172.         -- link values, first due to possible changes of indices
  173.         for _,v in ipairs( tolinkv ) do
  174.             tables[idx][v[1]] = v[2]
  175.         end
  176.         -- link indices
  177.         for _,v in ipairs( tolinki ) do
  178.             tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
  179.         end
  180.     end
  181.     return tables[1]
  182. end
  183.  
  184.  
  185.  
  186. local function convert( chars, dist, inv )
  187.   return string.char( ( string.byte( chars ) - 32 + ( inv and -dist or dist ) ) % 95 + 32 )
  188. end
  189.  
  190.  
  191. local function crypt(str,k,inv)
  192.   local enc= "";
  193.   for i=1,#str do
  194.     if(#str-k[5] >= i or not inv)then
  195.       for inc=0,3 do
  196.     if(i%4 == inc)then
  197.       enc = enc .. convert(string.sub(str,i,i),k[inc+1],inv);
  198.       break;
  199.     end
  200.       end
  201.     end
  202.   end
  203.   if(not inv)then
  204.     for i=1,k[5] do
  205.       enc = enc .. string.char(math.random(32,126));
  206.     end
  207.   end
  208.   return enc;
  209. end
  210.  
  211.  
  212. function buttonCallback(guiID, id)
  213.   local result = gui.getYesNo("", "Do you really want to exit?", "")
  214.   if result == true then
  215.     gui.exit()
  216.   end
  217. end
  218.  
  219. function eventCallback(ev, id)
  220.   if ev == "cardInsert" then
  221.     gui.setText(myGui, cardStatusLabel, "   Card present")
  222.   elseif ev == "cardRemove" then
  223.     gui.setText(myGui, cardStatusLabel, "     No card   ")
  224.   end
  225. end
  226.  
  227. function userListCallback(guiID, listID, selectedID, selectedText)
  228.   gui.setText(myGui, userNameText, userTable[selectedID].name)
  229.   gui.setText(myGui, userLevelLabel, tostring(userTable[selectedID].level))
  230.   if userTable[selectedID].blocked == true then
  231.     gui.setEnable(myGui, cardBlockedYesButton, false)
  232.     gui.setEnable(myGui, cardBlockedNoButton, true)
  233.   else
  234.     gui.setEnable(myGui, cardBlockedYesButton, true)
  235.     gui.setEnable(myGui, cardBlockedNoButton, false)
  236.   end
  237.   gui.setEnable(myGui, LevelUpButton, true)
  238.   gui.setEnable(myGui, LevelDownButton, true)
  239.   gui.setEnable(myGui, userNameText, true)
  240. end
  241.  
  242. function updateServer()
  243.   local data = ser.serialize(userTable)
  244.   local crypted = crypt(data, cryptKey)
  245.   if modem.isOpen(modemPort) == false then
  246.     modem.open(modemPort)
  247.   end
  248.   modem.broadcast(modemPort, "updateuser", crypted)
  249. end
  250.  
  251.  
  252. function updateList()
  253.   gui.clearList(myGui, userList)
  254.   for key,value in pairs(userTable) do
  255.     gui.insertList(myGui, userList, value.name)
  256.   end
  257.   saveTable(userTable, "userlist.txt")
  258.   updateServer()
  259. end
  260.  
  261. function blockUserCallback(guiID, id)
  262.   local selected = gui.getSelected(myGui, userList)
  263.   userTable[selected].blocked = true
  264.   updateList()
  265.   userListCallback(myGui, userList, selected)
  266. end
  267.  
  268. function unblockUserCallback(guiID, id)
  269.   local selected = gui.getSelected(myGui, userList)
  270.   userTable[selected].blocked = false
  271.   updateList()
  272.   userListCallback(myGui, userList, selected)
  273. end
  274.  
  275. function newUserCallback(guiID, id)
  276.   local tmpTable = {["name"] = "new", ["blocked"] = false, ["level"] = 1, ["date"] = os.date()}
  277.   table.insert(userTable, tmpTable)
  278.   updateList()
  279. end
  280.  
  281. function deleteUserCallback(guiID, id)
  282.   local selected = gui.getSelected(myGui, userList)
  283.   userTable[selected] = nil
  284.   updateList()
  285.   gui.setText(myGui, userNameText, "")
  286.   gui.setText(myGui, userLevelLabel, "")
  287.   gui.setEnable(myGui, cardBlockedYesButton, false)
  288.   gui.setEnable(myGui, cardBlockedNoButton, false)
  289.   gui.setEnable(myGui, LevelUpButton, false)
  290.   gui.setEnable(myGui, LevelDownButton, false)
  291.   gui.setEnable(myGui, userNameText, false)
  292. end
  293.  
  294. function writeCardCallback(guiID, id)
  295.   local selected = gui.getSelected(myGui, userList)
  296.   local data =  userTable[selected].date .. " " .. userTable[selected].name .. " " .. tostring(userTable[selected].level) .. " " .. tostring(userTable[selected].blocked)
  297.   local crypted = crypt(data, cryptKey)
  298.   writer.write(crypted, "SECURITY", false)
  299. end
  300.  
  301. function levelUpCallback(guiID, id)
  302.   local selected = gui.getSelected(myGui, userList)
  303.   if userTable[selected].level < 101 then
  304.     userTable[selected].level = userTable[selected].level + 1
  305.   end
  306.   updateList()
  307.   userListCallback(myGui, userList, selected)
  308. end
  309.  
  310. function levelDownCallback(guiID, id)
  311.   local selected = gui.getSelected(myGui, userList)
  312.   if userTable[selected].level > 1 then
  313.     userTable[selected].level = userTable[selected].level - 1
  314.   end
  315.   updateList()
  316.   userListCallback(myGui, userList, selected)
  317. end
  318.  
  319. function inputCallback(guiID, textID, text)
  320.   local selected = gui.getSelected(myGui, userList)
  321.   userTable[selected].name = text
  322.   updateList()
  323.   userListCallback(myGui, userList, selected)
  324. end
  325.  
  326. -- main gui setup
  327. myGui = gui.newGui(2, 2, 78, 23, true)
  328. button = gui.newButton(myGui, "center", 21, "exit", buttonCallback)
  329.  
  330. -- frame with user list
  331. gui.newFrame(myGui, 1, 1, 30, 18)
  332. userList = gui.newList(myGui, 2, 2, 28, 16, {}, userListCallback)
  333. userTable = loadTable("userlist.txt")
  334. if userTable == nil then
  335.   userTable = {}
  336. end
  337. updateList()
  338.  
  339. -- user infos
  340. gui.newLabel(myGui, 32, 6, "User name : ")
  341. gui.newLabel(myGui, 32, 8, "Level     : ")
  342. gui.newLabel(myGui, 32, 10, "Blocked   : [yes] / [no]")
  343. userNameText = gui.newText(myGui, 44, 6, 16, "", inputCallback)
  344. userLevelLabel = gui.newLabel(myGui, 44, 8, "")
  345. LevelUpButton = gui.newButton(myGui, 48, 8, "+", levelUpCallback)
  346. LevelDownButton = gui.newButton(myGui, 52, 8, "-", levelDownCallback)
  347. cardBlockedYesButton = gui.newButton(myGui, 44, 10, "yes", blockUserCallback)
  348. cardBlockedNoButton = gui.newButton(myGui, 52, 10, "no", unblockUserCallback)
  349. gui.setEnable(myGui, cardBlockedYesButton, false)
  350. gui.setEnable(myGui, cardBlockedNoButton, false)
  351. gui.setEnable(myGui, LevelUpButton, false)
  352. gui.setEnable(myGui, LevelDownButton, false)
  353. gui.setEnable(myGui, userNameText, false)
  354.  
  355. gui.newHLine(myGui, 32, 12, 43)
  356. userNewButton = gui.newButton(myGui, 2, 21, "new", newUserCallback)
  357. userDeleteButton = gui.newButton(myGui, 9, 21, "delete", deleteUserCallback)
  358.  
  359. -- frame with status of the writer
  360. gui.newFrame(myGui, 57, 1, 19, 3, "Writer status")
  361. cardStatusLabel = gui.newLabel(myGui, 58, 2, "     No card   ")
  362.  
  363. --updateServerButton = gui.newButton(myGui, 47, 21, "update server", updateServerCallback)
  364.  
  365. cardWriteButton = gui.newButton(myGui, 64, 21, "write card", writeCardCallback)
  366.  
  367.  
  368. gui.clearScreen()
  369. gui.setTop(prgName .. " " .. version)
  370.  
  371. event.listen("cardInsert", eventCallback)
  372. event.listen("cardRemove", eventCallback)
  373. while true do
  374.   gui.runGui(myGui)
  375. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement