r'''Show info for current webshell client.
'''

from api.exploit import Exploit, Cmdline
from api import logger, colour, tablor
import argparse

class exploit(Exploit):
    def __init__(self):
        self.parse = argparse.ArgumentParser(prog='help', description='Show info for current webshell client.', 
            epilog="if no options is set, show current webshell client options.")
        self.parse.add_argument('--webshell-help', help="Show current webshell client info.", action='store_true')
        self.parse.add_argument("--options", help="Displays simple information about the current options.", action='store_true')
        self.parse.add_argument('-i', "--options-help", help="Displays description information about the current options.", action='store_true')
        self.help = __doc__ + self.parse.format_help()

    def support(self, session)-> bool:
        '''判断当前命令是否支持指定的session， 一般来说session即为运行改命令时的session
        '''
        self.session = session
        return True

    def run(self, args: Cmdline)-> int:
        args = self.parse.parse_args(args.options)
        webshell = self.session.client
        if webshell is None:
            logger.error('No webshell client is using.')
            return self.STOP
        if args.webshell_help:
            print(colour(webshell.help))
        elif args.options_help:
            old = webshell.options.option_list()
            tmp = []
            tmp.append([old[0][0], old[0][4]])
            for l in old[1:]:
                tmp.append([l[0], l[4]])
            print(tablor(tmp, border=False, title="Options", max_width="30-1, 80-2"))
        else:
            old = webshell.options.option_list()
            tmp = []
            tmp.append([old[0][0], old[0][1], old[0][2], old[0][3]])
            for l in old[1:]:
                tmp.append([l[0], l[1], l[2], l[3]])
            print(tablor(tmp, border=False, title="Options", max_width="30-1, 10-3"))

        return self.SUCCESS