From b9d55b8aa28b48f58732de87f4dc99c8e3fd070b Mon Sep 17 00:00:00 2001 From: rht Date: Sat, 25 Nov 2023 08:53:04 -0500 Subject: [PATCH] IRC: Convert configuration from CLI args to config file. --- .../bridge_with_irc/irc-mirror.py | 60 +++++++++---------- .../bridge_with_irc/irc_mirror.conf | 17 ++++++ 2 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 zulip/integrations/bridge_with_irc/irc_mirror.conf diff --git a/zulip/integrations/bridge_with_irc/irc-mirror.py b/zulip/integrations/bridge_with_irc/irc-mirror.py index 7aaca7b96..8e255beaf 100755 --- a/zulip/integrations/bridge_with_irc/irc-mirror.py +++ b/zulip/integrations/bridge_with_irc/irc-mirror.py @@ -5,45 +5,45 @@ # import argparse +import configparser import sys import traceback +from typing import Any, Dict import zulip -usage = """./irc-mirror.py --irc-server=IRC_SERVER --channel= --nick-prefix= --stream= [optional args] +usage = """./irc-mirror.py --config irc_mirror.conf +""" -Example: -./irc-mirror.py --irc-server=127.0.0.1 --channel='#test' --nick-prefix=username --stream='test' --topic='#mypy' +def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]: + config: configparser.ConfigParser = configparser.ConfigParser() + config.read(config_file) ---stream is a Zulip stream. ---topic is a Zulip topic, is optionally specified, defaults to "IRC". -Optional arguments: ---nickserv-pw is a password for the nickserv. ---sasl-password is a password for SASL authentication. + config_irc = config["irc"] + for required in ["host", "port", "nickname", "channel"]: + if required not in config_irc: + raise Exception(f"Missing required configuration: {required}") + config_zulip = config["api"] + for required in ["stream", "topic"]: + if required not in config_zulip: + raise Exception(f"Missing required configuration: {required}") -Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options. + return config_irc, config_zulip -Note that "_zulip" will be automatically appended to the IRC nick provided -""" if __name__ == "__main__": parser = zulip.add_default_arguments( argparse.ArgumentParser(usage=usage), allow_provisioning=True ) - parser.add_argument("--irc-server", default=None) - parser.add_argument("--port", default=6667) - parser.add_argument("--nick-prefix", default=None) - parser.add_argument("--channel", default=None) - parser.add_argument("--stream", default="general") - parser.add_argument("--topic", default="IRC") - parser.add_argument("--nickserv-pw", default="") - parser.add_argument("--sasl-password", default=None) + parser.add_argument( + "-c", "--config", required=False, help="Path to the config file for the bridge." + ) options = parser.parse_args() # Setting the client to irc_mirror is critical for this to work options.client = "irc_mirror" - zulip_client = zulip.init_from_options(options) + zulip_client = zulip.Client(config_file=options.config) try: from irc_mirror_backend import IRCBot except ImportError: @@ -54,19 +54,17 @@ ) sys.exit(1) - if options.irc_server is None or options.nick_prefix is None or options.channel is None: - parser.error("Missing required argument") + config_irc, config_zulip = read_configuration(options.config) - nickname = options.nick_prefix + "_zulip" bot = IRCBot( zulip_client, - options.stream, - options.topic, - options.channel, - nickname, - options.irc_server, - options.nickserv_pw, - options.port, - sasl_password=options.sasl_password, + config_zulip["stream"], + config_zulip["topic"], + config_irc["channel"], + config_irc["nickname"], + config_irc["host"], + config_irc.get("nickserv_password", ""), + int(config_irc["port"]), + sasl_password=config_irc.get("sasl_password", None), ) bot.start() diff --git a/zulip/integrations/bridge_with_irc/irc_mirror.conf b/zulip/integrations/bridge_with_irc/irc_mirror.conf new file mode 100644 index 000000000..008c46976 --- /dev/null +++ b/zulip/integrations/bridge_with_irc/irc_mirror.conf @@ -0,0 +1,17 @@ +[irc] +host = irc.libera.chat +port = 6667 +channel = #test +nickname = bridge +# (Optional) Password for the nickserv +# nickserv_password = password +# (Optional) Password for SASL authentication +# sasl_password = password + +# This is for Zulip configuration +[api] +email = glitch-bot@chat.zulip.org +key = aPiKeY +site = https://chat.zulip.org +stream = test here +topic = IRC