Exemple #1
0
 def fetch(self):
     filter = {
         'user_id': self.user_id
     }
     if self.network_id:
         filter['network_id'] = self.network_id
     results = yield from ChannelStore.get({
         'filter': filter
     }, raw=True)
     return results
Exemple #2
0
 def _on_network_create_update(self, network):
     if network.status == '0':
         bot = self.bots.get(network.id)
         if bot and not bot.protocol.closed:
             return
         user = yield from UserStore.get({'query': network.user_id})
         logger.debug(
             'Creating new bot: {}-{}'.format(
                 user.username, network.name)
         )
         connected_channels = yield from ChannelStore.get({
             'filter': dict(
                 network_id=network.id,
                 status='1'
             )
         })
         config = dict(
             id=network.id,
             user_id=user.id,
             name=network.name,
             username=network.username or user.username,
             user=user.username,
             realname=network.realname or ' ',
             password=network.password,
             nick=network.nickname,
             host=network.hostname,
             port=network.port,
             ssl=network.ssl,
             ssl_verify=network.ssl_verify,
             lock=asyncio.Lock(),
             autojoins=[channel.name for channel in connected_channels]
         )
         # Acquire lock for connecting to IRC server, so that
         # other clients connecting to the same bot can wait on this
         # lock before trying to send messages to the bot
         yield from config['lock'].acquire()
         if bot and bot.protocol.closed:
             bot.reload_config(**config)
         else:
             bot = IrcbBot(**config)
         bot.run_in_loop()
         self.register_bot(network.id, bot)
     elif network.status == '2':
         bot = self.bots.get(network.id)
         if bot:
             bot.quit()
             bot.protocol.close()
Exemple #3
0
    def get_bot_handle(self, network, client):
        try:
            key = network.id
            bot = self.bots.get(key)
            self.register_client(key, client)
            connected_channels = yield from ChannelStore.get({
                'filter': dict(
                    network_id=key,
                    status='1'
                )
            })
            if bot is None:
                yield from NetworkStore.update(
                    dict(
                        filter=('id', network.id),
                        update={
                            'status': '0'
                        })
                )
                return
            logger.debug('Reusing existing bot: {}'.format(bot))
            # Wait for bot.config.lock to be release when connection is
            # made to remote IRC server
            if bot.config.lock:
                yield from bot.config.lock
                bot.config.lock = None
            joining_messages_list = [
                ':* 001 {nick} :You are now connected to {network}'.format(
                    nick=bot.nick, network=network.name),
                ':* 251 {nick} : '.format(nick=bot.nick)
            ]
            for channel in connected_channels:
                joining_messages_list.append(
                    ':{nick}!* JOIN {channel}'.format(
                        nick=bot.nick,
                        channel=channel.name)
                )
                bot.raw('NAMES %s' % channel.name)

            client.send(*['\r\n'.join(joining_messages_list)])

            return bot
        except Exception as e:
            logger.error('get_bot_handle error: {}'.format(e),
                         exc_info=True)
Exemple #4
0
    def get_bot_handle(self, network, client):
        try:
            key = network.id
            bot = self.bots.get(key)
            self.register_client(key, client)
            connected_channels = yield from ChannelStore.get({
                'query': dict(
                    network_id=key,
                    status='1'
                )
            })
            if bot is None:
                user = yield from UserStore.get({'query': network.user_id})
                logger.debug(
                    'Creating new bot: {}-{}'.format(
                        user.username, network.name)
                )
                yield from NetworkStore.update(
                    dict(
                        filter=('id', network.id),
                        update={
                            'status': '0'
                        })
                )
                config = dict(
                    id=network.id,
                    name=network.name,
                    userinfo=network.username,
                    password=network.password,
                    nick=network.nickname,
                    realname=network.realname,
                    host=network.hostname,
                    port=network.port,
                    ssl=network.ssl,
                    ssl_verify=network.ssl_verify.code,
                    lock=asyncio.Lock(),
                    autojoins=[channel.name for channel in connected_channels]
                )
                # Acquire lock for connecting to IRC server, so that
                # other clients connecting to the same bot can wait on this
                # lock before trying to send messages to the bot
                yield from config['lock'].acquire()
                bot = IrcbBot(**config)
                bot.run_in_loop()
                self.register_bot(key, bot)
            else:
                logger.debug('Reusing existing bot: {}'.format(bot))
                # Wait for bot.config.lock to be release when connection is
                # made to remote IRC server
                if bot.config.lock:
                    yield from bot.config.lock
                    bot.config.lock = None
                joining_messages_list = [
                    ':* 001 {nick} :You are now connected to {network}'.format(
                        nick=bot.nick, network=network.name),
                    ':* 251 {nick} : '.format(nick=bot.nick)
                ]
                for channel in connected_channels:
                    joining_messages_list.append(
                        ':{nick}!* JOIN {channel}'.format(
                            nick=bot.nick,
                            channel=channel.name)
                    )
                    bot.raw('NAMES %s' % channel.name)

                client.send(*['\r\n'.join(joining_messages_list)])

            def forward(line):
                bot = self.bots.get(key)
                if bot:
                    logger.debug(
                        'Forwarding {}\t {}'.format(bot, line))
                    bot.raw(line)

            return forward
        except Exception as e:
            logger.error('get_bot_handle error: {}'.format(e),
                         exc_info=True)
Exemple #5
0
    def get_bot_handle(self, network, client):
        try:
            key = network.id
            bot = self.bots.get(key)
            self.register_client(key, client)
            connected_channels = yield from ChannelStore.get(
                {'query': dict(network_id=key, status='1')})
            if bot is None:
                user = yield from UserStore.get({'query': network.user_id})
                logger.debug('Creating new bot: {}-{}'.format(
                    user.username, network.name))
                yield from NetworkStore.update(
                    dict(filter=('id', network.id), update={'status': '0'}))
                config = dict(
                    id=network.id,
                    name=network.name,
                    userinfo=network.username,
                    password=network.password,
                    nick=network.nickname,
                    realname=network.realname,
                    host=network.hostname,
                    port=network.port,
                    ssl=network.ssl,
                    ssl_verify=network.ssl_verify.code,
                    lock=asyncio.Lock(),
                    autojoins=[channel.name for channel in connected_channels])
                # Acquire lock for connecting to IRC server, so that
                # other clients connecting to the same bot can wait on this
                # lock before trying to send messages to the bot
                yield from config['lock'].acquire()
                bot = IrcbBot(**config)
                bot.run_in_loop()
                self.register_bot(key, bot)
            else:
                logger.debug('Reusing existing bot: {}'.format(bot))
                # Wait for bot.config.lock to be release when connection is
                # made to remote IRC server
                if bot.config.lock:
                    yield from bot.config.lock
                    bot.config.lock = None
                joining_messages_list = [
                    ':* 001 {nick} :You are now connected to {network}'.format(
                        nick=bot.nick, network=network.name),
                    ':* 251 {nick} : '.format(nick=bot.nick)
                ]
                for channel in connected_channels:
                    joining_messages_list.append(
                        ':{nick}!* JOIN {channel}'.format(
                            nick=bot.nick, channel=channel.name))
                    bot.raw('NAMES %s' % channel.name)

                client.send(*['\r\n'.join(joining_messages_list)])

            def forward(line):
                bot = self.bots.get(key)
                if bot:
                    logger.debug('Forwarding {}\t {}'.format(bot, line))
                    bot.raw(line)

            return forward
        except Exception as e:
            logger.error('get_bot_handle error: {}'.format(e), exc_info=True)