added boot and locked out scripts

This commit is contained in:
Josh d'Entremont 2015-05-26 22:11:31 -03:00
parent c99e2c9f2b
commit 766ba8f7bc
6 changed files with 134 additions and 34 deletions

View file

@ -17,5 +17,6 @@ To use the login in another program:
``` ```
import fallout_login import fallout_login
# returns true if the correct password is entered, false otherwise
fallout_login.beginLogin() fallout_login.beginLogin()
``` ```

View file

@ -1,6 +1,11 @@
#! /usr/bin/env python #! /usr/bin/env python
import fallout_login as login import fallout_login as login
import fallout_boot as boot
import fallout_locked as locked
login.beginLogin() if boot.beginBoot(False):
if login.beginLogin():
print 'Login successful'
else:
locked.beginLocked()
print 'Login failed'

40
fallout_boot.py Normal file
View file

@ -0,0 +1,40 @@
import curses
from fallout_functions import slowWrite
######################## global 'constants' ##############
ENTRY_1 = 'set terminal/inquire'
######################## text strings ####################
MESSAGE_1 = 'WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK'
MESSAGE_2 = 'RIT-V300'
######################## functions #######################
def runBoot(scr):
"""
Start the boot portion of the terminal
"""
curses.use_default_colors()
# set screen to initial position
scr.erase()
scr.move(0, 0)
slowWrite(scr, MESSAGE_1 + '\n\n>')
curses.napms(500)
return True
def beginBoot(hardMode):
"""
Initialize curses and start the boot process
hardMode - boolean indicating whether the user has to enter the ENTRY
constants, or if they are entered automatically
Returns true if hardMode == false or if the user entered the correct string
"""
res = curses.wrapper(runBoot)
return res

12
fallout_functions.py Normal file
View file

@ -0,0 +1,12 @@
import curses
LETTER_PAUSE = 20
def slowWrite(window, text, pause = LETTER_PAUSE):
"""
wrapper for curses.addstr() which writes the text slowely
"""
for i in xrange(len(text)):
window.addstr(text[i])
window.refresh()
curses.napms(pause)

40
fallout_locked.py Normal file
View file

@ -0,0 +1,40 @@
import curses
from fallout_functions import slowWrite
################## text strings ######################
LOCKED_1 = 'TERMINAL LOCKED'
LOCKED_2 = 'PLEASE CONTACT AN ADMINISTRATOR'
################## global 'constants' ################
# amount of time to pause after lockout
LOCKED_OUT_TIME = 5000
################## functions #########################
def runLocked(scr):
"""
Start the locked out portion of the terminal
"""
curses.use_default_colors()
size = scr.getmaxyx()
width = size[1]
height = size[0]
# set screen to initial position
scr.erase()
scr.move(height / 2 - 1, width / 2 - len(LOCKED_1) / 2)
scr.addstr(LOCKED_1)
scr.move(height / 2 + 1, width / 2 - len(LOCKED_2) / 2)
scr.addstr(LOCKED_2)
curses.curs_set(0)
scr.refresh()
curses.napms(LOCKED_OUT_TIME)
def beginLocked():
"""
Initialize curses and start the locked out process
"""
curses.wrapper(runLocked)

View file

@ -3,11 +3,16 @@
import curses import curses
import random import random
import os import os
from fallout_functions import slowWrite
################## text strings ######################
HEADER_TEXT = 'ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL'
################## global "constants" ################ ################## global "constants" ################
# number of characters for hex digits and spaces # number of characters for hex digits and spaces
CONST_WIDTH = 16 CONST_CHARS = 16
# position of the attempt squares # position of the attempt squares
SQUARE_X = 19 SQUARE_X = 19
@ -18,9 +23,6 @@ LOGIN_ATTEMPTS = 4
# amount of time to pause after correct password input # amount of time to pause after correct password input
LOGIN_PAUSE = 3000 LOGIN_PAUSE = 3000
# amount of time to pause after lockout
LOCKED_OUT_TIME = 5000
# starting number for hex generation # starting number for hex generation
START_HEX = 0xf650 START_HEX = 0xf650
@ -100,18 +102,19 @@ def initScreen(scr):
Fill the screen to prepare for password entry Fill the screen to prepare for password entry
scr - curses window returned from curses.initscr() scr - curses window returned from curses.initscr()
""" """
size = scr.getmaxyx() size = scr.getmaxyx()
height = size[0]
width = size[1] width = size[1]
height = size[0] - 5 # - 5 for header lines fillerHeight = height - 5 # - 5 for header lines
hexes = generateHex(height * 2) hexes = generateHex(fillerHeight * 2)
hexCol1 = hexes[:height] hexCol1 = hexes[:fillerHeight]
hexCol2 = hexes[height:] hexCol2 = hexes[fillerHeight:]
# generate the symbols and passwords # generate the symbols and passwords
fillerLength = width / 2 * height fillerLength = width / 2 * fillerHeight
passwords = getPasswords() passwords = getPasswords()
filler = getFiller(fillerLength, passwords) filler = getFiller(fillerLength, passwords)
fillerCol1 = filler[:len(filler) / 2] fillerCol1 = filler[:len(filler) / 2]
@ -121,8 +124,8 @@ def initScreen(scr):
fillerWidth = width / 4 fillerWidth = width / 4
# print the header stuff # print the header stuff
scr.addstr('ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL\n') scr.addstr(HEADER_TEXT)
scr.addstr('ENTER PASSWORD NOW\n\n') scr.addstr('\nENTER PASSWORD NOW\n\n')
scr.addstr(str(LOGIN_ATTEMPTS) + ' ATTEMPT(S) LEFT: ') scr.addstr(str(LOGIN_ATTEMPTS) + ' ATTEMPT(S) LEFT: ')
for i in xrange(LOGIN_ATTEMPTS): for i in xrange(LOGIN_ATTEMPTS):
scr.addch(curses.ACS_BLOCK) scr.addch(curses.ACS_BLOCK)
@ -130,9 +133,9 @@ def initScreen(scr):
scr.addstr('\n\n') scr.addstr('\n\n')
# print the hex and filler # print the hex and filler
for i in xrange(height): for i in xrange(fillerHeight):
scr.addstr("0x%X %s 0x%X %s" % (hexCol1[i], fillerCol1[i * fillerWidth: (i + 1) * fillerWidth], hexCol2[i], fillerCol2[i * fillerWidth: (i + 1) * fillerWidth])) scr.addstr("0x%X %s 0x%X %s" % (hexCol1[i], fillerCol1[i * fillerWidth: (i + 1) * fillerWidth], hexCol2[i], fillerCol2[i * fillerWidth: (i + 1) * fillerWidth]))
if i < height - 1: if i < fillerHeight - 1:
scr.addstr('\n') scr.addstr('\n')
scr.refresh() scr.refresh()
@ -147,7 +150,7 @@ def moveInput(scr, inputPad):
size = scr.getmaxyx() size = scr.getmaxyx()
height = size[0] height = size[0]
width = size[1] width = size[1]
inputPad.addstr('\n>') inputPad.addstr('\n>')
# cursor position relative to inputPad # cursor position relative to inputPad
@ -155,7 +158,7 @@ def moveInput(scr, inputPad):
inputPad.refresh(0, 0, inputPad.refresh(0, 0,
height - cursorPos[0] - 1, height - cursorPos[0] - 1,
width / 2 + CONST_WIDTH, width / 2 + CONST_CHARS,
height - 1, height - 1,
width - 1) width - 1)
@ -172,7 +175,7 @@ def userInput(scr, passwords):
width = size[1] width = size[1]
# set up a pad for user input # set up a pad for user input
inputPad = curses.newpad(height, width / 2 + CONST_WIDTH) inputPad = curses.newpad(height, width / 2 + CONST_CHARS)
attempts = LOGIN_ATTEMPTS attempts = LOGIN_ATTEMPTS
@ -182,7 +185,7 @@ def userInput(scr, passwords):
while attempts > 0: while attempts > 0:
# move the curser to the correct spot for typing # move the curser to the correct spot for typing
scr.move(height - 1, width / 2 + CONST_WIDTH + 1) scr.move(height - 1, width / 2 + CONST_CHARS + 1)
# scroll user input up as the user tries passwords # scroll user input up as the user tries passwords
moveInput(scr, inputPad) moveInput(scr, inputPad)
@ -204,7 +207,7 @@ def userInput(scr, passwords):
moveInput(scr, inputPad) moveInput(scr, inputPad)
curses.napms(LOGIN_PAUSE) curses.napms(LOGIN_PAUSE)
return return True
# wrong password # wrong password
else: else:
@ -234,28 +237,27 @@ def userInput(scr, passwords):
scr.addstr(' ') scr.addstr(' ')
# Out of attempts # Out of attempts
scr.erase() return False
scr.move(height / 2 - 1, width / 2 - 7)
scr.addstr('TERMINAL LOCKED')
scr.move(height / 2 + 1, width / 2 - 16)
scr.addstr('PLEASE CONTACT AN ADMINISTRATOR')
curses.curs_set(0)
scr.refresh()
curses.napms(LOCKED_OUT_TIME)
def runLogin(scr): def runLogin(scr):
""" """
Start the login portion of the terminal Start the login portion of the terminal
""" """
curses.use_default_colors()
size = scr.getmaxyx()
width = size[1]
height = size[0]
random.seed() random.seed()
# set screen to initial position
scr.erase()
scr.move(0, 0)
passwords = initScreen(scr) passwords = initScreen(scr)
userInput(scr, passwords) return userInput(scr, passwords)
def beginLogin(): def beginLogin():
""" """
Initialize curses and start the login process Initialize curses and start the login process
Returns true if the user correctly guesses the password
""" """
scr = curses.initscr() return curses.wrapper(runLogin)
curses.wrapper(runLogin)