def send_message(): # Protocol 7 username = get_username() priv_key = get_priv_key() header = '@' + username + ':7' packet = json.dumps({"Messages": 1, "private": priv_key}) message = send([header + ' ' + packet]) message = message.split(":201 ") message = message[1] message = json.loads(message) f = open("./web/db/friends.json", "r") friends_list = f.read() friends_list = json.loads(friends_list) f.close() for i in message: item = i item = str(item) item = item.split("@") item = item[1] item = item.split(":102 ") key = item[0] info = item[1] info = json.loads(info) friend_key = info["Key"] friends_list[key] = friend_key if info["Step"] == 1: add_friend(key, 2) f = open("./web/db/friends.json", "w") friends_list = json.dumps(friends_list) f.write(friends_list) f.close() return 'I get you all of the messages from your "inbox"'
def add_item(item_name, category, subcategory, groups, type, image, user_tag): # Protocol 3 # TODO may want to add a send all or send some function for own stuff after this call username = get_username() priv_key = get_priv_key() item = { "Current Owner": username, "Permanent Owner": username, "Category": category, "Subcategory": subcategory, "Name": item_name, "Groups": [], "Type Info": {}, "Image": image, "User Tags": [], "Available": true, } groups = groups.split(" ") for i in groups: item["Groups"].append(i) item["Type Info"] = type user_tag = user_tag.split(" ") for j in user_tag: item["User Tags"].append(j) item_key = create_item_key(item) header = '@' + username + ':3' packet = json.dumps({item_key: item, "private": priv_key}) message = send([header + ' ' + packet]) print(message) print(header) # item_key is returned for testing purposes return item_key
def item_request(item_hash, friend_name): # Protocol 100 username = get_username() pub_key = get_pub_key() # print(pub_key) # pub_key = "pub_key" header = '@' + username + ':100' borrower_info = {"Public": pub_key, "Username": username} f = open("./web/db/friends.json", "r") my_friends = f.read() f.close() my_friends = json.loads(my_friends) friend_key = my_friends[friend_name] # print(friend_key) # friend_key = "friend_key" lender_info = {"Public": friend_key, "Username": friend_name} packet = {} packet["Key"] = item_hash packet["Borrower"] = borrower_info packet["Lender"] = lender_info # print(packet) packet = json.dumps(packet) message = send([header + ' ' + packet]) print(message) return 'I request items'
def send_pending_friends(): # Protocol 10 # TODO need to implement remove friend. username = get_username() priv_key = get_priv_key() header = '@' + username + ':10' packet = json.dumps({ "Friends": 1, "private": priv_key, "Sender": username }) message = send([header + ' ' + packet]) print(message) message = message.split(":204 ") message = message[1] message = json.loads(message) f = open("./web/db/messages.json", "r") message_db = f.read() f.close() message_db = json.loads(message_db) message_db["pending friends"] = message message_db = json.dumps(message_db) f = open("./web/db/messages.json", "w") f.write(message_db) f.close()
def delete_friend(friend_name): # Protocol 103 username = get_username() header = '@' + username + ':103' packet = json.dumps({"Target": friend_name}) message = send([header + ' ' + packet]) print(message) return 'I delete friends'
def friend_request(friend_name): # Protocol 101 username = get_username() header = '@' + username + ':101' packet = json.dumps({"Target": friend_name}) message = send([header + ' ' + packet]) print(message) return 'I request friends'
def clear_messagese(): # Protocol 9 username = get_username() priv_key = get_priv_key() header = '@' + username + ':9' packet = json.dumps({"Messages": -1, "private": priv_key}) message = send([header + ' ' + packet]) print(message)
def delete_item(item_key): # Protocol 2 username = get_username() priv_key = get_priv_key() header = '@' + username + ':2' packet = json.dumps({"Key": [item_key], "private": priv_key}) message = send([header + ' ' + packet]) print(message) return 'I delete items from your database'
def delete_user(): # Protocol 1 username = get_username() priv_key = get_priv_key() pub_key = get_pub_key() header = '@' + username + ':1' packet = json.dumps({"Delete": 1, "public": pub_key, "private": priv_key}) message = send([header + ' ' + packet]) print(message)
def add_friend(friend_name, step): # Protocol 102 username = get_username() pub_key = get_pub_key() header = '@' + username + ':102' packet = {} packet["Target"] = friend_name packet["Key"] = pub_key packet["Step"] = step packet = json.dumps(packet) message = send([header + ' ' + packet]) print(message) return 'I accept friend requests'
def send_pending_exchanges(): # Protocol 11 username = get_username() priv_key = get_priv_key() header = '@' + username + ':11' packet = json.dumps({"Exchanges": 1, "private": priv_key}) message = send([header + ' ' + packet]) print("pending ex") message = message.split(":203 ") message = message[1] message = json.loads(message) f = open("./web/db/messages.json", "r") message_db = f.read() message_db = json.loads(message_db) f.close() message_db["pending exchanges"] = message f = open("./web/db/messages.json", "w") message_db = json.dumps(message_db) f.write(message_db) f.close()
def send_all(): # Protocol 4 # TODO detect when the received packet is not an id 200 my_dir = './web/db/' username = get_username() pub_key = get_pub_key() header = '@' + username + ':4' packet = json.dumps({"public": pub_key, "Library": 1}) message = send([header + ' ' + packet]) # print(message) message = message.split(' ', 1)[1] f = open(os.path.join(my_dir, "mine.json"), "w") f.write(message) f.close() f = open(os.path.join(my_dir, "theirs.json"), "r") friend_library = f.read() f.close() friend_library = json.loads(friend_library) f = open("./web/db/friends.json", "r") my_friends = f.read() f.close() my_friends = json.loads(my_friends) for friend in my_friends: header = '@' + friend + ':4' packet = json.dumps({"public": my_friends[friend], "Library": 1}) a_friend = send([header + ' ' + packet]) a_friend = a_friend.split(' ', 1)[1] a_friend = json.loads(a_friend) for item in a_friend: friend_library[item] = a_friend[item] f = open(os.path.join(my_dir, "theirs.json"), "w") friend_library = json.dumps(friend_library) f.write(friend_library) f.close() return 'I get you all of your stuff and your friends'
def send_exchange(): # Protocol 8 username = get_username() priv_key = get_priv_key() header = '@' + username + ':8' packet = json.dumps({"Exchanges": 1, "private": priv_key}) message = send([header + ' ' + packet]) print("send exchanges") print(message) message = message.split(":201 ") message = message[1] message = json.loads(message) f = open("./web/db/messages.json", "r") message_db = f.read() message_db = json.loads(message_db) f.close() message_db["exchanges"] = message f = open("./web/db/messages.json", "w") message_db = json.dumps(message_db) f.write(message_db) f.close() return 'I get you all of your exchanges'
def change_owner(item_key, friend_name, in_date, out_date): # Protocol 6 # permanent owner agrees to loan item, this protocol is sent username = get_username() priv_key = get_priv_key() f = open("./web/db/friends.json", "r") friend_list = f.read() f.close() friend_list = json.loads(friend_list) header = '@' + username + ':6' packet = {} packet['key'] = item_key packet['New Owner'] = friend_name packet['public'] = friend_list[friend_name] packet['private'] = priv_key packet['Schedule']['In'] = in_date packet['Schedule']['Out'] = out_date packet = json.dumps(packet) message = send([header + ' ' + packet]) print(message) return 'I change current owner'
key2 = add_item('another great flick', 'Entertainment', 'Movie', 'men', 'it is in good condition', 'action') print('send all') send_all() print('delete item') delete_item(key1) delete_item(key2) print('item request') item_request(other_1, use1) print('add friend') friend_request(use1) add_friend(use1, 1) add_friend_test(get_username(), get_pub_key(), 1, use1, pub1) add_friend_test(get_username(), get_pub_key(), 1, use2, pub2) print('delete friend') delete_friend_test('user1', use1) delete_friend_test('user1', use1) print('send messages') send_message() send_exchange() send_pending_exchanges() send_pending_friends() print('delete user') delete_user() delete_other_user(use1, priv1, pub1) delete_other_user(use2, priv2, pub2)