diff --git a/ircthing.py b/ircthing.py new file mode 100644 index 0000000..6492fdc --- /dev/null +++ b/ircthing.py @@ -0,0 +1,62 @@ +import threading +import os +import sys +import time +import socket +from ircthing_core import irc_router, connect_to_irc_server +from ircthing_utils import read_config + +Threads = {} +Stop_Toggle = threading.Event() + +def clean_exit(fifo_files, socket): + socket.send(bytes(f"QUIT :Bye\r\n", "UTF-8")) + Stop_Toggle.set() + for file in fifo_files: + try: + os.unlink(file) + except FileNotFoundError: + # We are okay if some fifo files has been removed before this. + pass + +def main(): + my_name = sys.argv[0] + my_name_pyless, _ = os.path.splitext(my_name) + base_path = f'/tmp/{my_name_pyless}' + + config_path = '../ircthing3.ini' + + # Read configurations for all topics + 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"] + print(f"{time.time()} | Found configs for {net_name} network.") + server = network["server"] + port = network["port"] + nickname = network["nickname"] + password = network["password"] + irc_socket, fifo_files = connect_to_irc_server(base_path, net_name, server, port, nickname, password) + + router_instance = irc_router(fifo_files, irc_socket, server, nickname) + Threads[net_name] = threading.Thread(target=router_instance.start) + Threads[net_name].daemon = True + Threads[net_name].start() + + for thread in Threads.values(): + print(thread) + thread.join() + + +if __name__ == "__main__": + print(f"{time.time()} | Lets start!") + try: + main() + except Exception as e: + print(f"Got error {e}") + finally: + print(f"{time.time()} | Bye!") + exit(0) diff --git a/ircthing3.py b/ircthing3.py deleted file mode 100644 index c779596..0000000 --- a/ircthing3.py +++ /dev/null @@ -1,138 +0,0 @@ -import threading -import os -import sys -import time -import socket -import configparser -from ircthing_core import irc_router - -Threads = {} -Stop_Toggle = threading.Event() - -def read_config(config_path): - config = configparser.ConfigParser() - config.read(config_path) - - network_configs = [] - - try: - # Collect information from each topic / network - for topic in config.sections(): - network = config[topic] - server = network.get("server") - port = network.getint("port") - channels = network.get("channels", fallback=None) - nickname = network.get("nickname") - password = network.get("password", fallback=None) - - network_config = { - "net_name": network.name, - "server": server, - "port": port, - "channels": channels, - "nickname": nickname, - "password": password, - } - network_configs.append(network_config) - return network_configs - except Exception as e: - print(f"Failure while reading configuration file. {e}") - exit(1) - -def clean_exit(fifo_files, socket): - socket.send(bytes(f"QUIT :Bye\r\n", "UTF-8")) - Stop_Toggle.set() - for file in fifo_files: - try: - os.unlink(file) - except FileNotFoundError: - # We are okay if some fifo files has been removed before this. - pass - -def make_files(path, net_name): - - os.makedirs(path, exist_ok=True) - server_dir = os.path.join(path, net_name) - os.makedirs(server_dir, exist_ok=True) - try: - os.mkfifo(f"{server_dir}/in") - except FileExistsError: - pass - try: - os.mkfifo(f"{server_dir}/out") - except FileExistsError: - pass - fifo_files = [] - fifo_files.append(f"{server_dir}/in") - fifo_files.append(f"{server_dir}/out") - return fifo_files - -def connect_to_irc_server( - base_path, - net_name, - server, - port, - nickname, - password=None, -): - - print(f"Going to connect to: {server}") - # Create a socket connection to the IRC server - irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - irc_socket.connect((server, port)) - - # Send the server password if provided - if password: - irc_socket.send(bytes(f"PASS {password}\r\n", "UTF-8")) - - print(f"Going to use username: {nickname}") - # Send user and nickname information to the server - irc_socket.send(bytes(f"USER {nickname} 0 * :{nickname}\r\n", "UTF-8")) - irc_socket.send(bytes(f"NICK {nickname}\r\n", "UTF-8")) - - # Create directories for the server and channel - fifo_files = make_files(base_path, net_name) - - return irc_socket, fifo_files - -def main(): - my_name = sys.argv[0] - my_name_pyless, _ = os.path.splitext(my_name) - base_path = f'/tmp/{my_name_pyless}' - - config_path = '../ircthing3.ini' - - # Read configurations for all topics - 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"] - print(f"{time.time()} | Found configs for {net_name} network.") - server = network["server"] - port = network["port"] - nickname = network["nickname"] - password = network["password"] - irc_socket, fifo_files = connect_to_irc_server(base_path, net_name, server, port, nickname, password) - - router_instance = irc_router(fifo_files, irc_socket, server, nickname) - Threads[net_name] = threading.Thread(target=router_instance.start) - Threads[net_name].daemon = True - Threads[net_name].start() - - for thread in Threads.values(): - print(thread) - thread.join() - - -if __name__ == "__main__": - print(f"{time.time()} | Lets start!") - try: - main() - except Exception as e: - print(f"Got error {e}") - finally: - print(f"{time.time()} | Bye!") - exit(0) diff --git a/ircthing_core.py b/ircthing_core.py index e78e934..7e3492c 100644 --- a/ircthing_core.py +++ b/ircthing_core.py @@ -1,7 +1,38 @@ import time import re import asyncio +import socket import os +from ircthing_utils import make_files + +def connect_to_irc_server( + base_path, + net_name, + server, + port, + nickname, + password=None, +): + + print(f"Going to connect to: {server}") + # Create a socket connection to the IRC server + irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + irc_socket.connect((server, port)) + + # Send the server password if provided + if password: + irc_socket.send(bytes(f"PASS {password}\r\n", "UTF-8")) + + print(f"Going to use username: {nickname}") + # Send user and nickname information to the server + irc_socket.send(bytes(f"USER {nickname} 0 * :{nickname}\r\n", "UTF-8")) + irc_socket.send(bytes(f"NICK {nickname}\r\n", "UTF-8")) + + # Create directories for the server and channel + fifo_files = make_files(base_path, net_name) + + return irc_socket, fifo_files + class irc_router: def __init__(self, fifo_files, irc_socket, server, nickname): diff --git a/ircthing_utils.py b/ircthing_utils.py new file mode 100644 index 0000000..fcde6ef --- /dev/null +++ b/ircthing_utils.py @@ -0,0 +1,51 @@ +import configparser +import os + +def read_config(config_path): + config = configparser.ConfigParser() + config.read(config_path) + + network_configs = [] + + try: + # Collect information from each topic / network + for topic in config.sections(): + network = config[topic] + server = network.get("server") + port = network.getint("port") + channels = network.get("channels", fallback=None) + nickname = network.get("nickname") + password = network.get("password", fallback=None) + + network_config = { + "net_name": network.name, + "server": server, + "port": port, + "channels": channels, + "nickname": nickname, + "password": password, + } + network_configs.append(network_config) + return network_configs + except Exception as e: + print(f"Failure while reading configuration file. {e}") + exit(1) + +def make_files(path, net_name): + + os.makedirs(path, exist_ok=True) + server_dir = os.path.join(path, net_name) + os.makedirs(server_dir, exist_ok=True) + try: + os.mkfifo(f"{server_dir}/in") + except FileExistsError: + pass + try: + os.mkfifo(f"{server_dir}/out") + except FileExistsError: + pass + fifo_files = [] + fifo_files.append(f"{server_dir}/in") + fifo_files.append(f"{server_dir}/out") + return fifo_files +