Added viewing of saved messages

This commit is contained in:
2026-01-06 01:15:23 +01:00
parent 7dccc50b87
commit 0bf6e710d4
3 changed files with 109 additions and 24 deletions
+35 -12
View File
@@ -8,6 +8,7 @@
#include "commands.h"
// Make a macro for this cause I'm not typing sizeof twice every time
#define send(queue_id, msgbuf_ptr) \
msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
@@ -82,24 +83,30 @@ static void input_loop(int server_queue_id, const char *client_id) {
perror("msgsnd(/msg) failed");
}
} else if (strncmp(buffer, "/list ", 6) == 0) {
char *offset = buffer + 6;
char *offset = buffer + 6;
if (strcmp(offset, "active") != 0 && strcmp(offset, "all") != 0) {
printf("Invalid list type. Use /list active or /list all\n");
continue;
}
if (strcmp(offset, "active") != 0 && strcmp(offset, "all") != 0) {
printf("Invalid list type. Use /list active or /list all\n");
continue;
}
msgbuf_t msg = {.mtype = List_clients, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
strncpy(msg.command, offset, COMMAND_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/list) failed");
}
msgbuf_t msg = {.mtype = List_clients, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
strncpy(msg.command, offset, COMMAND_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/list) failed");
}
} else if (strncmp(buffer, "/mute ", 6) == 0) {
// handle mute
} else if (strcmp(buffer, "/quit") == 0) {
// TODO: Would be nice to have a way to gracefully logout and not just
// depend on heartbeats, maybe
} else if (strncmp(buffer, "/inbox", 8) == 0) {
msgbuf_t msg = {.mtype = Inbox, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/mailbox) failed");
}
} else {
printf("Unknown command\n");
}
@@ -161,7 +168,7 @@ int main(int argc, char *argv[]) {
return 1;
}
printf("Login accepted for id '%s'\n", client_id);
printf("Login accepted. Welcome, %s!\n", resp.command);
if (fork() == 0) {
heartbeat_loop(server_queue_id, client_id);
@@ -200,6 +207,22 @@ int main(int argc, char *argv[]) {
}
break;
}
case Inbox: {
// Upon recieving an inbox message, check the command field for the index
// Index of 0 means that there are messages in the mailbox
if (strcmp(incoming.command, "0") == 0) {
printf("You have messages in your mailbox!\n");
continue;
}
// Index of 1 is just the first message so print a header
if (strcmp(incoming.command, "1") == 0) {
printf("Your inbox messages:\n");
}
printf("[%s] %s: %s\n", incoming.command, incoming.sender,
incoming.message);
break;
}
case Signal:
// Tell the user if the previous message was accepted and/or delivered
if (incoming.stype == ACK_ACCEPTED) {