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
+72 -11
View File
@@ -166,15 +166,23 @@ int main(int argc, char **argv) {
client->last_seen = tv.tv_sec;
msgbuf_t response = {
.mtype = Login,
.command = "",
.stype = ACK_ACCEPTED,
};
strncpy(response.command, client->nickname, COMMAND_LENGTH);
printf("User accepted: %s\n", msgbuf.sender);
send(msg_queue_id, &response);
// Check if there are any saved messages for this client
if (client->saved_message_count > 0) {
response.mtype = Inbox;
snprintf(response.command, COMMAND_LENGTH, "0");
send(client->queue_id, &response);
}
break;
}
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 *client = NULL;
@@ -185,12 +193,12 @@ int main(int argc, char **argv) {
}
}
printf("Received message from client id %s: %s\n", msgbuf.sender,
msgbuf.message);
printf("Received message from client id %s to %s\n", msgbuf.sender,
msgbuf.command);
if (client == NULL || !client->logged_in) {
// Client not found or not logged in
printf("Client not found or not logged in");
printf("Sender not found or not logged in");
msgbuf_t response = {
.mtype = Message,
.stype = ERR_NOT_LOGGED_IN,
@@ -217,7 +225,7 @@ int main(int argc, char **argv) {
if (target_client == NULL) {
// Target client not found or not logged in
printf("Target client %s not found or not logged in\n", target_id);
printf("Target client %s not found\n", target_id);
msgbuf_t response = {
.mtype = Message,
.stype = ERR_USER_NOT_FOUND,
@@ -330,13 +338,7 @@ int main(int argc, char **argv) {
}
}
// 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;
}
@@ -369,7 +371,66 @@ int main(int argc, char **argv) {
send(client->queue_id, &response);
break;
}
case Inbox: {
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;
}
}
if (client == NULL || !client->logged_in)
continue;
msgbuf_t response = {
.mtype = Inbox,
.sender = "",
.command = "",
.message = "",
};
// Now, for all the stored messages, send them one by one, incrementing
// the counter in command
for (int i = 0; i < client->saved_message_count; i++) {
snprintf(response.command, COMMAND_LENGTH, "%d", i + 1);
strncpy(response.sender, client->saved_messages[i].sender,
COMMAND_LENGTH);
strncpy(response.message, client->saved_messages[i].message,
MESSAGE_LENGTH);
send(client->queue_id, &response);
}
// Now send ACK_DELIVERED to senders
for (int i = 0; i < client->saved_message_count; i++) {
saved_message_t *saved_msg = &client->saved_messages[i];
Client *original_sender = NULL;
for (int j = 0; j < client_count; j++) {
if (strncmp(clients[j].nickname, saved_msg->sender, COMMAND_LENGTH) ==
0) {
original_sender = &clients[j];
break;
}
}
// Technically, the first check should never fail, as we can't remove
// users dynamically but I'll leave it here anyway
if (original_sender == NULL || !original_sender->logged_in) {
continue;
}
msgbuf_t ack_msg = {
.mtype = Signal,
.sender = "",
.command = "",
.stype = ACK_DELIVERED,
};
strncpy(ack_msg.sender, client->nickname, COMMAND_LENGTH);
send(original_sender->queue_id, &ack_msg);
}
client->saved_message_count = 0;
break;
}
case Mute:
case Unmute:
break;