View difference between Paste ID: ePsdUXng and CL3Ff3zQ
SHOW: | | - or go back to the newest paste.
1-
-- Security #1
1+
-- Security #2
2-
-- Password lock
2+
-- Username and password lock
3
-- Code written by jag_e_nummer_ett
4
5
local oldPull = os.pullEvent;
6
os.pullEvent = os.pullEventRaw;
7
8
function printPos(stringMessage, x, y)
9
  local unused = 0
10
  if not stringMessage then
11
    error("Needs a string to function!")
12-
  while inputPass ~= password do
12+
  else
13
    if not x then
14
      x, unused = term.getCursorPos()
15-
      term.setCursorPos(3,5)
15+
16-
      print("Incorrect password!")
16+
    if not y then
17
      unused, y = term.getCursorPos()
18-
    term.setCursorPos(3,2)
18+
19-
    print("Please enter password")
19+
    term.setCursorPos(x,y)
20-
    term.setCursorPos(3,4)
20+
    print(stringMessage)
21-
    print("Tries left: "..tries)
21+
22-
    term.setCursorPos(3,3)
22+
23-
    write("Password: ")
23+
24-
    inputPass = read("*")
24+
function readPos(x, y, readChar)
25
  local unused = 0
26
  if not x then
27
    x, unused = term.getCursorPos()
28
  end
29
  if not y then
30
    unused, y = term.getCursorPos()
31
  end
32
  term.setCursorPos(x,y)
33
  if readChar then return read(readChar)
34
  else return read() end
35
end
36
37
function login()
38
  local username = "admin"
39
  local password = "password"
40
  local inputUser = ""
41
  local inputPass = ""
42
  local tries = 5
43
  while inputPass ~= password and inputUser ~= username do
44
    term.clear()
45
    if tries < 5 then
46
      term.setCursorPos(3,6)
47
      print("Incorrect username or password!")
48
    end
49
    printPos("Please enter login",3,2)
50
    printPos("Username: ",3,3)
51
    printPos("Password: ",3,4)
52
    printPos("Tries left: "..tries,3,5)
53
    inputUser = readPos(13,3)
54
    inputPass = readPos(13,4,"*")
55
    tries = tries - 1
56
    if tries == 0 then
57
      return false
58
    end
59
  end
60
  return true
61
end
62
63
if login() then
64
  term.clear()
65
  term.setCursorPos(3,2)
66
  print("Login successful!")
67
  sleep(1.5)
68
else
69
  term.clear()
70
  term.setCursorPos(3,2)
71
  print("Login failed!")
72
  sleep(1.5)
73
  os.shutdown()
74
end
75
76
os.pullEvent = oldPull;