matrix bridge: Configure using config(parser) file instead of python module.
This commit is contained in:
parent
e7e9059cb8
commit
d45b43a9b1
12
zulip/integrations/matrix/matrix_bridge.conf
Normal file
12
zulip/integrations/matrix/matrix_bridge.conf
Normal file
|
@ -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
|
|
@ -7,11 +7,11 @@ import zulip
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
|
import configparser
|
||||||
|
|
||||||
from types import FrameType
|
from types import FrameType
|
||||||
from typing import Any, Callable, Dict, Optional
|
from typing import Any, Callable, Dict, Optional
|
||||||
|
|
||||||
from matrix_bridge_config import config
|
|
||||||
from matrix_client.api import MatrixRequestError
|
from matrix_client.api import MatrixRequestError
|
||||||
from matrix_client.client import MatrixClient
|
from matrix_client.client import MatrixClient
|
||||||
from requests.exceptions import MissingSchema
|
from requests.exceptions import MissingSchema
|
||||||
|
@ -23,6 +23,9 @@ MATRIX_USERNAME_REGEX = '@([a-zA-Z0-9-_]+):matrix.org'
|
||||||
ZULIP_MESSAGE_TEMPLATE = "**{username}**: {message}"
|
ZULIP_MESSAGE_TEMPLATE = "**{username}**: {message}"
|
||||||
MATRIX_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:
|
def matrix_login(matrix_client: Any, matrix_config: Dict[str, Any]) -> None:
|
||||||
try:
|
try:
|
||||||
matrix_client.login_with_password(matrix_config["username"],
|
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():
|
def parse_args():
|
||||||
# type: () -> Any
|
# type: () -> Any
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('-c', '--config', required=True,
|
||||||
|
help="Path to the config file for the bridge.")
|
||||||
parser.add_argument('--no_noise',
|
parser.add_argument('--no_noise',
|
||||||
default=True,
|
default=True,
|
||||||
help="Suppress the IRC join/leave events.")
|
help="Suppress the IRC join/leave events.")
|
||||||
return parser.parse_args()
|
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:
|
def main() -> None:
|
||||||
signal.signal(signal.SIGINT, die)
|
signal.signal(signal.SIGINT, die)
|
||||||
logging.basicConfig(level=logging.WARNING)
|
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"]
|
zulip_config = config["zulip"]
|
||||||
matrix_config = config["matrix"]
|
matrix_config = config["matrix"]
|
||||||
|
|
||||||
options = parse_args()
|
|
||||||
|
|
||||||
# Initiate clients
|
# Initiate clients
|
||||||
backoff = zulip.RandomExponentialBackoff(timeout_success_equivalent=300)
|
backoff = zulip.RandomExponentialBackoff(timeout_success_equivalent=300)
|
||||||
while backoff.keep_going():
|
while backoff.keep_going():
|
||||||
|
|
|
@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue