def ping(username, password):  #public_key, private_key):
    url = "http://cs302.kiwi.land/api/ping"

    payload = {}
    response = makerequest.sendPayload(url, payload, username, password)
    print("My ping response is: ")
    print(response)
    return response
def load_new_apikey():
    url = "http://cs302.kiwi.land/api/load_new_apikey"

    username = cherrypy.session['username']
    password = cherrypy.session['password']

    payload = {}
    response = makerequest.sendPayload(url, payload, username, password)
    cherrypy.session['api_key'] = response['api_key']
def list_users():
    url = "http://cs302.kiwi.land/api/list_users"

    username = cherrypy.session['username']
    password = cherrypy.session['password']

    payload = {}
    response = makerequest.sendPayload(url, payload, username, password)

    status = response['response']
    users = response['users']
    return users
def get_privatedata():  #TODO PUT IN TRY CATCH
    url = "http://cs302.kiwi.land/api/get_privatedata"
    username = cherrypy.session['username']
    password = cherrypy.session['password']

    payload = {}
    response = makerequest.sendPayload(url, payload, username, password)

    encrypted = response['privatedata']
    privatedata = decrypt(encrypted)

    print("Private Data: ")
    print(privatedata)
    return privatedata
def report(status):
    url = "http://cs302.kiwi.land/api/report"
    public_key = cherrypy.session['public_key_str']
    print("Reporting for Duty, Sir")

    username = cherrypy.session['username']
    password = cherrypy.session['password']

    payload = {
        "connection_address":
        str(socket.gethostbyname(socket.getfqdn())) + ":1234",
        "connection_location": 2,  #HARD CODED PLS CHANGE
        "incoming_pubkey": public_key,
        "status": status
    }
    response = makerequest.sendPayload(url, payload, username, password)
def broadcast(message, url):

    username = cherrypy.session['username']
    password = cherrypy.session['password']
    loginserver_record = cherrypy.session['loginserver_record']
    sender_created_at = time.time() - 100

    payload = {
        "loginserver_record":
        loginserver_record,
        "message":
        message,
        "sender_created_at":
        str(sender_created_at),
        "signature":
        sign(str(loginserver_record) + message + str(sender_created_at))
    }
    response = makerequest.sendPayload(url, payload, username, password)
    return response
def ping_check(url):  #TODO I THINK IT NEEDS TO BE TRY CATCH
    username = cherrypy.session['username']
    password = cherrypy.session['password']

    my_time = str(time.time())
    connection_address = "125.239.153.97"  #TODO HARD CODED PLS CHANGe
    connection_location = 2  #TODO Hard coded

    payload = {
        "my_time": my_time,
        "connection_address": connection_address,
        "connection_location": connection_location
    }

    try:
        response = makerequest.sendPayload(url, payload, username, password)
        print("Yo I ping checked: ")
        print(response)
    except:
        print("Ping check failed")
def rx_privatemessage(message, url, target_username, target_pubkey_str):
    username = cherrypy.session['username']
    password = cherrypy.session['password']

    loginserver_record = cherrypy.session['loginserver_record']
    target_pubkey = target_pubkey_str.encode(
        'utf-8')  #TODO HARD CODED PLS CHANGE
    encrypted_message = sealed_box(message, target_pubkey)
    sender_created_at = str(time.time())
    signature = sign(loginserver_record + target_pubkey_str + target_username +
                     encrypted_message + sender_created_at)

    payload = {
        "loginserver_record": loginserver_record,
        "target_pubkey": target_pubkey_str,
        "target_username": target_username,
        "encrypted_message": encrypted_message,
        "sender_created_at": sender_created_at,
        "signature": signature
    }

    # Loads up database to store sent message
    conn = sqlite3.connect('database.db')
    c = conn.cursor()
    public_key = cherrypy.session['public_key']
    myencrypted_message = sealed_box(message, public_key)
    # Writes credentials & keys into database
    data = [
        str(username), public_key, myencrypted_message, sender_created_at,
        str(signature),
        str(username)
    ]
    c.execute(
        """INSERT INTO Private_Messages (Username, Public_Key, Encrypted_Message, Send_Time, Signature, Recipient) VALUES (?,?,?,?,?,?)""",
        data)
    conn.commit()
    conn.close()

    print(payload)
    response = makerequest.sendPayload(url, payload, username, password)
    print(response)
def addPubKey(username, password):
    url = "http://cs302.kiwi.land/api/add_pubkey"

    cherrypy.session['private_key'] = nacl.signing.SigningKey.generate()

    cherrypy.session['public_key'] = cherrypy.session[
        'private_key'].verify_key.encode(encoder=nacl.encoding.HexEncoder)
    cherrypy.session['public_key_str'] = cherrypy.session['public_key'].decode(
        'utf-8')
    public_key_bytes = bytes(cherrypy.session['public_key_str'] + username,
                             encoding='utf-8')

    signed = cherrypy.session['private_key'].sign(
        public_key_bytes, encoder=nacl.encoding.HexEncoder)

    payload = {
        "pubkey": cherrypy.session['public_key_str'],
        "username": username,
        "signature": signed.signature.decode('utf-8')
    }
    response = makerequest.sendPayload(url, payload, username, password)
    cherrypy.session['loginserver_record'] = response['loginserver_record']
def add_privatedata():  #TODO NOT DONE
    url = "http://cs302.kiwi.land/api/add_privatedata"

    username = cherrypy.session['username']
    password = cherrypy.session['password']

    #PRIVATE DATA
    prikey = ""
    blocked_pubkeys = ""
    blocked_usernames = "jono, mpop"
    blocked_message_signatures = ""
    blocked_words = ""
    favourite_message_signatures = ""
    friends_username = ""
    Data = {
        "prikey": prikey,
        "blocked_pubkeys": blocked_pubkeys,
        "blocked_usernames": blocked_usernames,
        "blocked_message_signatures": blocked_message_signatures,
        "blocked_words": blocked_words,
        "favourite_message_signatures": favourite_message_signatures,
        "friends_username": friends_username
    }
    privatedata = encrypt(Data)

    loginserver_record = cherrypy.session['loginserver_record']
    client_saved_at = str(time.time())
    signature = sign(
        str(privatedata) + str(loginserver_record) + client_saved_at)

    payload = {
        "privatedata": privatedata,
        "loginserver_record": loginserver_record,
        "client_saved_at": client_saved_at,
        "signature": signature
    }
    response = makerequest.sendPayload(url, payload, username, password)
    print("add_privatedata response: " + response['response'])
def list_apis(username, password):
    url = "http://cs302.kiwi.land/api/list_apis"
    payload = {}
    response = makerequest.sendPayload(url, payload, username, password)