import os

from acsploit.options import Options


options = Options()
options.add_option('file_format', 'sh', 'Format for fork bomb', ['bash', 'bat', 'c', 'cc', 'cs', 'erl', 'fasm', 'go',
                                                                 'hs', 'html', 'java', 'js', 'lsp', 'lua', 'mdb', 'php',
                                                                 'pl', 'py', 'r', 'rb', 'rs', 's', 'scm', 'sh', 'vbs'])

DESCRIPTION = 'Produces a fork bomb in one of many formats.\n\n  Fork bombs cause denial of service by forking processes' \
              'infinitely, depleting available system resources. Fork bombs are also known as "rabbit viruses" or ' \
              '"wabbits". ACsploit provides canned fork bombs in ' \
              'a variety of programming languages shown through the "file_format" option.'


NO_INPUT = True
DEFAULT_OUTPUT = 'file'
DEFAULT_OUTPUT_OPTIONS = {
    'final_newline': False
}


def run(output):
    ret = fork_bomb(options['file_format'])
    output.output([ret])


# Returns contents of fork bomb with the given format. Fork bombs found at https://github.com/aaronryank/fork-bomb/
def fork_bomb(file_format):
    filename = 'fork-bomb.' + file_format
    with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fork_bombs', filename), 'r') as fork_bomb_file:
        return fork_bomb_file.read()
