2024-02-11 09:05:43 +02:00
|
|
|
"""
|
|
|
|
ircthing_utils
|
|
|
|
|
|
|
|
Some functions that make life nicer.
|
|
|
|
"""
|
|
|
|
|
2024-02-08 21:39:18 +02:00
|
|
|
import configparser
|
2024-02-09 09:14:24 +02:00
|
|
|
import argparse
|
2024-02-08 21:39:18 +02:00
|
|
|
import os
|
2024-02-09 09:14:24 +02:00
|
|
|
import sys
|
2024-02-11 09:05:43 +02:00
|
|
|
from typing import Union, List, Tuple
|
|
|
|
|
2024-02-09 09:14:24 +02:00
|
|
|
|
2024-02-11 09:05:43 +02:00
|
|
|
def base_path() -> str:
|
|
|
|
"""
|
|
|
|
Returns location that the script will use as root for its dir structure
|
|
|
|
Returns:
|
|
|
|
str: path for run time files
|
|
|
|
"""
|
2024-02-09 09:14:24 +02:00
|
|
|
my_name = sys.argv[0]
|
|
|
|
my_name_pyless, _ = os.path.splitext(my_name)
|
2024-02-11 09:05:43 +02:00
|
|
|
return f"/tmp/{my_name_pyless}"
|
2024-02-09 09:14:24 +02:00
|
|
|
|
2024-02-11 09:05:43 +02:00
|
|
|
|
|
|
|
def cli_args() -> Union[str, None]:
|
|
|
|
"""
|
|
|
|
Parse arguments and provide small usage message
|
|
|
|
Returns:
|
|
|
|
str or None: path to config file
|
|
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Usage: python3.11 ircthing.py /myconfig.ini"
|
|
|
|
)
|
|
|
|
parser.add_argument("config", help="Path to the configuration file.")
|
2024-02-09 09:14:24 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.config:
|
|
|
|
return args.config
|
|
|
|
return None
|
2024-02-08 21:39:18 +02:00
|
|
|
|
2024-02-11 09:05:43 +02:00
|
|
|
|
|
|
|
def read_config(config_path: str) -> List[dict]:
|
|
|
|
"""
|
|
|
|
Read configuration file and return list of dicts.
|
|
|
|
Dictionaries hold information about irc networks/server to connecto to.
|
|
|
|
args:
|
|
|
|
str: Path to the configuration file.
|
|
|
|
Returns:
|
|
|
|
list[{}, {}]: List of dictionaries.
|
|
|
|
"""
|
2024-02-08 21:39:18 +02:00
|
|
|
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
|
2024-02-11 09:05:43 +02:00
|
|
|
except Exception as config_read_error:
|
|
|
|
print(f"Failure while reading configuration file. {config_read_error}")
|
|
|
|
sys.exit(1)
|
2024-02-08 21:39:18 +02:00
|
|
|
|
|
|
|
|
2024-02-11 09:05:43 +02:00
|
|
|
def make_files(path: str, net_name: str) -> Tuple[List[str], str]:
|
|
|
|
"""
|
|
|
|
Make directories and fifo files need to make irc server connection.
|
|
|
|
Args:
|
|
|
|
str: The root path for run time files.
|
|
|
|
str: The network name of the irc server we are joining
|
|
|
|
Returns:
|
|
|
|
list: List containing the fio files created.
|
|
|
|
str: Path to the directory created for the network connection.
|
|
|
|
"""
|
2024-02-08 21:39:18 +02:00
|
|
|
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")
|
2024-02-09 09:14:24 +02:00
|
|
|
return fifo_files, server_dir
|