tommy2805

Operatore porta treni con lista

Oct 11th, 2025 (edited)
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.25 KB | None | 0 0
  1. local allowedFile = "allowed_users"
  2.  
  3. -- Funzione XOR per criptare/decriptare
  4. local function xorCrypt(str, key)
  5.     local res = {}
  6.     for i = 1, #str do
  7.         local k = string.byte(key, ((i - 1) % #key) + 1)
  8.         local c = string.byte(str, i)
  9.         table.insert(res, string.char(bit.bxor(c, k)))
  10.     end
  11.     return table.concat(res)
  12. end
  13.  
  14. -- Carica utenti
  15. local function loadAllowed()
  16.     if not fs.exists(allowedFile) then return {} end
  17.     local f = fs.open(allowedFile, "r")
  18.     local enc = f.readAll()
  19.     f.close()
  20.     local dec = xorCrypt(enc, "passwordchiave")
  21.     local t = textutils.unserialize(dec)
  22.     return t or {}
  23. end
  24.  
  25. -- Salva utenti
  26. local function saveAllowed(list)
  27.     local data = textutils.serialize(list)
  28.     local enc = xorCrypt(data, "passwordchiave")
  29.     local f = fs.open(allowedFile, "w")
  30.     f.write(enc)
  31.     f.close()
  32. end
  33.  
  34. -- Password menu
  35. local function login()
  36.     write("Inserisci password operatore: ")
  37.     local pass = read("*")
  38.     return pass == "tommy2805"
  39. end
  40.  
  41. if not login() then
  42.     print("Accesso negato.")
  43.     return
  44. end
  45.  
  46. local allowed = loadAllowed()
  47.  
  48. while true do
  49.     print("\n=== MENU OPERATORE ===")
  50.     print("1. Mostra utenti consentiti")
  51.     print("2. Aggiungi utente")
  52.     print("3. Rimuovi utente")
  53.     print("4. Esci")
  54.     write("Scelta: ")
  55.     local c = read()
  56.  
  57.     if c == "1" then
  58.         print("\nUtenti autorizzati:")
  59.         for i, u in ipairs(allowed) do
  60.             print(i .. ". " .. u)
  61.         end
  62.  
  63.     elseif c == "2" then
  64.         write("Nome utente da aggiungere: ")
  65.         local newUser = read()
  66.         table.insert(allowed, newUser)
  67.         saveAllowed(allowed)
  68.         print("Utente aggiunto.")
  69.  
  70.     elseif c == "3" then
  71.         print("Utenti:")
  72.         for i, u in ipairs(allowed) do
  73.             print(i .. ". " .. u)
  74.         end
  75.         write("Numero da rimuovere: ")
  76.         local n = tonumber(read())
  77.         if n and allowed[n] then
  78.             table.remove(allowed, n)
  79.             saveAllowed(allowed)
  80.             print("Utente rimosso.")
  81.         else
  82.             print("Scelta non valida.")
  83.         end
  84.  
  85.     elseif c == "4" then
  86.         print("Uscita dal menu.")
  87.         break
  88.     else
  89.         print("Scelta non valida.")
  90.     end
  91. end
  92.  
Advertisement
Add Comment
Please, Sign In to add comment