139 lines
3.9 KiB
Python
139 lines
3.9 KiB
Python
import threading
|
|
import os
|
|
import sys
|
|
import time
|
|
import socket
|
|
import configparser
|
|
from ircthing_core import irc_router
|
|
|
|
Threads = {}
|
|
Stop_Toggle = threading.Event()
|
|
|
|
def read_config(config_path):
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
|
|
network_configs = []
|
|
|
|
try:
|
|
# Collect information from each topic / network
|
|
for topic in config.sections():
|
|
network = config[topic]
|
|
server = network.get("server")
|
|
port = network.getint("port")
|
|
channels = network.get("channels", fallback=None)
|
|
nickname = network.get("nickname")
|
|
password = network.get("password", fallback=None)
|
|
|
|
network_config = {
|
|
"net_name": network.name,
|
|
"server": server,
|
|
"port": port,
|
|
"channels": channels,
|
|
"nickname": nickname,
|
|
"password": password,
|
|
}
|
|
network_configs.append(network_config)
|
|
return network_configs
|
|
except Exception as e:
|
|
print(f"Failure while reading configuration file. {e}")
|
|
exit(1)
|
|
|
|
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 make_files(path, net_name):
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
server_dir = os.path.join(path, net_name)
|
|
os.makedirs(server_dir, exist_ok=True)
|
|
try:
|
|
os.mkfifo(f"{server_dir}/in")
|
|
except FileExistsError:
|
|
pass
|
|
try:
|
|
os.mkfifo(f"{server_dir}/out")
|
|
except FileExistsError:
|
|
pass
|
|
fifo_files = []
|
|
fifo_files.append(f"{server_dir}/in")
|
|
fifo_files.append(f"{server_dir}/out")
|
|
return fifo_files
|
|
|
|
def connect_to_irc_server(
|
|
base_path,
|
|
net_name,
|
|
server,
|
|
port,
|
|
nickname,
|
|
password=None,
|
|
):
|
|
|
|
print(f"Going to connect to: {server}")
|
|
# Create a socket connection to the IRC server
|
|
irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
irc_socket.connect((server, port))
|
|
|
|
# Send the server password if provided
|
|
if password:
|
|
irc_socket.send(bytes(f"PASS {password}\r\n", "UTF-8"))
|
|
|
|
print(f"Going to use username: {nickname}")
|
|
# Send user and nickname information to the server
|
|
irc_socket.send(bytes(f"USER {nickname} 0 * :{nickname}\r\n", "UTF-8"))
|
|
irc_socket.send(bytes(f"NICK {nickname}\r\n", "UTF-8"))
|
|
|
|
# Create directories for the server and channel
|
|
fifo_files = make_files(base_path, net_name)
|
|
|
|
return irc_socket, fifo_files
|
|
|
|
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)
|