
newmenu [auth setup]
menuinit [ tempalias ___ppass "" ; tempalias ___pass ""; tempalias ___rememberpass 0; ]
menuitem "To play online you need to create an account" -1
menuitem "which servers will use to identify you." -1
menuitem "This identity is stored as a public/private keypair." -1
menuitem "\f5Keep the \f2private key \f3private\f5!" -1
menuitem "You can change the unlock password at any time." -1
menuitem "You can NOT change the master password. Ever." -1
menuitem "" -1
//TODO: auth.html does not exist and the procedure does not exist
//menuitem "The way to recover a lost identity uses" -1
//menuitem "a seperate and unchangeable password." -1
//menuitem "" -1
//menuitem "To learn more about this functionality you can go to this URL in your browser" -1
//menuitem "\f1https://assault.cubers.net/docs/auth.html" -1
//menuitem "" -1
//ABOUT: the master password was advertised as 'password for account restoration'
menuitem "\f0Set passwords \f4(recommended)" -1
menuitemtextinput "Unlock password\t" "" [___pass = $arg1] [] 64
menuitemtextinput "Master password\t" "" [___ppass = $arg1] [] 64
menuitem "" -1
menuitemradio "Remember game account password: " 0 1 [ ] [ "No \f4(recommended)" "Yes"] [ ___rememberpass = (= $arg1 1)]
menuitem "\f2\t\t\t        [ OK ]" [
    authsetup genpre 
    authsetup genpriv 
    authsetup genpub
    if (= $___rememberpass 1) [ alias savedpass $___pass ] [ if (checkalias savedpass) [ delalias savedpass ] ]
    
    closecurmenu
    showmenu "auth setup progress"
]

newmenu "auth setup progress"
menuinit [ sleep 100 [ 
    if (strcmp $___ppass "") [ authsetup savepre ] [ authsetup newppass $___ppass; authsetup savepre ]
    if (strcmp $___pass "") [ authsetup savepriv ] [ authsetup newpass $___pass ]
    ___ppass = ""
    ___pass = ""
    if (authsetup) [ echo "\f0created game account" ] 
    closecurmenu
    checkaccount 
    ] ]
menuitem "Please wait while we create your account..." -1


newmenu [auth password change]
menuinit [ tempalias ___pass ""; tempalias ___rememberpass 0; ]
menuitem "Please choose a new password for your private key" -1
menuitem "" -1
menuitemtextinput "Password for your game account\t" "" [___pass = $arg1] [] 64
menuitemradio "Remember game account password: " 0 1 [ ] [ "No \f4(recommended)" "Yes"] [ ___rememberpass = (= $arg1 1)]
menuitem "\f2[ Set Password ]" [
    if (= $___rememberpass 1) [ alias savedpass $___pass ] [ if (checkalias savedpass) [ delalias savedpass ] ]
    
    closecurmenu
    showmenu "auth password change progress"
]
menuitem "\f7[ Cancel ]" closecurmenu

newmenu "auth password change progress"
menuinit [ sleep 100 [ 
    if (strcmp $___pass "") [  ] [ authsetup newpass $___pass ]
    ___pass = ""
    if (authsetup) [ echo "\f0updated game account" ] 
    closecurmenu
    checkaccount 
    ] ]
menuitem "Please wait while we update your account..." -1

  
newmenu [auth]
menuinit [
    resetcurmenu

    tempalias ___pass ""; tempalias ___rememberpass 0;

    if (authsetup) [
        menuitem "\f0Pub key:" -1
        menuitem (format "\fY%1" (mypubkey)) -1
        menuitem "\f2[ Copy to clipboard ]" [setclipboardtext (mypubkey)]
        menuitem "" -1
        menuitem "\f0Change Password" -1    
        menuitemtextinput "New password" "" [___pass = $arg1] [] 64
        menuitemradio "Remember game account password: " 0 1 [ ] [ "No \f4(recommended)" "Yes"] [ ___rememberpass = (= $arg1 1)]
        menuitem "\f2[ Update Password ]" [
            if (= $___rememberpass 1) [ alias savedpass $___pass ] [ if (checkalias savedpass) [ delalias savedpass ] ]
            
            closecurmenu
            showmenu "auth password change progress"
        ]
        menuitem "" -1
        menuitem "\f3[ Reset auth ]" [ showmenu [auth setup]]
    ] [
        if (authsetup needpass) [ 
            menuitem "\f3Password required to authenticate \f2[ Enter password ]" [ showmenu [enter password]]
            menuitem "" -1
            menuitem "\f3[ Reset Auth ]" [ showmenu [auth setup] ]
        ] [
            menuitem "\f3Auth not setup" -1
            menuitem "\f2[ Setup Auth ]" [ showmenu [auth setup] ]
        ]
    ]
]


newmenu "game account"
menuitem "To play online you need to create a game account first." -1
menuitem "" -1
menuitem "\t\t\f0[Create game account]" [ closecurmenu; showmenu "auth setup"; ]
menuitem "\t\t[Not now...]" closecurmenu


newmenu "enter password"
menuitemtextinput "please enter the password for your game account\t" "" [ __loginpass = $arg1; closecurmenu; showmenu authentication ] [] 64 1

newmenu "authentication"
menuinit [ sleep 100 [ echo "checking..." ; authsetup passd $__loginpass authfinish; __loginpass = "" ] ]
menuitem "Please wait while we authenticate your game account..." -1

const authfinish [
    closecurmenu 
    if (authsetup) [
        echo "\f0authentication successful"
    ] [
        echo "\f3authentication failed, please enter the correct password for your game account"
    ]
]

// try to authenticate the game account or offer to create a new game account if none exists
// set $arg1 to 1 to enable silent mode to prevent any user interactions
const checkaccount [
  if (authsetup) [] [
    if (authsetup needpass) [] [ 
        // load the password protected private key
        exec private/authprivate.cfg 
    ]
    if (authsetup needpass) [
        if (checkalias savedpass) [
            // try the saved password if there is one
            authsetup passd $savedpass
        ] [
            // saved password does not exist so ask for password
            if (= $arg1 0) [ showmenu "enter password" ]
        ]
    ] [
        // game account does not seem to be set up, ask user to create new game account
        if (= $arg1 0) [ showmenu "game account" ]
    ]
  ]
]
