def add_cli(self, username: str, port: str, tree_sent: str, transport): tree_database = database.find_user(username, 'pass') if tree_database is None: logger.info('user {} does not exists'.format(username)) return None ts = tree.Tree(tree_sent) td = tree.Tree(tree_database) cli = Client(username, port, ts, transport) if username not in self.clients: self.clients[username] = [cli] else: self.clients[username].append(cli) logger.debug("new instance of client {}".format(username)) self.transports[transport] = cli tree.Tree.process_subtree(cli, ts.raw_tree, td.raw_tree) # TODO: think about client conflicts + database sync between instances logger.debug('tree has been processed for {}'.format(username)) database.save_new_tree(username, ts.raw_tree) return cli
def index(): if session.get("id", False): user = db.find_user({"_id": session["id"]}) # Clear the color queue color_queue[session["id"]] = [] return render_template('colors.html', user=user) else: return render_template('index.html')
def sign_up(usr, pwd): if find_user(usr): #bad, reenter info flash("Username taken.") return 1 else: #good, add user new_user(usr, pwd) return 0
def check_auth(username, password): """This function is called to check if a username / password combination is valid. """ user = find_user(username) if (user == None): return False hashed_password = user['hashed_password'] print hashed_password return check_password_hash(hashed_password, password)
def verify_token(token): print(token) payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM]) t1 = datetime.strptime(payload['time'], '%Y-%m-%d %H:%M:%S') t2 = datetime.now() print("current time = " + t2.strftime('%Y-%m-%d %H:%M:%S')) print("token time = " + payload['time']) print("delta seconds = " + str((t2-t1).seconds)) if (t2-t1).seconds < 12*3600: from database import find_user return find_user(payload['username']) return None
def login(usr, pwd): #good username? if find_user(usr): #good username but password? if match_pass(usr, pwd): session['user'] = usr return 0 #bad password else: flash("Incorrect password.") return 1 #bad username else: flash("Incorrect username.") return 1
def user_dashboard(): user = session["user"] name = database.find_user(user) user_invites_header = ["event id", "event name", "organizer", "date", "location"] invites_data = database.future_events_with_status(user, 3) if request.method=="GET": events_data = database.user_future_events(user) past_events = database.user_past_events(user) return render_template( 'dash.html', user_first=name, invites_table = "Received Invites", invites_table_header=user_invites_header, invites_table_data=invites_data, event_redir="/user_display_event_read/", invite_redir="/user_display_event/", events_table = "Upcoming Events", events_table_header= user_invites_header, events_table_data= events_data, search_redir="/userdash", past_events_table = "Past Events Attended", past_events_table_header = user_invites_header, past_events_table_data = past_events) if request.method=="POST": events_data = database.user_future_events(user) past_events = database.user_past_events(user) query = request.form["query"] if query != "": try: events_data = database.user_invites_by_category(user, request.form["query"]) print events_data except: print "invalid query" return render_template( 'dash.html', user_first=str(name[0]), invites_table = "Received Invites", invites_table_header=user_invites_header, invites_table_data=invites_data, event_redir="/user_display_event_read/", invite_redir="/user_display_event/", events_table = "Upcoming Events", events_table_header= user_invites_header, events_table_data= events_data, search_redir="/userdash", past_events_table = "Past Events Attended", past_events_table_header = user_invites_header, past_events_table_data = past_events)
def bot_message_handler(current_updates, update_id, message_id, sender_id, group_id, dict_checker, cur, callback_data=0, callback=False): try: if callback == True: print(callback_data) if callback_data == 'Deposit': depo_add = database.get_depo(cur) bot.send_message(sender_id, depo_add) bot.send_message_four( sender_id, 'Send your Deposit to the address above and paste the Transaction Hash. Minimum Deposit 0.005 BTC', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) elif callback_data == 'Withdraw': cu_add = database.with_add(sender_id, cur) with_add = database.with_add(sender_id, cur) if with_add == 'Nothing': bot.edit_message_two( sender_id, message_id, 'No Withdrawal Address Found. Send your wallet address now', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) change_address.append(sender_id) bot.get_updates(offset=update_id + 1) else: bot.edit_message_two( sender_id, message_id, f'Current Wallet Address\n\n{cu_add}\n\nEnter the amount of BTC to withdraw', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) with_amount.append(sender_id) bot.get_updates(offset=update_id + 1) elif callback_data == 'Terms': for i in range(1, 9): terms = database.get_term(i, cur) bot.send_message(sender_id, terms) last_term = database.get_term(9, cur) bot.send_message_four(sender_id, last_term, [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) elif callback_data == 'Performance': profit = database.get_profit(cur) cu_bal = database.find_bala(sender_id, cur) bot.edit_message_two( sender_id, message_id, f'Current Balance: {cu_bal}\n24 hour profit: {profit}%', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) elif callback_data == 'Referral': ref_link = f'https://t.me/profit_share_bot?start={sender_id}' bot.edit_message_two(sender_id, message_id, f'Your referral link:\n{ref_link}', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) elif callback_data == 'Change Address': cu_add = database.with_add(sender_id, cur) bot.edit_message_two( sender_id, message_id, f'Current Withdrawal Address\n\n{cu_add}\n\nSend your new address now to change it', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) change_address.append(sender_id) bot.get_updates(offset=update_id + 1) elif callback_data == 'Back': if sender_id in with_amount: with_amount.remove(sender_id) if sender_id in change_address: change_address.remove(sender_id) bot.send_message_four( sender_id, 'Select an option from below', [[{ 'text': 'Performance', 'callback_data': 'Performance' }, { 'text': 'Deposit', 'callback_data': 'Deposit' }], [{ 'text': 'Withdraw', 'callback_data': 'Withdraw' }, { 'text': 'Change Address', 'callback_data': 'Change Address' }], [{ 'text': 'Referral', 'callback_data': 'Referral' }], [{ 'text': 'Terms', 'callback_data': 'Terms' }, { 'text': 'History', 'callback_data': 'History' }]]) bot.get_updates(offset=update_id + 1) elif callback_data == 'History': depo_his = database.find_history(sender_id, 'Deposit', cur) with_his = database.find_history(sender_id, 'Withdraw', cur) full_text = '' if depo_his == 'Nothing': full_text += 'Deposit History\n\nNoting\n\n' else: full_text += f'Deposit History\n\n' for i in depo_his: if len(full_text) > 3800: bot.send_message(sender_id, full_text) full_text = '' da_ti = datetime.fromtimestamp(float(i[3])) full_text += f'Transaction Hash: {i[0]}\nAmount: {i[2]} BTC\nTime: {da_ti}\n\n' if with_his == 'Nothing': full_text += 'Withdraw History\n\nNothing' else: full_text += f'Withdraw History\n\n' for i in with_his: if len(full_text) > 3800: bot.send_message(sender_id, full_text) full_text = '' da_ti = datetime.fromtimestamp(float(i[3])) full_text += f'Transaction Hash: {i[0]}\nWallet: {i[1]}\nAmount: {i[2]} BTC\nTime: {da_ti}\n\n' bot.send_message_four(sender_id, full_text, [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) else: text = current_updates['message']['text'] first_name = current_updates['message']['from']['first_name'] print(text) if text.startswith('/start'): try: last_name = current_updates['message']['from']['last_name'] except: last_name = '' if sender_id in with_amount: with_amount.remove(sender_id) if sender_id in change_address: change_address.remove(sender_id) if database.find_user(sender_id, cur) == 'Nothing': database.add_user(sender_id, first_name, last_name, cur) if database.find_ref(sender_id, cur) == 0: try: data = text.split(' ')[1] database.update_ref(sender_id, data, cur) except: pass bot.send_message_four( sender_id, 'Select an option from below', [[{ 'text': 'Performance', 'callback_data': 'Performance' }, { 'text': 'Deposit', 'callback_data': 'Deposit' }], [{ 'text': 'Withdraw', 'callback_data': 'Withdraw' }, { 'text': 'Change Address', 'callback_data': 'Change Address' }], [{ 'text': 'Referral', 'callback_data': 'Referral' }], [{ 'text': 'Terms', 'callback_data': 'Terms' }, { 'text': 'History', 'callback_data': 'History' }]]) bot.get_updates(offset=update_id + 1) elif sender_id in change_address: if len(text) < 20: bot.send_message_four(sender_id, 'Invalid Address. Try Again', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) else: database.update_with(sender_id, text, cur) bot.send_message_four(sender_id, 'Wallet Address Updated', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) change_address.remove(sender_id) bot.get_updates(offset=update_id + 1) elif sender_id in with_amount: try: amount = float(text) cu_bal = float(database.find_bala(sender_id, cur)) cu_add = database.with_add(sender_id, cur) if amount < 0.005: bot.send_message_four( sender_id, 'Amount too small. Minimum withdraw 0.005 BTC. Press Back to Cancel or enter a new Amount', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) else: if amount <= cu_bal: bot.send_message_four( sender_id, f'Request Sent to withdraw {amount} BTC. Withdrawal will be processed soon!', [[{ 'text': 'Done', 'callback_data': 'Back' }]]) with_amount.remove(sender_id) database.with_trans(sender_id, cu_add, amount, time.time(), cur) cu_bal -= amount database.update_bala(sender_id, cu_bal, cur) bot.get_updates(offset=update_id + 1) elif amount > cu_bal: bot.send_message_four( sender_id, 'Not Enough Balance. Press Back to Cancel or enter a new Amount', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) except: bot.send_message_four( sender_id, 'Invalid Number to Withdraw. Enter a Valid BTC Amount', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) elif len(text) > 60: try: bot.send_message(sender_id, 'Transaction Hash Detected') if database.get_trans(text, cur) == 'Nothing': depo_add = database.get_depo(cur) a = requests.get( f'https://blockchain.info/rawtx/{text}').json() data = a['out'] outputs = {} for i in data: try: outputs[i['addr']] = i['value'] except: pass if depo_add in outputs: amount = float(int(outputs[depo_add]) / 100000000) if amount < 0.005: bot.send_message_four( sender_id, 'Deposit amount too small. Deposit failed. Contact @dashe79 to resolve it', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) else: bot.send_message_four( sender_id, f'Deposit successful. {amount} Deposited', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) database.depo_trans(sender_id, text, amount, time.time(), cur) cu_bal = float( database.find_bala(sender_id, cur)) + amount database.update_bala(sender_id, cu_bal, cur) bot.get_updates(offset=update_id + 1) else: bot.send_message_four( sender_id, 'Deposit Failed. Transaction not sent to the Deposit Address', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) else: bot.send_message_four( sender_id, 'Transaction Hash Already Used. Invalid Transaction Hash', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) except: bot.send_message_four(sender_id, 'Invalid Transaction Hash', [[{ 'text': 'Back', 'callback_data': 'Back' }]]) bot.get_updates(offset=update_id + 1) except Exception as e: print(e) print('got an error') bot.get_updates(offset=update_id + 1) pass
import logging import time from common.log import logger import common.network import argparse logfile = '/tmp/storeit-server.log' parser = argparse.ArgumentParser(description='StoreIt backend server.') parser.add_argument('-l', '--log', nargs='?', const=logfile, help='log to a file (default is {})'.format(logfile)) args = parser.parse_args() if args.log is not None: logging.basicConfig(filename=args.log, level=logging.DEBUG) try: database.__init__() database.find_user('nonexistent', 'user') except Exception: logger.warn('Looks like postgre daemon is not running. It will be started') subprocess.call("./database/postgre.sh") time.sleep(0.2) database.__init__() try: common.network.loop(network.NetManager, '0', 7641) except Exception: logger.error("exception")
def __init__(self, user_id, name=None, email=None): if user_id is not None: self.name, self.email, self.authorized = find_user(user_id) else: self.name, self.email, self.authorized = name, email, False
def is_registered(user_id): return find_user(user_id) is not None
def logged_in_user(): if not logged_in(): return None return db.find_user({"_id": session["id"]})
def logged_in(): if not session.get("id"): return False user = db.find_user({"_id": session["id"]}) return not user == None