implementation of registration (not finished)

This commit is contained in:
2025-11-07 17:42:59 +02:00
parent 3ef6ff6c41
commit 2336b7bb01
7 changed files with 178 additions and 38 deletions
+134 -1
View File
@@ -1,13 +1,26 @@
#include "main.h"
#include "ui.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <termios.h>
#include <unistd.h>
void ask_uname_and_password(params_t *params);
void ask_register(params_t *params);
int process_server_command(char *line, int l_len, query_args_t *q_args) {
int ws_pos = l_len;
params_t *params = q_args->params;
const char *login_options[] = {"Username", "Anonymous", "Register", NULL};
uint32_t answer;
char r_buf[INBUFSIZE];
char *cptr = strchr(line, ' ');
if (cptr != NULL && cptr > line)
@@ -15,12 +28,39 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) {
/* LOGIN */
if (!strncmp(line, "login>", ws_pos)) {
if (params->uname == NULL) {
fflush(stdout);
answer = print_ask_list("Select login type:", login_options);
switch (answer) {
case 1:
ask_uname_and_password(q_args->params);
break;
case 2:
params->uname = malloc(sizeof("anonymous"));
strcpy(params->uname, "anonymous");
q_args->state = WAIT_CLIENT;
break;
case 3:
write(q_args->sd, "register\n", sizeof "register\n");
q_args->state = WAIT_REGISTER;
ask_register(q_args->params);
sprintf(r_buf, "%s %s\n", params->uname, params->pass);
write(q_args->sd, r_buf, strlen(r_buf));
return 3;
default:
return -1;
}
}
write(q_args->sd, params->uname, strlen(params->uname));
return 0;
}
/* PASSWORD */
if (!strncmp(line, "password>", ws_pos)) {
if (q_args->params->pass == NULL) {
return 1;
}
write(q_args->sd, params->pass, strlen(params->pass));
return 0;
}
@@ -43,6 +83,7 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) {
/* WELCOME MES */
if (!strncmp(line, "Welcome, ", ws_pos)) {
printf("%s\n", line);
print_prompt(q_args->params);
q_args->state = WAIT_CLIENT;
return 0;
@@ -63,7 +104,9 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) {
}
static void file_receive(int sd, char *line) {
char fname[128]; char command[32]; size_t fsize;
char fname[128];
char command[32];
size_t fsize;
sscanf(line, "%s %s %zu", command, fname, &fsize);
char buf[INBUFSIZE];
int rlen;
@@ -85,4 +128,94 @@ static void file_receive(int sd, char *line) {
}
close(file_d);
}
void ask_uname_and_password(params_t *params) {
size_t lsize;
char *bufptr = NULL;
struct termios ts_hide, ts_show;
if (isatty(0)) {
tcgetattr(0, &ts_show);
memcpy(&ts_hide, &ts_show, sizeof(ts_show));
ts_hide.c_lflag &= ~ECHO;
}
if (params->uname == NULL) {
printf("login> ");
fflush(stdout);
getline(&(params->uname), &lsize, stdin);
lsize = strlen(params->uname);
params->uname[lsize - 1] = '\0'; /* get rid of '\n' */
}
if (params->pass == NULL) {
if (isatty(0)) {
tcsetattr(0, TCSANOW, &ts_hide);
}
printf("password> ");
getline(&(params->pass), &lsize, stdin);
if (isatty(0)) {
tcsetattr(0, TCSANOW, &ts_show);
putchar('\n');
}
lsize = strlen(params->pass);
params->pass[lsize - 1] = '\0'; /* get rid of '\n' */
free(bufptr);
bufptr = NULL;
}
}
void ask_register(params_t *params) {
size_t lsize;
char *bufptr = NULL;
struct termios ts_hide, ts_show;
if (isatty(0)) {
tcgetattr(0, &ts_show);
memcpy(&ts_hide, &ts_show, sizeof(ts_show));
ts_hide.c_lflag &= ~ECHO;
}
if (params->uname == NULL) {
printf("username> ");
fflush(stdout);
getline(&(params->uname), &lsize, stdin);
lsize = strlen(params->uname);
params->uname[lsize - 1] = '\0'; /* get rid of '\n' */
free(bufptr);
bufptr = NULL;
}
if (params->pass == NULL) {
if (isatty(0)) {
tcsetattr(0, TCSANOW, &ts_hide);
}
do {
if (params->pass != NULL) {
printf("\nERROR: password do not match! Try again!\n");
free(params->pass);
params->pass = NULL;
}
if (bufptr != NULL) {
free(bufptr);
bufptr = NULL;
}
printf("password> ");
getline(&(params->pass), &lsize, stdin);
printf("\npassword (repeat)> ");
getline(&(bufptr), &lsize, stdin);
} while(strcmp(bufptr, params->pass));
if (isatty(0)) {
tcsetattr(0, TCSANOW, &ts_show);
putchar('\n');
}
lsize = strlen(params->pass);
params->pass[lsize - 1] = '\0'; /* get rid of '\n' */
free(bufptr);
bufptr = NULL;
}
}