// main.c // Angela Finlayson // Uses the email management system to allow a user to create a folder // and and emails (with not content) to it. // Only works for 1 folder... // (Provided) #include #include #include #include "email_management_system.h" void print_instructions(void); void handle_search(struct folder *current_folder); void handle_add_email(struct folder *current_folder); void trim_newline(char str[]); int main(void) { struct folder *current_folder = create_folder("inbox"); char command; print_instructions(); printf("\nEnter command: "); while (scanf(" %c", &command) == 1 && command != 'q') { if (command == 'a') { handle_add_email(current_folder); } else if (command == 'p') { print_emails(current_folder); } else if (command == 'n') { printf("Number of emails: %d\n", count_emails(current_folder)); } else if (command == 's') { handle_search(current_folder); } else { printf("Unknown command. Try again.\n"); } printf("\nEnter command: "); } printf("Exiting\n"); clear_folder(current_folder); return 0; } // Prints instructions to users void print_instructions(void) { printf("Email Manager - Command Interface\n"); printf("Commands:\n"); printf("a - Add a new email to the folder\n"); printf("p - Print all emails in the folder\n"); printf("n - Count emails in the folder\n"); printf("s - Search for an email by subject\n"); printf("q - Quit\n"); } // Handles adding a new email to the folder // Assumes no error checking needed void handle_add_email(struct folder *current_folder) { char sender[MAX_LEN] = ""; char subject[MAX_LEN]= ""; double size; int priority, type; // Remove whitespace from buffer int whitespace_ch; while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {} printf("Enter sender: "); fgets(sender, MAX_LEN, stdin); trim_newline(sender); printf("Enter subject: "); fgets(subject, MAX_LEN, stdin); trim_newline(subject); printf("Enter size (in MB): "); scanf("%lf", &size); printf("Enter email type (0 for DRAFT, 1 for RECEIVED, 2 for SENT): "); scanf("%d", &type); printf("Enter priority (0 for LOW, 1 for NORMAL, 3 for HIGH): "); scanf("%d", &priority); insert_email_at_head(current_folder, sender, subject, size, type, priority); printf("Email added to folder '%s'.\n", current_folder->name); } // Handles searching for an email by subject in the folder void handle_search(struct folder *current_folder) { char subject[MAX_LEN]; // Remove whitespace from buffer int whitespace_ch; while ((whitespace_ch = getchar()) != '\n' && whitespace_ch != EOF) {} printf("Enter subject to search: "); fgets(subject, MAX_LEN, stdin); trim_newline(subject); struct email *found_email = search_email(current_folder, subject); if (found_email != NULL) { printf("Email found:\n"); print_single_email(found_email); } else { printf("No email with subject '%s' found.\n", subject); } } // removes trailing newline character void trim_newline(char str[]) { int len = strlen(str); if (len > 0 && str[len - 1] == '\n') { str[len - 1] = '\0'; } }