Added muting and unmuting

This commit is contained in:
2026-01-26 22:07:17 +01:00
parent bebc068762
commit 6ecbdfa9fa
3 changed files with 281 additions and 26 deletions
+49 -6
View File
@@ -143,7 +143,31 @@ static void input_loop(int server_queue_id, const char *client_id) {
perror("msgsnd(/leave) failed");
}
} else if (strncmp(buffer, "/mute ", 6) == 0) {
// handle mute
char *nickname = buffer + 6;
if (strlen(nickname) == 0) {
printf("Nickname cannot be empty\n");
continue;
}
msgbuf_t msg = {.mtype = Mute, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
strncpy(msg.command, nickname, COMMAND_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/mute) failed");
}
} else if (strncmp(buffer, "/unmute ", 8) == 0) {
char *nickname = buffer + 8;
if (strlen(nickname) == 0) {
printf("Nickname cannot be empty\n");
continue;
}
msgbuf_t msg = {.mtype = Unmute, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
strncpy(msg.command, nickname, COMMAND_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/unmute) failed");
}
} else if (strcmp(buffer, "/quit") == 0 || strcmp(buffer, "/exit") == 0 || strcmp(buffer, "/q") == 0) {
return;
} else if (strncmp(buffer, "/inbox", 8) == 0) {
@@ -210,7 +234,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Login failed: User already logged in\n");
cleanup_queue(client_queue_id);
return 1;
} else if (resp.stype == ERR_USER_NOT_FOUND) {
} else if (resp.stype == ERR_NOT_FOUND) {
fprintf(stderr, "Login failed: User not found\n");
cleanup_queue(client_queue_id);
return 1;
@@ -222,7 +246,6 @@ int main(int argc, char *argv[]) {
client_queue_id_global = client_queue_id;
server_queue_id_global = server_queue_id;
heartbeat_pid = fork();
if (heartbeat_pid == 0) {
heartbeat_loop(server_queue_id, client_id);
@@ -296,7 +319,7 @@ int main(int argc, char *argv[]) {
printf("Message sent successfully.\n");
} else if (incoming.stype == ACK_DELIVERED) {
printf("Message delivered to %s.\n", incoming.sender);
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
} else if (incoming.stype == ERR_NOT_FOUND) {
printf("Your message could not be delivered: User not found.\n");
} else {
// Display whatever if I forgot to handle something
@@ -306,7 +329,7 @@ int main(int argc, char *argv[]) {
case JoinGroup:
if (incoming.stype == ACK_ACCEPTED) {
printf("Successfully joined the group.\n");
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
} else if (incoming.stype == ERR_NOT_FOUND) {
printf("Group does not exist.\n");
} else if (incoming.stype == ERR_ALREADY_IN_GROUP) {
printf("You are already in this group.\n");
@@ -317,12 +340,32 @@ int main(int argc, char *argv[]) {
case LeaveGroup:
if (incoming.stype == ACK_ACCEPTED) {
printf("Successfully left the group.\n");
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
} else if (incoming.stype == ERR_NOT_FOUND) {
printf("Group does not exist.\n");
} else {
printf("Failed to leave group: Unknown error (code %d).\n", incoming.stype);
}
break;
case Mute:
if (incoming.stype == ACK_ACCEPTED) {
printf("Successfully muted %s.\n", incoming.command);
} else if (incoming.stype == ERR_NOT_FOUND) {
printf("%s not found.\n", incoming.command);
} else if (incoming.stype == ERR_ALREADY_IN_GROUP) {
printf("%s is already muted.\n", incoming.command);
} else {
printf("Failed to mute: Unknown error (code %d).\n", incoming.stype);
}
break;
case Unmute:
if (incoming.stype == ACK_ACCEPTED) {
printf("Successfully unmuted %s.\n", incoming.command);
} else if (incoming.stype == ERR_NOT_FOUND) {
printf("%s wasn't muted.\n", incoming.command);
} else {
printf("Failed to unmute: 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;