-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.c
234 lines (209 loc) · 5.38 KB
/
support.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "support.h"
/**
* Adds a client to the queue
* @param client The client_data to add to the queue
*/
void add_client(client_data *client) {
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(!CLIENTS[i]){
CLIENTS[i] = client;
CLIENT_COUNT++;
return;
}
}
}
/**
* Removes a client from the queue
* @param id The id of the client to be removed
*/
void remove_client(client_data *client) {
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(CLIENTS[i]){
if(CLIENTS[i]->id == client->id){
CLIENTS[i] = NULL;
CLIENT_COUNT--;
return;
}
}
}
}
/**
* Handle all input and output for clients
* @param client The client to 'handle'
*/
void *handle_client(client_data *client) {
char output[1024];
char input[1024];
int len;
printf("New client with id: %d and address: %s\n", client->id, addr_to_string(client->addr));
sprintf(output, "(server): %s joined the room\n", client->username);
send_all(output);
while((len = read(client->fd, input, sizeof(input)-1)) > 0){
input[len] = '\0';
output[0] = '\0';
strip_newline(input);
if(!strlen(input)){
continue;
}
/* Commands */
if(input[0] == '('){
char *command, *param;
command = strtok(input," ");
if(!strcmp(command, "(LEAVE)")){
break;
} else if(!strcmp(command, "(PING)")) {
send_client("(server): pong\n", client);
} else if(!strcmp(command, "(USERNAME)")) {
param = strtok(NULL, " ");
if(param){
char *old_username = strdup(client->username);
strcpy(client->username, param);
sprintf(output, "(server): %s changed to %s\n", old_username, client->username);
free(old_username);
send_all(output);
}else{
send_client("(server): Error new username cannot be empty\n", client);
}
} else if(!strcmp(command, "(DM)")) {
param = strtok(NULL, " ");
if(param) {
int id = atoi(param);
param = strtok(NULL, " ");
if(param){
sprintf(output, "(server): Direct message from (%s)", client->username);
while(param != NULL){
strcat(output, " ");
strcat(output, param);
param = strtok(NULL, " ");
}
strcat(output, "\n");
int i;
for(i=0;i<MAX_CLIENTS; i++){
if(CLIENTS[i]){
if(CLIENTS[i]->id == id){
send_client(output, CLIENTS[i]);
}
}
}
} else {
send_client("(server): Error message can't be empty\n", client);
}
} else {
send_client("(server): Error tried to send message to nonexistant user\n", client);
}
} else if(!strcmp(command, "(ONLINE)")) {
sprintf(output, "(server): %d people online\n", CLIENT_COUNT);
send_client(output, client);
list_online(client);
} else if(!strcmp(command, "(HELP)")){
strcat(output, "(USERNAME) <username> Change your username\n");
strcat(output, "(DM) <username/id> <message> Send a direct message\n");
strcat(output, "(ONLINE) List people online\n");
strcat(output, "(HELP) Show help menu\n");
strcat(output, "(LEAVE) Leave the room\n");
send_client(output, client);
} else {
send_client("(server): I have no clue what you are trying to do. :p\n", client);
}
} else { // Message
sprintf(output, "(%s) %s\n", client->username, input);
send_message(output, client);
}
}
close(client->fd);
sprintf(output, "(server): %s left the room\n", client->username);
send_all(output);
remove_client(client);
printf("Client %d with address %s left\n", client->id, addr_to_string(client->addr));
free(client);
return NULL;
}
/**
* Send a message to all clients but the provided one
* @param s The message to send
* @param client The client to exclude
*/
void send_message(char *s, client_data *client){
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(CLIENTS[i]){
if(CLIENTS[i]->id != client->id){
write(CLIENTS[i]->fd, s, strlen(s));
}
}
}
}
/**
* Send a message to all clients
* @param s The message to send
*/
void send_all(char *s) {
int i;
for(i=0;i<MAX_CLIENTS;i++){
if(CLIENTS[i]){
write(CLIENTS[i]->fd, s, strlen(s));
}
}
}
/**
* Send a message to a specific client
* @param s The message to send
* @param client The client to send to
*/
void send_client(char *s, client_data *client){
write(client->fd, s, strlen(s));
}
/**
* Lists all online clients
* @param client The client to send the list to
*/
void list_online(client_data *client){
int i;
char s[64];
for(i=0;i<MAX_CLIENTS;i++){
if(CLIENTS[i]){
send_client("Online Now:\n", client);
send_client("------------\n", client);
sprintf(s, "%d | %s\n", CLIENTS[i]->id, CLIENTS[i]->username);
send_client(s, client);
}
}
}
/**
* Strips newline and replaces with terminating null
* @param s [description]
*/
void strip_newline(char *s){
while(*s != '\0'){
if(*s == '\n'){
*s = '\0';
}
s++;
}
}
/**
* Converts sockaddr_in to a printable string
* @param addr A sockaddr_in instance
* @return The string address in IPv4 form
*/
char *addr_to_string(struct sockaddr_in addr) {
char *buff = (char*)malloc(17 * sizeof(char));
sprintf(buff, "%d.%d.%d.%d",
addr.sin_addr.s_addr & 0xFF,
(addr.sin_addr.s_addr & 0xFF00)>>8,
(addr.sin_addr.s_addr & 0xFF0000)>>16,
(addr.sin_addr.s_addr & 0xFF000000)>>24);
return buff;
}