From 633535b5a4c3058b2805f4b33df5a684ac59fcb9 Mon Sep 17 00:00:00 2001 From: Lucas Verdelho Date: Sun, 12 May 2024 21:46:20 +0100 Subject: [PATCH] [TS] TP3 Message reader cli needs testing with actual permissions and files --- test/message_cli.c | 118 ++++++++++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 39 deletions(-) diff --git a/test/message_cli.c b/test/message_cli.c index 8df0038..bf69a67 100644 --- a/test/message_cli.c +++ b/test/message_cli.c @@ -5,6 +5,7 @@ #include #include #include +#include void displayHeader() { printf("╭━━━╮╱╱╱╱╱╱╭╮\n"); @@ -18,14 +19,75 @@ void displayHeader() { } void displayAllMessages(uid_t userID) { - char dirname[256]; - sprintf(dirname, "%d", userID); // Convert userID to string + char userDir[256]; + // sprintf(userDir, "/djumbai/user/%d/mailbox", userID); // Construct user directory path + sprintf(userDir, "/mnt/e/Desktop/CSI/djumbai/test/%d/mailbox", userID); // TODO MUDAR ESTE PATH DIR *dir; struct dirent *entry; printf("Reading all messages for UserID %d...\n", userID); - dir = opendir(dirname); + dir = opendir(userDir); + if (dir == NULL) { + printf("Error opening directory\n"); + return; + } + + while ((entry = readdir(dir)) != NULL) { + if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { + char senderDir[256]; + sprintf(senderDir, "%s/%s", userDir, entry->d_name); // Construct sender directory path + displayMessagesFromSender(senderDir); + } + } + + closedir(dir); +} + + + +void displayGroupMessages(uid_t groupID) { + char groupDir[256]; + sprintf(groupDir, "/djumbai/group/%d/mailbox", groupID); + DIR *dir; + struct dirent *entry; + + printf("Reading all messages for GroupID %d...\n", groupID); + + dir = opendir(groupDir); + if (dir == NULL) { + printf("Error opening directory\n"); + return; + } + + while ((entry = readdir(dir)) != NULL) { + if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { + char senderDir[256]; + sprintf(senderDir, "%s/%s", groupDir, entry->d_name); // Construct sender directory path + displayMessagesFromSender(senderDir); + } + } + + closedir(dir); +} + + + + +void displayMessagesFromSender(char *senderDir) { + char *senderName = strrchr(senderDir, '/'); // Find the last occurrence of '/' + if (senderName == NULL) { + printf("Error parsing sender directory\n"); + return; + } + senderName++; // Move past the '/' + + DIR *dir; + struct dirent *entry; + + printf("Reading messages from sender %s...\n", senderName); + + dir = opendir(senderDir); if (dir == NULL) { printf("Error opening directory\n"); return; @@ -33,46 +95,18 @@ void displayAllMessages(uid_t userID) { while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { // If it's a regular file - displayMessage(dirname, entry->d_name); + displayMessage(senderDir, entry->d_name); } } closedir(dir); } -void displayUserMessages(char *userID) { - char currentUserDir[256]; - sprintf(currentUserDir, "%d", getuid()); // Convert currentUserID to string - char userDir[256]; - sprintf(userDir, "%s/%s", currentUserDir, userID); // Construct user directory path - DIR *dir; - struct dirent *entry; - printf("Reading messages from user %s...\n", userID); - dir = opendir(userDir); - if (dir == NULL) { - printf("Error opening directory for user %s\n", userID); - return; - } - - while ((entry = readdir(dir)) != NULL) { - if (entry->d_type == DT_REG) { // If it's a regular file - displayMessage(userDir, entry->d_name); - } - } - - closedir(dir); -} - -void displayGroupMessages(char *groupID) { - printf("Reading messages from group %s...\n", groupID); - // Your code to read messages from a specific group -} - -void displayMessage(char *dirname, char *filename) { +void displayMessage(char *senderDir, char *filename) { char filepath[256]; - sprintf(filepath, "%s/%s", dirname, filename); + sprintf(filepath, "%s/%s", senderDir, filename); FILE *file = fopen(filepath, "r"); if (file == NULL) { printf("Error opening file: %s\n", filename); @@ -84,14 +118,14 @@ void displayMessage(char *dirname, char *filename) { while (fgets(buffer, sizeof(buffer), file)) { printf("%s", buffer); } - printf("\n----- End of %s -----\n", filename); + printf("----- End of %s -----\n", filename); fclose(file); } int main() { int choice; - char userID[20]; + char sender[20]; char groupID[20]; displayHeader(); @@ -111,14 +145,20 @@ int main() { displayAllMessages(getuid()); break; case 2: - printf("Enter userID: "); - scanf("%s", userID); - displayUserMessages(userID); + printf("Enter sender userID: "); + scanf("%s", sender); + + uid_t senderID = atoi(sender); + char userDir[256]; + sprintf(userDir, "/mnt/e/Desktop/CSI/djumbai/test/%d/mailbox/%d", getuid(), senderID); // TODO MUDAR PATH AQUI + displayMessagesFromSender(userDir); + break; case 3: printf("Enter groupID: "); scanf("%s", groupID); displayGroupMessages(groupID); + printf("Group messages functionality is not implemented yet.\n"); break; case 4: printf("Exiting program...\n");