summaryrefslogtreecommitdiff
path: root/src/file/mail.cpp
blob: 5452136f7ed446bc425b6322db97d07168264655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <fstream>

#include "mail.hpp"
#include "managment/logger.hpp"

namespace smtp::file
{
    Mail::Mail( std::string const& path_file )
        : mPathFile( path_file )
    {
    }

    manage::MailsSet Mail::Read() const
    {
        std::ifstream mail_file{ mPathFile, std::fstream::in };
        if ( !mail_file.is_open() )
        {
            manage::Logger::LogError( "Unable to open file to read " + mPathFile );
            return {};
        }
        std::string line{};
        manage::MailsSet result;

        while ( std::getline( mail_file, line ) )
        {
            auto parsed_data = GetMailFromLine( line );
            if( parsed_data )
            {
                result.push_back( *parsed_data );
            }
        }
        mail_file.close();
        return result;
    }

    bool Mail::Write( manage::MailsSet const& data ) const
    {
        std::ofstream mail_file{ mPathFile, std::fstream::out | std::fstream::trunc };
        if ( !mail_file.is_open() )
        {
            manage::Logger::LogError( "Unable to open file to write " + mPathFile );
            return false;
        }
        for( const auto& pair : data )
        {
            mail_file << pair << "\n";
        }
        mail_file.close();
        return true;
    }

    std::optional<std::string> Mail::GetMailFromLine( std::string const& line ) const
    {
        //TODO registrator if checking parsing
        auto pos = line.find('@');
        if(pos == std::string::npos)
        {
            return {};
        }
        return line;
    }

}