#include #include #include #include #include #include #include #include void displayMessagesFromSender(char *senderDir); void displayMessage(char *senderDir, char *filename); void displayHeader() { printf("╭━━━╮╱╱╱╱╱╱╭╮\n"); printf("╰╮╭╮┣╮╱╱╱╱╱┃┃\n"); printf("╱┃┃┃┣╋╮╭┳╮╭┫╰━┳━━┳╮\n"); printf("╱┃┃┃┣┫┃┃┃╰╯┃╭╮┃╭╮┣┫\n"); printf("╭╯╰╯┃┃╰╯┃┃┃┃╰╯┃╭╮┃┃\n"); printf("╰━━━┫┣━━┻┻┻┻━━┻╯╰┻╯\n"); printf("╱╱╱╭╯┃\n"); printf("╱╱╱╰━╯\n"); } void displayAllMessages(uid_t userID) { 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(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(gid_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; } while ((entry = readdir(dir)) != NULL) { if (entry->d_type == DT_REG) { // If it's a regular file displayMessage(senderDir, entry->d_name); } } closedir(dir); } void displayMessage(char *senderDir, char *filename) { char filepath[256]; sprintf(filepath, "%s/%s", senderDir, filename); FILE *file = fopen(filepath, "r"); if (file == NULL) { printf("Error opening file: %s\n", filename); return; } printf("----- %s -----\n", filename); char buffer[256]; while (fgets(buffer, sizeof(buffer), file)) { printf("%s", buffer); } printf("----- End of %s -----\n", filename); fclose(file); } int main() { int choice; char sender[20]; char group[20]; displayHeader(); while (1) { printf("\nCurrent UserID: %d\n", getuid()); printf("Options:\n"); printf("1. all (read all messages)\n"); printf("2. user -userID_here (read messages from given userID)\n"); printf("3. group -groupID_here\n"); printf("4. kill (exit the program)\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: displayAllMessages(getuid()); break; case 2: printf("Enter sender userID: "); scanf("%s", sender); uid_t senderID = atoi(sender); char userDir[256]; sprintf(userDir, "/djumbai/user/%d/mailbox/%d", getuid(), senderID); displayMessagesFromSender(userDir); break; case 3: printf("Enter groupID: "); scanf("%s", group); gid_t groupID = atoi(group); displayGroupMessages(groupID); printf("Group messages functionality is not implemented yet.\n"); break; case 4: printf("Exiting program...\n"); exit(0); default: printf("Invalid choice. Please try again.\n"); } } return 0; }