[TS] TP3 Message reader cli needs testing with actual permissions and files

This commit is contained in:
Lucas Verdelho 2024-05-12 21:46:20 +01:00
parent 1b7e95c249
commit 633535b5a4

View file

@ -5,6 +5,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <string.h> #include <string.h>
#include <time.h>
void displayHeader() { void displayHeader() {
printf("╭━━━╮╱╱╱╱╱╱╭╮\n"); printf("╭━━━╮╱╱╱╱╱╱╭╮\n");
@ -18,14 +19,75 @@ void displayHeader() {
} }
void displayAllMessages(uid_t userID) { void displayAllMessages(uid_t userID) {
char dirname[256]; char userDir[256];
sprintf(dirname, "%d", userID); // Convert userID to string // 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; DIR *dir;
struct dirent *entry; struct dirent *entry;
printf("Reading all messages for UserID %d...\n", userID); 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) { if (dir == NULL) {
printf("Error opening directory\n"); printf("Error opening directory\n");
return; return;
@ -33,46 +95,18 @@ void displayAllMessages(uid_t userID) {
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) { // If it's a regular file if (entry->d_type == DT_REG) { // If it's a regular file
displayMessage(dirname, entry->d_name); displayMessage(senderDir, entry->d_name);
} }
} }
closedir(dir); 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); void displayMessage(char *senderDir, char *filename) {
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) {
char filepath[256]; char filepath[256];
sprintf(filepath, "%s/%s", dirname, filename); sprintf(filepath, "%s/%s", senderDir, filename);
FILE *file = fopen(filepath, "r"); FILE *file = fopen(filepath, "r");
if (file == NULL) { if (file == NULL) {
printf("Error opening file: %s\n", filename); printf("Error opening file: %s\n", filename);
@ -84,14 +118,14 @@ void displayMessage(char *dirname, char *filename) {
while (fgets(buffer, sizeof(buffer), file)) { while (fgets(buffer, sizeof(buffer), file)) {
printf("%s", buffer); printf("%s", buffer);
} }
printf("\n----- End of %s -----\n", filename); printf("----- End of %s -----\n", filename);
fclose(file); fclose(file);
} }
int main() { int main() {
int choice; int choice;
char userID[20]; char sender[20];
char groupID[20]; char groupID[20];
displayHeader(); displayHeader();
@ -111,14 +145,20 @@ int main() {
displayAllMessages(getuid()); displayAllMessages(getuid());
break; break;
case 2: case 2:
printf("Enter userID: "); printf("Enter sender userID: ");
scanf("%s", userID); scanf("%s", sender);
displayUserMessages(userID);
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; break;
case 3: case 3:
printf("Enter groupID: "); printf("Enter groupID: ");
scanf("%s", groupID); scanf("%s", groupID);
displayGroupMessages(groupID); displayGroupMessages(groupID);
printf("Group messages functionality is not implemented yet.\n");
break; break;
case 4: case 4:
printf("Exiting program...\n"); printf("Exiting program...\n");