ircthing/ircthing.py

81 lines
2.1 KiB
Python
Raw Normal View History

from ircthing_core import irc_router, connect_to_irc_server
2024-02-09 09:14:24 +02:00
from ircthing_utils import read_config, cli_args, base_path
import time
import os
import multiprocessing
2024-02-09 09:14:24 +02:00
Processes = {}
Stop_Toggle = multiprocessing.Event()
2024-02-09 09:14:24 +02:00
def clean_exit():
Stop_Toggle.set()
def main():
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
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"]
2024-02-09 09:14:24 +02:00
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
2024-02-09 09:14:24 +02:00
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:
2024-02-09 09:14:24 +02:00
clean_exit()
print(f"{time.time()} | Bye!")
exit(0)