-
Notifications
You must be signed in to change notification settings - Fork 0
/
rooms.c
345 lines (265 loc) · 6.71 KB
/
rooms.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "chatter.h"
// typedef struct room_member room_member_t;
// struct room_member {
// user_info_t *user;
// room_member_t *next;
// };
// typedef struct room room_t;
// struct room {
// char room_name[MAX_USERNAME];
// bool private_room;
// room_member_t *room_members;
// room_t *next;
// };
room_t *rooms;
size_t num_rooms = 0;
int room_inc_id = 1; // easier to start from 1 because of strtol on client side
//checks if room exists first
int create_room(char *room_name, user_info_t *user, bool private_room, char *password){
room_t *new_room, *check_room;
room_member_t *first_user;
check_room = get_room(room_name);
if (check_room != NULL){
debugw("create_room: room with name %s already exists.", room_name);
return 10;
}
if (num_rooms >= MAX_ROOMS){
debugw("create_room: maximum number of rooms reached already.");
return 11;
}
//allocate structs
new_room = calloc(1,sizeof(room_t));
first_user = calloc(1,sizeof(room_member_t));
//point the room_member_t to the user_info_t
first_user->user = user;
first_user->owner = true;
first_user->next = NULL;
user->room = new_room;
//copy in the fields into the room struct
strcpy(new_room->room_name,room_name);
new_room->private_room = private_room;
new_room->room_members = first_user;
new_room->room_id = room_inc_id++;
if (private_room){
strcpy(new_room->password, password);
}
//add the room to the linked list
new_room->next = rooms;
rooms = new_room;
return 0;
}
//int destroy_room
//int join_room
// "get a room" - lol
room_t *get_room(char *room_name){
room_t *cur_room;
cur_room = rooms;
while (cur_room != NULL){
if (!strcmp(cur_room->room_name, room_name)){
return cur_room;
}
cur_room = cur_room->next;
}
return NULL;
}
room_t *get_room_by_id(int room_id){
room_t *cur_room;
cur_room = rooms;
while (cur_room != NULL){
if (cur_room->room_id == room_id){
return cur_room;
}
cur_room = cur_room->next;
}
return NULL;
}
int free_room_members(room_member_t *room_members){
room_member_t *save;
while(room_members != NULL){
save = room_members;
room_members->user->room = NULL;
room_members = room_members->next;
free(save);
}
return 0;
}
int add_room_member(room_t *room, user_info_t *user, char *password){
room_member_t *new_room_member;
if (user == NULL){
error("Cannot add null user.");
return 100;
}
if (room->private_room){
if (strcmp(room->password,password)){
return 61;
}
}
//allocate and populate the new room_member struct
new_room_member = calloc(1,sizeof(room_member_t));
new_room_member->owner = false;
new_room_member->user = user;
user->room = room;
//add it to the linked list
new_room_member->next = room->room_members;
room->room_members = new_room_member;
return 0;
}
int remove_room_member(room_t *room, char *username){
room_member_t *cur_member, *prev;
room_t *iter_room, *prev_room;
char message_data[MAX_SEND];
prev = NULL;
cur_member = room->room_members;
while(cur_member != NULL){
if (!strcmp(cur_member->user->username,username)){
if (prev == NULL){
room->room_members = cur_member->next;
}
else {
prev->next = cur_member->next;
}
sprintf(message_data,"User %s has left room %d %s.",cur_member->user->username,room->room_id,room->room_name);
echo_all_room(room,ECHO,message_data);
cur_member->user->room = NULL;
free(cur_member);
//set the owner to the next user
if (room->room_members != NULL){
room->room_members->owner = true;
}
// otherwise, nobody in it, close the room
else {
prev_room = NULL;
iter_room = rooms;
while(iter_room != NULL){
if (iter_room == room){
if (prev_room == NULL){
rooms = iter_room->next;
}
else {
prev_room->next = iter_room->next;
}
free_room_members(iter_room->room_members);
free(iter_room);
return 0;
}
prev_room = iter_room;
iter_room = iter_room->next;
}
}
return 0;
}
prev = cur_member;
cur_member = cur_member->next;
}
// debug("remove room member couldn't find room member with username %s", username);
return 1;
}
//happens on logout
int remove_user_from_rooms(char *username){
room_t *cur_room;
int ret;
cur_room = rooms;
while (cur_room != NULL){
ret = remove_room_member(cur_room,username);
if (ret == 0){
return 0;
}
cur_room = cur_room->next;
}
infow("User %s was not found in any rooms.", username);
return 1;
}
int list_rooms(char *sendbuff){
char room_info[64];
room_t *cur_room;
int num_rooms;
int length;
int offset;
memset(room_info,0,64);
num_rooms = 0;
sprintf(sendbuff,"RTSIL " );
offset = strlen("RTSIL ");
cur_room = rooms;
while (cur_room != NULL){
sprintf(room_info,"%s %d\r\n",cur_room->room_name, cur_room->room_id);
length = strlen(room_info);
sprintf(sendbuff + offset,"%s",room_info);
offset+=length;
cur_room = cur_room->next;
num_rooms++;
}
sprintf(sendbuff+offset,"\r\n");
return num_rooms;
}
void list_users(room_t *room, char *sendbuff){
char user_info[64];
room_member_t *member;
int length;
int offset;
memset(user_info,0,64);
sprintf(sendbuff,"UTSIL " );
offset = strlen("UTSIL ");
member = room->room_members;
while (member != NULL){
sprintf(user_info,"%s\r\n",member->user->username);
length = strlen(user_info);
sprintf(sendbuff + offset,"%s",user_info);
offset+=length;
member = member->next;
}
sprintf(sendbuff+offset,"\r\n");
}
int check_room(room_t *room){
if (room->room_members == NULL){
return 1;
}
return 0;
}
int close_room_by_name(char *room_name){
room_t *cur_room, *prev;
cur_room = rooms;
prev = NULL;
while (cur_room != NULL){
if (!strcmp(cur_room->room_name, room_name)){
//if the room of interest is first in the list, have to change
//the global rooms pointer as well
if(prev == NULL){
rooms = cur_room->next;
}
else {
prev->next = cur_room->next;
}
free_room_members(cur_room->room_members);
free(cur_room);
return 0;
}
prev = cur_room;
cur_room = cur_room->next;
}
error("Tried to remove rooom (%s) that doesn't exist.", room_name);
return 1;
}
// int close_room(room_t *room){
// room_t *cur_room, *prev;
// cur_room = rooms;
// prev = NULL;
// while (cur_room != NULL){
// if (!strcmp(cur_room->room_name, room_name)){
// //if the room of interest is first in the list, have to change
// //the global rooms pointer as well
// if(prev == NULL){
// rooms = cur_room->next;
// }
// else {
// prev->next = cur_room->next;
// }
// free_room_members(cur_room->room_members);
// free(cur_room);
// return 0;
// }
// prev = cur_room;
// cur_room = cur_room->next;
// }
// error("Tried to remove rooom (%s) that doesn't exist.", room_name);
// return 1;
// }