Skip to content

Commit 8bbb222

Browse files
committed
add basic structure
1 parent 2265892 commit 8bbb222

File tree

12 files changed

+294
-3
lines changed

12 files changed

+294
-3
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@
3434
# Folders
3535
.vscode
3636
build
37+
38+
# Env files
39+
.env

.sample.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
EMAIL_USERNAME=[email protected]
2+
EMAIL_PASSWORD=<app-password>

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.16)
2-
project(bloomail VERSION 0.1.6)
2+
project(bloomail VERSION 0.1.0)
33

44
set(CMAKE_CXX_STANDARD 20)
55
set(CMAKE_CXX_EXTENSIONS OFF)
@@ -16,6 +16,8 @@ option(USE_INSTALL_RESOURCE_PATH "Set resource path to install location" OFF)
1616

1717
set(BUILD_SHARED_LIBS OFF)
1818

19+
find_package(OpenSSL REQUIRED)
20+
1921
file(GLOB_RECURSE SOURCES "src/*.cpp")
2022
file(GLOB_RECURSE LIB_SOURCES "lib/**/src/*.cpp")
2123

@@ -35,3 +37,9 @@ target_include_directories(bloomail
3537
$<INSTALL_INTERFACE:lib/brewtils/include>
3638
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/brewtils/include>
3739
)
40+
41+
target_link_libraries(bloomail
42+
PRIVATE
43+
OpenSSL::SSL
44+
OpenSSL::Crypto
45+
)

example/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
#include <iostream>
1+
#include <brewtils/env.h>
2+
3+
#include <bloomail/clients/gmail.h>
24

35
int main(int argc, char **argv) {
4-
std::cout << "Hello World" << std::endl;
6+
brewtils::env::init("../.env");
7+
std::string emailUsername = brewtils::env::get("EMAIL_USERNAME");
8+
std::string emailPassword = brewtils::env::get("EMAIL_PASSWORD");
9+
10+
bloomail::Client::Gmail gmail = bloomail::Client::Gmail(true);
11+
gmail.login(emailUsername, emailPassword);
12+
513
return EXIT_SUCCESS;
614
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <brewtils/base64.h>
4+
5+
#include <bloomail/helper_clients/tls_client.h>
6+
7+
namespace bloomail {
8+
9+
namespace BaseClient {
10+
11+
class Smtp {
12+
private:
13+
bool debug;
14+
bool tlsConnected;
15+
std::string username;
16+
std::string password;
17+
18+
bloomail::HelperClient::Tcp tcp;
19+
bloomail::HelperClient::Tls tls;
20+
21+
void send(const std::string &cmd);
22+
std::string recv();
23+
void startTls();
24+
void authenticate();
25+
void quit();
26+
27+
public:
28+
Smtp(const std::string &host, int port, bool debug = false);
29+
~Smtp();
30+
31+
void login(const std::string &username, const std::string &password);
32+
};
33+
34+
} // namespace BaseClient
35+
36+
} // namespace bloomail

include/bloomail/clients/gmail.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <bloomail/base_clients/smtp_client.h>
4+
5+
namespace bloomail {
6+
7+
namespace Client {
8+
9+
class Gmail : public bloomail::BaseClient::Smtp {
10+
public:
11+
Gmail(bool debug = false);
12+
~Gmail();
13+
};
14+
15+
} // namespace Client
16+
17+
} // namespace bloomail
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <netdb.h>
4+
5+
#include <brewtils/sys.h>
6+
7+
namespace bloomail {
8+
9+
namespace HelperClient {
10+
11+
class Tcp {
12+
private:
13+
int sockfd;
14+
15+
public:
16+
Tcp(const std::string &host, int port);
17+
~Tcp();
18+
19+
int socket() const;
20+
std::string read();
21+
void write(const std::string &data);
22+
};
23+
24+
} // namespace HelperClient
25+
26+
} // namespace bloomail
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <openssl/ssl.h>
4+
5+
#include <bloomail/helper_clients/tcp_client.h>
6+
7+
namespace bloomail {
8+
9+
namespace HelperClient {
10+
11+
class Tls {
12+
private:
13+
SSL_CTX *ctx;
14+
SSL *ssl;
15+
bloomail::HelperClient::Tcp tcp;
16+
17+
public:
18+
explicit Tls(bloomail::HelperClient::Tcp &client);
19+
~Tls();
20+
21+
void connect();
22+
void write(const std::string &data);
23+
std::string read();
24+
};
25+
26+
} // namespace HelperClient
27+
28+
} // namespace bloomail

src/base_clients/smtp_client.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <bloomail/base_clients/smtp_client.h>
2+
3+
bloomail::BaseClient::Smtp::Smtp(const std::string &host, int port, bool debug)
4+
: tcp(host, port), tls(tcp), debug(debug) {
5+
this->recv();
6+
this->send("EHLO localhost");
7+
this->recv();
8+
return;
9+
}
10+
11+
bloomail::BaseClient::Smtp::~Smtp() { return; }
12+
13+
void bloomail::BaseClient::Smtp::login(const std::string &username,
14+
const std::string &password) {
15+
this->username = brewtils::base64::encode(username);
16+
this->password = brewtils::base64::encode(password);
17+
this->startTls();
18+
this->authenticate();
19+
this->quit();
20+
}
21+
22+
void bloomail::BaseClient::Smtp::send(const std::string &cmd) {
23+
if (this->tlsConnected) {
24+
this->tls.write(cmd + "\r\n");
25+
} else {
26+
this->tcp.write(cmd + "\r\n");
27+
}
28+
}
29+
30+
std::string bloomail::BaseClient::Smtp::recv() {
31+
std::string response =
32+
this->tlsConnected ? this->tls.read() : this->tcp.read();
33+
if (this->debug) {
34+
logger::debug(response);
35+
}
36+
return response;
37+
}
38+
39+
void bloomail::BaseClient::Smtp::startTls() {
40+
this->send("STARTTLS");
41+
this->recv();
42+
this->tls.connect();
43+
this->tlsConnected = true;
44+
this->send("EHLO localhost");
45+
this->recv();
46+
}
47+
48+
void bloomail::BaseClient::Smtp::authenticate() {
49+
this->send("AUTH LOGIN");
50+
this->recv();
51+
this->send(this->username);
52+
this->recv();
53+
this->send(this->password);
54+
this->recv();
55+
}
56+
57+
void bloomail::BaseClient::Smtp::quit() {
58+
this->send("QUIT");
59+
this->recv();
60+
this->tlsConnected = false;
61+
}

src/clients/gmail.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <bloomail/clients/gmail.h>
2+
3+
bloomail::Client::Gmail::Gmail(bool debug)
4+
: bloomail::BaseClient::Smtp("smtp.gmail.com", 587, debug) {}
5+
6+
bloomail::Client::Gmail::~Gmail() { return; }

0 commit comments

Comments
 (0)