Exemplo n.º 1
0
 def _create_or_update(self, data, username, network=None):
     if network:
         network_data = network.to_dict()
         network_data.update(data)
         form = NetworkForm(formdata=MultiDict(network_data))
     else:
         form = NetworkForm(formdata=MultiDict(data))
     form.validate()
     if form.errors:
         return web.Response(
             body=json.dumps(form.errors).encode(),
             status=400,
             content_type='application/json')
     cleaned_data = form.data
     cleaned_data['user'] = username
     if network:
         network = yield from NetworkStore.update(
             dict(
                 filter=('id', network.id),
                 update=cleaned_data
             )
         )
     else:
         network = yield from NetworkStore.create(**cleaned_data)
     return web.Response(body=self.serialize(network).encode(),
                         content_type='application/json')
Exemplo n.º 2
0
 def handle_connection_lost(self):
     logger.debug('Network disconnected: %s, %s, %s',
                  self.factory.config.userinfo,
                  self.factory.config.name, self.factory.config.nick)
     yield from NetworkStore.update(
         dict(
             filter=('id', self.factory.config.id),
             update={
                 'status': '3',
                 'lhost': None,
                 'lport': None,
                 'rhost': None,
                 'rport': None
             }
         )
     )
Exemplo n.º 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)
Exemplo n.º 4
0
 def handle_connection_made(self, transport):
     logger.debug('Network connected: %s, %s, %s',
                  self.factory.config.user,
                  self.factory.config.name, self.factory.config.nick)
     socket = transport.get_extra_info('socket')
     lhost, lport = socket.getsockname()
     rhost, rport = socket.getpeername()
     yield from NetworkStore.update(
         dict(
             filter=('id', self.factory.config.id),
             update={
                 'status': '1',
                 'lhost': lhost,
                 'lport': lport,
                 'rhost': rhost,
                 'rport': rport
             }
         )
     )
Exemplo n.º 5
0
 def put(self):
     network_id = self.request.match_info['id']
     action = self.request.match_info['action']
     if action not in ('connect', 'disconnect'):
         raise web.HTTPNotFound()
     username = yield from get_auth(self.request)
     user = yield from UserStore.get(
         dict(query=('username', username)))
     networks = yield from NetworkStore.get(
         dict(query={'user_id': user.id, 'id': network_id})
     )
     if not networks:
         raise web.HTTPNotFound()
     network = networks[0]
     network = yield from NetworkStore.update(
         dict(filter=('id', network.id),
              update={'status': '0' if action == 'connect' else '2'})
     )
     return web.Response(body=self.serialize(network).encode(),
                         content_type='application/json')
Exemplo n.º 6
0
 def put(self):
     network_id = self.request.match_info['id']
     action = self.request.match_info['action']
     if action not in ('connect', 'disconnect'):
         raise web.HTTPNotFound()
     username = yield from get_auth(self.request)
     user = yield from UserStore.get(dict(query=('username', username)))
     networks = yield from NetworkStore.get(
         dict(query={
             'user_id': user.id,
             'id': network_id
         }))
     if not networks:
         raise web.HTTPNotFound()
     network = networks[0]
     network = yield from NetworkStore.update(
         dict(filter=('id', network.id),
              update={'status': '0' if action == 'connect' else '2'}))
     return web.Response(body=self.serialize(network).encode(),
                         content_type='application/json')
Exemplo n.º 7
0
 def _create_or_update(self, data, username, network=None):
     if network:
         network_data = network.to_dict()
         network_data.update(data)
         form = NetworkForm(formdata=MultiDict(network_data))
     else:
         form = NetworkForm(formdata=MultiDict(data))
     form.validate()
     if form.errors:
         return web.Response(body=json.dumps(form.errors).encode(),
                             status=400,
                             content_type='application/json')
     cleaned_data = form.data
     cleaned_data['user'] = username
     if network:
         network = yield from NetworkStore.update(
             dict(filter=('id', network.id), update=cleaned_data))
     else:
         network = yield from NetworkStore.create(**cleaned_data)
     return web.Response(body=self.serialize(network).encode(),
                         content_type='application/json')
Exemplo n.º 8
0
def disconnect(id):
    network = yield from NetworkStore.update(
        dict(filter=('id', id), update={'status': '2'}))
Exemplo n.º 9
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)
Exemplo n.º 10
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)