def request(payload): # pylint disable=R1771 ''' Function to request a reset code ''' reset_store = get_reset_code_store() send_to_email = test_email(payload['email']) email_match = 0 # if found = 1 user = get_user_from('email', send_to_email) # pylint: disable=W0612 email_match = 1 reset_code = id_generator() code_password = { 'email': send_to_email, 'reset_code': reset_code, } server = smtplib.SMTP_SSL('smtp.gmail.com', 465) server.login(EMAIL, PASSWORD) server.sendmail( EMAIL, send_to_email, reset_code) server.quit() if email_match == 0: raise InputError(description="Email entered does not belong to a user") reset_store.append(code_password)
def send(payload): ''' function to take a payload, create a new mesaage, fill in the parameters and append it to the channels messages store as well as the gloabal message_store ''' user = get_user_from('token', payload['token']) channel = get_channel(payload['channel_id']) if not test_in_channel(user['u_id'], channel): raise AccessError(description='User is not in channel') # create a message data type, and fill in details # then append to the channels list of messages txt = payload['message'] if len(txt) > 1000: raise InputError(description='Message is more than 1000 characters') # create the message dictionary new_message = create_message() new_message['u_id'] = user['u_id'] new_message['message'] = txt new_message['channel_id'] = payload['channel_id'] channel['messages'].append(new_message) if txt == '/hangman': if not channel['hangman_active']: hangman.start(channel) channel['hangman_active'] = True else: hangman.message(channel, 'There is already an active game running') hangman.message(channel, hangman.display_hangman()) if txt.split()[0] == '/guess': if not channel['hangman_active']: hangman.message( channel, 'There is not a current game of hangman running.\n' + r'If you would like to start one, type \hangman into the chat') else: if len(txt.split()) == 2: new_guess = txt.split()[1] if new_guess in r'!@#$%^&*()_+-=[]\;<>?/~`:': hangman.message(channel, 'Invalid guess, guess again') else: hangman.guess(channel, new_guess) else: hangman.message(channel, 'Invalid guess, guess again') # append it to the messages_store messages = get_messages_store() messages.append(new_message) save_messages_store() # append it to the channels file save_channel_store() return new_message
def logout(payload): ''' Function to logout a user ''' user = get_user_from('token', payload['token']) if user['status'] == LOGGED_ON: user['status'] = LOGGED_OFF user['token'] = '' return True return False
def profile(payload): ''' For a valid user, returns information about their user id, email, first name, last name, handle and profile_img_url ''' if validate_uid(payload['u_id']) is False: raise InputError(description='Invalid u_id') user1 = get_user_from('token', payload['token']) #returns user information user2 = get_user_from('u_id', int(payload['u_id'])) return ({ 'u_id': user2['u_id'], 'email': user2['email'], 'name_first': user2['name_first'], 'name_last': user2['name_last'], 'handle_str': user2['handle_str'], 'profile_img_url': user2['profile_img_url'] })
def sendlater(payload): ''' Function to create a message and have it be sent at a ''' user = get_user_from('token', payload['token']) channel = get_channel(payload['channel_id']) messages = get_messages_store() if not test_in_channel(user['u_id'], channel): raise InputError(description='User is not in channel') # create a message data type, and fill in details # then append to the channels list of messages txt = payload['message'] if len(txt) > 1000: raise InputError(description='Message is more than 1000 characters') # create the message dictionary time = payload['time_sent'] print(type(time)) if time < int(datetime.now().timestamp()): raise InputError(description='Unable to send as ' + 'time sent is a time in the past') new_message = create_message() new_message['time_created'] = time new_message['u_id'] = user['u_id'] new_message['message'] = txt new_message['channel_id'] = payload['channel_id'] # append it to the messages_store first messages.append(new_message) save_messages_store() interval = (time - datetime.now().timestamp()) # append to the channel message store at a later time timer = threading.Timer(interval, append_later, args=[new_message['message_id']]) timer.start() # debugging purposes for msg in channel['messages']: print(msg['message']) return new_message['message_id']
def login(payload): ''' Function to login a user ''' email = test_email(payload['email']) user = get_user_from('email', email) # if the user is not currently logged off, raise error if user['password'] != payload['password']: raise InputError(description="Incorrect password") user['status'] = LOGGED_ON user['token'] = generate_token(user['u_id']) return user
def get_hangman(): ''' Function to get the hangman details, otherwise we need to register the hangman ''' hangman = get_user_from('name_first', 'Hangman') if hangman is None: # if we get returned an empty dictionary register hangman print('if we get returned an empty dictionary register hangman') payload = { 'email': '*****@*****.**', 'password': '******', 'name_first': 'Hangman', 'name_last': 'Bot' } hangman = auth.register(payload) return hangman
def permission_change(payload): #what does it mean for permision id to not refer to permission id? ''' changes the permision of a authorised uid We must also update their permissions in all channels they have joined ''' if validate_uid(payload['u_id']) is False: raise InputError(description='Invalid u_id') owner = get_user_token(payload['token']) change_user = get_user_from('u_id', payload['u_id']) if owner['permission_id'] != 1: raise AccessError(description='The authorised user is not an owner') change_user['permission_id'] = payload['permission_id'] '''
def pin(payload): # pylint: disable=R1711 'testing functionability for message pin' user = get_user_from('token', payload['token']) message = find_message(payload['message_id']) channel = get_channel(message['channel_id']) if message['is_pinned'] is True: raise InputError(description='Message is already pinned') if not test_in_channel(user['u_id'], channel): raise AccessError( description='You do not have permission to pin this message') if not check_owner(user, channel): raise InputError(description='You do not have permission') message['is_pinned'] = True return
def remove(payload): # pylint: disable=R1711 ''' Function to remove a message from a channel ''' user = get_user_from('token', payload['token']) messages = get_messages_store() message = find_message(payload['message_id']) channel = get_channel(message['channel_id']) if message['u_id'] != user['u_id']: if not check_owner(user, channel): raise AccessError(description='You do not have permission') messages.remove(message) channel['messages'].remove(message) return
def reset(payload): # pylint disable=R1771 ''' Function to reset the user's password ''' reset_store = get_reset_code_store() print(reset_store) for code in reset_store: if code['reset_code'] == payload['reset_code']: requested_email = code['email'] user = get_user_from('email', requested_email) if len(payload['new_password']) > 6: new_password = payload['new_password'] user['password'] = new_password return else: raise InputError(description='Password is too short') raise InputError(description='Reset code is incorrect')
def edit(payload): ''' Function to remove a message from a channel ''' message_store = get_messages_store() user = get_user_from('token', payload['token']) message = find_message(payload['message_id']) channel = get_channel(message['channel_id']) if message['u_id'] != user['u_id']: if not check_owner(user, channel): raise AccessError(description='You do not have permission') if len(payload['message']) == 0: # pylint: disable=C1801, R1705 channel['messages'].remove(message) message_store.remove(message) return else: message['message'] = payload['message'] return
def react(payload): ''' Function to add a react to a given message ''' global REACT_IDS # pylint: disable=W0603 user = get_user_from('token', payload['token']) message = find_message(int(payload['message_id'])) channel = get_channel(message['channel_id']) if int(payload['react_id']) not in REACT_IDS: raise InputError(description='Unable to react with react_id ' + str(payload['react_id'])) if not test_in_channel(user['u_id'], channel): raise InputError(description='Unable to react as you are ' + 'not a part of that channel') for i in message['reacts']: if i['react_id'] == payload['react_id']: # this react is already present in the message # just add another u_id if user['u_id'] in i['u_ids']: # if the user has reacted, unreact them unreact(payload) i['u_ids'].append(user['u_id']) return # no previous react wih react_id new_react = { 'react_id': payload['react_id'], 'u_ids': [], 'is_user_reacted': False } new_react['u_ids'].append(user['u_id']) message['reacts'].append(new_react) return
def Listall(token): # pylint: disable=W0613, C0103 'implementations of channels listall function' channel_store = get_channel_data_store() user = get_user_from('token', token) channels_return = [] channel_info = {} if len(channel_store) == 0: # pylint: disable=C1801 # the channel store is empty return channels_return for channel in channel_store: channel_info = { 'channel_id': channel['channel_id'], 'name': channel['name'] } if channel['is_public']: channels_return.append(channel_info) elif test_in_channel(user['u_id'], channel): channels_return.append(channel_info) return channels_return
def unreact(payload): ''' Function to remove a react from a message ''' global REACT_IDS # pylint: disable=W0603 user = get_user_from('token', payload['token']) message = find_message(payload['message_id']) channel = get_channel(message['channel_id']) if int(payload['react_id']) not in REACT_IDS: raise InputError(description='Unable to react with react_id ' + str(payload['react_id'])) if not test_in_channel(user['u_id'], channel): raise InputError(description='Unable to react as you are ' + 'not a part of that channel') for i in message['reacts']: if i['react_id'] == payload['react_id']: # this react is already present in the message # just remove u_id if user['u_id'] not in i['u_ids']: raise InputError(description='Attempting to uncreact ' + 'a message you have not reacted to') i['u_ids'].remove(user['u_id']) if len(i['u_ids']) == 0: # pylint: disable=C1801 # no one else has reacted, so remove react message['reacts'].remove(i) print(message['reacts']) return # unable to find react of react_id in messages raise InputError(description='Message with ID message_id ' + 'does not contain an active react with with ID react_id')