Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- AUTH DATABASE INTERFACE
- local databasefile = "/disk/authdb"
- -- Database functions
- function savedb(dbtable, dbfile)
- local file = fs.open(dbfile, "w")
- file.write(textutils.serialize(dbtable))
- file.close()
- end
- function loaddb(dbfile)
- local file = fs.open(dbfile, "r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- function printdb()
- local dbtable = loaddb(databasefile)
- print("\n*** authdb entries ***")
- for key,value in pairs(dbtable) do
- print("Username: "..tostring(key).."\nPassword: "..tostring(value))
- print()
- end
- end
- function addentry(user, pass)
- local dbtable = loaddb(databasefile)
- dbtable[user] = pass
- savedb(dbtable, databasefile)
- end
- function deleteentry(user)
- local dbtable = loaddb(databasefile)
- dbtable[user] = nil
- savedb(dbtable, databasefile)
- end
- -- Main program loop
- while true do
- -- Clear the terminal
- term.clear()
- term.setCursorPos(1, 1)
- -- Display the main menu
- print("Welcome to the AUTHDB Interface!")
- print("[1] Print out all entries")
- print("[2] Add an entry")
- print("[3] Delete an entry")
- print("[4] Exit Program")
- write("> ")
- -- Get the menu selection
- local input = read()
- -- Print out all entries in the database
- if input == "1" then
- printdb()
- print("Press any key to continue...")
- read()
- -- Add an entry
- elseif input == "2" then
- print("\n*** add an entry ***")
- write("add username> ")
- local user = read()
- write("add password> ")
- local pass = read()
- addentry(user, pass)
- print("\nAdded entry. Press any key to continue...")
- read()
- -- Delete an entry
- elseif input == "3" then
- print("\n*** delete an entry ***")
- write("delete user> ")
- local user = read()
- deleteentry(user)
- print("\nDeleted entry. Press any key to continue...")
- read()
- -- Exit the program
- elseif input == "4" then
- break
- -- Unknown menu option was provided
- else
- print("\nInvalid character. Press any key to continue...")
- read()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement