-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.c
132 lines (122 loc) · 2.57 KB
/
chat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include"chat.h"
void chat_loop(int socket){
char buff[MAX], sbuff[MAX+MAX_UNAME];
char tmp;
int n;
timeout(1);
bzero(buff, sizeof(buff));
n=0;
while(1){
tmp = getch();
if(tmp == '`'){
sprintf(buff, "%d", BYE_MESSAGE);
send(socket, buff, sizeof(buff), 0);
return;
}
else if(tmp == '\n'){
if(send(socket, buff, sizeof(buff), 0)<0){
print_msg("Failed to send message to server\n");
}
bzero(buff, sizeof(buff));
clear_prompt();
n=0;
}
else if(tmp == ALT_BACKSPACE){
if(n > 0){
erase_char(stdscr);
buff[--n] = NULL;
}
}
else if(tmp != ERR){
print_char(stdscr, tmp);
buff[n++] = tmp;
}
bzero(sbuff, sizeof(sbuff));
recv(socket, sbuff, sizeof(sbuff), MSG_DONTWAIT);
if(check_buff(sbuff, sizeof(sbuff)) == 0){
print_msg(sbuff);
}
}
}
int send_uname(int socket){
char buff[MAX], unbuff[MAX_UNAME];
char tmp;
int n=0;
WINDOW* uname_win;
bzero(buff, sizeof(buff));
bzero(unbuff, sizeof(unbuff));
uname_win = init_popup();
if(recv(socket, buff, sizeof(buff), 0)<0){
print_popup_prompt(uname_win, "Failed to get responce from server");
return -1;
}
print_popup_prompt(uname_win, buff);
while((tmp = getch())!='\n' || n<1){
if(tmp == ALT_BACKSPACE){
if(n > n){
erase_char(uname_win);
unbuff[--n] = NULL;
}
}
else if(tmp != ERR && tmp != '\n'){
if(n < MAX_UNAME){
print_char(uname_win, tmp);
unbuff[n++] = tmp;
}
}
}
close_window(uname_win);
return send(socket, unbuff, sizeof(unbuff), 0);
}
int send_pass(int socket){
char buff[MAX], passbuff[MAX_UNAME];
char tmp;
int n=0;
WINDOW* pass_win;
bzero(buff, sizeof(buff));
pass_win = init_popup();
if(recv(socket, buff, sizeof(buff), 0)<0){
print_popup_prompt(pass_win, "Failed to get responce from server");
return -1;
}
print_popup_prompt(pass_win, buff);
while((tmp = getch())!='\n'){
if(tmp == ALT_BACKSPACE){
if(n > 0){
erase_char(pass_win);
passbuff[--n] = NULL;
}
}
else if(tmp != ERR && tmp != '\n'){
if(n < MAX_UNAME){
print_char(pass_win, '*');
passbuff[n++] = tmp;
}
}
}
close_window(pass_win);
return send(socket, passbuff, sizeof(passbuff), 0);
}
int check_buff(char* buffer, size_t n){
for(int i=0;i<n;i++){
if(buffer[i]!=NULL)
return 0;
}
return -1;
}
int find_free(int array[], size_t size){
for(int i =0; i<size;i++){
if(array[i] == NULL){return i;};
}
return -1;
}
int find_string_in_array(char** array, char* entry, size_t arr_len){
for(int i=0;i<arr_len;i++){
if(array[i]!=NULL){
if(strcmp(array[i], entry) == 0){
return i;
}
}
}
return -1;
}