#!/usr/bin/env python3

#
# MIT License
#
# Copyright (c) 2020 EntySec
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

import os

os.system("printf '\033]2;Ghost Framework\a'")

import sys
import subprocess
import readline
import time

from core.badges import badges
from core.server import server
from core.helper import helper

badges = badges()
server = server()
helper = helper()

readline.parse_and_bind('tab: complete')

def banner():
    os.system("clear")
    os.system("cat banner/banner.txt")
    print("")
    print("Ghost Framework " + helper.version)
    print("--------------------")
    print("")


def main():
    rport = str(helper.rport)
    rhost = ""
    try:
        ui = input('\033[4mghost\033[0m> ').strip(" ")
    except:
        sys.exit()
    ui = ui.split()
    while True:
        if ui == []:
            pass
        elif ui[0] == "exit":
            sys.exit()
        elif ui[0] == "clear":
            os.system("clear")
        elif ui[0] == "help":
            print("")
            print("Core Commands")
            print("=============")
            os.system("cat data/cmds/core_cmds.txt")
            print("")
        elif ui[0] == "options":
            if len(rhost) >= 12 or len(rport) >= 12:
                if len(rhost) > len(rport):
                    bigger = len(rhost) - 3
                else:
                    bigger = len(rport) - 3
            else:
                bigger = 8
            print("")
            print("Ghost Options")
            print("=============")
            print("")
            print("    Option        Value" + " " * bigger + "Description")
            print("    ------        -----" + " " * bigger + "-----------")
            print("    RHOST         " + rhost + " " * (5 - len(rhost) + bigger) + "Remote host.")
            print("    RPORT         " + rport + " " * (5 - len(rport) + bigger) + "Remote port.")
            print("")
        elif ui[0] == "set":
            if len(ui) < 3:
                print("Usage: set <option> <value>")
            else:
                if ui[1].lower() == "rhost":
                    rhost = ui[2]
                    print(badges.I + "RHOST ==> " + ui[2])
                elif ui[1].lower() == "rport":
                    rport = ui[2]
                    print(badges.I + "RPORT ==> " + ui[2])
                else:
                    print(badges.E + "Option is not found!")
        elif ui[0] == "run":
            if rhost == "" or rport == "":
                print(badges.E + "Failed to connect to target!")
            else:
                try:
                    server.connect(rhost, rport)
                except SystemExit:
                    pass
        else:
            print(badges.E + "Unrecognized command!")
        try:
            ui = input('\033[4mghost\033[0m> ').strip(" ")
        except:
            sys.exit()
        ui = ui.split()


if helper.check_adb_installation:
    banner()
    main()
else:
    sys.exit()
