]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_auth.c
d2db7e042b4ac04b4470d30300b43482e5b7d746
[dragonblocks_alpha.git] / src / client / client_auth.c
1 #include <stddef.h>
2 #include <stdio.h>
3 #include <linenoise/linenoise.h>
4 #include "client.h"
5 #include "client_auth.h"
6 #include "interrupt.h"
7 #include "types.h"
8
9 volatile struct ClientAuth client_auth;
10
11 static bool name_prompt()
12 {
13         if (! (client_auth.name = linenoise("Enter name: ")))
14                 return false;
15
16         printf("Authenticating as %s...\n", client_auth.name);
17         client_auth.state = AUTH_WAIT;
18
19         dragonnet_peer_send_ToServerAuth(client, &(ToServerAuth) {
20                 .name = client_auth.name,
21         });
22
23         return true;
24 }
25
26 bool client_auth_init()
27 {
28         client_auth.state = AUTH_INIT;
29
30         while (! interrupt->done) {
31                 switch (client_auth.state) {
32                         case AUTH_INIT:
33                                 if (name_prompt())
34                                         break;
35                                 else
36                                         return false;
37
38                         case AUTH_WAIT:
39                                 sched_yield();
40                                 break;
41
42                         case AUTH_SUCCESS:
43                                 return true;
44                 }
45         }
46
47         return false;
48 }
49
50 void client_auth_deinit()
51 {
52         linenoiseFree(client_auth.name);
53 }