Untitled

 avatar
unknown
plain_text
22 days ago
7.0 kB
1
Indexable
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
#include <vector>

using namespace std;

// Function Prototypes
void checkInbox();
int displayMenu();
void WriteToOutbox();
void filterSpam();
void filterSpamAndHam();

int main() {
    while (true) {
        switch(displayMenu()) {
            case 1:
                checkInbox();
                break;
            case 2:
                WriteToOutbox();
                break;
            case 3:
                filterSpam();
                break;
            case 4:
                filterSpamAndHam();
                break;
            case 5:
                cout << "Exiting program. Goodbye!" << endl;
                return 0;
            default:
                cout << "Invalid selection. Please try again." << endl;
        }
    }
}

int displayMenu() {
    int num = 0;
    cout << "\nEmail Management System" << endl;
    cout << "1. Check Inbox" << endl;
    cout << "2. Write Email" << endl;
    cout << "3. Filter Spam" << endl;
    cout << "4. Filter Spam + Ham" << endl;
    cout << "5. Exit" << endl;
    cout << "Enter your choice: ";
    cin >> num;
    cin.ignore();
    return num;
}

void checkInbox() {
    vector<string> filenames;

    // Collect filenames from inbox folder
    for (const auto& entry : filesystem::directory_iterator("inbox")) {
        if (entry.is_regular_file()) {
            filenames.push_back(entry.path().string());
        }
    }

    if (filenames.empty()) {
        cout << "Inbox is empty." << endl;
        return;
    }

    while (true) {
        cout << "\nYour Inbox:" << endl;
        for (size_t i = 0; i < filenames.size(); ++i) {
            ifstream emailFile(filenames[i]);
            string recipient, subject;
            getline(emailFile, recipient);
            getline(emailFile, subject);
            emailFile.close();
            cout << i + 1 << ". " << filenames[i] << " - " << subject << endl;
        }
        cout << "0. Return to Main Menu" << endl;

        int choice;
        cout << "Enter the number of the email to open: ";
        cin >> choice;
        cin.ignore();

        if (choice == 0) {
            break;
        } else if (choice > 0 && choice <= filenames.size()) {
            ifstream emailFile(filenames[choice - 1]);
            string line;
            cout << "\nEmail Contents:" << endl;
            while (getline(emailFile, line)) {
                cout << line << endl;
            }
            emailFile.close();
        } else {
            cout << "Invalid choice. Please try again." << endl;
        }
    }
}

void WriteToOutbox() {
    string recipient, subjectLine, body, line, filename;

    cout << "Enter recipient email: ";
    cin >> recipient;
    cin.ignore();

    cout << "Enter the subject line: ";
    getline(cin, subjectLine);

    cout << "Please enter the email body. Enter 2 empty lines to finish:" << endl;
    int emptyCounter = 0;
    while (true) {
        getline(cin, line);
        if (line.empty()) {
            emptyCounter++;
        } else {
            emptyCounter = 0;
        }
        if (emptyCounter == 2) {
            break;
        }
        body += line + '\n';
    }

    cout << "Enter the filename (e.g., outbox/email1.txt): ";
    cin >> filename;

    ofstream emailFile(filename);
    emailFile << recipient << endl << subjectLine << endl << body;
    emailFile.close();

    cout << "Email written to " << filename << endl;
}

void filterSpam() {
    vector<string> spamWords;
    vector<string> inboxFiles;

    ifstream spamFile("spam.txt");
    if (!spamFile) {
        cout << "Error: Could not open spam.txt." << endl;
        return;
    }
    string word;
    while (getline(spamFile, word)) {
        spamWords.push_back(word);
    }
    spamFile.close();

    for (const auto& entry : filesystem::directory_iterator("inbox")) {
        if (entry.is_regular_file()) {
            inboxFiles.push_back(entry.path().string());
        }
    }

    if (inboxFiles.empty()) {
        cout << "Inbox is empty." << endl;
        return;
    }

    for (const auto& file : inboxFiles) {
        ifstream emailFile(file);
        if (!emailFile) {
            cout << "Error: Could not open " << file << endl;
            continue;
        }

        string content((istreambuf_iterator<char>(emailFile)), istreambuf_iterator<char>());
        emailFile.close();

        for (const string& spamWord : spamWords) {
            if (content.find(spamWord) != string::npos) {
                string spamPath = "spam/" + filesystem::path(file).filename().string();
                filesystem::rename(file, spamPath);
                cout << "Moved " << file << " to spam folder." << endl;
                break;
            }
        }
    }
}

void filterSpamAndHam() {
    vector<string> spamWords, hamWords;
    vector<string> inboxFiles;

    ifstream spamFile("spam.txt");
    if (!spamFile) {
        cout << "Error: Could not open spam.txt." << endl;
        return;
    }
    string word;
    while (getline(spamFile, word)) {
        spamWords.push_back(word);
    }
    spamFile.close();

    ifstream hamFile("ham.txt");
    if (!hamFile) {
        cout << "Error: Could not open ham.txt." << endl;
        return;
    }
    while (getline(hamFile, word)) {
        hamWords.push_back(word);
    }
    hamFile.close();

    for (const auto& entry : filesystem::directory_iterator("inbox")) {
        if (entry.is_regular_file()) {
            inboxFiles.push_back(entry.path().string());
        }
    }

    if (inboxFiles.empty()) {
        cout << "Inbox is empty." << endl;
        return;
    }

    for (const auto& file : inboxFiles) {
        ifstream emailFile(file);
        if (!emailFile) {
            cout << "Error: Could not open " << file << endl;
            continue;
        }

        string content((istreambuf_iterator<char>(emailFile)), istreambuf_iterator<char>());
        emailFile.close();

        bool isSpam = false, isHam = false;

        for (const string& spamWord : spamWords) {
            if (content.find(spamWord) != string::npos) {
                isSpam = true;
                break;
            }
        }

        for (const string& hamWord : hamWords) {
            if (content.find(hamWord) != string::npos) {
                isHam = true;
                break;
            }
        }

        if (isSpam && !isHam) {
            string spamPath = "spam/" + filesystem::path(file).filename().string();
            filesystem::rename(file, spamPath);
            cout << "Moved " << file << " to spam folder." << endl;
        } else if (isSpam && isHam) {
            cout << file << " contains spam and ham words, so it remains in the inbox." << endl;
        }
    }
}
Leave a Comment