ircthing/ircthing.py
2024-02-11 09:05:43 +02:00

75 lines
2.0 KiB
Python

"""
My attempt to mimic Suckless.org's ii (irc it) software
"""
import sys
import time
import multiprocessing
from ircthing_core import irc_router, connect_to_irc_server
from ircthing_utils import read_config, cli_args, base_path
Processes = {}
Stop_Toggle = multiprocessing.Event()
def clean_exit():
"""
Sets the Stop_Toggle event to signal clean exit.
"""
Stop_Toggle.set()
def main():
"""
Main function to initialize irc connections specified on the configuration file.
This will run each irc server connection in invidual sub process.
Hold the process handlers on list.
Kill them with Stop_Toggle.
"""
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()
# Wait for all the sub processes to end.
for process in Processes.values():
process.join()
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!")
sys.exit(0)