Added client listing

This commit is contained in:
2026-01-06 00:12:48 +01:00
parent 6d495468c8
commit 7dccc50b87
2 changed files with 97 additions and 25 deletions
+58 -8
View File
@@ -125,7 +125,7 @@ int main(int argc, char **argv) {
msgrcv(server_queue_id, &msgbuf, sizeof(msgbuf) - sizeof(long), 0, 0);
switch (msgbuf.mtype) {
case Login:
case Login: {
printf("Received login request for id: %s\n", msgbuf.sender);
Client *client = NULL;
for (int i = 0; i < client_count; i++) {
@@ -172,11 +172,12 @@ int main(int argc, char **argv) {
printf("User accepted: %s\n", msgbuf.sender);
send(msg_queue_id, &response);
break;
case Message:
}
case Message: {
printf("Recieved message, checking which client\n");
// Find the client that sent the message then forward it to all the
// specified recipients
client = NULL;
Client *client = NULL;
for (int i = 0; i < client_count; i++) {
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
client = &clients[i];
@@ -208,7 +209,7 @@ int main(int argc, char **argv) {
if (target_type == 'u') {
Client *target_client = NULL;
for (int i = 0; i < client_count; i++) {
if (strncmp(clients[i].id, target_id, COMMAND_LENGTH) == 0) {
if (strncmp(clients[i].nickname, target_id, COMMAND_LENGTH) == 0) {
target_client = &clients[i];
break;
}
@@ -267,8 +268,8 @@ int main(int argc, char **argv) {
continue;
}
printf("Forwarding message to user %s: %s\n", target_client->id,
msgbuf.message);
printf("Forwarding message from user %s to %s\n", client->nickname,
target_client->nickname);
msgbuf_t forward_msg = {
.mtype = Message,
.sender = "",
@@ -296,7 +297,9 @@ int main(int argc, char **argv) {
}
// TODO: Accepted and Delivered ack
break;
case Logout:
}
case Logout: {
}
case Hearbeat:
for (int i = 0; i < client_count; i++) {
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0 &&
@@ -318,8 +321,55 @@ int main(int argc, char **argv) {
}
}
break;
case List_clients:
case List_clients: {
Client *client = NULL;
for (int i = 0; i < client_count; i++) {
if (strncmp(clients[i].id, msgbuf.sender, COMMAND_LENGTH) == 0) {
client = &clients[i];
break;
}
}
// TODO: Rethink if we even want to respond in such case
if (client == NULL || !client->logged_in) {
msgbuf_t response = {
.mtype = Message,
.stype = ERR_NOT_LOGGED_IN,
};
send(read_status, &response);
continue;
}
// Parse the command parameter
// Just check if the user wants the active clients
// Not worth checking if the other option is valid
bool active_only = false;
if (strncmp(msgbuf.command, "active", COMMAND_LENGTH) == 0) {
active_only = true;
}
char list_buffer[MESSAGE_LENGTH] = "";
for (int i = 0; i < client_count; i++) {
if (active_only && !clients[i].logged_in)
continue;
// Even a tiny bit of formatting, we fancy like that
char line[COMMAND_LENGTH + 4];
snprintf(line, COMMAND_LENGTH + 4, "- %s\n", clients[i].nickname);
strncat(list_buffer, line, MESSAGE_LENGTH - strlen(list_buffer) - 1);
}
msgbuf_t response = {
.mtype = List_clients,
.command = "",
};
strncpy(response.command, active_only ? "active" : "all", COMMAND_LENGTH);
strncpy(response.message, list_buffer, MESSAGE_LENGTH);
send(client->queue_id, &response);
break;
}
case Mute:
case Unmute:
break;