ircthing/ircthing.py

75 lines
2.0 KiB
Python
Raw Normal View History

2024-02-11 09:05:43 +02:00
"""
My attempt to mimic Suckless.org's ii (irc it) software
"""
import sys
2024-02-09 09:14:24 +02:00
import time
import multiprocessing
2024-02-11 09:05:43 +02:00
from ircthing_core import irc_router, connect_to_irc_server
from ircthing_utils import read_config, cli_args, base_path
2024-02-09 09:14:24 +02:00
Processes = {}
Stop_Toggle = multiprocessing.Event()
2024-02-11 09:05:43 +02:00
2024-02-09 09:14:24 +02:00
def clean_exit():
2024-02-11 09:05:43 +02:00
"""
Sets the Stop_Toggle event to signal clean exit.
"""
Stop_Toggle.set()
2024-02-11 09:05:43 +02:00
def main():
2024-02-11 09:05:43 +02:00
"""
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.
"""
2024-02-09 09:14:24 +02:00
root_path = base_path()
2024-02-09 09:14:24 +02:00
# Get configuration file path if given
2024-02-11 09:05:43 +02:00
config_path = "config.ini"
2024-02-09 09:14:24 +02:00
argument = cli_args()
if argument:
config_path = argument
2024-02-11 09:05:43 +02:00
# Read configuration
network_configs = read_config(config_path)
## Get irc socket for each network in configuration
2024-02-11 09:05:43 +02:00
## 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"]
2024-02-09 09:14:24 +02:00
print(f"{time.time()} | Found configs for {net_name} network.")
2024-02-11 09:05:43 +02:00
irc_socket, fifo_files, network_dir = connect_to_irc_server(
root_path, net_name, server, port, nickname, password
)
2024-02-09 09:14:24 +02:00
2024-02-11 09:05:43 +02:00
router_instance = irc_router(
fifo_files, irc_socket, server, nickname, network_dir
)
2024-02-09 09:14:24 +02:00
Processes[net_name] = multiprocessing.Process(target=router_instance.start)
Processes[net_name].daemon = True
Processes[net_name].start()
2024-02-11 09:05:43 +02:00
# Wait for all the sub processes to end.
2024-02-09 09:14:24 +02:00
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:
2024-02-09 09:14:24 +02:00
clean_exit()
print(f"{time.time()} | Bye!")
2024-02-11 09:05:43 +02:00
sys.exit(0)