implementation of registration
This commit is contained in:
+7
-6
@@ -1,6 +1,7 @@
|
||||
client.o: src/client.c src/main.h
|
||||
connection.o: src/connection.c src/main.h
|
||||
file_processor.o: src/file_processor.c src/main.h
|
||||
main.o: src/main.c src/main.h
|
||||
query.o: src/query.c src/main.h
|
||||
server.o: src/server.c src/main.h
|
||||
client.o: src/client.c src/main.h src/types.h
|
||||
connection.o: src/connection.c src/main.h src/types.h
|
||||
file_processor.o: src/file_processor.c src/main.h src/types.h
|
||||
main.o: src/main.c src/main.h src/types.h
|
||||
query.o: src/query.c src/main.h src/types.h
|
||||
server.o: src/server.c src/main.h src/types.h src/ui.h
|
||||
ui.o: src/ui.c
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <stddef.h>
|
||||
#include "types.h"
|
||||
|
||||
#define LOCAL_PORT 1999
|
||||
#define SERVER_PORT 2000
|
||||
@@ -18,6 +19,7 @@ enum state {
|
||||
WAIT_SERVER_INIT,
|
||||
WAIT_SERVER,
|
||||
WAIT_REGISTER,
|
||||
WAIT_REGISTER_CONFIRMATION,
|
||||
WAIT_CLIENT,
|
||||
UPLOAD_FILE,
|
||||
STATE_FILE_LIST,
|
||||
@@ -67,11 +69,13 @@ void get_missing_params(params_t *restrict);
|
||||
void connect_to_server(int sd, params_t *params);
|
||||
int init_client();
|
||||
void close_session(int sd);
|
||||
void wait_register(query_args_t *q_args);
|
||||
int process_query(query_args_t *query_args, file_args_t *file_args);
|
||||
void query_loop(int sd, params_t *);
|
||||
int query_extract_from_buf(char *buf, int *buf_used, char **output_line);
|
||||
int process_client_command(char *line, int l_len, query_args_t *q_args);
|
||||
int process_server_command(char *line, int l_len, query_args_t *q_args);
|
||||
void ask_register(params_t *params, char *email);
|
||||
|
||||
void clear_params(params_t *params);
|
||||
|
||||
|
||||
+14
-1
@@ -93,6 +93,16 @@ void query_loop(int sd, params_t *params) {
|
||||
}
|
||||
}
|
||||
|
||||
void wait_register(query_args_t *q_args) {
|
||||
params_t *params = q_args->params;
|
||||
char r_buf[INBUFSIZE];
|
||||
q_args->state = WAIT_REGISTER_CONFIRMATION;
|
||||
char email[EMAIL_LEN];
|
||||
ask_register(q_args->params, email);
|
||||
sprintf(r_buf, "register %s %s %s\n", params->uname, params->pass, email);
|
||||
write(q_args->sd, r_buf, strlen(r_buf) - 1);
|
||||
}
|
||||
|
||||
int process_query(query_args_t *query_args, file_args_t *file_args) {
|
||||
query_args->buf[query_args->buf_used] = 0;
|
||||
|
||||
@@ -100,7 +110,7 @@ int process_query(query_args_t *query_args, file_args_t *file_args) {
|
||||
case WAIT_SERVER:
|
||||
case WAIT_SERVER_INIT:
|
||||
case WAIT_CLIENT:
|
||||
case WAIT_REGISTER:
|
||||
case WAIT_REGISTER_CONFIRMATION:
|
||||
wait_side(query_args);
|
||||
break;
|
||||
case UPLOAD_FILE:
|
||||
@@ -138,6 +148,9 @@ int process_query(query_args_t *query_args, file_args_t *file_args) {
|
||||
break;
|
||||
case STATE_ASK_USER_BEFORE_LOGIN:
|
||||
break;
|
||||
case WAIT_REGISTER:
|
||||
wait_register(query_args);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
+27
-10
@@ -13,14 +13,14 @@
|
||||
#include <unistd.h>
|
||||
|
||||
void ask_uname_and_password(params_t *params);
|
||||
void ask_register(params_t *params);
|
||||
void ask_register(params_t *params, char *email);
|
||||
|
||||
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];
|
||||
uint32_t answer;
|
||||
|
||||
char *cptr = strchr(line, ' ');
|
||||
if (cptr != NULL && cptr > line)
|
||||
@@ -41,11 +41,7 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) {
|
||||
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));
|
||||
wait_register(q_args);
|
||||
return 3;
|
||||
default:
|
||||
return -1;
|
||||
@@ -76,6 +72,20 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* REGISTER CONFIRMATION */
|
||||
if (q_args->state == WAIT_REGISTER_CONFIRMATION) {
|
||||
if (!strcmp(line, "ok")) {
|
||||
printf("Welcome, %s\n", q_args->params->uname);
|
||||
print_prompt(q_args->params);
|
||||
q_args->state = WAIT_CLIENT;
|
||||
return 0;
|
||||
} else {
|
||||
write(STDOUT_FILENO, line, strlen(line));
|
||||
q_args->state = WAIT_REGISTER;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* DOWNLOAD START */
|
||||
if (!strncmp(line, "download_start", ws_pos)) {
|
||||
return 0;
|
||||
@@ -167,7 +177,7 @@ void ask_uname_and_password(params_t *params) {
|
||||
}
|
||||
}
|
||||
|
||||
void ask_register(params_t *params) {
|
||||
void ask_register(params_t *params, char *email) {
|
||||
size_t lsize;
|
||||
char *bufptr = NULL;
|
||||
|
||||
@@ -190,6 +200,7 @@ void ask_register(params_t *params) {
|
||||
}
|
||||
|
||||
if (params->pass == NULL) {
|
||||
// hides input letters
|
||||
if (isatty(0)) {
|
||||
tcsetattr(0, TCSANOW, &ts_hide);
|
||||
}
|
||||
@@ -198,7 +209,7 @@ void ask_register(params_t *params) {
|
||||
printf("\nERROR: password do not match! Try again!\n");
|
||||
free(params->pass);
|
||||
params->pass = NULL;
|
||||
}
|
||||
}
|
||||
if (bufptr != NULL) {
|
||||
free(bufptr);
|
||||
bufptr = NULL;
|
||||
@@ -207,12 +218,18 @@ void ask_register(params_t *params) {
|
||||
getline(&(params->pass), &lsize, stdin);
|
||||
printf("\npassword (repeat)> ");
|
||||
getline(&(bufptr), &lsize, stdin);
|
||||
} while(strcmp(bufptr, params->pass));
|
||||
} while (strcmp(bufptr, params->pass));
|
||||
|
||||
// shows input letters again
|
||||
if (isatty(0)) {
|
||||
tcsetattr(0, TCSANOW, &ts_show);
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
printf("email> ");
|
||||
fflush(stdout);
|
||||
scanf("%s", email);
|
||||
|
||||
lsize = strlen(params->pass);
|
||||
params->pass[lsize - 1] = '\0'; /* get rid of '\n' */
|
||||
free(bufptr);
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
#ifndef D_TYPES_H
|
||||
#define D_TYPES_H
|
||||
|
||||
/* user */
|
||||
#define EMAIL_LEN 256
|
||||
#define UNAME_LEN 51
|
||||
#define PASS_LEN 51
|
||||
/* file */
|
||||
#define FILE_NAME_LEN 65
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user