Advertisement
Guest User

authcmd

a guest
Dec 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. -- AUTH DATABASE INTERFACE
  2. local databasefile = "/disk/authdb"
  3.  
  4. -- Database functions
  5. function savedb(dbtable, dbfile)
  6.   local file = fs.open(dbfile, "w")
  7.   file.write(textutils.serialize(dbtable))
  8.   file.close()
  9. end
  10.  
  11. function loaddb(dbfile)
  12.   local file = fs.open(dbfile, "r")
  13.   local data = file.readAll()
  14.   file.close()
  15.   return textutils.unserialize(data)
  16. end
  17.  
  18. function printdb()
  19.   local dbtable = loaddb(databasefile)
  20.   print("\n*** authdb entries ***")
  21.   for key,value in pairs(dbtable) do
  22.     print("Username: "..tostring(key).."\nPassword: "..tostring(value))
  23.     print()
  24.   end    
  25. end
  26.  
  27. function addentry(user, pass)
  28.   local dbtable = loaddb(databasefile)
  29.   dbtable[user] = pass
  30.   savedb(dbtable, databasefile)
  31. end
  32.  
  33. function deleteentry(user)
  34.   local dbtable = loaddb(databasefile)
  35.   dbtable[user] = nil
  36.   savedb(dbtable, databasefile)
  37. end
  38.  
  39. -- Main program loop
  40. while true do
  41.   -- Clear the terminal
  42.   term.clear()
  43.   term.setCursorPos(1, 1)
  44.   -- Display the main menu
  45.   print("Welcome to the AUTHDB Interface!")
  46.   print("[1] Print out all entries")
  47.   print("[2] Add an entry")
  48.   print("[3] Delete an entry")
  49.   print("[4] Exit Program")
  50.   write("> ")
  51.   -- Get the menu selection
  52.   local input = read()
  53.   -- Print out all entries in the database
  54.   if input == "1" then
  55.     printdb()
  56.     print("Press any key to continue...")
  57.     read()
  58.   -- Add an entry
  59.   elseif input == "2" then
  60.     print("\n*** add an entry ***")
  61.     write("add username> ")
  62.     local user = read()
  63.     write("add password> ")
  64.     local pass = read()
  65.     addentry(user, pass)
  66.     print("\nAdded entry. Press any key to continue...")
  67.     read()
  68.   -- Delete an entry
  69.   elseif input == "3" then
  70.     print("\n*** delete an entry ***")
  71.     write("delete user> ")
  72.     local user = read()
  73.     deleteentry(user)
  74.     print("\nDeleted entry. Press any key to continue...")
  75.     read()
  76.   -- Exit the program
  77.   elseif input == "4" then
  78.     break
  79.   -- Unknown menu option was provided
  80.   else
  81.     print("\nInvalid character. Press any key to continue...")
  82.     read()
  83.   end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement