From ed90448164d9ee05149da04af72bd462769a4d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TC=C2=B2?= <130875305+TheCommsChannel@users.noreply.github.com> Date: Tue, 9 Jul 2024 14:05:33 -0400 Subject: [PATCH] Urgent board permissions This adds a feature for Urgent board permissions where you can limit who can post to the urgent board via a list in config.ini --- command_handlers.py | 9 +++++++-- config_init.py | 30 +++++++++++++++++++++++------- server.py | 8 +++----- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/command_handlers.py b/command_handlers.py index 9e96746..49df599 100644 --- a/command_handlers.py +++ b/command_handlers.py @@ -129,7 +129,7 @@ def handle_bb_steps(sender_id, message, step, state, interface, bbs_nodes): if message.lower() == 'e': handle_help_command(sender_id, interface, 'bbs') return - board_name = state['board'] + board_name = boards[int(message)] response = f"What would you like to do in the {board_name} board?\n[R]ead [P]ost" send_message(response, sender_id, interface) update_user_state(sender_id, {'command': 'BULLETIN_ACTION', 'step': 2, 'board': board_name}) @@ -147,6 +147,12 @@ def handle_bb_steps(sender_id, message, step, state, interface, bbs_nodes): send_message(f"No bulletins in {board_name}.", sender_id, interface) handle_bb_steps(sender_id, 'e', 1, state, interface, bbs_nodes) elif message.lower() == 'p': + if board_name.lower() == 'urgent': + node_id = get_node_id_from_num(sender_id, interface) + allowed_nodes = interface.allowed_nodes + if allowed_nodes and node_id not in allowed_nodes: + send_message("You don't have permission to post to this board.", sender_id, interface) + return send_message("What is the subject of your bulletin? Keep it short.", sender_id, interface) update_user_state(sender_id, {'command': 'BULLETIN_POST', 'step': 4, 'board': board_name}) @@ -182,7 +188,6 @@ def handle_bb_steps(sender_id, message, step, state, interface, bbs_nodes): update_user_state(sender_id, state) - def handle_mail_steps(sender_id, message, step, state, interface, bbs_nodes): if step == 1: choice = message.lower() diff --git a/config_init.py b/config_init.py index 17820b7..f1ad993 100644 --- a/config_init.py +++ b/config_init.py @@ -80,9 +80,11 @@ def merge_config(system_config:dict[str, Any], args:argparse.Namespace) -> dict[ return system_config -def initialize_config(config_file:str = None) -> dict[str, Any]: - """Function reads and parses system configuration file - + +def initialize_config(config_file: str = None) -> dict[str, Any]: + """ + Function reads and parses system configuration file + Returns a dict with the following entries: config - parsed config file interface_type - type of the active interface @@ -97,24 +99,38 @@ def initialize_config(config_file:str = None) -> dict[str, Any]: dict: dict with system configuration, ad described above """ config = configparser.ConfigParser() - + if config_file is None: config_file = "config.ini" config.read(config_file) interface_type = config['interface']['type'] hostname = config['interface'].get('hostname', None) - port = config['interface'].get('port', None) + port = config['interface'].get('port', None) bbs_nodes = config.get('sync', 'bbs_nodes', fallback='').split(',') if bbs_nodes == ['']: bbs_nodes = [] - return {'config':config, 'interface_type': interface_type, 'hostname': hostname, 'port': port, 'bbs_nodes': bbs_nodes, 'mqtt_topic': 'meshtastic.receive'} + allowed_nodes = config.get('allow_list', 'allowed_nodes', fallback='').split(',') + if allowed_nodes == ['']: + allowed_nodes = [] + + return { + 'config': config, + 'interface_type': interface_type, + 'hostname': hostname, + 'port': port, + 'bbs_nodes': bbs_nodes, + 'allowed_nodes': allowed_nodes, + 'mqtt_topic': 'meshtastic.receive' + } + def get_interface(system_config:dict[str, Any]) -> meshtastic.stream_interface.StreamInterface: - """Function opens and returns an instance meshtastic interface of type specified by the configuration + """ + Function opens and returns an instance meshtastic interface of type specified by the configuration Function creates and returns an instance of a class inheriting from meshtastic.stream_interface.StreamInterface. The type of the class depends on the type of the interface specified by the system configuration. diff --git a/server.py b/server.py index a31511a..52ce1f7 100644 --- a/server.py +++ b/server.py @@ -39,19 +39,17 @@ Meshtastic Version def main(): display_banner() - # config, interface_type, hostname, port, bbs_nodes = initialize_config() args = init_cli_parser() config_file = None if args.config is not None: config_file = args.config system_config = initialize_config(config_file) - + merge_config(system_config, args) - - # print(f"{system_config=}") - + interface = get_interface(system_config) interface.bbs_nodes = system_config['bbs_nodes'] + interface.allowed_nodes = system_config['allowed_nodes'] logging.info(f"TC²-BBS is running on {system_config['interface_type']} interface...")