def user_create(username, email, password): """Create a user""" yield from UserStore.create(dict( username=username, email=email, password=password ))
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')
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"], ) )
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)
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)
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')
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()
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()
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')
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
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')
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
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)
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()
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')
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')
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')
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')
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)
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)
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)
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)
def user_create(username, email, password): """Create a user""" yield from UserStore.create( dict(username=username, email=email, password=password))