# -*- coding: utf-8 -*-
#
# Minimal Shell
-# Use whenever a user should be able to launch a shell,
-# but not to execute commands.
+# Use whenever a user should be able to launch a shell,
+# but not to execute commands.
#
# Copyright (c) 2013-2015 Malte Bublitz.
# All rights reserved.
import bbs.env
import bbs.bofh
-def getuser():
- return "doctorwho"
-
-def issue():
- # Escape codes for clearing the screen:
- # ansi/vt100/vt220
- # \e[H\e[J
- # xterm-256color
- # \e[3J\e[H\e[2J
- #
- os.system("clear")
- #print("")
- #os.system("figlet -f slant T.A.R.D.I.S.")
- #print("")
- #print(chr(27)+"[H"+chr(27)+"[J", end="")
- print("""
- ______ ___ ____ ____ ____ _____
- /_ __/ / | / __ \ / __ \ / _/ / ___/
- / / / /| | / /_/ / / / / / / / \__ \
- / / _ / ___ |_ / _, _/ / /_/ / _/ / _ ___/ /
-/_/ (_)_/ |_(_)_/ |_(_)_____(_)___/(_)____(_)
-
-""")
-
+
+def issue(data_file = None):
+ if data_file is None:
+ data_file = os.path.join(
+ os.path.dirname(__file__),
+ "data",
+ "issue"
+ )
+
+ with open(data_file, "r") as f:
+ print(f.read())
+
+
def login(prompt1="login: ", prompt2="Password: ", clear_before = True):
- if clear_before:
- #os.system("clear")
- pass
-
- login_data = ["", ""]
- while len(login_data[0]) < 2:
- try:
- login_data[0] = input(prompt1)
- except EOFError:
- print("^D")
-
- login_data[1] = getpass.getpass(prompt2)
-
- return login_data
-
-def minishell(ps1="%w> "):
- """
- ps1 is used as the prompt, and %w will be replaced by the current working directory
- """
- env = bbs.env.BBSFakeUserEnv()
- commands_allowed = (
- "",
- "exit",
- "logout",
- "help",
- "whoami",
- "id",
- "hostname",
- "pwd",
- "ls", "dir",
- "cat", "type",
- "uname",
- "clear",
- "sry", "bofh",
- )
- command = ""
-
- # /etc/issue
- issue()
-
- # Log in
- try:
- env.setUser(login(clear_before=False)[0])
- except KeyboardInterrupt:
- #sys.exit(0)
- env.setUser("john.doe")
-
- env.setHideRealUname(True)
- env.setNode("bbs.malte70.de")
- if env.getUser() in ["doctor", "doctorwho", "doctor_who"]:
- #env.setNode("torchwood.tardis.malte70.de")
- env.setNode("bbs.torchwood-cardiff.torchwood.gov.uk")
-
- if "--lxc" in sys.argv[1:]:
- env.setRunningInsideLXC(True)
-
- print("\nWelcome on "+env.getNode()+", "+env.getUser()+"!\n")
-
- if env.getRunningInsideLXC():
- # Show an unneccessary cryptic message if running inside LXC
- print(" [SYS$LOG] Installing BBS-UX LXC services...\n")
-
- try:
- while command != "exit":
- #print(ps1, end="")
- print(env.getPrompt(ps1), end="")
- try:
- command = input().lower()
-
- except KeyboardInterrupt:
- print("")
- continue
-
- # Split command from command_args
- command_args = " ".join(command.split(" ")[1:])
- command = command.split(" ")[0]
-
- if command == "logout" or command == "exit":
- command = "exit"
-
- elif command == "help" and len(command_args) < 1:
- print("""Help
+ if clear_before:
+ #os.system("clear")
+ pass
+
+ login_data = ["", ""]
+ while len(login_data[0]) < 2:
+ try:
+ login_data[0] = input(prompt1)
+ except EOFError:
+ print("^D")
+
+ login_data[1] = getpass.getpass(prompt2)
+
+ return login_data
+
+def minishell(ps1 = "%w> ", filesystem_json = None, issue_file = None):
+ """
+ Args:
+ ps1 is used as the prompt, and %w will be replaced by the current working directory
+ """
+ env = bbs.env.BBSFakeUserEnv(filesystem_json)
+ commands_allowed = (
+ "",
+ "exit",
+ "logout",
+ "help",
+ "whoami",
+ "id",
+ "hostname",
+ "pwd",
+ "ls", "dir",
+ "cat", "type",
+ "uname",
+ "clear",
+ "sry", "bofh",
+ )
+ command = ""
+
+ # /etc/issue
+ issue(issue_file)
+
+ # Log in
+ try:
+ env.setUser(login(clear_before=False)[0])
+ except KeyboardInterrupt:
+ #sys.exit(0)
+ env.setUser("john.doe")
+
+ env.setHideRealUname(True)
+ env.setNode("bbs.malte70.de")
+ if env.getUser() in ["doctor", "doctorwho", "doctor_who"]:
+ #env.setNode("torchwood.tardis.malte70.de")
+ env.setNode("bbs.torchwood-cardiff.torchwood.gov.uk")
+
+ if "--lxc" in sys.argv[1:]:
+ env.setRunningInsideLXC(True)
+
+ print("\nWelcome on "+env.getNode()+", "+env.getUser()+"!\n")
+
+ if env.getRunningInsideLXC():
+ # Show an unneccessary cryptic message if running inside LXC
+ print(" [SYS$LOG] Installing BBS-UX LXC services...\n")
+
+ try:
+ while command != "exit":
+ #print(ps1, end="")
+ print(env.getPrompt(ps1), end="")
+ try:
+ command = input().lower()
+
+ except KeyboardInterrupt:
+ print("")
+ continue
+
+ # Split command from command_args
+ command_args = " ".join(command.split(" ")[1:])
+ command = command.split(" ")[0]
+
+ if command == "logout" or command == "exit":
+ command = "exit"
+
+ elif command == "help" and len(command_args) < 1:
+ print("""Help
Commands:
- whoami
- id
- hostname
- pwd
- ls
- cat
- uname
- clear
- help
- logout/exit
+ whoami
+ id
+ hostname
+ pwd
+ ls
+ cat
+ uname
+ clear
+ help
+ logout/exit
""")
-
- elif command == "help" and len(command_args) > 1:
- help_topic = command_args.split(" ")[0]
- if help_topic == "sry":
- print("SRY\n\tEaster Egg. Just try it!")
- elif help_topic == "two":
- print("TWO\n\tTime Wasting Option")
- print("\tDon't conflate TWO with TSO in the OS/360")
- print("\tfamily of mainframe systems!")
- elif help_topic in ("shutdown", "poweroff", "halt"):
- print("shutdown/poweroff/halt")
- print("\tRunning them is interpreted as an act of violence")
- print("\tagainst the Dalek!")
- else:
- print(help_topic)
- print("\tFAKENEWS!")
-
- elif command == "whoami":
- if not env.getUser() in ["doctor", "doctorwho", "doctor_who"]:
- print(env.getUser())
- else:
- #print("I am the Doctor!")
- print(env.getName())
- print("")
- print("I should behave politely, so maybe excuse for")
- print("future mistakes with \"sry\" (Yes, an easter egg!)")
- print("")
-
- elif command == "shutdown" or command == "poweroff" or command == "halt":
- print("Exterminate!")
- print("Exterminate!".upper())
- sys.exit(0)
-
- elif command == "id":
- print("uid=42(" + env.getUser() + ") gid=100(users) groups=42(" + env.getUser() + "),9999(telnet)")
-
- elif command == "hostname":
- print(env.getNode())
-
- elif command == "pwd":
- #print("/usr/home/"+getuser())
- print(env.getCurrentDir(True))
-
- elif command == "ls" or command == "dir":
- #print("A: TARDIS ZIP : DALEK EXE")
- #print("A: CLARA DOC : ASHILDR GIF")
- _dir = env.getDirListing()
- for _entry in _dir:
- print(" " + env.getCurrentDir()[:2] + " " + _entry)
-
- elif command == "cat" or command == "type":
- # DEBUG:
- #print("CMD = \"" + command + "\"")
- #print("ARGS = \"" + command_args + "\"")
-
- filename = command_args.upper()
- print(env.getFileContents(filename))
-
- elif command == "uname":
- print(env.getUName())
-
- elif command == "clear":
- # clear screen
- ret_code = os.system("clear")
-
- elif command == "sry" or command == "bofh":
- # BOFH excuse
- print("<BOFH> "+bbs.bofh.get_excuse())
-
- elif len(command) == 2 and command[1] == ":":
- # Change drive/working directory
- env.setCurrentDir(command.upper())
-
- #elif not command in commands_allowed:
- # print("-minishell: "+command.split(" ")[0]+": Command not found.")
- elif len(command) > 0:
- print("TWO: "+command.split(" ")[0]+": Command not found.")
-
- except EOFError:
- print("<EOF>")
-
- print("Good bye.")
+
+ elif command == "help" and len(command_args) > 1:
+ help_topic = command_args.split(" ")[0]
+ if help_topic == "sry":
+ print("SRY\n\tEaster Egg. Just try it!")
+ elif help_topic == "two":
+ print("TWO\n\tTime Wasting Option")
+ print("\tDon't conflate TWO with TSO in the OS/360")
+ print("\tfamily of mainframe systems!")
+ elif help_topic in ("shutdown", "poweroff", "halt"):
+ print("shutdown/poweroff/halt")
+ print("\tRunning them is interpreted as an act of violence")
+ print("\tagainst the Dalek!")
+ else:
+ print(help_topic)
+ print("\tFAKENEWS!")
+
+ elif command == "whoami":
+ if not env.getUser() in ["doctor", "doctorwho", "doctor_who"]:
+ print(env.getUser())
+ else:
+ #print("I am the Doctor!")
+ print(env.getName())
+ print("")
+ print("I should behave politely, so maybe excuse for")
+ print("future mistakes with \"sry\" (Yes, an easter egg!)")
+ print("")
+
+ elif command == "shutdown" or command == "poweroff" or command == "halt":
+ print("Exterminate!")
+ print("Exterminate!".upper())
+ sys.exit(0)
+
+ elif command == "id":
+ print("uid=42(" + env.getUser() + ") gid=100(users) groups=42(" + env.getUser() + "),9999(telnet)")
+
+ elif command == "hostname":
+ print(env.getNode())
+
+ elif command == "pwd":
+ print(env.getCurrentDir(True))
+
+ elif command == "ls" or command == "dir":
+ _dir = env.getDirListing()
+ for _entry in _dir:
+ print(" " + env.getCurrentDir()[:2] + " " + _entry)
+
+ elif command == "cat" or command == "type":
+ filename = command_args.upper()
+ try:
+ print(env.getFileContents(filename))
+ except FileNotFoundError:
+ full_filename = env.getCurrentDir() + filename
+ print("TWO: [ERROR] File not found: " + full_filename)
+
+ elif command == "uname":
+ print(env.getUName())
+
+ elif command == "clear":
+ # clear screen
+ ret_code = os.system("clear")
+
+ elif command == "sry" or command == "bofh":
+ # BOFH excuse
+ print("<BOFH> "+bbs.bofh.get_excuse())
+
+ elif len(command) == 2 and command[1] == ":":
+ # Change drive/working directory
+ try:
+ env.setCurrentDir(command.upper())
+ except FileNotFoundError:
+ print("TWO: [ERROR] Drive " + command.upper() + " does not exist!")
+
+ elif len(command) > 0:
+ print("TWO: "+command.split(" ")[0]+": Command not found.")
+
+ except EOFError:
+ print("<EOF>")
+
+ print("Good bye.")
+
if __name__ == "__main__":
- minishell(ps1='%w> ')
+ minishell(ps1='%w> ')
+