import json
import os
import multiprocessing
import struct
import importlib
from socketserver import TCPServer, StreamRequestHandler


def plugin_module_import(name):
    try:
        return importlib.import_module(name)
    except ModuleNotFoundError as e:
        prompt = 'plugin {} not found, please install it first.\n'.format(name)
        prompt += 'try follwing:\n\tpip3 install {}'.format(name)
        raise PluginNotFoundError(prompt)


class NotificationHandler(StreamRequestHandler):
    def handle(self):
        length = struct.unpack('<I', self.request.recv(4))[0] 
        json_content = self.request.recv(length) 
        content = json.loads(json_content)
        terminal = content['terminal']
        if content['exec'] != '': 
            command = 'ancypwn attach -c \'{}\''.format(content['exec'])
        else:
            command = 'ancypwn attach'
        realname = 'ancypwn_terminal_{}'.format(terminal)
        mod = plugin_module_import(realname) 
        mod.run(command) 


class ServerProcess(multiprocessing.Process):

    def __init__(self, port, *args, **kwargs):
        super(ServerProcess, self).__init__(*args, **kwargs)
        self.port = port

    def run(self):
        self.server = TCPServer(('', self.port), NotificationHandler)
        self.server.serve_forever()

[((11, 15, 11, 44), 'importlib.import_module', 'importlib.import_module', ({(11, 39, 11, 43): 'name'}, {}), '(name)', False, 'import importlib\n'), ((22, 18, 22, 42), 'json.loads', 'json.loads', ({(22, 29, 22, 41): 'json_content'}, {}), '(json_content)', False, 'import json\n'), ((40, 22, 40, 69), 'socketserver.TCPServer', 'TCPServer', ({(40, 32, 40, 47): "('', self.port)", (40, 49, 40, 68): 'NotificationHandler'}, {}), "(('', self.port), NotificationHandler)", False, 'from socketserver import TCPServer, StreamRequestHandler\n')]