#include "client.h" int send_message(unsigned int receiver,int isgroup) { int sender = getuid(); int pipe_to_child[2]; if (pipe(pipe_to_child) == -1) { perror("pipe"); exit(EXIT_FAILURE); } pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (pid == 0) { // Child process close(pipe_to_child[1]); // Close write end of pipe // Redirect stdin to read from pipe_to_child dup2(pipe_to_child[0], STDIN_FILENO); execlp("./bin/djumbai_client_send/djumbai_client_send", "djumbai_client_send", NULL); close(pipe_to_child[0]); // If execlp fails perror("execlp"); exit(EXIT_FAILURE); } else { // Parent process close(pipe_to_child[0]); // Close read end of pipe printf("Please enter your message (Max of %ld bytes):\n", MAX_CONTENT_SIZE - 1); char content[MAX_CONTENT_SIZE]; fgets(content, MAX_CONTENT_SIZE, stdin); message msg; if (new_message(&msg, sender, isgroup, receiver, content) != 0) { printf("Error when creating new message\n"); } // Serialize the message unsigned char buffer[MESSAGE_SIZE]; if (serialize_message(&msg, MESSAGE_SIZE, buffer) == -1) { fprintf(stderr, "Error: Serialization failed\n"); return 1; } write(pipe_to_child[1], buffer, MESSAGE_SIZE); // Close the write end of the pipe close(pipe_to_child[1]); // Wait for the child process to finish wait(NULL); } return 0; } // Function to print usage information void printUsage() { printf("Usage: message_cli [options]\n"); printf("Options:\n"); printf(" -h, --help Display this help message\n"); printf(" -g, --group Send message to a group\n"); printf(" -s, --sync Send message synchronously\n"); printf(" -u, --uid Specify the UID of the receiver\n"); } int main(int argc, char *argv[]) { // TODO: Client parsing to be done int groupFlag = 0; int syncFlag = 0; char receiverUid[MAX_VALUE_LENGTH]; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { printUsage(); exit(0); } else if (strcmp(argv[i], "-g") == 0 || strcmp(argv[i], "--group") == 0) { groupFlag = 1; if (i + 1 < argc && argv[i + 1][0] != '-') { strncpy(receiverUid, argv[i + 1], MAX_VALUE_LENGTH); i++; // Skip the next argument since it's the value } } else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--sync") == 0) { syncFlag = 1; if (i + 1 < argc && argv[i + 1][0] != '-') { strncpy(receiverUid, argv[i + 1], MAX_VALUE_LENGTH); i++; // Skip the next argument since it's the value } } else if (strcmp(argv[i], "-u") == 0 || strcmp(argv[i], "--uid") == 0) { if (i + 1 < argc && argv[i + 1][0] != '-') { strncpy(receiverUid, argv[i + 1], MAX_VALUE_LENGTH); i++; // Skip the next argument since it's the value } else { fprintf(stderr, "Error: No value provided for option %s\n", argv[i]); printUsage(); exit(1); } } else { fprintf(stderr, "Error: Unknown option %s\n", argv[i]); printUsage(); exit(1); } } int receiverUidInt = atoi(receiverUid); send_message(receiverUidInt,groupFlag); // unsigned int sender = getuid(); // unsigned int receiver = 1000; // send_message(sender, receiver); return 0; }