from ircthing_core import irc_router, connect_to_irc_server from ircthing_utils import read_config, cli_args, base_path import time import os import multiprocessing Processes = {} Stop_Toggle = multiprocessing.Event() def clean_exit(): Stop_Toggle.set() def main(): root_path = base_path() # Get configuration file path if given config_path = 'config.ini' argument = cli_args() if argument: config_path = argument # Read configuration network_configs = read_config(config_path) ## Get irc socket for each network in configuration ## Start thread for each socket for network in network_configs: net_name = network["net_name"] server = network["server"] port = network["port"] nickname = network["nickname"] password = network["password"] print(f"{time.time()} | Found configs for {net_name} network.") irc_socket, fifo_files, network_dir = connect_to_irc_server(root_path, net_name, server, port, nickname, password) router_instance = irc_router(fifo_files, irc_socket, server, nickname, network_dir) Processes[net_name] = multiprocessing.Process(target=router_instance.start) Processes[net_name].daemon = True Processes[net_name].start() main_handle(root_path) for process in Processes.values(): process.join() def main_handle(path): input = f"{path}/in" output = f"{path}/out" os.mkfifo(input) os.mkfifo(output) while True: line = read_input(input) write_output(output, line) def read_input(file): with open(file, 'r') as input: line = input.readline().strip() if not line: return if line == "exit": clean_exit() return line def write_output(file, line): with open(file, 'a') as output: output.write(f"{time.time()} | {line}\r\n") output.flush() output.close() if __name__ == "__main__": print(f"{time.time()} | Lets start!") try: main() except Exception as e: print(f"Got error {e}") finally: clean_exit() print(f"{time.time()} | Bye!") exit(0)