Reworked msg command to be easier to use. Added group messaging.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/msg.h>
|
||||
@@ -9,8 +10,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)
|
||||
#define send(queue_id, msgbuf_ptr) msgsnd(queue_id, msgbuf_ptr, sizeof(*(msgbuf_ptr)) - sizeof(long), 0)
|
||||
|
||||
static void heartbeat_loop(int server_queue_id, const char *client_id) {
|
||||
msgbuf_t msg = {.mtype = Hearbeat, .sender = ""};
|
||||
@@ -33,8 +33,7 @@ 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
|
||||
// I'd prefer some more space than required to not have to worry about cutoffs
|
||||
char buffer[COMMAND_LENGTH + MESSAGE_LENGTH + 64];
|
||||
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
|
||||
break;
|
||||
@@ -43,41 +42,32 @@ static void input_loop(int server_queue_id, const char *client_id) {
|
||||
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");
|
||||
printf("Invalid /msg format. Use /msg <nickname> or #<groupname> <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");
|
||||
|
||||
if (target_len == 0 || target_len >= COMMAND_LENGTH) {
|
||||
printf("Invalid target length\n");
|
||||
continue;
|
||||
}
|
||||
strncpy(target, buffer + 5, target_len);
|
||||
|
||||
strncpy(target, buffer + 5, target_len); // Copy everything including '#' if present
|
||||
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.command, target, COMMAND_LENGTH - 1); // Send as-is (with '#' if group)
|
||||
strncpy(msg.message, message, MESSAGE_LENGTH - 1);
|
||||
if (send(server_queue_id, &msg) == -1) {
|
||||
perror("msgsnd(/msg) failed");
|
||||
@@ -85,12 +75,12 @@ static void input_loop(int server_queue_id, const char *client_id) {
|
||||
} else if (strncmp(buffer, "/list ", 6) == 0) {
|
||||
char *offset = buffer + 6;
|
||||
|
||||
if (strcmp(offset, "active") != 0 && strcmp(offset, "all") != 0) {
|
||||
printf("Invalid list type. Use /list active or /list all\n");
|
||||
if (strcmp(offset, "active") != 0 && strcmp(offset, "all") != 0 && strcmp(offset, "groups") != 0) {
|
||||
printf("Invalid list type. Use /list active or /list all or /list groups\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
msgbuf_t msg = {.mtype = List_clients, .sender = ""};
|
||||
msgbuf_t msg = {.mtype = List, .sender = ""};
|
||||
strncpy(msg.sender, client_id, COMMAND_LENGTH - 1);
|
||||
strncpy(msg.command, offset, COMMAND_LENGTH - 1);
|
||||
if (send(server_queue_id, &msg) == -1) {
|
||||
@@ -154,16 +144,19 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
/* Wait for login response (server replies with mtype = Login) */
|
||||
msgbuf_t resp;
|
||||
ssize_t r =
|
||||
msgrcv(client_queue_id, &resp, sizeof(resp) - sizeof(long), Login, 0);
|
||||
ssize_t r = msgrcv(client_queue_id, &resp, sizeof(resp) - sizeof(long), Login, 0);
|
||||
if (r == -1) {
|
||||
perror("msgrcv(login response) failed");
|
||||
cleanup_queue(client_queue_id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (resp.stype != ACK_ACCEPTED) {
|
||||
fprintf(stderr, "Login rejected: code=%d\n", resp.stype);
|
||||
if (resp.stype == ERR_ALREADY_LOGGED_IN) {
|
||||
fprintf(stderr, "Login failed: User already logged in\n");
|
||||
cleanup_queue(client_queue_id);
|
||||
return 1;
|
||||
} else if (resp.stype == ERR_USER_NOT_FOUND) {
|
||||
fprintf(stderr, "Login failed: User not found\n");
|
||||
cleanup_queue(client_queue_id);
|
||||
return 1;
|
||||
}
|
||||
@@ -182,8 +175,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
while (1) {
|
||||
msgbuf_t incoming;
|
||||
ssize_t got = msgrcv(client_queue_id, &incoming,
|
||||
sizeof(incoming) - sizeof(long), 0, 0);
|
||||
ssize_t got = msgrcv(client_queue_id, &incoming, sizeof(incoming) - sizeof(long), 0, 0);
|
||||
if (got == -1) {
|
||||
perror("msgrcv(incoming) failed");
|
||||
|
||||
@@ -198,10 +190,12 @@ int main(int argc, char *argv[]) {
|
||||
case Message:
|
||||
printf("%s: %s\n", incoming.sender, incoming.message);
|
||||
break;
|
||||
case List_clients: {
|
||||
case List: {
|
||||
// Check the command field for the list type
|
||||
if (strncmp(incoming.command, "active", COMMAND_LENGTH) == 0) {
|
||||
printf("Active clients:\n%s\n", incoming.message);
|
||||
} else if (strncmp(incoming.command, "group", COMMAND_LENGTH) == 0) {
|
||||
printf("All group members:\n%s\n", incoming.message);
|
||||
} else {
|
||||
printf("All clients:\n%s\n", incoming.message);
|
||||
}
|
||||
@@ -209,9 +203,15 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
case Inbox: {
|
||||
// Upon recieving an inbox message, check the command field for the index
|
||||
// Index of -1 means that there are no messages in the mailbox
|
||||
if (strcmp(incoming.command, "-1") == 0) {
|
||||
printf("Your inbox is empty.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
// 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");
|
||||
printf("You have messages in your box!\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -219,8 +219,7 @@ int main(int argc, char *argv[]) {
|
||||
if (strcmp(incoming.command, "1") == 0) {
|
||||
printf("Your inbox messages:\n");
|
||||
}
|
||||
printf("[%s] %s: %s\n", incoming.command, incoming.sender,
|
||||
incoming.message);
|
||||
printf("[%s] %s: %s\n", incoming.command, incoming.sender, incoming.message);
|
||||
break;
|
||||
}
|
||||
case Signal:
|
||||
@@ -228,19 +227,16 @@ int main(int argc, char *argv[]) {
|
||||
if (incoming.stype == ACK_ACCEPTED) {
|
||||
printf("Message sent successfully.\n");
|
||||
} else if (incoming.stype == ACK_DELIVERED) {
|
||||
printf("Message delivered to the %s.\n", incoming.command);
|
||||
printf("Message delivered to %s.\n", incoming.sender);
|
||||
} else if (incoming.stype == ERR_USER_NOT_FOUND) {
|
||||
printf("Your message could not be delivered: User not found.\n");
|
||||
} else {
|
||||
// Display whatever if I forgot to handle something
|
||||
printf("Your message could not be delivered: Error code %d.\n",
|
||||
incoming.stype);
|
||||
printf("Unhandled signal - code %d.\n", incoming.stype);
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
printf("Received unknown command of mtype=%ld stype=%d\n",
|
||||
(long)incoming.mtype, incoming.stype);
|
||||
printf("Received unknown command of mtype=%ld stype=%d\n", (long)incoming.mtype, incoming.stype);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user