Esempio n. 1
0
def _str_value(value: Any) -> str:
    stream = StringIO()
    yaml.dump(value, stream)
    value_str = stream.getvalue()
    if "\n" in value_str:
        return f"\n```yaml\n{value_str}\n```\n"
    else:
        return f"`{value_str}`"
Esempio n. 2
0
def config_defaults(evt: CommandEvent) -> Awaitable[EventID]:
    stream = StringIO()
    yaml.dump(
        {
            "bridge_notices": {
                "default": evt.config["bridge.bridge_notices.default"],
                "exceptions": evt.config["bridge.bridge_notices.exceptions"],
            },
            "bot_messages_as_notices":
            evt.config["bridge.bot_messages_as_notices"],
            "inline_images": evt.config["bridge.inline_images"],
            "message_formats": evt.config["bridge.message_formats"],
            "state_event_formats": evt.config["bridge.state_event_formats"],
            "telegram_link_preview":
            evt.config["bridge.telegram_link_preview"],
        }, stream)
    return evt.reply(
        f"Bridge instance wide config:\n\n```yaml\n{stream.getvalue()}```")
Esempio n. 3
0
 def save(self) -> None:
     super().save()
     if self._registration and self.registration_path:
         with open(self.registration_path, "w") as stream:
             yaml.dump(self._registration, stream)
Esempio n. 4
0
def config_view(evt: CommandEvent, portal: po.Portal) -> Awaitable[EventID]:
    stream = StringIO()
    yaml.dump(portal.local_config, stream)
    return evt.reply(
        f"Room-specific config:\n\n```yaml\n{stream.getvalue()}```")
Esempio n. 5
0
def main():
    parser = argparse.ArgumentParser(
        prog=os.path.basename(sys.executable) + " -m " + __package__,
        description=f"a bouncer-style Matrix IRC bridge (v{__version__})",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "-v",
        "--verbose",
        help="logging verbosity level: once is info, twice is debug",
        action="count",
        default=0)
    req = parser.add_mutually_exclusive_group(required=True)
    req.add_argument(
        "-c",
        "--config",
        help="registration YAML file path, must be writable if generating",
    )
    req.add_argument(
        "--version",
        action="store_true",
        help="show bridge version",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "-l",
        "--listen-address",
        help=
        "bridge listen address (default: as specified in url in config, 127.0.0.1 otherwise)",
    )
    parser.add_argument(
        "-p",
        "--listen-port",
        help=
        "bridge listen port (default: as specified in url in config, 9898 otherwise)",
        type=int,
    )
    parser.add_argument("-u", "--uid", help="user id to run as", default=None)
    parser.add_argument("-g", "--gid", help="group id to run as", default=None)
    parser.add_argument("-i",
                        "--identd",
                        action="store_true",
                        help="enable identd service")
    parser.add_argument("--identd-port",
                        type=int,
                        default="113",
                        help="identd listen port")
    parser.add_argument(
        "--generate",
        action="store_true",
        help="generate registration YAML for Matrix homeserver (Synapse)",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--generate-compat",
        action="store_true",
        help=
        "generate registration YAML for Matrix homeserver (Dendrite and Conduit)",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--reset",
        action="store_true",
        help="reset ALL bridge configuration from homeserver and exit",
        default=argparse.SUPPRESS,
    )
    parser.add_argument(
        "--safe-mode",
        action="store_true",
        help=
        "prevent appservice from leaving invalid rooms on startup (for debugging)",
    )
    parser.add_argument(
        "-o",
        "--owner",
        help=
        "set owner MXID (eg: @user:homeserver) or first talking local user will claim the bridge",
        default=None,
    )
    parser.add_argument(
        "homeserver",
        nargs="?",
        help="URL of Matrix homeserver",
        default="http://*****:*****@irc_.*",
                    "exclusive": True
                }],
                "aliases": [],
                "rooms": [],
            },
        }

        if "generate_compat" in args:
            registration["namespaces"]["users"].append({
                "regex": "@heisenbridge:.*",
                "exclusive": True
            })

        if os.path.isfile(args.config):
            print("Registration file already exists, not overwriting.")
            sys.exit(1)

        if args.config == "-":
            yaml.dump(registration, sys.stdout)
        else:
            with open(args.config, "w") as f:
                yaml.dump(registration, f)

            print(f"Registration file generated and saved to {args.config}")
    elif "reset" in args:
        service = BridgeAppService()
        loop = asyncio.get_event_loop()
        loop.run_until_complete(service.reset(args.config, args.homeserver))
        loop.close()
    elif "version" in args:
        print(__version__)
    else:
        loop = asyncio.get_event_loop()
        service = BridgeAppService()
        identd = None

        service.load_reg(args.config)

        if args.identd:
            identd = Identd()
            loop.run_until_complete(
                identd.start_listening(service, args.identd_port))

        if os.getuid() == 0:
            if args.gid:
                gid = grp.getgrnam(args.gid).gr_gid
                os.setgid(gid)
                os.setgroups([])

            if args.uid:
                uid = pwd.getpwnam(args.uid).pw_uid
                os.setuid(uid)

        os.umask(0o077)

        listen_address = args.listen_address
        listen_port = args.listen_port

        if not listen_address:
            try:
                url = urllib.parse.urlparse(service.registration["url"])
                listen_address = url.hostname
            except Exception:
                listen_address = "127.0.0.1"

        if not listen_port:
            try:
                url = urllib.parse.urlparse(service.registration["url"])
                listen_port = url.port
            except Exception:
                listen_port = 9898

        loop.run_until_complete(
            service.run(listen_address, listen_port, args.homeserver,
                        args.owner, args.safe_mode))
        loop.close()