From d45b43a9b133f0eef40805c98adba77efcbf408b Mon Sep 17 00:00:00 2001 From: "neiljp (Neil Pilgrim)" Date: Thu, 31 May 2018 10:29:00 -0700 Subject: [PATCH] matrix bridge: Configure using config(parser) file instead of python module. --- zulip/integrations/matrix/matrix_bridge.conf | 12 +++++++ zulip/integrations/matrix/matrix_bridge.py | 33 ++++++++++++++++--- .../matrix/matrix_bridge_config.py | 15 --------- 3 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 zulip/integrations/matrix/matrix_bridge.conf delete mode 100644 zulip/integrations/matrix/matrix_bridge_config.py diff --git a/zulip/integrations/matrix/matrix_bridge.conf b/zulip/integrations/matrix/matrix_bridge.conf new file mode 100644 index 0000000..c1ebcf8 --- /dev/null +++ b/zulip/integrations/matrix/matrix_bridge.conf @@ -0,0 +1,12 @@ +[matrix] +host = https://matrix.org +username = username +password = password +room_id = #zulip:matrix.org + +[zulip] +email = glitch-bot@chat.zulip.org +api_key = aPiKeY +site = https://chat.zulip.org +stream = test here +topic = matrix diff --git a/zulip/integrations/matrix/matrix_bridge.py b/zulip/integrations/matrix/matrix_bridge.py index 9191d01..5be58e5 100644 --- a/zulip/integrations/matrix/matrix_bridge.py +++ b/zulip/integrations/matrix/matrix_bridge.py @@ -7,11 +7,11 @@ import zulip import sys import argparse import re +import configparser from types import FrameType from typing import Any, Callable, Dict, Optional -from matrix_bridge_config import config from matrix_client.api import MatrixRequestError from matrix_client.client import MatrixClient from requests.exceptions import MissingSchema @@ -23,6 +23,9 @@ MATRIX_USERNAME_REGEX = '@([a-zA-Z0-9-_]+):matrix.org' ZULIP_MESSAGE_TEMPLATE = "**{username}**: {message}" MATRIX_MESSAGE_TEMPLATE = "<{username}> {message}" +class Bridge_ConfigException(Exception): + pass + def matrix_login(matrix_client: Any, matrix_config: Dict[str, Any]) -> None: try: matrix_client.login_with_password(matrix_config["username"], @@ -149,21 +152,43 @@ def check_zulip_message_validity(msg: Dict[str, Any], config: Dict[str, Any]) -> def parse_args(): # type: () -> Any parser = argparse.ArgumentParser() + parser.add_argument('-c', '--config', required=True, + help="Path to the config file for the bridge.") parser.add_argument('--no_noise', default=True, help="Suppress the IRC join/leave events.") return parser.parse_args() +def read_configuration(config_file: str) -> Dict[str, Dict[str, str]]: + config = configparser.ConfigParser() + + try: + config.read(config_file) + except configparser.Error as e: + raise Bridge_ConfigException(str(e)) + + if set(config.sections()) != {'matrix', 'zulip'}: + raise Bridge_ConfigException("Please ensure the configuration has zulip & matrix sections.") + + # TODO Could add more checks for configuration content here + + return {section: dict(config[section]) for section in config.sections()} + def main() -> None: signal.signal(signal.SIGINT, die) logging.basicConfig(level=logging.WARNING) - # Get config for each clients + options = parse_args() + + try: + config = read_configuration(options.config) + except Bridge_ConfigException as exception: + sys.exit("Could not parse config file: {}".format(exception)) + + # Get config for each client zulip_config = config["zulip"] matrix_config = config["matrix"] - options = parse_args() - # Initiate clients backoff = zulip.RandomExponentialBackoff(timeout_success_equivalent=300) while backoff.keep_going(): diff --git a/zulip/integrations/matrix/matrix_bridge_config.py b/zulip/integrations/matrix/matrix_bridge_config.py deleted file mode 100644 index ca8e251..0000000 --- a/zulip/integrations/matrix/matrix_bridge_config.py +++ /dev/null @@ -1,15 +0,0 @@ -config = { - "matrix": { - "host": "https://matrix.org", - "username": "username", - "password": "password", - "room_id": "#zulip:matrix.org" - }, - "zulip": { - "email": "glitch-bot@chat.zulip.org", - "api_key": "aPiKeY", - "site": "https://chat.zulip.org", - "stream": "test here", - "topic": "matrix" - } -}