예제 #1
0
파일: user.py 프로젝트: waartaa/ircb
def user_create(username, email, password):
    """Create a user"""
    yield from UserStore.create(dict(
        username=username,
        email=email,
        password=password
    ))
예제 #2
0
 def get(self):
     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})) or []
     return web.Response(body=self.serialize(networks).encode(),
                         content_type='application/json')
예제 #3
0
def load_data(f):
    with open(f, 'r') as f:
        config = yaml.load(f)

        if validate_yaml(config):

            for user in config.keys():
                user_data = config[user]
                yield from UserStore.create(
                    dict(
                        username=user,
                        email=user_data['email'],
                        password=user_data['password']
                    )
                )
                networks = user_data['networks']
                for net in networks.keys():
                    net_data = networks[net]
                    network = yield from NetworkStore.create(
                        dict(
                            user_username=user,
                            name=net,
                            nickname=net_data["nick"],
                            hostname=net_data["host"],
                            port=net_data["port"],
                            realname=net_data["realname"],
                            username=net_data["username"],
                            network_password=net_data["password"],
                            usermode=net_data["usermode"],
                        )
                    )
예제 #4
0
파일: user.py 프로젝트: waartaa/ircb
 def _validate_email(self, form):
     email = form.email.data
     users = yield from UserStore.get(
         dict(query=('email', email)))
     if users:
         error_msg = 'Email already in use.'
         form.email.errors.append(error_msg)
예제 #5
0
파일: user.py 프로젝트: waartaa/ircb
 def _validate_username(self, form):
     username = form.username.data
     users = yield from UserStore.get(
         dict(query=('username', username)))
     if users:
         error_msg = 'Username already in use.'
         form.username.errors.append(error_msg)
예제 #6
0
파일: network.py 프로젝트: waartaa/ircb
 def get(self):
     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})) or []
     return web.Response(body=self.serialize(networks).encode(),
                         content_type='application/json')
예제 #7
0
 def post(self):
     data = yield from self.request.post()
     user = yield from UserStore.get(
         dict(query=('auth', (data.get('username'), data.get('password')))))
     if user:
         yield from auth.remember(self.request, user.username)
         return web.Response(body=b'OK')
     raise web.HTTPForbidden()
예제 #8
0
파일: user.py 프로젝트: waartaa/ircb
 def post(self):
     data = yield from self.request.post()
     user = yield from UserStore.get(
         dict(query=('auth', (data.get('username'), data.get('password'))))
     )
     if user:
         yield from auth.remember(self.request, user.username)
         return web.Response(body=b'OK')
     raise web.HTTPForbidden()
예제 #9
0
파일: network.py 프로젝트: waartaa/ircb
 def get(self):
     username = yield from get_auth(self.request)
     user = yield from UserStore.get(
         dict(query=('username', username)))
     network_id = self.request.match_info['id']
     networks = yield from NetworkStore.get(
         dict(query={'user_id': user.id, 'id': int(network_id)}))
     network = (networks and networks[0]) or None
     if network is None:
         raise web.HTTPNotFound()
     return web.Response(body=self.serialize(network).encode(),
                         content_type='application/json')
예제 #10
0
파일: network.py 프로젝트: waartaa/ircb
 def put(self):
     network_id = self.request.match_info['id']
     username = yield from get_auth(self.request)
     data = yield from self.request.post()
     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()
     resp = yield from self._create_or_update(data, username, networks[0])
     return resp
예제 #11
0
 def get(self):
     username = yield from get_auth(self.request)
     user = yield from UserStore.get(dict(query=('username', username)))
     network_id = self.request.match_info['id']
     networks = yield from NetworkStore.get(
         dict(query={
             'user_id': user.id,
             'id': int(network_id)
         }))
     network = (networks and networks[0]) or None
     if network is None:
         raise web.HTTPNotFound()
     return web.Response(body=self.serialize(network).encode(),
                         content_type='application/json')
예제 #12
0
 def put(self):
     network_id = self.request.match_info['id']
     username = yield from get_auth(self.request)
     data = yield from self.request.post()
     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()
     resp = yield from self._create_or_update(data, username, networks[0])
     return resp
예제 #13
0
    def handle_received_data(self, data):
        logger.debug('RECV: {}'.format(data))
        lport, rport = [_.strip() for _ in data.split(',')]

        networks = yield from NetworkStore.get({
            'query': {'lport': lport, 'rport': rport}
        })
        network = networks[0]
        user = yield from UserStore.get({
            'query': network.user_id
        })
        rpl_msg = '{lport}, {rport} : USERID : UNIX : {username}'.format(
            lport=lport, rport=rport, username=user.username)
        logger.debug('RPL: {}'.format(rpl_msg))
        self.send(rpl_mgs)
예제 #14
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()
예제 #15
0
파일: network.py 프로젝트: waartaa/ircb
 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')
예제 #16
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')
예제 #17
0
 def post(self):
     username = yield from auth.get_auth(self.request)
     if username:
         raise web.HTTPForbidden()
     data = yield from self.request.post()
     form = UserForm(formdata=data)
     form.validate()
     yield from self._validate_username(form)
     yield from self._validate_email(form)
     if form.errors:
         return web.Response(body=json.dumps(form.errors).encode(),
                             status=400,
                             content_type='application/json')
     cleaned_data = form.data
     yield from UserStore.create(
         dict(username=cleaned_data['username'],
              email=cleaned_data['email'],
              password=cleaned_data['password'],
              first_name=cleaned_data.get('first_name', ''),
              last_name=cleaned_data.get('last_name', '')))
     return web.Response(body=b'OK')
예제 #18
0
파일: user.py 프로젝트: waartaa/ircb
 def post(self):
     username = yield from auth.get_auth(self.request)
     if username:
         raise web.HTTPForbidden()
     data = yield from self.request.post()
     form = UserForm(formdata=data)
     form.validate()
     yield from self._validate_username(form)
     yield from self._validate_email(form)
     if form.errors:
         return web.Response(body=json.dumps(form.errors).encode(),
                             status=400,
                             content_type='application/json')
     cleaned_data = form.data
     yield from UserStore.create(
         dict(
             username=cleaned_data['username'],
             email=cleaned_data['email'],
             password=cleaned_data['password'],
             first_name=cleaned_data.get('first_name', ''),
             last_name=cleaned_data.get('last_name', '')
         )
     )
     return web.Response(body=b'OK')
예제 #19
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)
예제 #20
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)
예제 #21
0
 def _validate_email(self, form):
     email = form.email.data
     users = yield from UserStore.get(dict(query=('email', email)))
     if users:
         error_msg = 'Email already in use.'
         form.email.errors.append(error_msg)
예제 #22
0
 def _validate_username(self, form):
     username = form.username.data
     users = yield from UserStore.get(dict(query=('username', username)))
     if users:
         error_msg = 'Username already in use.'
         form.username.errors.append(error_msg)
예제 #23
0
def user_create(username, email, password):
    """Create a user"""
    yield from UserStore.create(
        dict(username=username, email=email, password=password))