#include #include #include #include #include #include #include #include #include #include "commands.h" #include "signal.h" // Wanted a graceful exit so need to keep this in global state static int client_queue_id_global = -1; static int server_queue_id_global = -1; static char client_id_global[COMMAND_LENGTH]; static pid_t heartbeat_pid = -1; static pid_t input_pid = -1; // 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) static void heartbeat_loop(int server_queue_id, const char *client_id) { msgbuf_t msg = {.mtype = Hearbeat, .sender = ""}; strncpy(msg.sender, client_id, COMMAND_LENGTH - 1); while (1) { if (send(server_queue_id, &msg) == -1) { perror("msgsnd(heartbeat) failed"); break; } sleep(HEARTBEAT_INTERVAL); } } static void cleanup_queue(int qid) { if (qid != -1) { msgctl(qid, IPC_RMID, NULL); } } void cleanup_and_exit(int sig) { printf("\nLogging out... "); if (server_queue_id_global != -1) { msgbuf_t logout = {.mtype = Logout, .sender = ""}; strncpy(logout.sender, client_id_global, COMMAND_LENGTH - 1); msgsnd(server_queue_id_global, &logout, sizeof(logout) - sizeof(long), 0); } // Burn the orphanage if (heartbeat_pid > 0) kill(heartbeat_pid, SIGTERM); if (input_pid > 0) kill(input_pid, SIGTERM); cleanup_queue(client_queue_id_global); printf("Bye!\n"); exit(0); } 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 cutoffs 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) { char *space_ptr = strchr(buffer + 5, ' '); if (space_ptr == NULL) { printf("Invalid /msg format. Use /msg or # \n"); continue; } char target[COMMAND_LENGTH]; size_t target_len = space_ptr - (buffer + 5); if (target_len == 0 || target_len >= COMMAND_LENGTH) { printf("Invalid target length\n"); continue; } 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; } msgbuf_t msg = {.mtype = Message, .sender = ""}; strncpy(msg.sender, client_id, COMMAND_LENGTH - 1); 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"); } } else if (strncmp(buffer, "/list ", 6) == 0) { char *offset = buffer + 6; 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, .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, "/join ", 6) == 0) { char *group_name = buffer + 6; if (strlen(group_name) == 0) { printf("Group name cannot be empty\n"); continue; } msgbuf_t msg = {.mtype = JoinGroup, .sender = ""}; strncpy(msg.sender, client_id, COMMAND_LENGTH); strncpy(msg.command, group_name, COMMAND_LENGTH - 1); if (send(server_queue_id, &msg) == -1) { perror("msgsnd(/join) failed"); } } else if (strncmp(buffer, "/leave ", 7) == 0) { char *group_name = buffer + 7; if (strlen(group_name) == 0) { printf("Group name cannot be empty\n"); continue; } msgbuf_t msg = {.mtype = LeaveGroup, .sender = ""}; strncpy(msg.sender, client_id, COMMAND_LENGTH); strncpy(msg.command, group_name, COMMAND_LENGTH - 1); if (send(server_queue_id, &msg) == -1) { perror("msgsnd(/leave) failed"); } } else if (strncmp(buffer, "/mute ", 6) == 0) { 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) { 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"); } } } int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } char *client_id = argv[1]; int client_queue_id = msgget(IPC_PRIVATE, IPC_CREAT | 0666); if (client_queue_id == -1) { perror("msgget(client) failed"); return 1; } int server_queue_id = -1; const char *id_path = "server_queue_id"; int fd = open(id_path, O_RDONLY); if (fd == -1) { perror("Failed to open server queue id file"); cleanup_queue(client_queue_id); return 1; } if (read(fd, &server_queue_id, sizeof(int)) == -1) { fprintf(stderr, "Failed to read server queue id from %s\n", id_path); server_queue_id = -1; } close(fd); msgbuf_t msg = {.mtype = Login, .sender = ""}; strncpy(msg.sender, client_id, COMMAND_LENGTH - 1); snprintf(msg.command, COMMAND_LENGTH, "%d", client_queue_id); if (msgsnd(server_queue_id, &msg, sizeof(msg) - sizeof(long), 0) == -1) { perror("msgsnd(login) failed"); cleanup_queue(client_queue_id); return 1; } /* 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); if (r == -1) { perror("msgrcv(login response) failed"); cleanup_queue(client_queue_id); return 1; } 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_NOT_FOUND) { fprintf(stderr, "Login failed: User not found\n"); cleanup_queue(client_queue_id); return 1; } printf("Login accepted. Welcome, %s!\n", resp.command); strncpy(client_id_global, client_id, COMMAND_LENGTH - 1); 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); return 0; } input_pid = fork(); if (input_pid == 0) { input_loop(server_queue_id, client_id); // The /quit command just returns from the input loop kill(getppid(), SIGTERM); return 0; } // Setup them below the forks to avoid multiple triggers signal(SIGINT, cleanup_and_exit); signal(SIGTERM, cleanup_and_exit); while (1) { msgbuf_t incoming; ssize_t got = msgrcv(client_queue_id, &incoming, sizeof(incoming) - sizeof(long), 0, 0); if (got == -1) { perror("msgrcv(incoming) failed"); if (errno == EINTR) { continue; } perror("msgrcv(incoming) failed"); break; } switch (incoming.mtype) { case Message: { printf("%s: %s\n", incoming.sender, incoming.message); msgbuf_t ack = {.mtype = Signal, .stype = ACK_DELIVERED, .command = ""}; strncpy(ack.command, incoming.command, COMMAND_LENGTH); send(server_queue_id, &ack); break; } 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, "groups", COMMAND_LENGTH) == 0) { printf("All group members:\n%s\n", incoming.message); } else { printf("All clients:\n%s\n", incoming.message); } break; } 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 box!\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) { printf("[Sent]\n"); } else if (incoming.stype == ACK_DELIVERED) { printf("[Delivered: %s]\n", incoming.sender); } else if (incoming.stype == ERR_NOT_FOUND) { printf("[Error: User not found]\n"); } else { // Display whatever if I forgot to handle something printf("[Error: Code %d]\n", incoming.stype); } break; case JoinGroup: if (incoming.stype == ACK_ACCEPTED) { printf("Successfully joined the group.\n"); } 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"); } else { printf("Failed to join group: Unknown error (code %d).\n", incoming.stype); } break; case LeaveGroup: if (incoming.stype == ACK_ACCEPTED) { printf("Successfully left the group.\n"); } 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; } } cleanup_queue(client_queue_id); return 0; }