63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
|
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)
|