forked from Jinzxc/bashenger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
302 lines (251 loc) · 6.54 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "main.h"
#include "util.c"
#include "friends.c"
#include "server.c"
#include "client.c"
char user_name[BUF_SIZE];
// password checker
int pass_auth(char *pass, char *input)
{
if (cmp(pass, input))
{
printf("\nAuthentication successful\n");
return 1;
}
printf("\nAuthentication failed\n\n");
return 0;
}
// password searcher
char *pass_find(char *data, char *input)
{
FILE *users = fopen("users.txt", "r");
char *pass = malloc(BUF_SIZE * sizeof(char *));
int got = 0;
// search for user then password
while (fgets(data, BUF_SIZE, users))
{
char * cur_username = strsep(&data, ";"); // get the username, data is now the password
if (cmp(cur_username, input))
{
strncpy(pass, data, strlen(data) - 1);
got = 1;
break;
}
}
if (!got)
{
printf("Please check the casing for your username.\n");
pass[0] = '\0';
}
fclose(users);
return pass;
}
void log_in(char *data, char *input, char *username)
{
char account[BUF_SIZE];
// input username
printf("Please enter a username: ");
readin(input, BUF_SIZE);
if(strchr(input, ' ')) {
printf("Please don't use spaces in your username\n");
username[0] = '\0';
return;
}
// set username to the given username
strcpy(username, input);
// check if the user exists
if (user_exists(username))
{
// get password
char *pass = pass_find(data, username);
if (pass[0] == '\0') {
username[0] = '\0';
free(pass);
return;
}
printf("Please enter a password: ");
readin(input, BUF_SIZE);
// authenticate password
if (!pass_auth(pass, input)) {
username[0] = '\0';
}
free(pass);
}
else
{
strncpy(account, input, BUF_SIZE);
printf("\nLooks like you are a new user. If so, please type a new password below.\nOtherwise exit the program\n\n");
readin(input, BUF_SIZE);
FILE *users = fopen("users.txt", "a");
strcat(account, ";");
strncat(account, input, BUF_SIZE);
strcat(account, "\n");
fputs(account, users);
// make user file
int cur_user = open(username, O_CREAT | O_WRONLY, 0644);
close(cur_user);
fclose(users);
}
// if things succeeded return the username
}
char ** parse_input(char * input) {
char * token;
int size = 1;
int i = 0;
while (input[i] != '\0') {
if (input[i] == ',') {
size++;
}
i++;
}
i = 0;
char ** names = malloc(size * sizeof(char *));
while ((token = strsep(&input, ",")) != NULL) {
char * tok = malloc(BUF_SIZE);
strncpy(tok, token, strlen(token));
names[i] = tok;
i++;
}
return names;
}
char ** get_friend_rooms(char *username)
{
char **friends = get_friends(username);
int i = 0;
while(friends[i])
i++;
char **rooms = malloc((i + 1) * sizeof(char *));
char room_name[BUF_SIZE];
i = 0;
int j = 0;
while(friends[i]) {
strncpy(room_name, friends[i], BUF_SIZE);
strncat(room_name, " room", 6);
if(user_exists(room_name)) {
char * room = malloc(BUF_SIZE * sizeof(char));
strncpy(room, room_name, BUF_SIZE);
rooms[j] = room;
j++;
}
free(friends[i]);
i++;
}
free(friends);
return rooms;
}
void select_rooms(char **rooms)
{
int i = 0;
int selected = 0;
char select[BUF_SIZE];
printf("\nHere are the available rooms from your friends:\n");
while(rooms[i]) {
printf("%s\n", rooms[i]);
i++;
}
printf("\n");
while(1) {
readin(select, BUF_SIZE);
if(cmp(select, "exit"))
break;
int j;
for(j = 0; j < i; j++) {
if(cmp(select, rooms[j])) {
selected = 1;
free_all(rooms);
printf("\n[%s selected]\n", select);
client(select, user_name);
}
}
if(!selected)
printf("Choose a correct room to join or type 'exit' to exit\n");
}
free_all(rooms);
printf("\n");
}
void talk_to_friends(char *username, char *input)
{
int f;
printf("Would you like to make a room (1) or join a room(2)?");
readin(input, BUF_SIZE);
char user_room[BUF_SIZE];
strncpy(user_room, username, BUF_SIZE);
strncat(user_room, " room", 6);
switch (input[0])
{
case '1':
f = fork();
if(f) {
sleep(1);
printf("%s\n", user_room);
client(user_room, user_name);
} else {
server(MAX_CLIENTS, user_room);
}
break;
case '2':
select_rooms(get_friend_rooms(username));
break;
default:
printf("Please select a valid action\n");
return;
}
}
void handle_friends(char *username, char *input)
{
printf("What would you like to do?\n");
printf("1 List your current friends\n2 Add a friend\n3 Remove a friend\n4 Talk to friends\n\n");
readin(input, BUF_SIZE);
switch (input[0])
{
case '1':
list_friends(username);
break;
case '2':
printf("Who would you like to add? ");
readin(input, BUF_SIZE);
add_friend(username, input);
break;
case '3':
printf("Who would you like to delete? ");
readin(input, BUF_SIZE);
remove_friend(username, input);
break;
case '4':
talk_to_friends(username, input);
break;
default:
printf("Please select a valid action\n");
}
}
// If you are a new user,
// add semicolons to your username at your own risk
int main()
{
char input[BUF_SIZE]; // store user inputs
char data[BUF_SIZE]; // store other data
char username[BUF_SIZE]; // store current user
// make a users file if there are none
// users file will contain all users
int users = open("users.txt", O_CREAT, 0644);
close(users);
log_in(data, input, username);
if (username[0] == '\0')
return 0;
strncpy(user_name, username, BUF_SIZE);
printf("\nWelcome %s!\n\n", username);
while (1)
{
handle_friends(username, input);
}
return 0;
}