Client can now actually send messages via command

Client can now specify the recipient of a message and type it. Messages for offline users are saved, unaccesible for now.
This commit is contained in:
2026-01-05 18:04:59 +01:00
parent 4a089ed368
commit 6d495468c8
5 changed files with 216 additions and 54 deletions
+79 -34
View File
@@ -29,6 +29,70 @@ static void cleanup_queue(int qid) {
}
}
static void input_loop(int server_queue_id, const char *client_id) {
while (1) {
// Minimum length plus extra space fore command name and seperators
// I'd prefer some more space than required to not have to worry about cut
// offs
char buffer[COMMAND_LENGTH + MESSAGE_LENGTH + 64];
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
break;
}
buffer[strcspn(buffer, "\n")] = '\0';
if (strncmp(buffer, "/msg ", 5) == 0) {
// In a /msg command we expect {u|g}:<nickname up to 14 chars> <message up
// to 256 chars>
char *space_ptr = strchr(buffer + 5, ' ');
if (space_ptr == NULL) {
printf("Invalid /msg format. Use /msg u:<nickname> <message>\n");
continue;
}
char target[COMMAND_LENGTH];
size_t target_len = space_ptr - (buffer + 5);
if (target_len >= COMMAND_LENGTH) {
printf("Target nickname too long\n");
continue;
}
strncpy(target, buffer + 5, target_len);
target[target_len] = '\0';
char *message = space_ptr + 1;
if (strlen(message) == 0) {
printf("Message cannot be empty\n");
continue;
}
// Check the format of the command string
if (target[1] != ':') {
printf("Invalid target format. Use u:<nickname> or g:<groupname>\n");
continue;
}
if (target[0] != 'u' && target[0] != 'g') {
printf("Invalid target type. Use 'u' for user or 'g' for group\n");
continue;
}
msgbuf_t msg = {.mtype = Message, .sender = ""};
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
snprintf(msg.command, COMMAND_LENGTH, "%s", target);
strncpy(msg.message, message, MESSAGE_LENGTH - 1);
if (send(server_queue_id, &msg) == -1) {
perror("msgsnd(/msg) failed");
}
} else if (strncmp(buffer, "/list", 5) == 0) {
// handle list
} else if (strncmp(buffer, "/mute ", 6) == 0) {
// handle mute
} else if (strcmp(buffer, "/quit") == 0) {
// exit
} else {
printf("Unknown command\n");
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <client_id>\n", argv[0]);
@@ -91,33 +155,27 @@ int main(int argc, char *argv[]) {
return 0;
}
/* Send a test message to the server */
msgbuf_t test = {.mtype = Message, .sender = ""};
strncpy(test.sender, client_id, COMMAND_LENGTH - 1);
snprintf(test.message, MESSAGE_LENGTH, "Hello from %s", client_id);
if (msgsnd(server_queue_id, &test, sizeof(test) - sizeof(long), 0) == -1) {
printf("DEBUG: test.mtype = %u\n", test.mtype);
perror("failed while sending a test message");
cleanup_queue(client_queue_id);
return 1;
if (fork() == 0) {
input_loop(server_queue_id, client_id);
return 0;
}
printf("Sent test message to server: \"%s\"\n", test.message);
// /* Send a test message to the server */
// msgbuf_t test = {.mtype = Message, .command = "u:test2", .sender = ""};
// strncpy(test.sender, client_id, COMMAND_LENGTH - 1);
// snprintf(test.message, MESSAGE_LENGTH, "Hello from %s", client_id);
/* Now wait for server forwarded message(s) and a signal ack.
We'll wait until we've seen both a Message and a Signal ack or timeout.
*/
int seen_message = 0;
int seen_ack = 0;
const int MAX_ITER = 10;
// if (msgsnd(server_queue_id, &test, sizeof(test) - sizeof(long), 0) == -1) {
// printf("DEBUG: test.mtype = %u\n", test.mtype);
// perror("failed while sending a test message");
// cleanup_queue(client_queue_id);
// return 1;
// }
// for (int i = 0; i < MAX_ITER && !(seen_message && seen_ack); ++i) {
while (1) {
msgbuf_t incoming;
ssize_t got = msgrcv(client_queue_id, &incoming,
sizeof(incoming) - sizeof(long), 0, 0);
perror("Got a message damn");
if (got == -1) {
perror("msgrcv(incoming) failed");
@@ -130,14 +188,10 @@ int main(int argc, char *argv[]) {
switch (incoming.mtype) {
case Message:
printf("Received Message: \"%s\"\n", incoming.message);
seen_message = 1;
printf("%s: %s\n", incoming.sender, incoming.message);
break;
case Signal:
printf("Received Signal: code=%d\n", incoming.stype);
if (incoming.stype == ACK_ACCEPTED || incoming.stype == ACK_DELIVERED) {
seen_ack = 1;
}
break;
default:
printf("Received unknown mtype=%ld stype=%d\n", (long)incoming.mtype,
@@ -146,15 +200,6 @@ int main(int argc, char *argv[]) {
}
}
if (!seen_message) {
fprintf(stderr, "Did not receive forwarded message from server\n");
}
if (!seen_ack) {
fprintf(stderr, "Did not receive ack from server\n");
}
/* Logout/cleanup: remove our queue */
cleanup_queue(client_queue_id);
printf("Client exiting\n");
return 0;
}