|
5 | 5 | #
|
6 | 6 |
|
7 | 7 | import argparse
|
| 8 | +import configparser |
8 | 9 | import sys
|
9 | 10 | import traceback
|
| 11 | +from typing import Any, Dict, Tuple |
10 | 12 |
|
11 | 13 | import zulip
|
12 | 14 |
|
13 |
| -usage = """./irc-mirror.py --irc-server=IRC_SERVER --channel=<CHANNEL> --nick-prefix=<NICK> --stream=<STREAM> [optional args] |
| 15 | +usage = """./irc-mirror.py --config irc_mirror.conf |
| 16 | +""" |
14 | 17 |
|
15 |
| -Example: |
16 | 18 |
|
17 |
| -./irc-mirror.py --irc-server=127.0.0.1 --channel='#test' --nick-prefix=username --stream='test' --topic='#mypy' |
| 19 | +class BridgeConfigError(Exception): |
| 20 | + pass |
18 | 21 |
|
19 |
| ---stream is a Zulip stream. |
20 |
| ---topic is a Zulip topic, is optionally specified, defaults to "IRC". |
21 |
| -Optional arguments: |
22 |
| ---nickserv-pw is a password for the nickserv. |
23 |
| ---sasl-password is a password for SASL authentication. |
24 | 22 |
|
25 |
| -Specify your Zulip API credentials and server in a ~/.zuliprc file or using the options. |
| 23 | +def read_configuration( |
| 24 | + config_file: str, |
| 25 | +) -> Tuple[configparser.SectionProxy, configparser.SectionProxy]: |
| 26 | + config: configparser.ConfigParser = configparser.ConfigParser() |
| 27 | + config.read(config_file) |
| 28 | + |
| 29 | + config_irc = config["irc"] |
| 30 | + for required in ["host", "port", "nickname", "channel"]: |
| 31 | + if required not in config_irc: |
| 32 | + raise BridgeConfigError(f"Missing required configuration: {required}") |
| 33 | + config_zulip = config["api"] |
| 34 | + for required in ["stream", "topic"]: |
| 35 | + if required not in config_zulip: |
| 36 | + raise BridgeConfigError(f"Missing required configuration: {required}") |
| 37 | + |
| 38 | + return config_irc, config_zulip |
26 | 39 |
|
27 |
| -Note that "_zulip" will be automatically appended to the IRC nick provided |
28 |
| -""" |
29 | 40 |
|
30 | 41 | if __name__ == "__main__":
|
31 | 42 | parser = zulip.add_default_arguments(
|
32 | 43 | argparse.ArgumentParser(usage=usage), allow_provisioning=True
|
33 | 44 | )
|
34 |
| - parser.add_argument("--irc-server", default=None) |
35 |
| - parser.add_argument("--port", default=6667) |
36 |
| - parser.add_argument("--nick-prefix", default=None) |
37 |
| - parser.add_argument("--channel", default=None) |
38 |
| - parser.add_argument("--stream", default="general") |
39 |
| - parser.add_argument("--topic", default="IRC") |
40 |
| - parser.add_argument("--nickserv-pw", default="") |
41 |
| - parser.add_argument("--sasl-password", default=None) |
| 45 | + parser.add_argument( |
| 46 | + "-c", "--config", required=False, help="Path to the config file for the bridge." |
| 47 | + ) |
42 | 48 |
|
43 | 49 | options = parser.parse_args()
|
44 | 50 | # Setting the client to irc_mirror is critical for this to work
|
45 | 51 | options.client = "irc_mirror"
|
46 |
| - zulip_client = zulip.init_from_options(options) |
| 52 | + zulip_client = zulip.Client(config_file=options.config) |
47 | 53 | try:
|
48 | 54 | from irc_mirror_backend import IRCBot
|
49 | 55 | except ImportError:
|
|
54 | 60 | )
|
55 | 61 | sys.exit(1)
|
56 | 62 |
|
57 |
| - if options.irc_server is None or options.nick_prefix is None or options.channel is None: |
58 |
| - parser.error("Missing required argument") |
| 63 | + config_irc, config_zulip = read_configuration(options.config) |
59 | 64 |
|
60 |
| - nickname = options.nick_prefix + "_zulip" |
61 | 65 | bot = IRCBot(
|
62 | 66 | zulip_client,
|
63 |
| - options.stream, |
64 |
| - options.topic, |
65 |
| - options.channel, |
66 |
| - nickname, |
67 |
| - options.irc_server, |
68 |
| - options.nickserv_pw, |
69 |
| - options.port, |
70 |
| - sasl_password=options.sasl_password, |
| 67 | + config_zulip["stream"], |
| 68 | + config_zulip["topic"], |
| 69 | + config_irc["channel"], |
| 70 | + config_irc["nickname"], |
| 71 | + config_irc["host"], |
| 72 | + config_irc.get("nickserv_password", ""), |
| 73 | + int(config_irc["port"]), |
| 74 | + sasl_password=config_irc.get("sasl_password", None), |
71 | 75 | )
|
72 | 76 | bot.start()
|
0 commit comments