Exemplo n.º 1
0
def send(user_details, text_body):
    print('Found send')
    account = nano.get_address(user_details.id + 1, str(wallet_seed))
    components = text_body.split(" ")
    previous = nano.get_previous(str(account))
    balance = int(nano.get_balance(previous))

    # Check amount is real
    try:
        print("Sending: ",
              Decimal(components[1]) * 1000000000000000000000000000000)
        amount = int(Decimal(components[1]) * 1000000000000000000000000000000)
        print('Amount to send: ', amount)
        authcode = int(components[3])
    except:
        resp = MessagingResponse()
        resp.message(
            "Error: Incorrect Amount please use the following format\nsend 10 +1234567890 1001"
        )
        return str(resp)
        print('Error with send')
    destination = components[2]

    destination = destination.replace("\u202d", "")
    destination = destination.replace("\u202c", "")
    authcode = int(components[3])

    #Test Number validity
    regex = "(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})"

    if re.search(regex, destination):
        valid_num = "Yes"
    else:
        valid_num = "No"

    print(f'Destination: {destination}\nValid Number: {valid_num}')

    if authcode == int(user_details.authcode):
        print('Authcode valid!')
        if amount > balance:
            print("Insufficient balance", "\nAmount to send: ",
                  str(amount), "\nBalance: ",
                  str(balance / 1000000000000000000000000000000))
            resp = MessagingResponse()
            resp.message(f'Insufficient balance!\nYour Balance: {balance}')
            return resp

        #Send to xrb address
        if destination[:4] == "xrb_":
            print("Destination is xrb addresses format")
            nano.send_xrb(destination, amount, account, user_details.id + 1,
                          str(wallet_seed))
            resp = MessagingResponse()
            new_authcode = authcode_gen_save(user_details)

            previous = nano.get_previous(str(account))
            balance = int(nano.get_balance(previous))/\
                                   1000000000000000000000000000000

            resp.message(
                f'Sent to {destination}!\nYour Balance approx.: {balance}\nNew Authcode: {new_authcode}'
            )
            return resp

    #Send to Alias
        if valid_num == "No":

            print("Sending request to alias address ", destination)
            dest_user_details = Alias.get_or_none(alias=destination)
            if dest_user_details is not None:
                print(dest_user_details)

                dest_address = dest_user_details.address
                alias = dest_user_details.alias
                dest_phone = dest_user_details.phonenumber
                print(f'Sending to {dest_address} \nAlias: , {alias}')
                nano.send_xrb(dest_address, amount, account,
                              user_details.id + 1, str(wallet_seed))
                new_authcode = authcode_gen_save(user_details)

                previous = nano.get_previous(str(account))
                balance = int(nano.get_balance(previous)) / \
                    1000000000000000000000000000000

                resp = MessagingResponse()
                resp.message(
                    f'Sent to {destination}!\nYour Balance approx.: {balance}\nNew Authcode: {new_authcode}'
                )
                return resp

                bodysend = 'You have recieved nano!\nPlease send register or balance to open your block on the Nano Network'
                twilionum = Config().get("twilionum")

                print(f'Sending: {bodysend}'
                      f' to {dest_phone} from {twilionum}')

                message = client.messages.create(from_=twilionum,
                                                 body=bodysend,
                                                 to=dest_phone)

                print(message.sid)

            else:
                print("Alias not found ", destination)
                resp = MessagingResponse()
                resp.message(
                    f'Error! Unrecognized Alias\nSet an alias with - set alias myAlias'
                )
                return resp

        #send to phonenumber
        else:
            try:
                phonenum = phonenumbers.parse(destination,
                                              user_details.country)
                dest_phone = phonenumbers.format_number(
                    phonenum, phonenumbers.PhoneNumberFormat.E164)
                print('Destination is phonenumber: ', dest_phone)
            except phonenumbers.phonenumberutil.NumberParseException:
                print("Error")
                resp = MessagingResponse()
                resp.message(
                    "Error: Incorrect destination address/number try using E164 format"
                )
                return resp

        if not phonenumbers.is_possible_number(phonenum):
            resp = MessagingResponse()
            resp.message("Error: Incorrect destination")
            return resp

        dest_user_details = User.get_or_none(phonenumber=dest_phone)
        print('\nReciepient ID: ', dest_user_details)

        #Send to phonenumber, and register if not registered.
        dest_user_details = User.get_or_none(phonenumber=dest_phone)
        if dest_user_details is None:
            dest_authcode = random.SystemRandom().randint(1000, 9999)
            rec_word = ''.join(
                random.sample(open("english.txt").read().split(), 5))
            x = phonenumbers.parse(dest_phone, None)
            region = phonenumbers.region_code_for_number(x)
            print("Destination region: ", region)
            dest_user_details = User.create(phonenumber=dest_phone,
                                            country=region,
                                            time=datetime.now(),
                                            count=1,
                                            authcode=dest_authcode,
                                            claim_last=0,
                                            rec_word=rec_word)
        print("User created", dest_phone)

        dest_user_details = User.get_or_none(phonenumber=dest_phone)
        dest_address = nano.get_address(dest_user_details.id + 1,
                                        str(wallet_seed))
        print("Sending to: " + dest_address)
        nano.send_xrb(dest_address, amount, account, user_details.id + 1,
                      str(wallet_seed))

        previous = nano.get_previous(str(account))
        balance = int(nano.get_balance(previous))/ \
                               1000000000000000000000000000000

        bodysend = 'You have recieved nano!\nPlease send register or balance to open your block on the Nano Network'
        twilionum = Config().get("twilionum")

        print(f'Sending: {bodysend}' f' to {dest_phone} from {twilionum}')

        message = client.messages.create(from_=twilionum,
                                         body=bodysend,
                                         to=dest_phone)

        print(message.sid)

        resp = MessagingResponse()
        new_authcode = authcode_gen_save(user_details)
        resp.message(
            f'Sent to {destination}!\nYour Balance approx.: {balance} \nNew Authcode: {new_authcode}'
        )
        return resp

    else:
        print('Invalid authcode! \nAuthcode submitted: ', authcode)
        new_authcode = authcode_gen_save(user_details)
        print('Authcode required: ', new_authcode)
        resp = MessagingResponse()
        resp.message("Error: Incorrect Auth Code try: " + str(new_authcode))
        return resp
Exemplo n.º 2
0
def sms_ahoy_reply():

    print(request.values)
    from_number = request.values.get('From')
    from_country = request.values.get('FromCountry')

    user_details = User.get_or_none(User.phonenumber == from_number)
    if user_details is None:  # User is not found in the database
        print(f'{from_number} is not in database.')
        authcode = random.SystemRandom().randint(1000, 9999)
        rec_word = ''.join(random.sample(
            open("english.txt").read().split(), 5))
        user_details = User.create(phonenumber=from_number,
                                   country=from_country,
                                   time=datetime.now(),
                                   count=1,
                                   authcode=authcode,
                                   claim_last=0,
                                   rec_word=rec_word)

    if (datetime.now() - user_details.time).total_seconds() < 5:
        time.sleep(5)
        print(user_details.phonenumber + ' user rate locked for 5 seconds')

    else:
        print(
            f'{user_details.id} - {user_details.phonenumber} sent a message.')
        user_details.phonenumber = from_number
        user_details.country = from_country
        user_details.time = datetime.now()
        user_details.count += 1
        user_details.save()

    text_body = request.values.get('Body')
    text_body = text_body.lower()
    print("Message details: ", text_body)

    components = text_body.split(" ")
    #amount = int(components[0]) * 1000000000000000000000000

    if 'register' in text_body:
        return str(register(user_details, text_body))

    elif 'commands' in text_body:
        return str(commands(user_details, text_body))

    elif 'address' in text_body:
        return str(address(user_details, text_body))

    elif 'alias' in text_body:
        return str(alias(user_details, text_body))

    elif 'history' in text_body:
        return str(history(user_details, text_body))

    elif 'balance' in text_body:
        return str(balance(user_details, text_body))

    elif 'send' in text_body:
        return str(send(user_details, text_body))

    #check if user is sending value
    elif "authcode" in text_body:
        return str(sendauthcode(user_details, text_body))

    elif 'claim' in text_body:
        return str(claim(user_details, text_body))

    elif 'trust' in text_body:
        return str(trust(user_details, text_body))

    elif 'recover' in text_body:
        return str(recover(user_details, text_body))

    elif 'topup' in text_body:
        return str(topup(user_details, text_body))

    else:
        print('Error ' + text_body)

        # Start our response
        resp = MessagingResponse()

        # Add a message
        resp.message(
            "Command not recognised, send commands for a list of commands")

    return str(resp)
Exemplo n.º 3
0
from modules.database import Domain, User
from modules.passlib_context import pwd_context

main_url = "127.0.0.1:5000"
secret = "CkOkSuddW7UikfytdPJ5xUdPiXrseDZi2Sq68i8PJUY7agKl"

if __name__ == "__main__":
    username = input("Username: "******"Password: "******"Domain: "))
    pass_hash = pwd_context.hash(non_pass)
    print(User.create(username=username, password=pass_hash, domain=domain))