def sign_out_user(m): if 'forced' in m['data']: d = m['data'] else: d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return { "success": False, "message": "You're not autorized to see this." } token = d['token'] try: if d['forced']: forced = True else: forced = False except: forced = False if storage.is_token_presented(token): email = storage.get_user_email(token) storage.remove_user(token) if not forced: socket_pool.remove_socket(email) notify_all_users() return {"success": True, "message": "Successfully signed out."} return {"success": False, "message": "You are not signed in."}
def sign_out_user(m): if 'forced' in m['data']: d = m['data'] else: d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return {"success": False, "message": "You're not autorized to see this."} token = d['token'] try: if d['forced']: forced = True else: forced = False except: forced = False if storage.is_token_presented(token): email = storage.get_user_email(token) storage.remove_user(token) if not forced: socket_pool.remove_socket(email) notify_all_users() return {"success": True, "message": "Successfully signed out."} return {"success": False, "message": "You are not signed in."}
def get_user_messages_by_email(m, local=False): # local means this this query is from internal source and there is # no need to check legitimacy of the user if not local: d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return {"success": False, "message": "You're not autorized to see this."} else: d = m token = d['token'] email = d['email'] if not storage.get_user_email(token): return {"success": False, "message": "You are not signed in."} try: db = get_db() c = db.cursor() except: return {"success": False, "message": "Database problems."} c.execute("SELECT COUNT(*) FROM User WHERE Email=?", (email, )) if c.fetchone()[0] != 1: return {"success": False, "message": "No such user."} c.execute("SELECT * FROM Message WHERE To_email=? ORDER BY ID DESC", (email, )) match = map(lambda x: {'writer': x[2], 'content': x[3], 'media': x[-1]}, c.fetchall()) return {"success": True, "message": "User messages retrieved.", "data": match}
def change_password(m): d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return {"success": False, "message": "You're not autorized to see this."} token = d['token'] old_pass = d['old_password'] new_pass = d['new_password'] email = storage.get_user_email(token) if not email: return {"success": False, "message": "You are not logged in."} try: db = get_db() c = db.cursor() except: return {"success": False, "message": "Database problems."} c.execute("SELECT COUNT(*) FROM User WHERE Email=? AND Password=?", (email, old_pass)) # there is such user with such password if c.fetchone()[0] == 1: c.execute("UPDATE User SET Password=? WHERE Email=? AND Password=?", (new_pass, email, old_pass)) db.commit() return {"success": True, "message": "Password changed."} return {"success": False, "message": "Wrong password."}
def get_user_messages_by_token(m, local=False): if local: d = m else: d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return {"success": False, "message": "You're not autorized to see this."} token = d['token'] email = storage.get_user_email(token) return get_user_messages_by_email({'token': token, 'email': email}, True)
def get_user_messages_by_email(m, local=False): # local means this this query is from internal source and there is # no need to check legitimacy of the user if not local: d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return { "success": False, "message": "You're not autorized to see this." } else: d = m token = d['token'] email = d['email'] if not storage.get_user_email(token): return {"success": False, "message": "You are not signed in."} try: db = get_db() c = db.cursor() except: return {"success": False, "message": "Database problems."} c.execute("SELECT COUNT(*) FROM User WHERE Email=?", (email, )) if c.fetchone()[0] != 1: return {"success": False, "message": "No such user."} c.execute("SELECT * FROM Message WHERE To_email=? ORDER BY ID DESC", (email, )) match = map(lambda x: { 'writer': x[2], 'content': x[3], 'media': x[-1] }, c.fetchall()) return { "success": True, "message": "User messages retrieved.", "data": match }
def get_user_messages_by_token(m, local=False): if local: d = m else: d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return { "success": False, "message": "You're not autorized to see this." } token = d['token'] email = storage.get_user_email(token) return get_user_messages_by_email({'token': token, 'email': email}, True)
def post_message(m): d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return { "success": False, "message": "You're not autorized to see this." } token = d['token'] message = d['message'] to_email = d['email'] from_email = storage.get_user_email(token) if not from_email: return {"success": False, "message": "You are not signed in."} try: db = get_db() c = db.cursor() except: return {"success": False, "message": "Database problems."} c.execute("SELECT COUNT(*) FROM User WHERE Email=?", (to_email, )) if c.fetchone()[0] != 1: return {"success": False, "message": "No such user."} c.execute( 'INSERT INTO Message(To_email, From_email, Content, Media) VALUES (?, ?, ?, ?)', (to_email, from_email, message, False)) db.commit() # token_of_receiver = storage.get_token_by_email(to_email) # if token_of_receiver: # notify_user(token_of_receiver[0]) notify_all_users() return {"success": True, "message": "Message posted"}
def post_message(m): d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return {"success": False, "message": "You're not autorized to see this."} token = d['token'] message = d['message'] to_email = d['email'] from_email = storage.get_user_email(token) if not from_email: return {"success": False, "message": "You are not signed in."} try: db = get_db() c = db.cursor() except: return {"success": False, "message": "Database problems."} c.execute("SELECT COUNT(*) FROM User WHERE Email=?", (to_email, )) if c.fetchone()[0] != 1: return {"success": False, "message": "No such user."} c.execute('INSERT INTO Message(To_email, From_email, Content, Media) VALUES (?, ?, ?, ?)', (to_email, from_email, message, False)) db.commit() # token_of_receiver = storage.get_token_by_email(to_email) # if token_of_receiver: # notify_user(token_of_receiver[0]) notify_all_users() return {"success": True, "message": "Message posted"}
def change_password(m): d = m['data'] h = m['hash'] t = m['timestamp'] if not helper.is_legid(d, h, t): return { "success": False, "message": "You're not autorized to see this." } token = d['token'] old_pass = d['old_password'] new_pass = d['new_password'] email = storage.get_user_email(token) if not email: return {"success": False, "message": "You are not logged in."} try: db = get_db() c = db.cursor() except: return {"success": False, "message": "Database problems."} c.execute("SELECT COUNT(*) FROM User WHERE Email=? AND Password=?", (email, old_pass)) # there is such user with such password if c.fetchone()[0] == 1: c.execute("UPDATE User SET Password=? WHERE Email=? AND Password=?", (new_pass, email, old_pass)) db.commit() return {"success": True, "message": "Password changed."} return {"success": False, "message": "Wrong password."}