Many config-loading memory fixes and group join/leave

This commit is contained in:
2026-01-26 18:56:38 +01:00
parent fe703b6ddd
commit bebc068762
4 changed files with 216 additions and 7 deletions
+46
View File
@@ -116,6 +116,32 @@ static void input_loop(int server_queue_id, const char *client_id) {
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/list) failed");
}
} else if (strncmp(buffer, "/join ", 6) == 0) {
char *group_name = buffer + 6;
if (strlen(group_name) == 0) {
printf("Group name cannot be empty\n");
continue;
}
msgbuf_t msg = {.mtype = JoinGroup, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH);
strncpy(msg.command, group_name, COMMAND_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/join) failed");
}
} else if (strncmp(buffer, "/leave ", 7) == 0) {
char *group_name = buffer + 7;
if (strlen(group_name) == 0) {
printf("Group name cannot be empty\n");
continue;
}
msgbuf_t msg = {.mtype = LeaveGroup, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH);
strncpy(msg.command, group_name, COMMAND_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/leave) failed");
}
} else if (strncmp(buffer, "/mute ", 6) == 0) {
// handle mute
} else if (strcmp(buffer, "/quit") == 0 || strcmp(buffer, "/exit") == 0 || strcmp(buffer, "/q") == 0) {
@@ -277,6 +303,26 @@ int main(int argc, char *argv[]) {
printf("Unhandled signal - code %d.\n", incoming.stype);
}
break;
case JoinGroup:
if (incoming.stype == ACK_ACCEPTED) {
printf("Successfully joined the group.\n");
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
printf("Group does not exist.\n");
} else if (incoming.stype == ERR_ALREADY_IN_GROUP) {
printf("You are already in this group.\n");
} else {
printf("Failed to join group: Unknown error (code %d).\n", incoming.stype);
}
break;
case LeaveGroup:
if (incoming.stype == ACK_ACCEPTED) {
printf("Successfully left the group.\n");
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
printf("Group does not exist.\n");
} else {
printf("Failed to leave group: Unknown error (code %d).\n", incoming.stype);
}
break;
default:
printf("Received unknown command of mtype=%ld stype=%d\n", (long)incoming.mtype, incoming.stype);
break;