From 766ba8f7bc38075ba90564340b0fb15d93e8f932 Mon Sep 17 00:00:00 2001 From: Josh d'Entremont Date: Tue, 26 May 2015 22:11:31 -0300 Subject: [PATCH] added boot and locked out scripts --- README.md | 1 + fallout.py | 11 +++++--- fallout_boot.py | 40 +++++++++++++++++++++++++++ fallout_functions.py | 12 +++++++++ fallout_locked.py | 40 +++++++++++++++++++++++++++ fallout_login.py | 64 +++++++++++++++++++++++--------------------- 6 files changed, 134 insertions(+), 34 deletions(-) create mode 100644 fallout_boot.py create mode 100644 fallout_functions.py create mode 100644 fallout_locked.py diff --git a/README.md b/README.md index 0caeec0..7668417 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,6 @@ To use the login in another program: ``` import fallout_login +# returns true if the correct password is entered, false otherwise fallout_login.beginLogin() ``` diff --git a/fallout.py b/fallout.py index aa4d596..f5fb2f7 100755 --- a/fallout.py +++ b/fallout.py @@ -1,6 +1,11 @@ #! /usr/bin/env python 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' diff --git a/fallout_boot.py b/fallout_boot.py new file mode 100644 index 0000000..3b3820e --- /dev/null +++ b/fallout_boot.py @@ -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 diff --git a/fallout_functions.py b/fallout_functions.py new file mode 100644 index 0000000..4b5550d --- /dev/null +++ b/fallout_functions.py @@ -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) diff --git a/fallout_locked.py b/fallout_locked.py new file mode 100644 index 0000000..100e6fa --- /dev/null +++ b/fallout_locked.py @@ -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) diff --git a/fallout_login.py b/fallout_login.py index 23c7b8e..4d6c00d 100644 --- a/fallout_login.py +++ b/fallout_login.py @@ -3,11 +3,16 @@ import curses import random import os +from fallout_functions import slowWrite + +################## text strings ###################### + +HEADER_TEXT = 'ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL' ################## global "constants" ################ # number of characters for hex digits and spaces -CONST_WIDTH = 16 +CONST_CHARS = 16 # position of the attempt squares SQUARE_X = 19 @@ -18,9 +23,6 @@ LOGIN_ATTEMPTS = 4 # amount of time to pause after correct password input LOGIN_PAUSE = 3000 -# amount of time to pause after lockout -LOCKED_OUT_TIME = 5000 - # starting number for hex generation START_HEX = 0xf650 @@ -100,18 +102,19 @@ def initScreen(scr): Fill the screen to prepare for password entry scr - curses window returned from curses.initscr() - """ + """ size = scr.getmaxyx() + height = size[0] 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] - hexCol2 = hexes[height:] + hexCol1 = hexes[:fillerHeight] + hexCol2 = hexes[fillerHeight:] # generate the symbols and passwords - fillerLength = width / 2 * height + fillerLength = width / 2 * fillerHeight passwords = getPasswords() filler = getFiller(fillerLength, passwords) fillerCol1 = filler[:len(filler) / 2] @@ -121,8 +124,8 @@ def initScreen(scr): fillerWidth = width / 4 # print the header stuff - scr.addstr('ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL\n') - scr.addstr('ENTER PASSWORD NOW\n\n') + scr.addstr(HEADER_TEXT) + scr.addstr('\nENTER PASSWORD NOW\n\n') scr.addstr(str(LOGIN_ATTEMPTS) + ' ATTEMPT(S) LEFT: ') for i in xrange(LOGIN_ATTEMPTS): scr.addch(curses.ACS_BLOCK) @@ -130,9 +133,9 @@ def initScreen(scr): scr.addstr('\n\n') # 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])) - if i < height - 1: + if i < fillerHeight - 1: scr.addstr('\n') scr.refresh() @@ -147,7 +150,7 @@ def moveInput(scr, inputPad): size = scr.getmaxyx() height = size[0] width = size[1] - + inputPad.addstr('\n>') # cursor position relative to inputPad @@ -155,7 +158,7 @@ def moveInput(scr, inputPad): inputPad.refresh(0, 0, height - cursorPos[0] - 1, - width / 2 + CONST_WIDTH, + width / 2 + CONST_CHARS, height - 1, width - 1) @@ -172,7 +175,7 @@ def userInput(scr, passwords): width = size[1] # 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 @@ -182,7 +185,7 @@ def userInput(scr, passwords): while attempts > 0: # 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 moveInput(scr, inputPad) @@ -204,7 +207,7 @@ def userInput(scr, passwords): moveInput(scr, inputPad) curses.napms(LOGIN_PAUSE) - return + return True # wrong password else: @@ -234,28 +237,27 @@ def userInput(scr, passwords): scr.addstr(' ') # Out of attempts - scr.erase() - 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) + return False def runLogin(scr): """ Start the login portion of the terminal """ + curses.use_default_colors() + size = scr.getmaxyx() + width = size[1] + height = size[0] random.seed() + # set screen to initial position + scr.erase() + scr.move(0, 0) passwords = initScreen(scr) - userInput(scr, passwords) + return userInput(scr, passwords) def beginLogin(): """ Initialize curses and start the login process + Returns true if the user correctly guesses the password """ - scr = curses.initscr() - curses.wrapper(runLogin) + return curses.wrapper(runLogin)