Пример #1
0
    def __init__(self, cfg, loop):
        self.loop = loop
        # List of bridges
        self.bridges = list()
        # Mapping of MUC-JID -> Nickname
        self.mucs = dict()
        # Mapping of MUC-JID -> Password
        self.muc_passwords = dict()

        try:
            # Get the optional XMPP address (host, port) if specified
            xmpp_address = tuple()
            if 'host' in cfg['xmpp']:
                xmpp_address = (cfg['xmpp']['host'], cfg['xmpp']['port'])
            # Parse the MUC definitions
            self.get_mucs(cfg)
            if len(self.mucs) == 0:
                logging.info("No MUCs defined.")
        except KeyError:
            raise InvalidConfigError

        # Parse the bridges from the config file
        need_incoming_webhooks = False
        for bridge_cfg in cfg['bridges']:
            bridge = SingleBridge(bridge_cfg, self)
            self.bridges.append(bridge)
            if bridge.has_incoming_webhooks():
                need_incoming_webhooks = True

        # Initialize XMPP client
        self.xmpp_client = XMPPBridgeBot(cfg['xmpp']['jid'],
                                         cfg['xmpp']['password'], self)
        self.xmpp_client.connect(address=xmpp_address)

        # Initialize HTTP server if needed
        if not need_incoming_webhooks:
            self.http_server = None
            logging.info("No incoming webhooks defined.")
        elif 'incoming_webhook_listener' in cfg:
            bind_address = cfg['incoming_webhook_listener']['bind_address']
            port = cfg['incoming_webhook_listener']['port']
            self.http_app = aiohttp.web.Application(loop=loop)
            self.http_app.router.add_route('POST', '/',
                                           self.handle_incoming_webhook)
            self.http_handler = self.http_app.make_handler()
            http_create_server = loop.create_server(self.http_handler,
                                                    bind_address, port)
            self.http_server = loop.run_until_complete(http_create_server)
            logging.info("Listening for incoming webhooks on "
                         "http://{}:{}/".format(bind_address, port))
        else:
            self.http_server = None
            logging.warn("No 'incoming_webhook_listener' in the config even "
                         "though incoming webhooks are defined. Ignoring all "
                         "incoming webhooks.")

        if not self.http_server:
            logging.info("Not listening for incoming webhooks.")
Пример #2
0
    def __init__(self, cfg, loop):
        self.loop = loop
        self.incoming_normal_mappings = defaultdict(set)
        self.incoming_muc_mappings = defaultdict(set)
        self.outgoing_mappings = defaultdict(list)
        # Mapping of MUC-JID -> Nickname
        self.mucs = dict()
        # Mapping of MUC-JID -> Password
        self.muc_passwords = dict()

        try:
            # Get the optional XMPP address (host, port) if specified
            xmpp_address = tuple()
            if 'host' in cfg['xmpp']:
                xmpp_address = (cfg['xmpp']['host'], cfg['xmpp']['port'])

            self.get_mucs(cfg)
            self.get_outgoing_mappings(cfg)
            self.get_incoming_mappings(cfg)
        except KeyError:
            raise InvalidConfigError

        # Initialize XMPP client
        self.xmpp_client = XMPPBridgeBot(cfg['xmpp']['jid'],
                                         cfg['xmpp']['password'], self)
        self.xmpp_client.connect(address=xmpp_address)

        # Initialize HTTP server if needed
        incoming_webhooks_count = (len(self.incoming_muc_mappings) +
                                   len(self.incoming_normal_mappings))
        if incoming_webhooks_count == 0:
            self.http_server = None
            logging.info("No incoming webhooks defined.")
        elif 'incoming_webhook_listener' in cfg:
            bind_address = cfg['incoming_webhook_listener']['bind_address']
            port = cfg['incoming_webhook_listener']['port']
            self.http_app = aiohttp.web.Application(loop=loop)
            self.http_app.router.add_route('POST', '/', self.handle_incoming)
            self.http_handler = self.http_app.make_handler()
            http_create_server = loop.create_server(self.http_handler,
                                                    bind_address, port)
            self.http_server = loop.run_until_complete(http_create_server)
            logging.info("Listening for incoming webhooks on "
                         "http://{}:{}/".format(bind_address, port))
        else:
            self.http_server = None
            logging.warn("No 'incoming_webhook_listener' in the config even "
                         "though incoming webhooks are defined. Ignoring all "
                         "incoming webhooks.")

        if not self.http_server:
            logging.info("Not listening for incoming webhooks.")