You are on page 1of 5

#!

/usr/bin/python
import os,sys,commands,time,operator,imp,signal,readline,rlcompleter,threading
from output import *
from ConfigParser import ConfigParser,RawConfigParser
class seashell:
def __init__(self):
# The default commands -- These defaults may be edited
self.allow = ['ls', 'cd', 'pwd', 'nano', 'cat', 'irssi', 'clear', 'su',
'ping', 'vim', 'vi', 'mpg123', 'echo']
self.modules = ['set', 'recall', 'help', 'motd', 'screen', 'conch']
# END default
# The following may be edited, but everything else can be done using
config files

# START SETUP VARS


try:
argvs = sys.argv[1]
except:
argvs = False
self.custom_configs = "/var/SeaShell"
self.user = commands.getoutput('whoami')
self.hostname = commands.getoutput('uname -n')
self.home = commands.getoutput('echo $HOME')
if not os.path.isfile(self.home + "/.seashell_history"):
os.system('touch ' + self.home + "/.seashell_history")
readline.read_history_file(self.home + "/.seashell_history")
readline.write_history_file(self.home + "/.seashell_history")
sys.path.append('./modules')
sys.path.append(self.home)
sys.path.append('/bin')
self.blacklist = ['bash', 'sh', 'csh']
self.exit_msg = "Have a good day, %s." %self.user
self.motd_msg = blue("""
./+++/- `-/+++/` ++ `s. s-
om:...-- .-::. `-:::. `hh-...:` `N+-::. .-::. +m :N`
sdo/:. -yo:-/m: .:--/N/ `dh+/-. +N+:-+N. .yo:-/m+ do hs
.-/+dy `mho+ooh/ -oooosM/ `.:/om+ do /N` ddo+ooy+ -N` .M-
:.` `-dy .N+` `` No` .hm``:` `:N/ -N` hy `Ns` `. yy sd
+ooso+:` -oooo+. :so+:o/ .+osso+. :+ `s- .oooo+- s- o:

Welcome to SeaShell %s.


To exit, use the `exit` command.""") % self.user
# End editable variables
query = ""
running = True
self.front = green(self.user + ":" + self.hostname)
self.uid = commands.getoutput('echo $UID')
modules_list = os.listdir('./modules')
self.external_modules = {}
for line in modules_list:
if "pyc" not in line:
split = line.split(".")
self.external_modules[split[0]] = __import__(split[0])
# END SETUP VARS

# RUN SEASHELL
self.motd(query)
self.getinput()
# //-----------
# ///////////////////////////////
# START FUNCTIONS
# ///////////////////////////////
def getinput(self):
while True:
# INPUT COMMAND
self.located = commands.getoutput('pwd')
# GRAB CURRENT FOLDER
if self.located == "/":
self.folder = "/"
else:
count = len(self.located.split("/"))
self.folder = self.located.split("/")[count-1]
# SET APPEARANCE, INNITIATE TAB COMPLETE, GRAB COMMAND
self.appearance = green(self.user + " at " + self.hostname) + " "
+ blue(self.folder) + " "
words = os.listdir(self.located)
self.tab_complete(words)
self.com = self.command(self.appearance)
# EVALUATE COMMAND
try:
split = self.com.split(None, 1)
function = split[0]
try:
query = split[1]
except IndexError:
query = ""
if (function == "exit"):
self.exit()
if (function in self.allow):
if (function == "cd"):
try:
os.chdir(query)
except OSError:
print "No such directory."
else:
self.execute(function, query)
elif (function in self.modules):
try:
eval("self.%s('%s')" %(function, query))
except AttributeError:
print "Module function does not exist."
else:
print "No function, or no permissions."
except IndexError:
pass
def tab_complete(self, array):
completer = Completer(array)
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
def command(self, appearance):
com = raw_input(appearance)
return com
def execute(self, function, query):
os.system(function + " " + query)
# Here is where we will insert the `modules` like help,set,exit
# /////////////////////////////////////////////////////////////
def motd(self, query):
if (query):
print "Incorrect useage for motd. motd does not accept
arguments."
else:
print self.motd_msg
print
def exit(self):
print self.exit_msg
time.sleep(1)
sys.exit()
def help(self, query):
print "Welcome to the SeaShell help function. This function has been
designed to aid\nusers with the uses of this shell.\n\nCommands available to
you:\n\tUnix Applications:"
for line in self.allow:
print "\t\t- " + line
print
print "\tSeaShell Modular Applications:"
for line in self.modules:
print "\t\t- " + line
print "Thank you,\n-SeaShell team"
def set(self, query):
print "This function allows you to set variables."
var = raw_input("Variable name " + blue("o "))
data = raw_input("Variable data " + blue("o "))
if (var[0] == "$"):
var = "UDEF_" + var[1:]
f = open('/tmp/' + var, 'w')
f.write(data)
f.close()
print
print "Variable stored."
else:
print "Variable must begin with an $."
def recall(self, query):
if (query):
var = "UDEF_" + query[1:]
try:
f = open('/tmp/' + var, 'r')
data = f.read()
print data
f.close()
except IOError:
print "That variable does not exist!"
else:
print "echo requires an argument."
def no_exec(self, query):
print "Sorry, you cannot run that function directly!"
def screen(self, query):
run = True
for line in self.blacklist:
if line in query:
run = False
else:
pass
if (run):
os.system('screen ' + query)
else:
print "You cannot run bash,sh, or csh in screen!"
def conch(self, query):
camefrom = self.located
print red("Welcome to conch, the module import device. (help for
information, l for list.)")
self.tab_complete(self.external_modules)
self.conchrunning = True
while self.conchrunning == True:
x = raw_input(red('[> ] '))
if x == "exit":
self.conchrunning = False
elif x == "help":
print "Welcome to the module loader help section."
print "This modular loader allows SeaShell modules to be"
print "loaded into the shell. All modules placed in"
print "/usr/share/SeaShell may be loaded.\n"
print "Any module written for SeaShell"
print "_must_ include a class named SeaShellModule,"
print "that takes two arguements, self and seashell,"
print "and all python code _must_ run from within"
print "this class.\n"
print "The following is a _basic_ SeaShell module file:"
print
print "//////////////////////////////"
print "class SeaShellModule:"
print "\tdef __init__(self, seashell):"
print "\t\tself.seashell = seashell()"
print "\t\tprint 'Demo Module'"
print "\t\tself.run()"
print "\tdef run(self):"
print "\t\tprint 'Module running.'"
print "//////////////////////////////"
print
print "Press l to view which modules are loadable,"
print "and load them by entering their name."
print
elif x == "l":
print "Modules list:"
for line in self.external_modules:
print "\t- " + line
elif x == "":
pass
else:
if x in self.external_modules:
run = self.external_modules[x].SeaShellModule(self)
else:
print "Module does not exist. l for list."
os.chdir(camefrom)
# END of main class

# Catch some signals


def sighand(signum, frame):
print "\nCaught CTRL+C, quitting..."
sys.exit()
signal.signal(signal.SIGINT, sighand)
# Tab completion
class Completer:
def __init__(self, words):
self.words = words
self.prefix = None
def complete(self, prefix, index):
if prefix != self.prefix:
# we have a new prefix!
# find all words that start with this prefix
self.matching_words = [
w for w in self.words if w.startswith(prefix)
]
self.prefix = prefix
try:
return self.matching_words[index]
except IndexError:
return None

try:
start = seashell()
except EOFError:
print
pass