2024-02-08 21:39:18 +02:00
|
|
|
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-08 21:39:18 +02:00
|
|
|
|
2024-02-09 09:14:24 +02:00
|
|
|
Processes = {}
|
|
|
|
Stop_Toggle = multiprocessing.Event()
|
2024-02-08 21:39:18 +02:00
|
|
|
|
2024-02-09 09:14:24 +02:00
|
|
|
def clean_exit():
|
2024-02-08 21:39:18 +02:00
|
|
|
Stop_Toggle.set()
|
|
|
|
|
|
|
|
def main():
|
2024-02-09 09:14:24 +02:00
|
|
|
root_path = base_path()
|
2024-02-08 21:39:18 +02:00
|
|
|
|
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
|
2024-02-08 21:39:18 +02:00
|
|
|
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-08 21:39:18 +02:00
|
|
|
|
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()
|
2024-02-08 21:39:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
2024-02-08 21:39:18 +02:00
|
|
|
print(f"{time.time()} | Bye!")
|
|
|
|
exit(0)
|